From 209a288ad9aa9a96427bc6fd8266a9803ae7c1d9 Mon Sep 17 00:00:00 2001 From: Vitalii Samolovskikh Date: Thu, 11 Jul 2024 21:05:47 +0200 Subject: [PATCH] Structure tests --- README.md | 4 +- .../java/mina/context/MinaContextHolder.java | 5 +- src/main/java/mina/core/ConditionStep.java | 4 + src/main/java/mina/core/DoNothingCheck.java | 17 ++ src/test/java/mina/core/MinaTest.java | 145 ++++++++++++++++++ .../{test => example}/QuadraticEquation.java | 2 +- .../QuadraticEquationTest.java | 2 +- .../mina/{test => subject}/EmptyCode.java | 2 +- src/test/java/mina/subject/Simple.java | 67 ++++++++ src/test/java/mina/test/MinaTest.java | 95 ------------ src/test/java/mina/test/Simple.java | 20 --- 11 files changed, 239 insertions(+), 124 deletions(-) create mode 100644 src/main/java/mina/core/DoNothingCheck.java create mode 100644 src/test/java/mina/core/MinaTest.java rename src/test/java/mina/{test => example}/QuadraticEquation.java (97%) rename src/test/java/mina/{test => example}/QuadraticEquationTest.java (97%) rename src/test/java/mina/{test => subject}/EmptyCode.java (79%) create mode 100644 src/test/java/mina/subject/Simple.java delete mode 100644 src/test/java/mina/test/MinaTest.java delete mode 100644 src/test/java/mina/test/Simple.java diff --git a/README.md b/README.md index c560fad..e673546 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ public class QuadraticEquation { } ``` -[QuadraticEquation.java](src/test/java/mina/test/QuadraticEquation.java) +[QuadraticEquation.java](src/test/java/mina/example/QuadraticEquation.java) Then use Mina in the unit test. @@ -66,5 +66,5 @@ public class QuadraticEquationTest { } ``` -[QuadraticEquationTest.java](src/test/java/mina/test/QuadraticEquationTest.java) +[QuadraticEquationTest.java](src/test/java/mina/example/QuadraticEquationTest.java) diff --git a/src/main/java/mina/context/MinaContextHolder.java b/src/main/java/mina/context/MinaContextHolder.java index 3d0b1fe..3a745b8 100644 --- a/src/main/java/mina/context/MinaContextHolder.java +++ b/src/main/java/mina/context/MinaContextHolder.java @@ -37,11 +37,8 @@ public static void removeContext() { if (useGlobalContext) { GLOBAL.set(null); } else { - removeThreadLocalContext(); + CONTEXT.remove(); } } - private static void removeThreadLocalContext() { - CONTEXT.remove(); - } } diff --git a/src/main/java/mina/core/ConditionStep.java b/src/main/java/mina/core/ConditionStep.java index a6cc7c7..a47385e 100644 --- a/src/main/java/mina/core/ConditionStep.java +++ b/src/main/java/mina/core/ConditionStep.java @@ -27,6 +27,10 @@ public void check(Object... arguments) { checkCanonical(new MinaEqualsCheck(arguments)); } + public void check() { + checkCanonical(DoNothingCheck.getInstance()); + } + public void checkArguments(MinaArgumentsCheck minaCheck) { checkCanonical(minaCheck); } diff --git a/src/main/java/mina/core/DoNothingCheck.java b/src/main/java/mina/core/DoNothingCheck.java new file mode 100644 index 0000000..6e53487 --- /dev/null +++ b/src/main/java/mina/core/DoNothingCheck.java @@ -0,0 +1,17 @@ +package mina.core; + +public class DoNothingCheck implements MinaCheck { + private static final MinaCheck INSTANCE = new DoNothingCheck(); + + private DoNothingCheck() { + } + + public static MinaCheck getInstance() { + return INSTANCE; + } + + @Override + public void verify(int index, Object[] arguments, Throwable throwable) { + // Do nothing + } +} diff --git a/src/test/java/mina/core/MinaTest.java b/src/test/java/mina/core/MinaTest.java new file mode 100644 index 0000000..8d6de5f --- /dev/null +++ b/src/test/java/mina/core/MinaTest.java @@ -0,0 +1,145 @@ +package mina.core; + + +import mina.subject.EmptyCode; +import mina.subject.Simple; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.atomic.AtomicInteger; + +import static mina.core.Mina.assertAllCalled; +import static mina.core.Mina.on; +import static org.junit.jupiter.api.Assertions.*; +import static org.slf4j.event.Level.*; + +public class MinaTest { + @AfterEach + public void clean() { + Mina.clean(); + } + + @Test + public void testDoNothing() { + on(EmptyCode.class, INFO, "Log something: {}") + .checkArguments(arguments -> assertEquals(3, arguments.length)); + + new EmptyCode().doNothing(); + + assertThrows(AssertionError.class, Mina::assertAllCalled); + } + + @Test + public void testSomething() { + on(Simple.class, TRACE, "Trace something").check(); + on(Simple.class, DEBUG, Simple.TEST_MARKER, "Debug something").check(); + on(Simple.class, INFO, "This is a {} log").check(3); + on(Simple.class, WARN, "Warn {}").check((String problem, Throwable throwable) -> { + assertEquals("problem", problem); + assertInstanceOf(Exception.class, throwable); + }); + on(Simple.class, ERROR, "Test error") + .check((Throwable throwable) -> assertInstanceOf(RuntimeException.class, throwable)); + + new Simple().doSomething(); + + assertAllCalled(); + } + + @Test + public void testOnlyLoggerCondition() { + on(Simple.class).check(); + new Simple().doSingleLog(); + assertAllCalled(); + } + + @Test + public void testOnlyLevelCondition() { + on(INFO).check(); + new Simple().doSingleLog(); + assertAllCalled(); + } + + @Test + public void testLoggerLevelCondition() { + on(Simple.class, INFO).check(); + new Simple().doSingleLog(); + assertAllCalled(); + } + + @Test + public void testNoCondition() { + on().check(); + new Simple().doSingleLog(); + assertAllCalled(); + } + + @Test + public void testPartialCondition() { + on(Simple.class, DEBUG, Simple.MARKER_1).check(1); + on(Simple.class, Simple.MARKER_2, "message 2 {}").check(2); + on(Simple.class, Simple.MARKER_3).check(3); + on(Simple.class, "message 4 {}").check(4); + on(INFO, Simple.MARKER_1, "message 5 {}").check(5); + on(INFO, Simple.MARKER_2).check(6); + on(INFO, "message 7 {}").check(7); + on(Simple.MARKER_1, "message 8 {}").check(8); + on(Simple.TEST_MARKER).check(9); + on("message 10 {}").check(10); + + new Simple().doConditions(); + assertAllCalled(); + } + + @Test + public void testIndex() { + AtomicInteger count = new AtomicInteger(); + on(Simple.class).checkCanonical((index, arguments, throwable) -> assertEquals(count.incrementAndGet(), index)); + + new Simple().doSomething(); + + assertAllCalled(); + } + + @Test + public void testPartialLoggerName() { + on("mina.subject", INFO, null, null).check(); + + new Simple().doSomething(); + + assertAllCalled(); + } + + @Test + public void testException() { + on(ERROR) + .checkThrowable((throwable) -> assertInstanceOf(RuntimeException.class, throwable)); + + new Simple().doException(); + + assertAllCalled(); + } + + @Test + public void testExceptionWithArguments() { + on(ERROR) + .checkArguments((arguments, throwable) -> { + assertInstanceOf(RuntimeException.class, throwable); + assertEquals("Vitalii", arguments[0]); + assertNull(arguments[1]); + }); + + new Simple().doException(); + + assertAllCalled(); + } + + @Test + public void testForbidden() { + on(ERROR).exception(); + + assertThrows(AssertionError.class, () -> new Simple().doException()); + + assertAllCalled(); + } +} diff --git a/src/test/java/mina/test/QuadraticEquation.java b/src/test/java/mina/example/QuadraticEquation.java similarity index 97% rename from src/test/java/mina/test/QuadraticEquation.java rename to src/test/java/mina/example/QuadraticEquation.java index ea81ded..7d2f950 100644 --- a/src/test/java/mina/test/QuadraticEquation.java +++ b/src/test/java/mina/example/QuadraticEquation.java @@ -1,4 +1,4 @@ -package mina.test; +package mina.example; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/test/java/mina/test/QuadraticEquationTest.java b/src/test/java/mina/example/QuadraticEquationTest.java similarity index 97% rename from src/test/java/mina/test/QuadraticEquationTest.java rename to src/test/java/mina/example/QuadraticEquationTest.java index bf181ff..89d6b02 100644 --- a/src/test/java/mina/test/QuadraticEquationTest.java +++ b/src/test/java/mina/example/QuadraticEquationTest.java @@ -1,4 +1,4 @@ -package mina.test; +package mina.example; import mina.core.Mina; import org.junit.jupiter.api.AfterEach; diff --git a/src/test/java/mina/test/EmptyCode.java b/src/test/java/mina/subject/EmptyCode.java similarity index 79% rename from src/test/java/mina/test/EmptyCode.java rename to src/test/java/mina/subject/EmptyCode.java index c2fc975..a1d9030 100644 --- a/src/test/java/mina/test/EmptyCode.java +++ b/src/test/java/mina/subject/EmptyCode.java @@ -1,4 +1,4 @@ -package mina.test; +package mina.subject; public class EmptyCode { public void doNothing() { diff --git a/src/test/java/mina/subject/Simple.java b/src/test/java/mina/subject/Simple.java new file mode 100644 index 0000000..f9a208f --- /dev/null +++ b/src/test/java/mina/subject/Simple.java @@ -0,0 +1,67 @@ +package mina.subject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.slf4j.Marker; +import org.slf4j.MarkerFactory; + +public class Simple { + private final Logger log = LoggerFactory.getLogger(Simple.class); + public static final Marker TEST_MARKER = MarkerFactory.getMarker("TEST"); + + public static final Marker MARKER_1 = MarkerFactory.getMarker("MARKER_1"); + public static final Marker MARKER_2 = MarkerFactory.getMarker("MARKER_2"); + public static final Marker MARKER_3 = MarkerFactory.getMarker("MARKER_3"); + + public void doSomething() { + log.trace("Trace something"); + log.debug(TEST_MARKER, "Debug something"); + log.info("This is a {} log", 3); + log.warn("Warn {}", "problem", new Exception("Test exception")); + log.error("Test error", new RuntimeException("Test runtime exception")); + } + + public void doSingleLog() { + log.info("message"); + } + + public void doConditions() { + // Logger, level, marker + log.debug(MARKER_1, "message 1 {}", 1); + + // Logger, marker, message + log.debug(MARKER_2, "message 2 {}", 2); + + // Logger marker + log.debug(MARKER_3, "message 3 {}", 3); + + // Logger, message + log.debug("message 4 {}", 4); + + // Level, marker, message + log.info(MARKER_1, "message 5 {}", 5); + + // Level, marker + log.info(MARKER_2, "message 6 {}", 6); + + // Level message + log.info("message 7 {}", 7); + + // Marker, message + log.warn(MARKER_1, "message 8 {}", 8); + + // Just marker + log.warn(TEST_MARKER, "message 9 {}", 9); + + // Just message + log.warn("message 10 {}", 10); + } + + public void doException() { + try { + throw new RuntimeException("Test exception"); + } catch (Exception e) { + log.error("Test error (c) {} {} {}", "Vitalii", null, "Samolovskikh", e); + } + } +} diff --git a/src/test/java/mina/test/MinaTest.java b/src/test/java/mina/test/MinaTest.java deleted file mode 100644 index 3a2b703..0000000 --- a/src/test/java/mina/test/MinaTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package mina.test; - - -import mina.core.Mina; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Test; -import org.slf4j.event.Level; - -import static org.junit.jupiter.api.Assertions.*; - -public class MinaTest { - @AfterEach - public void clean() { - Mina.clean(); - } - - @Test - public void testDoNothing() { - Mina - .on(EmptyCode.class, Level.INFO, "Log something: {}") - .checkArguments(arguments -> assertEquals(3, arguments.length)); - - new EmptyCode().doNothing(); - - assertThrows(AssertionError.class, Mina::assertAllCalled); - } - - @Test - public void testSomething() { - Mina - .on("mina.test.Simple", Level.INFO, null, "My first test with {}") - .check("Mina"); - - new Simple().doSomething(); - } - - @Test - public void testIndex() { - Mina - .on("mina.test.Simple", Level.INFO, null, "My first test with {}") - .checkCanonical((index, arguments, throwable) -> { - assertEquals("Mina", arguments[0]); - assertEquals(1, index); - }); - - new Simple().doSomething(); - } - - @Test - public void testLoggerClass() { - Mina - .on(Simple.class, Level.INFO, "My first test with {}") - .check(text -> assertEquals("Mina", text)); - - new Simple().doSomething(); - } - - @Test - public void testPartialLoggerName() { - Mina - .on("mina.test", Level.INFO, null, "My first test with {}") - .check(text -> assertEquals("Mina", text)); - - new Simple().doSomething(); - } - - @Test - public void testException() { - Mina - .on(Level.ERROR) - .checkThrowable((throwable) -> assertInstanceOf(RuntimeException.class, throwable)); - - new Simple().doException(); - } - - @Test - public void testExceptionWithArguments() { - Mina - .on(Level.ERROR) - .checkArguments((arguments, throwable) -> { - assertInstanceOf(RuntimeException.class, throwable); - assertEquals("Vitalii", arguments[0]); - assertNull(arguments[1]); - }); - - new Simple().doException(); - } - - @Test - public void testForbidden() { - Mina.on(Level.ERROR).exception(); - - assertThrows(AssertionError.class, () -> new Simple().doException()); - } -} diff --git a/src/test/java/mina/test/Simple.java b/src/test/java/mina/test/Simple.java deleted file mode 100644 index 81170f5..0000000 --- a/src/test/java/mina/test/Simple.java +++ /dev/null @@ -1,20 +0,0 @@ -package mina.test; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class Simple { - private final Logger log = LoggerFactory.getLogger(Simple.class); - - public void doSomething() { - log.info("My first test with {}", "Mina"); - } - - public void doException() { - try { - throw new RuntimeException("Test exception"); - } catch (Exception e) { - log.error("Test error (c) {} {} {}", "Vitalii", null, "Samolovskikh", e); - } - } -}