Skip to content

Commit c61418d

Browse files
authored
Merge pull request #1218 from quickfix-j/copilot/add-logging-for-specific-test-failures
`AcceptanceTestSuite`: suppress logs per test, only emit on failure
2 parents 17ed2c1 + 359ec18 commit c61418d

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

quickfixj-core/src/test/java/quickfix/test/acceptance/AcceptanceTestSuite.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import java.util.Map;
2828
import java.util.concurrent.ExecutorService;
2929
import java.util.concurrent.Executors;
30+
import java.util.logging.Handler;
31+
import java.util.logging.Level;
32+
import java.util.logging.LogRecord;
3033

3134
/**
3235
* Acceptance test suite that programmatically discovers and runs FIX protocol tests.
@@ -42,6 +45,7 @@ public class AcceptanceTestSuite {
4245
private static final String ATEST_TIMEOUT_KEY = "atest.timeout";
4346
private static final String ATEST_TRANSPORT_KEY = "atest.transport";
4447
private static final String ATEST_SKIPSLOW_KEY = "atest.skipslow";
48+
private static final String ATEST_VERBOSE_KEY = "atest.verbose";
4549
private static final Logger log = LoggerFactory.getLogger(AcceptanceTestSuite.class);
4650
private static final String acceptanceTestResourcePath = "quickfix/test/acceptance/definitions/";
4751
private static final String acceptanceTestBaseDir = AcceptanceTestSuite.class.getClassLoader().getResource(acceptanceTestResourcePath).getPath();
@@ -50,6 +54,7 @@ public class AcceptanceTestSuite {
5054
private static int port = AvailablePortFinder.getNextAvailable();
5155

5256
private final boolean skipSlowTests;
57+
private final boolean verboseLogging;
5358
private final boolean multithreaded;
5459

5560
private final Map<Object, Object> overridenProperties;
@@ -81,20 +86,51 @@ public void run(TestResult result) {
8186
result.startTest(this);
8287
TestConnection connection = null;
8388
String failureString = "test " + filename + " failed with message: ";
89+
90+
log.info("Running test {}, filename : {}", this.testname, this.filename);
91+
92+
java.util.logging.Logger rootLogger = java.util.logging.Logger.getLogger("");
93+
Handler[] existingHandlers = rootLogger.getHandlers();
94+
Level[] originalLevels = new Level[existingHandlers.length];
95+
CapturingLogHandler capturingHandler = null;
96+
if (!verboseLogging) {
97+
for (int i = 0; i < existingHandlers.length; i++) {
98+
originalLevels[i] = existingHandlers[i].getLevel();
99+
existingHandlers[i].setLevel(Level.OFF);
100+
}
101+
capturingHandler = new CapturingLogHandler();
102+
rootLogger.addHandler(capturingHandler);
103+
}
104+
105+
boolean testFailed = false;
84106
try {
85-
log.info("Running test {}, filename : {}", this.testname, this.filename);
86107
connection = new TestConnection();
87108
List<TestStep> testSteps = load(filename);
88109
for (TestStep testStep : testSteps) {
89110
testStep.run(result, connection);
90111
}
91112
} catch (AssertionFailedError e) {
113+
testFailed = true;
92114
result.addFailure(this, e);
93115
log.error(failureString + e.getMessage());
94116
} catch (Throwable t) {
117+
testFailed = true;
95118
result.addError(this, t);
96119
log.error(failureString + t.getMessage());
97120
} finally {
121+
if (!verboseLogging) {
122+
rootLogger.removeHandler(capturingHandler);
123+
for (int i = 0; i < existingHandlers.length; i++) {
124+
existingHandlers[i].setLevel(originalLevels[i]);
125+
}
126+
if (testFailed) {
127+
for (LogRecord record : capturingHandler.drainRecords()) {
128+
for (Handler handler : existingHandlers) {
129+
handler.publish(record);
130+
}
131+
}
132+
}
133+
}
98134
if (connection != null) {
99135
connection.tearDown();
100136
}
@@ -150,6 +186,7 @@ public AcceptanceTestSuite(String testDirectory, boolean multithreaded, Map<Obje
150186
}
151187

152188
this.skipSlowTests = Boolean.getBoolean(ATEST_SKIPSLOW_KEY);
189+
this.verboseLogging = Boolean.getBoolean(ATEST_VERBOSE_KEY);
153190

154191
addTests(new File(acceptanceTestBaseDir + testDirectory + "/fix40"));
155192
addTests(new File(acceptanceTestBaseDir + testDirectory + "/fix41"));
@@ -209,6 +246,29 @@ private boolean isTestSkipped(File file) {
209246
|| file.getName().contains("SendTestRequest"));
210247
}
211248

249+
private static final class CapturingLogHandler extends Handler {
250+
private final List<LogRecord> records = new ArrayList<>();
251+
252+
@Override
253+
public synchronized void publish(LogRecord record) {
254+
records.add(record);
255+
}
256+
257+
@Override
258+
public void flush() {
259+
}
260+
261+
@Override
262+
public void close() throws SecurityException {
263+
}
264+
265+
public synchronized List<LogRecord> drainRecords() {
266+
List<LogRecord> drained = new ArrayList<>(records);
267+
records.clear();
268+
return drained;
269+
}
270+
}
271+
212272
private static final class AcceptanceTestServerSetUp extends TestSetup {
213273
private final AcceptanceTestSuite acceptanceSuite;
214274
private final ExecutorService executor = Executors.newSingleThreadExecutor();

0 commit comments

Comments
 (0)