From b453f616731fe4ae1a18bf6488683c82eafae833 Mon Sep 17 00:00:00 2001 From: Gaston Dombiak Date: Fri, 22 Apr 2005 04:24:25 +0000 Subject: [PATCH] Initial version. SMACK-53 git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2487 b35dd754-fafc-0310-a699-88a17e54d16e --- .../jivesoftware/smackx/packet/Version.java | 132 ++++++++++++++++++ test/org/jivesoftware/smackx/VersionTest.java | 76 ++++++++++ 2 files changed, 208 insertions(+) create mode 100644 source/org/jivesoftware/smackx/packet/Version.java create mode 100644 test/org/jivesoftware/smackx/VersionTest.java diff --git a/source/org/jivesoftware/smackx/packet/Version.java b/source/org/jivesoftware/smackx/packet/Version.java new file mode 100644 index 000000000..206208f3f --- /dev/null +++ b/source/org/jivesoftware/smackx/packet/Version.java @@ -0,0 +1,132 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright 2003-2004 Jive Software. + * + * All rights reserved. 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.packet; + +import org.jivesoftware.smack.packet.IQ; + +/** + * A Version IQ packet, which is used by XMPP clients to discover version information + * about the software running at another entity's JID.

+ * + * An example to discover the version of the server: + *

+ * // Request the version from the server.
+ * Version versionRequest = new Version();
+ * timeRequest.setType(IQ.Type.GET);
+ * timeRequest.setTo("example.com");
+ *
+ * // Create a packet collector to listen for a response.
+ * PacketCollector collector = con.createPacketCollector(
+ *                new PacketIDFilter(versionRequest.getPacketID()));
+ *
+ * con.sendPacket(versionRequest);
+ *
+ * // Wait up to 5 seconds for a result.
+ * IQ result = (IQ)collector.nextResult(5000);
+ * if (result != null && result.getType() == IQ.Type.RESULT) {
+ *     Version versionResult = (Version)result;
+ *     // Do something with result...
+ * }

+ * + * @author Gaston Dombiak + */ +public class Version extends IQ { + + private String name; + private String version; + private String os; + + /** + * Returns the natural-language name of the software. This property will always be + * present in a result. + * + * @return the natural-language name of the software. + */ + public String getName() { + return name; + } + + /** + * Sets the natural-language name of the software. This message should only be + * invoked when parsing the XML and setting the property to a Version instance. + * + * @param name the natural-language name of the software. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Returns the specific version of the software. This property will always be + * present in a result. + * + * @return the specific version of the software. + */ + public String getVersion() { + return version; + } + + /** + * Sets the specific version of the software. This message should only be + * invoked when parsing the XML and setting the property to a Version instance. + * + * @param version the specific version of the software. + */ + public void setVersion(String version) { + this.version = version; + } + + /** + * Returns the operating system of the queried entity. This property will always be + * present in a result. + * + * @return the operating system of the queried entity. + */ + public String getOs() { + return os; + } + + /** + * Sets the operating system of the queried entity. This message should only be + * invoked when parsing the XML and setting the property to a Version instance. + * + * @param os operating system of the queried entity. + */ + public void setOs(String os) { + this.os = os; + } + + public String getChildElementXML() { + StringBuffer buf = new StringBuffer(); + buf.append(""); + if (name != null) { + buf.append("").append(name).append(""); + } + if (version != null) { + buf.append("").append(version).append(""); + } + if (os != null) { + buf.append("").append(os).append(""); + } + buf.append(""); + return buf.toString(); + } +} diff --git a/test/org/jivesoftware/smackx/VersionTest.java b/test/org/jivesoftware/smackx/VersionTest.java new file mode 100644 index 000000000..824ff2919 --- /dev/null +++ b/test/org/jivesoftware/smackx/VersionTest.java @@ -0,0 +1,76 @@ +/** + * $RCSfile$ + * $Revision$ + * $Date$ + * + * Copyright 2003-2004 Jive Software. + * + * All rights reserved. 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; + +import org.jivesoftware.smack.PacketCollector; +import org.jivesoftware.smack.XMPPConnection; +import org.jivesoftware.smack.filter.PacketIDFilter; +import org.jivesoftware.smack.packet.IQ; +import org.jivesoftware.smack.test.SmackTestCase; +import org.jivesoftware.smackx.packet.Version; + +/** + * Test case to ensure that Smack is able to get and parse correctly iq:version packets. + * + * @author Gaston Dombiak + */ +public class VersionTest extends SmackTestCase { + + public VersionTest(String arg0) { + super(arg0); + } + + /** + * Get the version of the server and make sure that all the required data is present + * + * Note: This test expects the server to answer an iq:version packet. + */ + public void testGetServerVersion() { + Version version = new Version(); + version.setType(IQ.Type.GET); + version.setTo(getHost()); + + // Create a packet collector to listen for a response. + PacketCollector collector = getConnection(0).createPacketCollector(new PacketIDFilter(version.getPacketID())); + + getConnection(0).sendPacket(version); + + // Wait up to 5 seconds for a result. + IQ result = (IQ)collector.nextResult(5000); + // Close the collector + collector.cancel(); + + assertNotNull("No result from the server", result); + + assertEquals("Incorrect result type", IQ.Type.RESULT, result.getType()); + assertNotNull("No name specified in the result", ((Version)result).getName()); + assertNotNull("No version specified in the result", ((Version)result).getVersion()); + } + + protected int getMaxConnections() { + return 1; + } + + protected void setUp() throws Exception { + XMPPConnection.DEBUG_ENABLED = false; + super.setUp(); + } +}