1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-12-07 03:21:08 +01:00

Add Integration Test Framework

and resurrect a few integration tests.
This commit is contained in:
Florian Schmaus 2015-03-18 09:52:33 +01:00
parent 4e6fbe7293
commit b8f046706b
34 changed files with 2333 additions and 100 deletions

View file

@ -0,0 +1,36 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.igniterealtime.smack.inttest;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
public class DummySmackIntegrationTestFramework extends SmackIntegrationTestFramework {
public DummySmackIntegrationTestFramework(Configuration configuration) {
super(configuration);
}
@Override
protected SmackIntegrationTestEnvironment prepareEnvironment() {
return new SmackIntegrationTestEnvironment(null, null, testRunResult.getTestRunId(), config);
}
@Override
protected void disconnectAndMaybeDelete(XMPPTCPConnection connection) {
// This method is a no-op in DummySmackIntegrationTestFramework
}
}

View file

@ -0,0 +1,33 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.igniterealtime.smack.inttest;
import org.jxmpp.jid.JidTestUtil;
public class SmackIntegrationTestUnitTestUtil {
public static DummySmackIntegrationTestFramework getFrameworkForUnitTest(Class<? extends AbstractSmackIntTest> unitTest) {
// @formatter:off
Configuration configuration = Configuration.builder()
.setService(JidTestUtil.DOMAIN_BARE_JID_1)
.setUsernamesAndPassword("dummy1", "dummy1pass", "dummy2", "dummy2pass")
.addEnabledTest(unitTest).build();
// @formatter:on
return new DummySmackIntegrationTestFramework(configuration);
}
}

View file

@ -0,0 +1,98 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.igniterealtime.smack.inttest.unittest;
import static org.igniterealtime.smack.inttest.SmackIntegrationTestUnitTestUtil.getFrameworkForUnitTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
import org.igniterealtime.smack.inttest.DummySmackIntegrationTestFramework;
import org.igniterealtime.smack.inttest.FailedTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
import org.igniterealtime.smack.inttest.SmackIntegrationTestFramework.TestRunResult;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
import org.jivesoftware.smack.packet.XMPPError;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
public class SmackIntegrationTestFrameworkUnitTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
@Test
public void throwsRuntimeExceptionsTest() throws KeyManagementException, NoSuchAlgorithmException, SmackException,
IOException, XMPPException, InterruptedException {
expectedException.expect(RuntimeException.class);
expectedException.expectMessage(ThrowsRuntimeExceptionDummyTest.RUNTIME_EXCEPTION_MESSAGE);
DummySmackIntegrationTestFramework sinttest = getFrameworkForUnitTest(ThrowsRuntimeExceptionDummyTest.class);
sinttest.run();
}
public static class ThrowsRuntimeExceptionDummyTest extends AbstractSmackIntegrationTest {
public ThrowsRuntimeExceptionDummyTest(SmackIntegrationTestEnvironment environment) {
super(environment);
}
public static final String RUNTIME_EXCEPTION_MESSAGE = "Dummy RuntimeException";
@SmackIntegrationTest
public void throwRuntimeExceptionTest() {
throw new RuntimeException(RUNTIME_EXCEPTION_MESSAGE);
}
}
@Test
public void logsNonFatalExceptionTest() throws KeyManagementException, NoSuchAlgorithmException, SmackException,
IOException, XMPPException, InterruptedException {
DummySmackIntegrationTestFramework sinttest = getFrameworkForUnitTest(ThrowsNonFatalExceptionDummyTest.class);
TestRunResult testRunResult = sinttest.run();
List<FailedTest> failedTests = testRunResult.getFailedTests();
assertEquals(1, failedTests.size());
FailedTest failedTest = failedTests.get(0);
assertTrue(failedTest.failureReason instanceof XMPPErrorException);
XMPPErrorException ex = (XMPPErrorException) failedTest.failureReason;
assertEquals(XMPPError.Condition.bad_request, ex.getXMPPError().getCondition());
assertEquals(ThrowsNonFatalExceptionDummyTest.DESCRIPTIVE_TEXT, ex.getXMPPError().getDescriptiveText());
}
public static class ThrowsNonFatalExceptionDummyTest extends AbstractSmackIntegrationTest {
public static final String DESCRIPTIVE_TEXT = "I'm not fatal";
public ThrowsNonFatalExceptionDummyTest(SmackIntegrationTestEnvironment environment) {
super(environment);
}
@SmackIntegrationTest
public void throwRuntimeExceptionTest() throws XMPPErrorException {
throw new XMPPException.XMPPErrorException(
XMPPError.from(XMPPError.Condition.bad_request, DESCRIPTIVE_TEXT));
}
}
}

View file

@ -0,0 +1,70 @@
/**
*
* Copyright 2015 Florian Schmaus
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.igniterealtime.smack.inttest.util;
import static org.junit.Assert.assertEquals;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeoutException;
import org.jivesoftware.smack.util.Async;
import org.junit.Test;
public class ResultSyncPointTest {
@Test
public void testResultSyncPoint() throws InterruptedException, TimeoutException, Exception {
final String result = "Hip Hip Hurrary!!111!";
final CyclicBarrier barrier = new CyclicBarrier(2);
final ResultSyncPoint<String, Exception> rsp = new ResultSyncPoint<>();
Async.go(new Async.ThrowingRunnable() {
@Override
public void runOrThrow() throws InterruptedException, BrokenBarrierException {
barrier.await();
rsp.signal(result);
}
});
barrier.await();
String receivedResult = rsp.waitForResult(60 * 1000);
assertEquals(result, receivedResult);
}
@Test(expected=TestException.class)
public void exceptionTestResultSyncPoint() throws InterruptedException, TimeoutException, Exception {
final CyclicBarrier barrier = new CyclicBarrier(2);
final ResultSyncPoint<String, TestException> rsp = new ResultSyncPoint<>();
Async.go(new Async.ThrowingRunnable() {
@Override
public void runOrThrow() throws InterruptedException, BrokenBarrierException {
barrier.await();
rsp.signal(new TestException());
}
});
barrier.await();
rsp.waitForResult(60 * 1000);
}
private static class TestException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
}
}