use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.
the class OmemoDeviceTest method testEquals.
/**
* Test, if the equals() method works as intended.
*/
@Test
public void testEquals() {
BareJid romeo, juliet, guyUnderTheBalcony;
try {
romeo = JidCreate.bareFrom("romeo@shakespeare.lit");
guyUnderTheBalcony = JidCreate.bareFrom("romeo@shakespeare.lit/underTheBalcony");
juliet = JidCreate.bareFrom("juliet@shakespeare.lit");
} catch (XmppStringprepException e) {
Assert.fail(e.getMessage());
return;
}
OmemoDevice r = new OmemoDevice(romeo, 1);
OmemoDevice g = new OmemoDevice(guyUnderTheBalcony, 1);
OmemoDevice r2 = new OmemoDevice(romeo, 2);
OmemoDevice j = new OmemoDevice(juliet, 3);
OmemoDevice j2 = new OmemoDevice(juliet, 1);
assertTrue(r.equals(g));
assertFalse(r.equals(r2));
assertFalse(j.equals(j2));
assertFalse(j2.equals(r2));
}
use of org.jxmpp.stringprep.XmppStringprepException in project Smack by igniterealtime.
the class PacketWriterTest method shouldBlockAndUnblockTest.
/**
* Make sure that stanza writer does block once the queue reaches
* {@link PacketWriter#QUEUE_SIZE} and that
* {@link PacketWriter#sendStanza(org.jivesoftware.smack.tcp.packet.Packet)} does unblock after the
* interrupt.
*
* @throws InterruptedException if the calling thread was interrupted.
* @throws BrokenBarrierException in case of a broken barrier.
* @throws NotConnectedException if the XMPP connection is not connected.
* @throws XmppStringprepException if the provided string is invalid.
* @throws SecurityException if there was a security violation.
* @throws NoSuchFieldException if there is no such field.
* @throws IllegalAccessException if there was an illegal access.
* @throws IllegalArgumentException if an illegal argument was given.
*/
@Test
public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org");
// Get the protected "Reader reader" field from AbstractXMPPConnection and set it to a dummy Reader,
// as otherwise it will be null when we perform the writer threads init() which will make the StAX parser
// to throw an exception.
Field readerField = AbstractXMPPConnection.class.getDeclaredField("reader");
readerField.setAccessible(true);
readerField.set(connection, DUMMY_READER);
final PacketWriter pw = connection.packetWriter;
BlockingStringWriter blockingStringWriter = new BlockingStringWriter();
connection.setWriter(blockingStringWriter);
connection.packetWriter.init();
// blocking writer.
for (int i = 0; i < XMPPTCPConnection.PacketWriter.QUEUE_SIZE + 1; i++) {
pw.sendStreamElement(StanzaBuilder.buildMessage().build());
}
final CyclicBarrier barrier = new CyclicBarrier(2);
final AtomicReference<Exception> unexpectedThreadExceptionReference = new AtomicReference<>();
final AtomicReference<Exception> expectedThreadExceptionReference = new AtomicReference<>();
shutdown = false;
prematureUnblocked = false;
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
barrier.await();
pw.sendStreamElement(StanzaBuilder.buildMessage().build());
// should only return after the pw was interrupted
if (!shutdown) {
prematureUnblocked = true;
}
} catch (InterruptedException | SmackException.NotConnectedException e) {
// This is the exception we expect.
expectedThreadExceptionReference.set(e);
} catch (BrokenBarrierException e) {
unexpectedThreadExceptionReference.set(e);
}
try {
barrier.await();
} catch (InterruptedException | BrokenBarrierException e) {
unexpectedThreadExceptionReference.set(e);
}
}
});
t.start();
// This barrier is not strictly necessary, but may increases the chances that the threat
// will block before we call shutdown. Otherwise we may get false positives (which is still
// better then false negatives).
barrier.await();
// Not really cool, but may increases the chances for 't' to block in sendStanza.
Thread.sleep(250);
// Shutdown the packetwriter, this will also interrupt the writer thread, which is what we hope to happen in the
// thread created above.
pw.shutdown(false);
shutdown = true;
barrier.await();
t.join(60000);
Exception unexpectedThreadException = unexpectedThreadExceptionReference.get();
try {
if (prematureUnblocked) {
String failureMessage = "Should not unblock before the thread got shutdown.";
if (unexpectedThreadException != null) {
String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
failureMessage += " Unexpected thread exception thrown: " + unexpectedThreadException + "\n" + stacktrace;
}
fail(failureMessage);
} else if (unexpectedThreadException != null) {
String stacktrace = ExceptionUtil.getStackTrace(unexpectedThreadException);
fail("Unexpected thread exception: " + unexpectedThreadException + "\n" + stacktrace);
}
assertNotNull(expectedThreadExceptionReference.get(), "Did not encounter expected exception on sendStreamElement()");
} finally {
blockingStringWriter.unblock();
}
}
Aggregations