Skip to content

Commit

Permalink
on ... check ... notation
Browse files Browse the repository at this point in the history
  • Loading branch information
kefirfromperm committed Jul 10, 2024
1 parent dfc37ab commit b0f7448
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 45 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,18 @@ public class QuadraticEquation {
}
```

[QuadraticEquation.java](src/test/java/mina/test/QuadraticEquation.java)

Then use Mina in the unit test.

```java
public class QuadraticEquationTest {
@Test
public void testSolve() {
Mina.when(QuadraticEquation.class, DEBUG, "discriminant: {}")
.then(args ->
// Verify discriminant value inside the solve method
assertEquals(9., (double) args[0])
);
// Verify discriminant value inside the solve method
Mina
.on(QuadraticEquation.class, DEBUG, "discriminant: {}")
.check(args -> assertEquals(9., (double) args[0]));

// Run our code
List<Double> roots = new QuadraticEquation().solve(1, -1, -2);
Expand All @@ -65,3 +66,5 @@ public class QuadraticEquationTest {
}
```

[QuadraticEquationTest.java](src/test/java/mina/test/QuadraticEquationTest.java)

138 changes: 120 additions & 18 deletions src/main/java/mina/core/Mina.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,134 @@ public final class Mina {
private Mina() {
}

public static MinaCallBuilder when(
public static MinaBuilder on(
String loggerName,
Level level,
Marker marker,
String messagePattern
) {
return new MinaCallBuilder(
return new MinaBuilder(
MinaContextHolder.getContext(),
new MinaCondition(loggerName, level, marker, messagePattern)
);
}

public static MinaCallBuilder when(Class<?> loggerClass, Level level, String messagePattern) {
return when(
public static MinaBuilder on(
Class<?> loggerClass,
Level level,
Marker marker,
String messagePattern
) {
return on(
loggerClass != null ? loggerClass.getName() : null,
level, null, messagePattern
level, marker, messagePattern
);
}

public static void forbid(
String loggerName,
public static MinaBuilder on(
Class<?> loggerClass,
Level level,
Marker marker
) {
return on(loggerClass, level, marker, null);
}

public static MinaBuilder on(
Class<?> loggerClass,
Level level,
String messagePattern
) {
return on(loggerClass, level, null, messagePattern);
}

public static MinaBuilder on(
Class<?> loggerClass,
Level level
) {
return on(loggerClass, level, null, null);
}

public static MinaBuilder on(
Class<?> loggerClass,
Marker marker,
String messagePattern
) {
MinaContextHolder.getContext().addForbiddenCall(
new MinaCondition(loggerName, level, marker, messagePattern)
return on(
loggerClass, null, marker, messagePattern
);
}

public static MinaBuilder on(
Class<?> loggerClass,
Marker marker
) {
return on(loggerClass, null, marker, null);
}

public static MinaBuilder on(
Class<?> loggerClass,
String messagePattern
) {
return on(loggerClass, null, null, messagePattern);
}

public static MinaBuilder on(
Class<?> loggerClass
) {
return on(loggerClass, null, null, null);
}

public static MinaBuilder on(
Level level,
Marker marker,
String messagePattern
) {
return on((String) null, level, marker, messagePattern);
}

public static MinaBuilder on(
Level level,
Marker marker
) {
return on((String) null, level, marker, null);
}

public static MinaBuilder on(
Level level,
String messagePattern
) {
return on((String) null, level, null, messagePattern);
}

public static MinaBuilder on(
Level level
) {
return on((String) null, level, null, null);
}

public static MinaBuilder on(
Marker marker,
String messagePattern
) {
return on((String) null, null, marker, messagePattern);
}

public static MinaBuilder on(
Marker marker
) {
return on((String) null, null, marker, null);
}

public static MinaBuilder on(
String messagePattern
) {
return on((String) null, null, null, messagePattern);
}

public static MinaBuilder on() {
return on((String) null, null, null, null);
}

public static void assertAllCalled() {
MinaContextHolder.getContext().verifyLost();
}
Expand All @@ -48,29 +146,33 @@ public static void clean() {
MinaContextHolder.removeContext();
}

public static class MinaCallBuilder {
public static class MinaBuilder {
private final MinaContext context;
private final MinaCondition condition;

public MinaCallBuilder(MinaContext context, MinaCondition condition) {
public MinaBuilder(MinaContext context, MinaCondition condition) {
this.context = context;
this.condition = condition;
}

public void then(MinaVerification verification) {
public void check(MinaVerification verification) {
context.addVerifyCall(condition, verification);
}

public void then(MinaArgumentVerification verification) {
then((MinaVerification) verification);
public void check(MinaArgumentVerification verification) {
check((MinaVerification) verification);
}

public void check(MinaArgumentThrowableVerification verification) {
check((MinaVerification) verification);
}

public void then(MinaArgumentThrowableVerification verification) {
then((MinaVerification) verification);
public void checkThrowable(MinaThrowableVerification verification) {
check(verification);
}

public void thenThrowable(MinaThrowableVerification verification) {
then(verification);
public void exception() {
context.addForbiddenCall(condition);
}
}
}
32 changes: 15 additions & 17 deletions src/test/java/mina/test/MinaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ public void clean() {
@Test
public void testDoNothing() {
Mina
.when(EmptyCode.class, Level.INFO, "Log something: {}")
.then(arguments -> {
assertEquals(3, arguments.length);
});
.on(EmptyCode.class, Level.INFO, "Log something: {}")
.check(arguments -> assertEquals(3, arguments.length));

new EmptyCode().doNothing();

Expand All @@ -30,17 +28,17 @@ public void testDoNothing() {
@Test
public void testSomething() {
Mina
.when("mina.test.Simple", Level.INFO, null, "My first test with {}")
.then((arguments) -> assertEquals("Mina", arguments[0]));
.on("mina.test.Simple", Level.INFO, null, "My first test with {}")
.check((arguments) -> assertEquals("Mina", arguments[0]));

new Simple().doSomething();
}

@Test
public void testIndex() {
Mina
.when("mina.test.Simple", Level.INFO, null, "My first test with {}")
.then((index, arguments, throwable) -> {
.on("mina.test.Simple", Level.INFO, null, "My first test with {}")
.check((index, arguments, throwable) -> {
assertEquals("Mina", arguments[0]);
assertEquals(1, index);
});
Expand All @@ -51,35 +49,35 @@ public void testIndex() {
@Test
public void testLoggerClass() {
Mina
.when(Simple.class, Level.INFO, "My first test with {}")
.then((arguments) -> assertEquals("Mina", arguments[0]));
.on(Simple.class, Level.INFO, "My first test with {}")
.check((arguments) -> assertEquals("Mina", arguments[0]));

new Simple().doSomething();
}

@Test
public void testPartialLoggerName() {
Mina
.when("mina.test", Level.INFO, null, "My first test with {}")
.then((arguments) -> assertEquals("Mina", arguments[0]));
.on("mina.test", Level.INFO, null, "My first test with {}")
.check((arguments) -> assertEquals("Mina", arguments[0]));

new Simple().doSomething();
}

@Test
public void testException() {
Mina
.when(null, Level.ERROR, null, null)
.thenThrowable((throwable) -> assertInstanceOf(RuntimeException.class, throwable));
.on(Level.ERROR)
.checkThrowable((throwable) -> assertInstanceOf(RuntimeException.class, throwable));

new Simple().doException();
}

@Test
public void testExceptionWithArguments() {
Mina
.when(null, Level.ERROR, null, null)
.then((arguments, throwable) -> {
.on(Level.ERROR)
.check((arguments, throwable) -> {
assertInstanceOf(RuntimeException.class, throwable);
assertEquals("Vitalii", arguments[0]);
assertNull(arguments[1]);
Expand All @@ -90,7 +88,7 @@ public void testExceptionWithArguments() {

@Test
public void testForbidden() {
Mina.forbid(null, Level.ERROR, null, null);
Mina.on(Level.ERROR).exception();

assertThrows(AssertionError.class, () -> new Simple().doException());
}
Expand Down
9 changes: 4 additions & 5 deletions src/test/java/mina/test/QuadraticEquationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,10 @@
public class QuadraticEquationTest {
@Test
public void testSolve() {
Mina.when(QuadraticEquation.class, DEBUG, "discriminant: {}")
.then(args ->
// Verify discriminant value inside the solve method
assertEquals(9., (double) args[0])
);
// Verify discriminant value inside the solve method
Mina
.on(QuadraticEquation.class, DEBUG, "discriminant: {}")
.check(args -> assertEquals(9., (double) args[0]));

// Run our code
List<Double> roots = new QuadraticEquation().solve(1, -1, -2);
Expand Down

0 comments on commit b0f7448

Please sign in to comment.