/** * * 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.junit.Assert.assertEquals; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.jivesoftware.smack.packet.Stanza; /** * This class can be used in conjunction with a mocked XMPP connection ( * {@link ConnectionUtils#createMockedConnection(Protocol, String, String)}) to * verify an XMPP protocol. This can be accomplished in the following was: *
*
* public void methodToTest() {
* Packet packet = new Packet(); // create an XMPP packet
* PacketCollector collector = connection.createPacketCollector(new StanzaIdFilter());
* connection.sendStanza(packet);
* Packet reply = collector.nextResult();
* }
*
* public void testMethod() {
* // create protocol
* Protocol protocol = new Protocol();
* // create mocked connection
* XMPPConnection connection = ConnectionUtils.createMockedConnection(protocol, "user@xmpp-server", "xmpp-server");
*
* // add reply packet to protocol
* Packet reply = new Packet();
* protocol.add(reply);
*
* // call method to test
* methodToTest();
*
* // verify protocol
* protocol.verifyAll();
* }
*
*
*
* Additionally to adding the response to the protocol instance you can pass
* verifications that will be executed when {@link #verifyAll()} is invoked.
* (See {@link Verification} for more details.)
*
* If the {@link #printProtocol} flag is set to true {@link #verifyAll()} will
* also print out the XML messages in the order they are sent to the console.
* This may be useful to inspect the whole protocol "by hand".
*
* @author Henning Staib
*/
public class Protocol {
/**
* Set to true
to print XML messages to the console while
* verifying the protocol.
*/
public boolean printProtocol = false;
// responses to requests are taken form this queue
Queue