mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-11-12 23:32:05 +01:00
701aa7d9c4
Conflicts: smack-core/src/main/java/org/jivesoftware/smack/AbstractXMPPConnection.java smack-core/src/main/java/org/jivesoftware/smack/PacketCollector.java smack-core/src/main/java/org/jivesoftware/smack/PacketListener.java smack-core/src/main/java/org/jivesoftware/smack/XMPPConnection.java smack-core/src/main/java/org/jivesoftware/smack/debugger/SmackDebugger.java smack-core/src/main/java/org/jivesoftware/smack/packet/Packet.java smack-core/src/main/java/org/jivesoftware/smack/util/XmlStringBuilder.java smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java smack-extensions/src/main/java/org/jivesoftware/smackx/address/provider/MultipleAddressesProvider.java smack-extensions/src/main/java/org/jivesoftware/smackx/bytestreams/ibb/DataListener.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/FaultTolerantNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/IBBTransferNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/Socks5TransferNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/filetransfer/StreamNegotiator.java smack-extensions/src/main/java/org/jivesoftware/smackx/muc/MultiUserChat.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/LeafNode.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/Node.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/PubSubManager.java smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java smack-extensions/src/main/java/org/jivesoftware/smackx/vcardtemp/VCardManager.java smack-extensions/src/test/java/org/jivesoftware/smackx/receipts/DeliveryReceiptTest.java smack-im/src/main/java/org/jivesoftware/smack/chat/ChatManager.java smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/JingleSession.java smack-jingle-old/src/main/java/org/jivesoftware/smackx/jingleold/provider/JingleProvider.java smack-legacy/src/main/java/org/jivesoftware/smackx/workgroup/packet/UserID.java smack-legacy/src/main/java/org/jivesoftware/smackx/xroster/provider/RosterExchangeProvider.java smack-tcp/src/main/java/org/jivesoftware/smack/tcp/XMPPTCPConnection.java version.gradle
130 lines
5.2 KiB
Java
130 lines
5.2 KiB
Java
/**
|
|
*
|
|
* Copyright the original author or authors
|
|
*
|
|
* 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.jivesoftware.util;
|
|
|
|
import static org.mockito.Matchers.anyInt;
|
|
import static org.mockito.Matchers.anyLong;
|
|
import static org.mockito.Matchers.isA;
|
|
import static org.mockito.Mockito.doAnswer;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import org.jivesoftware.smack.PacketCollector;
|
|
import org.jivesoftware.smack.SmackException;
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
import org.jivesoftware.smack.XMPPException.XMPPErrorException;
|
|
import org.jivesoftware.smack.filter.StanzaFilter;
|
|
import org.jivesoftware.smack.packet.IQ;
|
|
import org.jivesoftware.smack.packet.Stanza;
|
|
import org.jivesoftware.smack.packet.XMPPError;
|
|
import org.jivesoftware.smackx.disco.ServiceDiscoveryManager;
|
|
import org.jxmpp.jid.DomainBareJid;
|
|
import org.jxmpp.jid.FullJid;
|
|
import org.mockito.invocation.InvocationOnMock;
|
|
import org.mockito.stubbing.Answer;
|
|
|
|
/**
|
|
* A collection of utility methods to create mocked XMPP connections.
|
|
*
|
|
* @author Henning Staib
|
|
*/
|
|
public class ConnectionUtils {
|
|
|
|
/**
|
|
* Creates a mocked XMPP connection that stores every packet that is send over this
|
|
* connection in the given protocol instance and returns the predefined answer packets
|
|
* form the protocol instance.
|
|
* <p>
|
|
* This mocked connection can used to collect packets that require a reply using a
|
|
* PacketCollector.
|
|
*
|
|
* <pre>
|
|
* <code>
|
|
* PacketCollector collector = connection.createPacketCollector(new PacketFilter());
|
|
* connection.sendStanza(packet);
|
|
* Packet reply = collector.nextResult();
|
|
* </code>
|
|
* </pre>
|
|
*
|
|
* @param protocol protocol helper containing answer packets
|
|
* @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 SmackException
|
|
* @throws XMPPErrorException
|
|
* @throws InterruptedException
|
|
*/
|
|
public static XMPPConnection createMockedConnection(final Protocol protocol,
|
|
FullJid initiatorJID, DomainBareJid xmppServer) throws SmackException, XMPPErrorException, InterruptedException {
|
|
|
|
// mock XMPP connection
|
|
XMPPConnection connection = mock(XMPPConnection.class);
|
|
when(connection.getUser()).thenReturn(initiatorJID);
|
|
when(connection.getServiceName()).thenReturn(xmppServer);
|
|
|
|
// mock packet collector
|
|
final PacketCollector collector = mock(PacketCollector.class);
|
|
when(connection.createPacketCollector(isA(StanzaFilter.class))).thenReturn(
|
|
collector);
|
|
Answer<PacketCollector> collectorAndSend = new Answer<PacketCollector>() {
|
|
@Override
|
|
public PacketCollector answer(InvocationOnMock invocation) throws Throwable {
|
|
Stanza packet = (Stanza) invocation.getArguments()[0];
|
|
protocol.getRequests().add(packet);
|
|
return collector;
|
|
}
|
|
|
|
};
|
|
when(connection.createPacketCollectorAndSend(isA(IQ.class))).thenAnswer(collectorAndSend);
|
|
|
|
// mock send method
|
|
Answer<Object> addIncoming = new Answer<Object>() {
|
|
public Object answer(InvocationOnMock invocation) throws Throwable {
|
|
protocol.getRequests().add((Stanza) invocation.getArguments()[0]);
|
|
return null;
|
|
}
|
|
};
|
|
doAnswer(addIncoming).when(connection).sendStanza(isA(Stanza.class));
|
|
|
|
// mock receive methods
|
|
Answer<Stanza> answer = new Answer<Stanza>() {
|
|
public Stanza answer(InvocationOnMock invocation) throws Throwable {
|
|
return protocol.getResponses().poll();
|
|
}
|
|
};
|
|
when(collector.nextResult(anyInt())).thenAnswer(answer);
|
|
when(collector.nextResult()).thenAnswer(answer);
|
|
Answer<Stanza> answerOrThrow = new Answer<Stanza>() {
|
|
@Override
|
|
public Stanza answer(InvocationOnMock invocation) throws Throwable {
|
|
Stanza packet = protocol.getResponses().poll();
|
|
if (packet == null) return packet;
|
|
XMPPError xmppError = packet.getError();
|
|
if (xmppError != null) throw new XMPPErrorException(xmppError);
|
|
return packet;
|
|
}
|
|
};
|
|
when(collector.nextResultOrThrow()).thenAnswer(answerOrThrow);
|
|
when(collector.nextResultOrThrow(anyLong())).thenAnswer(answerOrThrow);
|
|
|
|
// initialize service discovery manager for this connection
|
|
ServiceDiscoveryManager.getInstanceFor(connection);
|
|
|
|
return connection;
|
|
}
|
|
|
|
}
|