diff --git a/smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java b/smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java index d3b347ccc..38aec0998 100644 --- a/smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java +++ b/smack-core/src/test/java/org/jivesoftware/smack/ThreadedDummyConnection.java @@ -16,6 +16,7 @@ */ package org.jivesoftware.smack; +import java.io.IOException; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -37,13 +38,8 @@ public class ThreadedDummyConnection extends DummyConnection { private volatile boolean timeout = false; @Override - public void sendPacket(Packet packet) { - try { - super.sendPacket(packet); - } - catch (NotConnectedException e) { - e.printStackTrace(); - } + public void sendPacket(Packet packet) throws NotConnectedException { + super.sendPacket(packet); if (packet instanceof IQ && !timeout) { timeout = false; @@ -106,4 +102,10 @@ public class ThreadedDummyConnection extends DummyConnection { } } + public static ThreadedDummyConnection newInstance() throws SmackException, IOException, XMPPException { + ThreadedDummyConnection threadedDummyConnection = new ThreadedDummyConnection(); + threadedDummyConnection.connect(); + return threadedDummyConnection; + } + } diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java index e9713f8ef..ed979cc82 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/packet/PubSub.java @@ -32,23 +32,18 @@ public class PubSub extends IQ public static final String ELEMENT = "pubsub"; public static final String NAMESPACE = "http://jabber.org/protocol/pubsub"; - private PubSubNamespace ns = PubSubNamespace.BASIC; - public PubSub() { super(ELEMENT, NAMESPACE); } - public PubSub(String to, Type type) { - this(); - setTo(to); - setType(type); + public PubSub(PubSubNamespace ns) { + super(ELEMENT, ns.getXmlns()); } public PubSub(String to, Type type, PubSubNamespace ns) { - this(to, type); - if (ns != null) { - setPubSubNamespace(ns); - } + super(ELEMENT, (ns == null ? PubSubNamespace.BASIC : ns).getXmlns()); + setTo(to); + setType(type); } /** @@ -60,49 +55,12 @@ public class PubSub extends IQ return ELEMENT; } - /** - * Returns the XML namespace of the extension sub-packet root element. - * According the specification the namespace is - * http://jabber.org/protocol/pubsub with a specific fragment depending - * on the request. The namespace is defined at XMPP Registrar at - * - * The default value has no fragment. - * - * @return the XML namespace of the packet extension. - */ - public String getNamespace() - { - return ns.getXmlns(); - } - - /** - * Set the namespace for the packet if it something other than the default - * case of {@link PubSubNamespace#BASIC}. The {@link #getNamespace()} method will return - * the result of calling {@link PubSubNamespace#getXmlns()} on the specified enum. - * - * @param ns - The new value for the namespace. - */ - public void setPubSubNamespace(PubSubNamespace ns) - { - this.ns = ns; - } - @SuppressWarnings("unchecked") public PE getExtension(PubSubElementType elem) { return (PE) getExtension(elem.getElementName(), elem.getNamespace().getXmlns()); } - /** - * Returns the current value of the namespace. The {@link #getNamespace()} method will return - * the result of calling {@link PubSubNamespace#getXmlns()} this value. - * - * @return The current value of the namespace. - */ - public PubSubNamespace getPubSubNamespace() - { - return ns; - } /** * Returns the XML representation of a pubsub element according the specification. * diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java index 806406a9c..43a357ddc 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/pubsub/provider/PubSubProvider.java @@ -38,25 +38,22 @@ public class PubSubProvider extends IQProvider @Override public PubSub parse(XmlPullParser parser, int initialDepth) throws XmlPullParserException, IOException, SmackException { - PubSub pubsub = new PubSub(); String namespace = parser.getNamespace(); - pubsub.setPubSubNamespace(PubSubNamespace.valueOfFromXmlns(namespace)); - boolean done = false; + PubSubNamespace pubSubNamespace = PubSubNamespace.valueOfFromXmlns(namespace); + PubSub pubsub = new PubSub(pubSubNamespace); - while (!done) + outerloop: while (true) { int eventType = parser.next(); - - if (eventType == XmlPullParser.START_TAG) - { + switch (eventType) { + case XmlPullParser.START_TAG: PacketParserUtils.addPacketExtension(pubsub, parser); - } - else if (eventType == XmlPullParser.END_TAG) - { - if (parser.getName().equals("pubsub")) - { - done = true; + break; + case XmlPullParser.END_TAG: + if (parser.getDepth() == initialDepth) { + break outerloop; } + break; } } return pubsub; diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java index 9ce1490fc..8639a3e5e 100644 --- a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/ConfigureFormTest.java @@ -18,6 +18,8 @@ package org.jivesoftware.smackx.pubsub; import static org.junit.Assert.assertEquals; +import java.io.IOException; + import org.jivesoftware.smack.SmackConfiguration; import org.jivesoftware.smack.SmackException; import org.jivesoftware.smack.ThreadedDummyConnection; @@ -47,9 +49,9 @@ public class ConfigureFormTest } @Test - public void getConfigFormWithInsufficientPriviliges() throws XMPPException, SmackException + public void getConfigFormWithInsufficientPriviliges() throws XMPPException, SmackException, IOException { - ThreadedDummyConnection con = new ThreadedDummyConnection(); + ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); PubSubManager mgr = new PubSubManager(con); DiscoverInfo info = new DiscoverInfo(); Identity ident = new Identity("pubsub", null, "leaf"); diff --git a/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/PubSubManagerTest.java b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/PubSubManagerTest.java new file mode 100644 index 000000000..f90cf1736 --- /dev/null +++ b/smack-extensions/src/test/java/org/jivesoftware/smackx/pubsub/PubSubManagerTest.java @@ -0,0 +1,42 @@ +/** + * + * Copyright 2014 Florian Schmaus + * + * 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.smackx.pubsub; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; + +import org.jivesoftware.smack.SmackException; +import org.jivesoftware.smack.ThreadedDummyConnection; +import org.jivesoftware.smack.XMPPException; +import org.jivesoftware.smackx.pubsub.packet.PubSub; +import org.junit.Test; + +public class PubSubManagerTest { + + @Test + public void deleteNodeTest() throws InterruptedException, SmackException, IOException, XMPPException { + ThreadedDummyConnection con = ThreadedDummyConnection.newInstance(); + PubSubManager mgr = new PubSubManager(con); + + mgr.deleteNode("foo@bar.org"); + + PubSub pubSubDeleteRequest = con.getSentPacket(); + assertEquals("http://jabber.org/protocol/pubsub#owner", pubSubDeleteRequest.getChildElementNamespace()); + assertEquals("pubsub", pubSubDeleteRequest.getChildElementName()); + } +}