mirror of
https://codeberg.org/Mercury-IM/Smack
synced 2025-12-06 21:21:08 +01:00
Improve packet send and result collecting API
Instead of repeating the same pattern, when sending an IQ get/set packet
and collecting the response
PacketFilter filter = new PacketIDFilter(request.getPacketID()),
PacketCollector collector = connection.createPacketCollector(filter);
connection.sendPacket(reg);
IQ result = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());
// Stop queuing results
collector.cancel();
if (result == null) {
throw new XMPPException("No response from server.");
}
else if (result.getType() == IQ.Type.ERROR) {
throw new XMPPException(result.getError());
}
the API got redesigned, so that the above code block can be replaced
with
Packet result = connection.createPacketCollectorAndSend(request).nextResultOrThrow();
This commit is contained in:
parent
e6d5385129
commit
7bd7b3d24c
50 changed files with 333 additions and 1489 deletions
|
|
@ -54,9 +54,10 @@ public class InBandBytestreamManagerTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import java.util.Random;
|
|||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
|
@ -71,9 +72,10 @@ public class InBandBytestreamSessionMessageTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ import java.util.Random;
|
|||
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.PacketListener;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.packet.IQ;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smack.util.StringUtils;
|
||||
|
|
@ -72,9 +73,10 @@ public class InBandBytestreamSessionTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -71,9 +71,10 @@ public class Socks5ByteStreamManagerTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -60,9 +60,10 @@ public class Socks5ByteStreamRequestTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -65,9 +65,10 @@ public class Socks5ClientForInitiatorTest {
|
|||
|
||||
/**
|
||||
* Initialize fields used in the tests.
|
||||
* @throws XMPPException
|
||||
*/
|
||||
@Before
|
||||
public void setup() {
|
||||
public void setup() throws XMPPException {
|
||||
|
||||
// build protocol verifier
|
||||
protocol = new Protocol();
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ public class ConfigureFormTest
|
|||
|
||||
Node node = mgr.getNode("princely_musings");
|
||||
|
||||
SmackConfiguration.setPacketReplyTimeout(100);
|
||||
SmackConfiguration.setDefaultPacketReplyTimeout(100);
|
||||
con.setTimeout();
|
||||
|
||||
node.getNodeConfiguration();
|
||||
|
|
|
|||
|
|
@ -21,8 +21,10 @@ import static org.mockito.Mockito.*;
|
|||
|
||||
import org.jivesoftware.smack.PacketCollector;
|
||||
import org.jivesoftware.smack.Connection;
|
||||
import org.jivesoftware.smack.XMPPException;
|
||||
import org.jivesoftware.smack.filter.PacketFilter;
|
||||
import org.jivesoftware.smack.packet.Packet;
|
||||
import org.jivesoftware.smack.packet.XMPPError;
|
||||
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
|
|
@ -54,9 +56,10 @@ public class ConnectionUtils {
|
|||
* @param initiatorJID the user associated to the XMPP connection
|
||||
* @param xmppServer the XMPP server associated to the XMPP connection
|
||||
* @return a mocked XMPP connection
|
||||
* @throws XMPPException
|
||||
*/
|
||||
public static Connection createMockedConnection(final Protocol protocol,
|
||||
String initiatorJID, String xmppServer) {
|
||||
String initiatorJID, String xmppServer) throws XMPPException {
|
||||
|
||||
// mock XMPP connection
|
||||
Connection connection = mock(Connection.class);
|
||||
|
|
@ -64,29 +67,49 @@ public class ConnectionUtils {
|
|||
when(connection.getServiceName()).thenReturn(xmppServer);
|
||||
|
||||
// mock packet collector
|
||||
PacketCollector collector = mock(PacketCollector.class);
|
||||
final PacketCollector collector = mock(PacketCollector.class);
|
||||
when(connection.createPacketCollector(isA(PacketFilter.class))).thenReturn(
|
||||
collector);
|
||||
Answer<Object> addIncoming = new Answer<Object>() {
|
||||
Answer<PacketCollector> collectorAndSend = new Answer<PacketCollector>() {
|
||||
@Override
|
||||
public PacketCollector answer(InvocationOnMock invocation) throws Throwable {
|
||||
Packet packet = (Packet) invocation.getArguments()[0];
|
||||
protocol.getRequests().add(packet);
|
||||
return collector;
|
||||
}
|
||||
|
||||
};
|
||||
when(connection.createPacketCollectorAndSend(isA(Packet.class))).thenAnswer(collectorAndSend);
|
||||
|
||||
// mock send method
|
||||
Answer<Object> addIncoming = new Answer<Object>() {
|
||||
public Object answer(InvocationOnMock invocation) throws Throwable {
|
||||
protocol.getRequests().add((Packet) invocation.getArguments()[0]);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// mock send method
|
||||
doAnswer(addIncoming).when(connection).sendPacket(isA(Packet.class));
|
||||
Answer<Packet> answer = new Answer<Packet>() {
|
||||
|
||||
// mock receive methods
|
||||
Answer<Packet> answer = new Answer<Packet>() {
|
||||
public Packet answer(InvocationOnMock invocation) throws Throwable {
|
||||
return protocol.getResponses().poll();
|
||||
}
|
||||
};
|
||||
|
||||
// mock nextResult method
|
||||
when(collector.nextResult(anyInt())).thenAnswer(answer);
|
||||
when(collector.nextResult()).thenAnswer(answer);
|
||||
Answer<Packet> answerOrThrow = new Answer<Packet>() {
|
||||
@Override
|
||||
public Packet answer(InvocationOnMock invocation) throws Throwable {
|
||||
Packet packet = protocol.getResponses().poll();
|
||||
if (packet == null) return packet;
|
||||
XMPPError xmppError = packet.getError();
|
||||
if (xmppError != null) throw new XMPPException(xmppError);
|
||||
return packet;
|
||||
}
|
||||
};
|
||||
when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
|
||||
when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
|
||||
|
||||
// initialize service discovery manager for this connection
|
||||
ServiceDiscoveryManager.getInstanceFor(connection);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue