Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[sint] Add connection pre- and postconditions #628

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.SmackException.NoResponseException;
import org.jivesoftware.smack.SmackException.NotConnectedException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
Expand Down Expand Up @@ -406,6 +407,9 @@ private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
continue;
}

ConcreteTest.Executor precondition = null;
ConcreteTest.Executor postcondition = null;

XmppConnectionDescriptor<?, ?, ?> specificLowLevelConnectionDescriptor = null;
final TestType testType;
if (test instanceof AbstractSmackSpecificLowLevelIntegrationTest) {
Expand All @@ -415,7 +419,13 @@ private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
} else if (test instanceof AbstractSmackLowLevelIntegrationTest) {
testType = TestType.LowLevel;
} else if (test instanceof AbstractSmackIntegrationTest) {
precondition = () -> { if (!((AbstractSmackIntegrationTest) test).connections.stream().allMatch(XMPPConnection::isConnected)) {
throw new IOException("Cannot execute test of '" + test.getClass() + "', as not all connections are connected. A likely cause is that a previous test caused one or more connections to be disconnected.");
}};
testType = TestType.Normal;
postcondition = () -> { if (!((AbstractSmackIntegrationTest) test).connections.stream().allMatch(XMPPConnection::isConnected)) {
throw new IOException("Test execution of '" + test.getClass() + "' completed, but left one or more connections disconnected. This will prevent issues for tests to be executed next.");
}};
} else {
throw new AssertionError();
}
Expand Down Expand Up @@ -474,7 +484,7 @@ private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
switch (testType) {
case Normal: {
ConcreteTest.Executor concreteTestExecutor = () -> testMethod.invoke(test);
ConcreteTest concreteTest = new ConcreteTest(testType, testMethod, concreteTestExecutor);
ConcreteTest concreteTest = new ConcreteTest(testType, testMethod, concreteTestExecutor, precondition, postcondition);
concreteTests.add(concreteTest);
}
break;
Expand All @@ -489,7 +499,7 @@ private void runTests(Set<Class<? extends AbstractSmackIntTest>> classes)
case SpecificLowLevel: {
ConcreteTest.Executor concreteTestExecutor = () -> invokeSpecificLowLevel(
lowLevelTestMethod, (AbstractSmackSpecificLowLevelIntegrationTest<?>) test);
ConcreteTest concreteTest = new ConcreteTest(testType, testMethod, concreteTestExecutor);
ConcreteTest concreteTest = new ConcreteTest(testType, testMethod, concreteTestExecutor, precondition, postcondition);
concreteTests.add(concreteTest);
break;
}
Expand Down Expand Up @@ -553,7 +563,15 @@ private void runConcreteTest(ConcreteTest concreteTest)
LOGGER.info(concreteTest + " Start");
long testStart = System.currentTimeMillis();
try {
if (concreteTest.precondition != null) {
concreteTest.precondition.execute();
}
concreteTest.executor.execute();

if (concreteTest.postcondition != null) {
concreteTest.postcondition.execute();
}

long testEnd = System.currentTimeMillis();
LOGGER.info(concreteTest + " Success");
testRunResult.successfulIntegrationTests.add(new SuccessfulTest(concreteTest, testStart, testEnd, null));
Expand Down Expand Up @@ -623,7 +641,7 @@ private List<ConcreteTest> invokeLowLevel(LowLevelTestMethod lowLevelTestMethod,
}

ConcreteTest.Executor executor = () -> lowLevelTestMethod.invoke(test, connectionDescriptor);
ConcreteTest concreteTest = new ConcreteTest(TestType.LowLevel, lowLevelTestMethod.testMethod, executor, connectionDescriptor.getNickname());
ConcreteTest concreteTest = new ConcreteTest(TestType.LowLevel, lowLevelTestMethod.testMethod, executor, null, null, connectionDescriptor.getNickname());
resultingConcreteTests.add(concreteTest);
}

Expand Down Expand Up @@ -818,11 +836,15 @@ public static final class ConcreteTest {
private final Method method;
private final Executor executor;
private final List<String> subdescriptons;
private final Executor precondition;
private final Executor postcondition;

private ConcreteTest(TestType testType, Method method, Executor executor, String... subdescriptions) {
private ConcreteTest(TestType testType, Method method, Executor executor, Executor precondition, Executor postcondition, String... subdescriptions) {
this.testType = testType;
this.method = method;
this.executor = executor;
this.precondition = precondition;
this.postcondition = postcondition;
this.subdescriptons = List.of(subdescriptions);
}

Expand Down
Loading