Skip to content

Commit cd2e2d5

Browse files
Copilotchrjohn
andcommitted
Refactor AcceptanceTestSuite to use composition instead of inheritance
Co-authored-by: chrjohn <[email protected]>
1 parent dd948d3 commit cd2e2d5

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,16 @@
2929
import java.util.concurrent.Executors;
3030

3131
/**
32-
* Note on inheritance:
33-
* This class intentionally extends junit.framework.TestSuite (JUnit 3) even though
34-
* it is run with the JUnit 4 runner {@link org.junit.runners.AllTests}.
35-
*
36-
* Rationale:
37-
* - The static {@code suite()} method below programmatically builds a hierarchical
38-
* TestSuite tree and wraps instances of this class into {@link junit.extensions.TestSetup}
39-
* via {@code new AcceptanceTestServerSetUp(new AcceptanceTestSuite(...))}.
40-
* - {@link junit.extensions.TestSetup} expects a {@link junit.framework.Test} and later
41-
* casts {@code getTest()} to {@link junit.framework.TestSuite}. Therefore
42-
* {@code AcceptanceTestSuite} must itself be a TestSuite instance.
43-
* - The AllTests runner discovers and executes the returned {@link junit.framework.Test}
44-
* from {@code suite()}; the inheritance here is required for composition, not for discovery.
32+
* Acceptance test suite that programmatically discovers and runs FIX protocol tests.
33+
* <p>
34+
* This class uses composition to hold a {@link TestSuite} internally, rather than
35+
* extending it directly. The static {@code suite()} method builds a hierarchical
36+
* test tree that is executed via the JUnit 4 {@link AllTests} runner.
4537
*/
4638
@RunWith(AllTests.class)
47-
public class AcceptanceTestSuite extends TestSuite {
39+
public class AcceptanceTestSuite {
40+
41+
private final TestSuite delegate;
4842
private static final String ATEST_TIMEOUT_KEY = "atest.timeout";
4943
private static final String ATEST_TRANSPORT_KEY = "atest.transport";
5044
private static final String ATEST_SKIPSLOW_KEY = "atest.skipslow";
@@ -149,7 +143,7 @@ public AcceptanceTestSuite(String testDirectory, boolean multithreaded, Map<Obje
149143
SystemTime.setTimeSource(null);
150144

151145
String name = testDirectory.substring(testDirectory.lastIndexOf(File.separatorChar) + 1);
152-
this.setName(name + (multithreaded ? "-threaded" : ""));
146+
this.delegate = new TestSuite(name + (multithreaded ? "-threaded" : ""));
153147
Long timeout = Long.getLong(ATEST_TIMEOUT_KEY);
154148
if (timeout != null) {
155149
ExpectMessageStep.TIMEOUT_IN_MS = timeout;
@@ -178,17 +172,24 @@ protected void addTest(String name) {
178172
addTests(new File(acceptanceTestBaseDir + "server/" + name));
179173
}
180174

175+
/**
176+
* Returns the internal {@link TestSuite} that contains all the acceptance tests.
177+
*/
178+
public TestSuite getTestSuite() {
179+
return delegate;
180+
}
181+
181182
protected void addTests(File directory) {
182183
if (!directory.exists())
183184
return;
184185
if (!directory.isDirectory()) {
185-
addTest(new AcceptanceTest(directory.getPath()));
186+
delegate.addTest(new AcceptanceTest(directory.getPath()));
186187
} else {
187188
if (directory.exists()) {
188189
File[] files = directory.listFiles(new TestDefinitionFilter());
189190
for (File file : files) {
190191
if (!file.isDirectory() && !isTestSkipped(file)) {
191-
addTest(new AcceptanceTest(file.getPath()));
192+
delegate.addTest(new AcceptanceTest(file.getPath()));
192193
}
193194
}
194195
for (File file : files) {
@@ -209,21 +210,24 @@ private boolean isTestSkipped(File file) {
209210
}
210211

211212
private static final class AcceptanceTestServerSetUp extends TestSetup {
212-
private final boolean threaded;
213-
private final Map<Object, Object> overridenProperties;
213+
private final AcceptanceTestSuite acceptanceSuite;
214214
private final ExecutorService executor = Executors.newSingleThreadExecutor();
215215

216216
private ATServer server;
217217

218218
private AcceptanceTestServerSetUp(AcceptanceTestSuite suite) {
219-
super(suite);
220-
this.threaded = suite.isMultithreaded();
221-
this.overridenProperties = suite.getOverridenProperties();
219+
super(suite.getTestSuite());
220+
this.acceptanceSuite = suite;
222221
}
223222

224223
protected void setUp() throws Exception {
225224
super.setUp();
226-
server = new ATServer((TestSuite) getTest(), threaded, transportType, port, overridenProperties);
225+
server = new ATServer(
226+
acceptanceSuite.getTestSuite(),
227+
acceptanceSuite.isMultithreaded(),
228+
transportType,
229+
port,
230+
acceptanceSuite.getOverridenProperties());
227231
server.setUsingMemoryStore(true);
228232
executor.execute(server);
229233
server.waitForInitialization();

0 commit comments

Comments
 (0)