1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02: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:
Florian Schmaus 2014-02-18 15:05:19 +01:00
parent e6d5385129
commit 7bd7b3d24c
50 changed files with 333 additions and 1489 deletions

View file

@ -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);