Skip to content

Commit

Permalink
Improve coverage, fix another style issue
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalijr2 committed Nov 26, 2024
1 parent 95a7860 commit e911061
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*-
* ---------------LICENSE_START-----------------
* Mock Loggers
* ---------------------------------------------
* Copyright (C) 2024 Vitalij Berdinskih
* ---------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---------------LICENSE_END-------------------
*/
package io.github.vitalijr2.logging.mock.tinylog;

public class MockLoggerException extends RuntimeException {

public MockLoggerException(String message, Throwable cause) {
super(message, cause);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*-
* ---------------LICENSE_START-----------------
* Mock Loggers
* ---------------------------------------------
* Copyright (C) 2024 Vitalij Berdinskih
* ---------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---------------LICENSE_END-------------------
*/
package io.github.vitalijr2.logging.mock.tinylog;

import java.lang.annotation.ElementType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
/*-
* ---------------LICENSE_START-----------------
* Mock Loggers
* ---------------------------------------------
* Copyright (C) 2024 Vitalij Berdinskih
* ---------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---------------LICENSE_END-------------------
*/
package io.github.vitalijr2.logging.mock.tinylog;

import java.util.stream.Stream;
import org.jetbrains.annotations.VisibleForTesting;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
Expand All @@ -10,18 +30,23 @@

public class MockTinylogProviderExtension implements TestInstancePostProcessor, ParameterResolver {

@VisibleForTesting
static void injectMockProvider(Object testInstance, Class<?> testClass) {
Stream.of(testClass.getDeclaredFields()).filter(
field -> field.isAnnotationPresent(MockTinylogProvider.class) && field.getType()
.isAssignableFrom(LoggingProvider.class)).forEach(field -> {
field.setAccessible(true);
try {
field.set(testInstance, MockLoggerProvider.MOCK_INSTANCE);
} catch (IllegalAccessException | RuntimeException exception) {
throw new MockLoggerException("Cannot inject a mock provider", exception);
}
});
}

@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
Stream.of(testInstance.getClass().getDeclaredFields())
.filter(field -> field.isAnnotationPresent(MockTinylogProvider.class) && field.getType()
.isAssignableFrom(LoggingProvider.class)).forEach(field -> {
field.setAccessible(true);
try {
field.set(testInstance, MockLoggerProvider.MOCK_INSTANCE);
} catch (IllegalAccessException exception) {
throw new RuntimeException(exception);
}
});
injectMockProvider(testInstance, testInstance.getClass());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsInstanceOf.instanceOf;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -71,4 +73,22 @@ void resolveParameter() {
() -> assertThat(value, instanceOf(LoggingProvider.class)));
}

@DisplayName("Throw an exception on injection")
@Test
void throwExceptionOnInjection() {
// when
var exception = assertThrows(MockLoggerException.class,
() -> MockTinylogProviderExtension.injectMockProvider(null, JustTest.class));

// then
assertEquals("Cannot inject a mock provider", exception.getMessage());
}

static class JustTest {

@MockTinylogProvider
private LoggingProvider provider;

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ void setUp() {

@DisplayName("Happy path")
@Test
void happyPath() throws IllegalAccessException {
void happyPath() {
// given
var testInstance = new HappyTest();

Expand All @@ -43,7 +43,7 @@ void happyPath() throws IllegalAccessException {

@DisplayName("Unannotated field")
@Test
void unannotatedField() throws IllegalAccessException {
void unannotatedField() {
// given
var testInstance = new UnannotatedFieldTest();

Expand All @@ -56,7 +56,7 @@ void unannotatedField() throws IllegalAccessException {

@DisplayName("Wrong type")
@Test
void wrongType() throws IllegalAccessException {
void wrongType() {
// given
var testInstance = new WrongTypeTest();

Expand Down

0 comments on commit e911061

Please sign in to comment.