Skip to content

Commit 7d2e623

Browse files
committed
Add silent mode option for testing
DEVSIX-8007
1 parent 637f3e6 commit 7d2e623

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

pdftest/src/main/java/com/itextpdf/test/LogListener.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,20 @@ This file is part of the iText (R) project.
4545
public class LogListener extends TestWatcher {
4646

4747
private static final String ROOT_ITEXT_PACKAGE = "com.itextpdf";
48+
private static final String ITEXT_LICENCING_PACKAGE = "com.itextpdf.licensing";
49+
private static final String ITEXT_ACTIONS_PACKAGE = "com.itextpdf.commons.actions.processors";
50+
private static final String TOKEN_ITEXT_LOGLEVEL = "ITEXT_SILENT_MODE";
4851

49-
private final CustomListAppender<ILoggingEvent> listAppender = new CustomListAppender<>();
50-
52+
private final CustomListAppender<ILoggingEvent> listAppender;
5153
private final ILoggerFactory lc = LoggerFactory.getILoggerFactory();
5254

5355
private Map<Logger, Map<String, Appender<ILoggingEvent>>> appenders;
5456

57+
public LogListener() {
58+
String logLevel = getPropertyOrEnvironmentVariable(TOKEN_ITEXT_LOGLEVEL);
59+
listAppender = new CustomListAppender<>(parseSilentMode(logLevel));
60+
}
61+
5562
@Override
5663
protected void starting(Description description) {
5764
before(description);
@@ -63,6 +70,13 @@ protected void finished(Description description) {
6370
after();
6471
}
6572

73+
private boolean parseSilentMode(String logLevel) {
74+
if (logLevel == null){
75+
return false;
76+
}
77+
return logLevel.equalsIgnoreCase("TRUE");
78+
}
79+
6680
private int contains(LogMessage loggingStatement) {
6781
List<ILoggingEvent> list = listAppender.list;
6882
int index = 0;
@@ -159,9 +173,23 @@ private void checkLogMessages(Description description) {
159173
}
160174
}
161175

176+
private static String getPropertyOrEnvironmentVariable(String name) {
177+
String s = System.getProperty(name);
178+
if (s == null) {
179+
s = System.getenv(name);
180+
}
181+
return s;
182+
}
183+
162184
private class CustomListAppender<E> extends ListAppender<ILoggingEvent> {
163185

164186
private Map<String, Boolean> expectedTemplates = new HashMap<>();
187+
private final boolean runTestsInSilentMode;
188+
189+
private CustomListAppender(boolean runTestsInSilentMode) {
190+
this.runTestsInSilentMode = runTestsInSilentMode;
191+
}
192+
165193

166194
public void setExpectedTemplates(Map<String, Boolean> expectedTemplates) {
167195
this.expectedTemplates.clear();
@@ -175,14 +203,27 @@ public void clear() {
175203

176204
protected void append(ILoggingEvent e) {
177205
if(!isExpectedMessageQuiet(e.getMessage())){
178-
System.out.println(e.getLoggerName() + " " + e.getLevel() + " " + e.getMessage());
206+
if (shouldPrintMessage(e)){
207+
System.out.println(e.getLoggerName() + " " + e.getLevel() + " " + e.getMessage());
208+
}
179209
}
180210
printStackTraceIfAny(e);
181211
if (e.getLevel().isGreaterOrEqual(Level.WARN) || isExpectedMessage(e.getMessage())) {
182212
this.list.add(e);
183213
}
184214
}
185215

216+
private boolean shouldPrintMessage(ILoggingEvent level) {
217+
//Those 2 if statements are when we rely on the logmessages being printed for certain tests
218+
if (level.getLoggerName().startsWith(ITEXT_LICENCING_PACKAGE)){
219+
return true;
220+
}
221+
if (level.getLoggerName().startsWith(ITEXT_ACTIONS_PACKAGE)){
222+
return true;
223+
}
224+
return !runTestsInSilentMode;
225+
}
226+
186227
private boolean isExpectedMessage(String message) {
187228
if (message != null) {
188229
for (String template : expectedTemplates.keySet()) {

0 commit comments

Comments
 (0)