Skip to content

Commit

Permalink
Structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kefirfromperm committed Jul 11, 2024
1 parent 6a38253 commit 209a288
Show file tree
Hide file tree
Showing 11 changed files with 239 additions and 124 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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)

5 changes: 1 addition & 4 deletions src/main/java/mina/context/MinaContextHolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@ public static void removeContext() {
if (useGlobalContext) {
GLOBAL.set(null);
} else {
removeThreadLocalContext();
CONTEXT.remove();
}
}

private static void removeThreadLocalContext() {
CONTEXT.remove();
}
}
4 changes: 4 additions & 0 deletions src/main/java/mina/core/ConditionStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/mina/core/DoNothingCheck.java
Original file line number Diff line number Diff line change
@@ -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
}
}
145 changes: 145 additions & 0 deletions src/test/java/mina/core/MinaTest.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mina.test;
package mina.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mina.test;
package mina.example;

import mina.core.Mina;
import org.junit.jupiter.api.AfterEach;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mina.test;
package mina.subject;

public class EmptyCode {
public void doNothing() {
Expand Down
67 changes: 67 additions & 0 deletions src/test/java/mina/subject/Simple.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
95 changes: 0 additions & 95 deletions src/test/java/mina/test/MinaTest.java

This file was deleted.

Loading

0 comments on commit 209a288

Please sign in to comment.