2010-02-16 09:33:16 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2010 Jive Software.
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2010-02-16 09:33:16 +01:00
|
|
|
* 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.smack;
|
|
|
|
|
2015-01-05 21:42:35 +01:00
|
|
|
import java.io.IOException;
|
2010-02-16 09:33:16 +01:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.LinkedBlockingQueue;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2015-03-14 15:19:52 +01:00
|
|
|
import org.jivesoftware.smack.packet.ExtensionElement;
|
2015-07-14 22:41:33 +02:00
|
|
|
import org.jivesoftware.smack.packet.Nonza;
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2014-10-21 11:59:11 +02:00
|
|
|
import org.jivesoftware.smack.packet.TopLevelStreamElement;
|
2017-06-14 17:12:43 +02:00
|
|
|
|
2015-05-27 19:29:51 +02:00
|
|
|
import org.jxmpp.jid.EntityFullJid;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.jid.JidTestUtil;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
2015-05-04 20:14:45 +02:00
|
|
|
import org.jxmpp.jid.parts.Resourcepart;
|
2015-02-14 17:15:02 +01:00
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
2010-02-16 09:33:16 +01:00
|
|
|
|
|
|
|
/**
|
2014-03-10 09:45:50 +01:00
|
|
|
* A dummy implementation of {@link XMPPConnection}, intended to be used during
|
2010-02-16 09:33:16 +01:00
|
|
|
* unit tests.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2010-02-16 09:33:16 +01:00
|
|
|
* Instances store any packets that are delivered to be send using the
|
2015-03-04 21:44:43 +01:00
|
|
|
* {@link #sendStanza(Stanza)} method in a blocking queue. The content of this queue
|
2010-02-16 09:33:16 +01:00
|
|
|
* can be inspected using {@link #getSentPacket()}. Typically these queues are
|
|
|
|
* used to retrieve a message that was generated by the client.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2010-02-16 09:33:16 +01:00
|
|
|
* Packets that should be processed by the client to simulate a received stanza
|
2015-06-05 14:04:17 +02:00
|
|
|
* can be delivered using the {@linkplain #processStanza(Stanza)} method.
|
2018-03-31 14:17:30 +02:00
|
|
|
* It invokes the registered stanza interceptors and listeners.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2014-03-10 21:48:46 +01:00
|
|
|
* @see XMPPConnection
|
2010-02-16 09:33:16 +01:00
|
|
|
* @author Guenther Niess
|
|
|
|
*/
|
2014-05-25 12:28:08 +02:00
|
|
|
public class DummyConnection extends AbstractXMPPConnection {
|
2010-02-16 09:33:16 +01:00
|
|
|
|
2014-10-21 11:59:11 +02:00
|
|
|
private final BlockingQueue<TopLevelStreamElement> queue = new LinkedBlockingQueue<TopLevelStreamElement>();
|
2010-02-16 09:33:16 +01:00
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
public static DummyConnectionConfiguration.Builder getDummyConfigurationBuilder() {
|
2015-06-08 14:39:05 +02:00
|
|
|
return DummyConnectionConfiguration.builder().setXmppDomain(JidTestUtil.EXAMPLE_ORG).setUsernameAndPassword("dummy",
|
2014-11-09 18:30:16 +01:00
|
|
|
"dummypass");
|
|
|
|
}
|
|
|
|
|
2010-02-16 09:33:16 +01:00
|
|
|
public DummyConnection() {
|
2014-11-09 18:30:16 +01:00
|
|
|
this(getDummyConfigurationBuilder().build());
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 11:57:27 +01:00
|
|
|
public DummyConnection(CharSequence username, String password, String serviceName) throws XmppStringprepException {
|
|
|
|
this(getDummyConfigurationBuilder().setUsernameAndPassword(username, password).setXmppDomain(
|
|
|
|
JidCreate.domainBareFrom(serviceName)).build());
|
|
|
|
}
|
|
|
|
|
2015-05-27 19:29:51 +02:00
|
|
|
private EntityFullJid getUserJid() {
|
2015-02-14 17:15:02 +01:00
|
|
|
try {
|
2015-06-06 09:00:19 +02:00
|
|
|
return JidCreate.entityFullFrom(config.getUsername()
|
2015-02-14 17:15:02 +01:00
|
|
|
+ "@"
|
2015-05-18 16:48:23 +02:00
|
|
|
+ config.getXMPPServiceDomain()
|
2015-02-14 17:15:02 +01:00
|
|
|
+ "/"
|
|
|
|
+ (config.getResource() != null ? config.getResource() : "Test"));
|
|
|
|
}
|
|
|
|
catch (XmppStringprepException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 08:59:39 +01:00
|
|
|
public DummyConnection(DummyConnectionConfiguration configuration) {
|
2010-02-16 09:33:16 +01:00
|
|
|
super(configuration);
|
2013-03-19 03:37:36 +01:00
|
|
|
|
2014-05-25 12:28:08 +02:00
|
|
|
for (ConnectionCreationListener listener : XMPPConnectionRegistry.getConnectionCreationListeners()) {
|
|
|
|
listener.connectionCreated(this);
|
|
|
|
}
|
2015-02-14 17:15:02 +01:00
|
|
|
user = getUserJid();
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-05-15 15:04:46 +02:00
|
|
|
protected void connectInternal() {
|
2014-11-09 18:30:16 +01:00
|
|
|
connected = true;
|
2015-01-28 17:14:33 +01:00
|
|
|
streamId = "dummy-" + new Random(new Date().getTime()).nextInt();
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-05-04 20:31:25 +02:00
|
|
|
protected void shutdown() {
|
2010-02-16 09:33:16 +01:00
|
|
|
user = null;
|
|
|
|
authenticated = false;
|
2015-03-17 11:33:02 +01:00
|
|
|
|
2015-01-10 01:23:20 +01:00
|
|
|
callConnectionClosedListener();
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
2019-03-04 17:08:53 +01:00
|
|
|
@Override
|
|
|
|
public void instantShutdown() {
|
|
|
|
shutdown();
|
|
|
|
}
|
|
|
|
|
2010-02-16 09:33:16 +01:00
|
|
|
@Override
|
|
|
|
public boolean isSecureConnection() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isUsingCompression() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-05-04 20:14:45 +02:00
|
|
|
protected void loginInternal(String username, String password, Resourcepart resource)
|
2010-02-16 09:33:16 +01:00
|
|
|
throws XMPPException {
|
2015-02-14 17:15:02 +01:00
|
|
|
user = getUserJid();
|
2010-02-16 09:33:16 +01:00
|
|
|
authenticated = true;
|
|
|
|
}
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
@Override
|
2015-07-14 22:41:33 +02:00
|
|
|
public void sendNonza(Nonza element) {
|
2014-09-11 09:49:16 +02:00
|
|
|
queue.add(element);
|
|
|
|
}
|
|
|
|
|
2010-02-16 09:33:16 +01:00
|
|
|
@Override
|
2015-03-04 21:44:43 +01:00
|
|
|
protected void sendStanzaInternal(Stanza packet) {
|
2010-02-16 09:33:16 +01:00
|
|
|
queue.add(packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-03-04 21:44:43 +01:00
|
|
|
* Returns the number of packets that's sent through {@link #sendStanza(Stanza)} and
|
2010-02-16 09:33:16 +01:00
|
|
|
* that has not been returned by {@link #getSentPacket()}.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2010-02-16 09:33:16 +01:00
|
|
|
* @return the number of packets which are in the queue.
|
|
|
|
*/
|
|
|
|
public int getNumberOfSentPackets() {
|
|
|
|
return queue.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Returns the first stanza that's sent through {@link #sendStanza(Stanza)}
|
2013-03-19 03:37:36 +01:00
|
|
|
* and that has not been returned by earlier calls to this method.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2010-02-16 09:33:16 +01:00
|
|
|
* @return a sent packet.
|
|
|
|
*/
|
2015-01-08 11:01:35 +01:00
|
|
|
public <P extends TopLevelStreamElement> P getSentPacket() {
|
|
|
|
return getSentPacket(5 * 60);
|
2013-03-19 03:37:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Returns the first stanza that's sent through {@link #sendStanza(Stanza)}
|
2013-03-19 03:37:36 +01:00
|
|
|
* and that has not been returned by earlier calls to this method. This
|
|
|
|
* method will block for up to the specified number of seconds if no packets
|
|
|
|
* have been sent yet.
|
2018-05-09 23:06:12 +02:00
|
|
|
*
|
2013-03-19 03:37:36 +01:00
|
|
|
* @return a sent packet.
|
|
|
|
*/
|
2014-10-21 11:59:11 +02:00
|
|
|
@SuppressWarnings("unchecked")
|
2015-01-08 11:01:35 +01:00
|
|
|
public <P extends TopLevelStreamElement> P getSentPacket(int wait) {
|
|
|
|
try {
|
|
|
|
return (P) queue.poll(wait, TimeUnit.SECONDS);
|
|
|
|
}
|
|
|
|
catch (InterruptedException e) {
|
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-03-31 14:17:30 +02:00
|
|
|
* Processes a stanza through the installed stanza collectors and listeners
|
|
|
|
* and letting them examine the stanza to see if they are a match with the
|
2010-02-16 09:33:16 +01:00
|
|
|
* filter.
|
|
|
|
*
|
2018-03-31 14:17:30 +02:00
|
|
|
* @param packet the stanza to process.
|
2010-02-16 09:33:16 +01:00
|
|
|
*/
|
2017-02-11 16:16:41 +01:00
|
|
|
@Override
|
2015-06-05 14:04:17 +02:00
|
|
|
public void processStanza(Stanza packet) {
|
2017-01-03 11:12:34 +01:00
|
|
|
invokeStanzaCollectorsAndNotifyRecvListeners(packet);
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|
2014-11-09 18:30:16 +01:00
|
|
|
|
2015-03-14 15:19:52 +01:00
|
|
|
/**
|
|
|
|
* Enable stream feature.
|
|
|
|
*
|
|
|
|
* @param streamFeature the stream feature.
|
|
|
|
* @since 4.2
|
|
|
|
*/
|
|
|
|
public void enableStreamFeature(ExtensionElement streamFeature) {
|
|
|
|
addStreamFeature(streamFeature);
|
|
|
|
}
|
|
|
|
|
2015-01-05 21:42:35 +01:00
|
|
|
public static DummyConnection newConnectedDummyConnection() {
|
|
|
|
DummyConnection dummyConnection = new DummyConnection();
|
|
|
|
try {
|
|
|
|
dummyConnection.connect();
|
|
|
|
dummyConnection.login();
|
|
|
|
}
|
2015-02-14 09:43:44 +01:00
|
|
|
catch (InterruptedException | SmackException | IOException | XMPPException e) {
|
2015-01-05 21:42:35 +01:00
|
|
|
throw new IllegalStateException(e);
|
|
|
|
}
|
|
|
|
return dummyConnection;
|
|
|
|
}
|
|
|
|
|
2014-11-09 18:30:16 +01:00
|
|
|
public static class DummyConnectionConfiguration extends ConnectionConfiguration {
|
2014-12-17 17:01:53 +01:00
|
|
|
protected DummyConnectionConfiguration(Builder builder) {
|
2014-11-09 18:30:16 +01:00
|
|
|
super(builder);
|
|
|
|
}
|
|
|
|
|
2014-12-17 17:01:53 +01:00
|
|
|
public static Builder builder() {
|
|
|
|
return new Builder();
|
2014-11-09 18:30:16 +01:00
|
|
|
}
|
|
|
|
|
2015-04-06 10:45:12 +02:00
|
|
|
public static final class Builder
|
2014-11-09 18:30:16 +01:00
|
|
|
extends
|
2014-12-17 17:01:53 +01:00
|
|
|
ConnectionConfiguration.Builder<Builder, DummyConnectionConfiguration> {
|
2014-11-09 18:30:16 +01:00
|
|
|
|
2014-12-17 17:01:53 +01:00
|
|
|
private Builder() {
|
2014-11-09 18:30:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DummyConnectionConfiguration build() {
|
|
|
|
return new DummyConnectionConfiguration(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-12-17 17:01:53 +01:00
|
|
|
protected Builder getThis() {
|
2014-11-09 18:30:16 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-03-04 17:08:53 +01:00
|
|
|
|
2010-02-16 09:33:16 +01:00
|
|
|
}
|