Skip to content

Commit

Permalink
ForbiddenCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
kefirfromperm committed Jul 11, 2024
1 parent c9e4b58 commit 6a38253
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 33 deletions.
32 changes: 0 additions & 32 deletions src/main/java/mina/context/MinaContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,14 @@
import org.slf4j.Marker;
import org.slf4j.event.Level;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class MinaContext {
private final Map<MinaCondition, MinaCheck> verifyCalls = new ConcurrentHashMap<>();
private final Set<MinaCondition> forbiddenCalls = new CopyOnWriteArraySet<>();
private final Map<MinaCondition, AtomicInteger> counters = new ConcurrentHashMap<>();

public MinaContext() {
Expand All @@ -37,27 +30,6 @@ public void handle(
verification.verify(index, arguments, throwable);
}
}

for (MinaCondition condition : forbiddenCalls) {
if (condition.match(loggerName, level, marker, messagePattern)) {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
writer.println(MessageFormat.format("A forbidden log was caught on condition [{0}].", condition));
writer.println(MessageFormat.format("logger name = [{0}]", loggerName));
writer.println(MessageFormat.format("level = [{0}]", level));
writer.println(MessageFormat.format("marker = [{0}]", marker));
writer.println(MessageFormat.format("message pattern = [{0}]", messagePattern));
writer.println(
MessageFormat.format("arguments = [{0}]", Arrays.stream(arguments).map(String::valueOf).collect(
Collectors.joining(","))));
if (throwable != null) {
writer.print("Exception: ");
writer.println(throwable.getMessage());
throwable.printStackTrace(writer);
}
throw new AssertionError(stringWriter.toString());
}
}
}

public void verifyLost() {
Expand All @@ -81,8 +53,4 @@ public void verifyLost() {
public void addVerifyCall(MinaCondition condition, MinaCheck verification) {
verifyCalls.put(condition, verification);
}

public void addForbiddenCall(MinaCondition condition) {
forbiddenCalls.add(condition);
}
}
2 changes: 1 addition & 1 deletion src/main/java/mina/core/ConditionStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ public void checkThrowable(MinaThrowableCheck minaCheck) {
}

public void exception() {
context.addForbiddenCall(condition);
checkCanonical(new ForbiddenCheck(condition));
}
}
36 changes: 36 additions & 0 deletions src/main/java/mina/core/ForbiddenCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package mina.core;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.stream.Collectors;

public class ForbiddenCheck implements MinaCheck {
private final MinaCondition condition;

public ForbiddenCheck(MinaCondition condition) {
this.condition = condition;
}

@Override
public void verify(int index, Object[] arguments, Throwable throwable) {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter);
writer.println(MessageFormat.format("A forbidden log was caught on condition [{0}].", condition));
if (arguments != null) {
writer.println(
MessageFormat.format(
"arguments = [{0}]",
Arrays.stream(arguments).map(String::valueOf).collect(Collectors.joining(","))
)
);
}
if (throwable != null) {
writer.print("Exception: ");
writer.println(throwable.getMessage());
throwable.printStackTrace(writer);
}
throw new AssertionError(stringWriter.toString());
}
}

0 comments on commit 6a38253

Please sign in to comment.