mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-02 00:42:07 +01:00
Added support for multiple message bodies and language on the message. SMACK-99
git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@8113 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
parent
985bc79aca
commit
8e08a8ba4a
11 changed files with 926 additions and 42 deletions
BIN
build/junit.jar
BIN
build/junit.jar
Binary file not shown.
|
@ -9,6 +9,7 @@
|
||||||
<sourceFolder url="file://$MODULE_DIR$/../resources" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/../resources" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/../../source" isTestSource="false" />
|
<sourceFolder url="file://$MODULE_DIR$/../../source" isTestSource="false" />
|
||||||
<sourceFolder url="file://$MODULE_DIR$/../../test" isTestSource="true" />
|
<sourceFolder url="file://$MODULE_DIR$/../../test" isTestSource="true" />
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/../../test-unit" isTestSource="true" />
|
||||||
<excludeFolder url="file://$MODULE_DIR$/../../target" />
|
<excludeFolder url="file://$MODULE_DIR$/../../target" />
|
||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
|
@ -31,15 +32,6 @@
|
||||||
<SOURCES />
|
<SOURCES />
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module-library">
|
|
||||||
<library>
|
|
||||||
<CLASSES>
|
|
||||||
<root url="jar://$MODULE_DIR$/../junit.jar!/" />
|
|
||||||
</CLASSES>
|
|
||||||
<JAVADOC />
|
|
||||||
<SOURCES />
|
|
||||||
</library>
|
|
||||||
</orderEntry>
|
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library">
|
||||||
<library>
|
<library>
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
|
@ -49,12 +41,23 @@
|
||||||
<SOURCES />
|
<SOURCES />
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library" exported="">
|
||||||
<library>
|
<library>
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
<root url="jar://$MODULE_DIR$/../build/xmlunit.jar!/" />
|
<root url="jar://$MODULE_DIR$/../build/xmlunit.jar!/" />
|
||||||
</CLASSES>
|
</CLASSES>
|
||||||
<JAVADOC />
|
<JAVADOC />
|
||||||
|
<SOURCES>
|
||||||
|
<root url="file://$MODULE_DIR$/../../../xmlunit-1.1beta1/src/java" />
|
||||||
|
</SOURCES>
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library" exported="">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MODULE_DIR$/../junit.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
<SOURCES />
|
<SOURCES />
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
|
|
|
@ -286,10 +286,43 @@
|
||||||
<modules>
|
<modules>
|
||||||
<module fileurl="file://$PROJECT_DIR$/../../jingle/extension/build/projects/JingleExtension.iml" filepath="$PROJECT_DIR$/../../jingle/extension/build/projects/JingleExtension.iml" />
|
<module fileurl="file://$PROJECT_DIR$/../../jingle/extension/build/projects/JingleExtension.iml" filepath="$PROJECT_DIR$/../../jingle/extension/build/projects/JingleExtension.iml" />
|
||||||
<module fileurl="file://$PROJECT_DIR$/Smack.iml" filepath="$PROJECT_DIR$/Smack.iml" />
|
<module fileurl="file://$PROJECT_DIR$/Smack.iml" filepath="$PROJECT_DIR$/Smack.iml" />
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/SmackUnitTest.iml" filepath="$PROJECT_DIR$/SmackUnitTest.iml" />
|
||||||
</modules>
|
</modules>
|
||||||
</component>
|
</component>
|
||||||
<component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" project-jdk-type="JavaSDK" />
|
<component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" project-jdk-type="JavaSDK" />
|
||||||
<component name="ProjectRunConfigurationManager" />
|
<component name="ProjectRunConfigurationManager">
|
||||||
|
<configuration default="false" name="Unit tests" type="JUnit" factoryName="JUnit" enabled="true" merge="true">
|
||||||
|
<pattern value="org.jivesoftware.smack.packet.*" />
|
||||||
|
<module name="SmackUnitTest" />
|
||||||
|
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
|
||||||
|
<option name="ALTERNATIVE_JRE_PATH" value="" />
|
||||||
|
<option name="PACKAGE_NAME" value="org.jivesoftware.smack" />
|
||||||
|
<option name="MAIN_CLASS_NAME" value="" />
|
||||||
|
<option name="METHOD_NAME" value="" />
|
||||||
|
<option name="TEST_OBJECT" value="package" />
|
||||||
|
<option name="VM_PARAMETERS" value="" />
|
||||||
|
<option name="PARAMETERS" value="" />
|
||||||
|
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
|
||||||
|
<option name="ADDITIONAL_CLASS_PATH" />
|
||||||
|
<option name="TEST_SEARCH_SCOPE">
|
||||||
|
<value defaultName="singleModule" />
|
||||||
|
</option>
|
||||||
|
<RunnerSettings RunnerId="Debug">
|
||||||
|
<option name="DEBUG_PORT" value="4533" />
|
||||||
|
<option name="TRANSPORT" value="0" />
|
||||||
|
<option name="LOCAL" value="true" />
|
||||||
|
</RunnerSettings>
|
||||||
|
<RunnerSettings RunnerId="JProfiler">
|
||||||
|
<option name="WINDOW" value="false" />
|
||||||
|
<option name="JVMPI" value="false" />
|
||||||
|
<option name="INTERPRETED" value="false" />
|
||||||
|
</RunnerSettings>
|
||||||
|
<ConfigurationWrapper RunnerId="Debug" />
|
||||||
|
<method>
|
||||||
|
<option name="Make" value="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
|
</component>
|
||||||
<component name="RmicSettings">
|
<component name="RmicSettings">
|
||||||
<option name="IS_EANABLED" value="false" />
|
<option name="IS_EANABLED" value="false" />
|
||||||
<option name="DEBUGGING_INFO" value="true" />
|
<option name="DEBUGGING_INFO" value="true" />
|
||||||
|
|
17
build/projects/SmackUnitTest.iml
Normal file
17
build/projects/SmackUnitTest.iml
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module version="4" relativePaths="true" type="JAVA_MODULE">
|
||||||
|
<component name="ModuleRootManager" />
|
||||||
|
<component name="NewModuleRootManager" inherit-compiler-output="false">
|
||||||
|
<output url="file://$MODULE_DIR$/../../classes" />
|
||||||
|
<exclude-output />
|
||||||
|
<output-test url="file://$MODULE_DIR$/../../classes" />
|
||||||
|
<content url="file://$MODULE_DIR$/../../test-unit">
|
||||||
|
<sourceFolder url="file://$MODULE_DIR$/../../test-unit" isTestSource="true" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="inheritedJdk" />
|
||||||
|
<orderEntry type="module" module-name="Smack" exported="" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntryProperties />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
|
|
|
@ -22,6 +22,8 @@ package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
import org.jivesoftware.smack.util.StringUtils;
|
import org.jivesoftware.smack.util.StringUtils;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents XMPP message packets. A message can be one of several types:
|
* Represents XMPP message packets. A message can be one of several types:
|
||||||
*
|
*
|
||||||
|
@ -50,8 +52,10 @@ public class Message extends Packet {
|
||||||
|
|
||||||
private Type type = Type.normal;
|
private Type type = Type.normal;
|
||||||
private String subject = null;
|
private String subject = null;
|
||||||
private String body = null;
|
|
||||||
private String thread = null;
|
private String thread = null;
|
||||||
|
private String language;
|
||||||
|
|
||||||
|
private final Set<Body> bodies = new HashSet<Body>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new, "normal" message.
|
* Creates a new, "normal" message.
|
||||||
|
@ -65,9 +69,6 @@ public class Message extends Packet {
|
||||||
* @param to the recipient of the message.
|
* @param to the recipient of the message.
|
||||||
*/
|
*/
|
||||||
public Message(String to) {
|
public Message(String to) {
|
||||||
if (to == null) {
|
|
||||||
throw new IllegalArgumentException("Parameter cannot be null");
|
|
||||||
}
|
|
||||||
setTo(to);
|
setTo(to);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,15 +79,13 @@ public class Message extends Packet {
|
||||||
* @param type the message type.
|
* @param type the message type.
|
||||||
*/
|
*/
|
||||||
public Message(String to, Type type) {
|
public Message(String to, Type type) {
|
||||||
if (to == null || type == null) {
|
|
||||||
throw new IllegalArgumentException("Parameters cannot be null.");
|
|
||||||
}
|
|
||||||
setTo(to);
|
setTo(to);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the type of the message.
|
* Returns the type of the message. If no type has been set this method will return {@link
|
||||||
|
* org.jivesoftware.smack.packet.Message.Type#normal}.
|
||||||
*
|
*
|
||||||
* @return the type of the message.
|
* @return the type of the message.
|
||||||
*/
|
*/
|
||||||
|
@ -98,6 +97,7 @@ public class Message extends Packet {
|
||||||
* Sets the type of the message.
|
* Sets the type of the message.
|
||||||
*
|
*
|
||||||
* @param type the type of the message.
|
* @param type the type of the message.
|
||||||
|
* @throws IllegalArgumentException if null is passed in as the type
|
||||||
*/
|
*/
|
||||||
public void setType(Type type) {
|
public void setType(Type type) {
|
||||||
if (type == null) {
|
if (type == null) {
|
||||||
|
@ -133,7 +133,38 @@ public class Message extends Packet {
|
||||||
* @return the body of the message.
|
* @return the body of the message.
|
||||||
*/
|
*/
|
||||||
public String getBody() {
|
public String getBody() {
|
||||||
return body;
|
return getBody(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the body corresponding to the language. If the language is null, the method result
|
||||||
|
* will be the same as {@link #getBody()}. Null will be returned if the language does not have
|
||||||
|
* a corresponding body.
|
||||||
|
*
|
||||||
|
* @param language the language of the body to return.
|
||||||
|
* @return the body related to the passed in language.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public String getBody(String language) {
|
||||||
|
language = parseXMLLang(language);
|
||||||
|
for (Body body : bodies) {
|
||||||
|
if ((body.langauge == null && language == null)
|
||||||
|
|| (body != null && body.langauge.equals(language))) {
|
||||||
|
return body.message;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a set of all bodies in this Message, including the default message body accessible
|
||||||
|
* from {@link #getBody()}.
|
||||||
|
*
|
||||||
|
* @return a collection of all bodies in this Message.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public Collection<Body> getBodies() {
|
||||||
|
return Collections.unmodifiableCollection(bodies);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -142,7 +173,75 @@ public class Message extends Packet {
|
||||||
* @param body the body of the message.
|
* @param body the body of the message.
|
||||||
*/
|
*/
|
||||||
public void setBody(String body) {
|
public void setBody(String body) {
|
||||||
this.body = body;
|
if (body == null) {
|
||||||
|
removeBody("");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
addBody(null, body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a body with a corresponding language.
|
||||||
|
*
|
||||||
|
* @param language the language of the body being added.
|
||||||
|
* @param body the body being added to the message.
|
||||||
|
* @return the new {@link org.jivesoftware.smack.packet.Message.Body}
|
||||||
|
* @throws NullPointerException if the body is null, a null pointer exception is thrown
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public Body addBody(String language, String body) {
|
||||||
|
if (body == null) {
|
||||||
|
throw new NullPointerException("Body must be specified");
|
||||||
|
}
|
||||||
|
language = parseXMLLang(language);
|
||||||
|
|
||||||
|
Body messageBody = new Body(language, body);
|
||||||
|
bodies.add(messageBody);
|
||||||
|
return messageBody;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the body with the given language from the message.
|
||||||
|
*
|
||||||
|
* @param language the language of the body which is to be removed
|
||||||
|
* @return true if a body was removed and false if it was not.
|
||||||
|
*/
|
||||||
|
public boolean removeBody(String language) {
|
||||||
|
language = parseXMLLang(language);
|
||||||
|
|
||||||
|
for (Body body : bodies) {
|
||||||
|
if (language.equals(body.langauge)) {
|
||||||
|
return bodies.remove(body);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the body from the message and returns true if the body was removed.
|
||||||
|
*
|
||||||
|
* @param body the body being removed from the message.
|
||||||
|
* @return true if the body was successfully removed and false if it was not.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public boolean removeBody(Body body) {
|
||||||
|
return bodies.remove(body);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns all the languages being used for the bodies, not including the default body.
|
||||||
|
*
|
||||||
|
* @return the languages being used for the bodies.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public Collection<String> getBodyLanguages() {
|
||||||
|
List<String> languages = new ArrayList<String>(bodies.size());
|
||||||
|
for (Body body : bodies) {
|
||||||
|
if (!parseXMLLang(body.langauge).equals(getDefaultLanguage())) {
|
||||||
|
languages.add(body.langauge);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableCollection(languages);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,9 +264,35 @@ public class Message extends Packet {
|
||||||
this.thread = thread;
|
this.thread = thread;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the xml:lang of this Message.
|
||||||
|
*
|
||||||
|
* @return the xml:lang of this Message.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
private String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the xml:lang of this Message.
|
||||||
|
*
|
||||||
|
* @param language the xml:lang of this Message.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public void setLanguage(String language) {
|
||||||
|
this.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
public String toXML() {
|
public String toXML() {
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
buf.append("<message");
|
buf.append("<message");
|
||||||
|
if (getXmlns() != null) {
|
||||||
|
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
|
||||||
|
}
|
||||||
|
if (language != null) {
|
||||||
|
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
|
||||||
|
}
|
||||||
if (getPacketID() != null) {
|
if (getPacketID() != null) {
|
||||||
buf.append(" id=\"").append(getPacketID()).append("\"");
|
buf.append(" id=\"").append(getPacketID()).append("\"");
|
||||||
}
|
}
|
||||||
|
@ -184,8 +309,19 @@ public class Message extends Packet {
|
||||||
if (subject != null) {
|
if (subject != null) {
|
||||||
buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
|
buf.append("<subject>").append(StringUtils.escapeForXML(subject)).append("</subject>");
|
||||||
}
|
}
|
||||||
if (body != null) {
|
// Add the body in the default language
|
||||||
buf.append("<body>").append(StringUtils.escapeForXML(body)).append("</body>");
|
if (getBody() != null) {
|
||||||
|
buf.append("<body>").append(StringUtils.escapeForXML(getBody())).append("</body>");
|
||||||
|
}
|
||||||
|
// Add the bodies in other languages
|
||||||
|
for (Body body : getBodies()) {
|
||||||
|
// Skip the default language
|
||||||
|
if (DEFAULT_LANGUAGE.equals(body.getLanguage()) || body.getLanguage() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
buf.append("<body xml:lang=\"").append(body.getLanguage()).append("\">");
|
||||||
|
buf.append(StringUtils.escapeForXML(body.getMessage()));
|
||||||
|
buf.append("</body>");
|
||||||
}
|
}
|
||||||
if (thread != null) {
|
if (thread != null) {
|
||||||
buf.append("<thread>").append(thread).append("</thread>");
|
buf.append("<thread>").append(thread).append("</thread>");
|
||||||
|
@ -203,6 +339,102 @@ public class Message extends Packet {
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Message message = (Message) o;
|
||||||
|
|
||||||
|
if(!super.equals(message)) { return false; }
|
||||||
|
if (bodies.size() != message.bodies.size() || !bodies.containsAll(message.bodies)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (language != null ? !language.equals(message.language) : message.language != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (subject != null ? !subject.equals(message.subject) : message.subject != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (thread != null ? !thread.equals(message.thread) : message.thread != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return type == message.type;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
int result;
|
||||||
|
result = (type != null ? type.hashCode() : 0);
|
||||||
|
result = 31 * result + (subject != null ? subject.hashCode() : 0);
|
||||||
|
result = 31 * result + (thread != null ? thread.hashCode() : 0);
|
||||||
|
result = 31 * result + (language != null ? language.hashCode() : 0);
|
||||||
|
result = 31 * result + bodies.hashCode();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a message body, its language and the content of the message.
|
||||||
|
*/
|
||||||
|
public static class Body {
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
private String langauge;
|
||||||
|
|
||||||
|
private Body(String language, String message) {
|
||||||
|
if (message == null) {
|
||||||
|
throw new NullPointerException("Message cannot be null.");
|
||||||
|
}
|
||||||
|
this.langauge = language;
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the language of this message body. If the language is null, then, no language
|
||||||
|
* was specified.
|
||||||
|
*
|
||||||
|
* @return the language of this message body.
|
||||||
|
*/
|
||||||
|
public String getLanguage() {
|
||||||
|
if (DEFAULT_LANGUAGE.equals(langauge)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return langauge;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the message content.
|
||||||
|
*
|
||||||
|
* @return the content of the message.
|
||||||
|
*/
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) { return false; }
|
||||||
|
|
||||||
|
Body body = (Body) o;
|
||||||
|
|
||||||
|
if (langauge != null ? !langauge.equals(body.langauge) : body.langauge != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return message.equals(body.message);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
int result;
|
||||||
|
result = message.hashCode();
|
||||||
|
result = 31 * result + (langauge != null ? langauge.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the type of a message.
|
* Represents the type of a message.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -42,6 +42,11 @@ import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
*/
|
*/
|
||||||
public abstract class Packet {
|
public abstract class Packet {
|
||||||
|
|
||||||
|
protected static final String DEFAULT_LANGUAGE =
|
||||||
|
java.util.Locale.getDefault().getLanguage().toLowerCase();
|
||||||
|
|
||||||
|
private static String DEFAULT_XML_NS = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
|
* Constant used as packetID to indicate that a packet has no id. To indicate that a packet
|
||||||
* has no id set this constant as the packet's id. When the packet is asked for its id the
|
* has no id set this constant as the packet's id. When the packet is asked for its id the
|
||||||
|
@ -60,23 +65,29 @@ public abstract class Packet {
|
||||||
*/
|
*/
|
||||||
private static long id = 0;
|
private static long id = 0;
|
||||||
|
|
||||||
|
private String xmlns = DEFAULT_XML_NS;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the next unique id. Each id made up of a short alphanumeric
|
* Returns the next unique id. Each id made up of a short alphanumeric
|
||||||
* prefix along with a unique numeric value.
|
* prefix along with a unique numeric value.
|
||||||
*
|
*
|
||||||
* @return the next id.
|
* @return the next id.
|
||||||
*/
|
*/
|
||||||
private static synchronized String nextID() {
|
public static synchronized String nextID() {
|
||||||
return prefix + Long.toString(id++);
|
return prefix + Long.toString(id++);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void setDefaultXmlns(String defaultXmlns) {
|
||||||
|
DEFAULT_XML_NS = defaultXmlns;
|
||||||
|
}
|
||||||
|
|
||||||
private String packetID = null;
|
private String packetID = null;
|
||||||
private String to = null;
|
private String to = null;
|
||||||
private String from = null;
|
private String from = null;
|
||||||
private final List<PacketExtension> packetExtensions
|
private final List<PacketExtension> packetExtensions
|
||||||
= new CopyOnWriteArrayList<PacketExtension>();
|
= new CopyOnWriteArrayList<PacketExtension>();
|
||||||
|
|
||||||
private Map<String,Object> properties = null;
|
private final Map<String,Object> properties = null;
|
||||||
private XMPPError error = null;
|
private XMPPError error = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -280,9 +291,6 @@ public abstract class Packet {
|
||||||
if (!(value instanceof Serializable)) {
|
if (!(value instanceof Serializable)) {
|
||||||
throw new IllegalArgumentException("Value must be serialiazble");
|
throw new IllegalArgumentException("Value must be serialiazble");
|
||||||
}
|
}
|
||||||
if (properties == null) {
|
|
||||||
properties = new HashMap<String, Object>();
|
|
||||||
}
|
|
||||||
properties.put(name, value);
|
properties.put(name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,4 +411,52 @@ public abstract class Packet {
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getXmlns() {
|
||||||
|
return this.xmlns;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String parseXMLLang(String language) {
|
||||||
|
if (language == null || "".equals(language)) {
|
||||||
|
language = DEFAULT_LANGUAGE;
|
||||||
|
}
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static String getDefaultLanguage() {
|
||||||
|
return DEFAULT_LANGUAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
|
Packet packet = (Packet) o;
|
||||||
|
|
||||||
|
if (error != null ? !error.equals(packet.error) : packet.error != null) { return false; }
|
||||||
|
if (from != null ? !from.equals(packet.from) : packet.from != null) { return false; }
|
||||||
|
if (!packetExtensions.equals(packet.packetExtensions)) { return false; }
|
||||||
|
if (packetID != null ? !packetID.equals(packet.packetID) : packet.packetID != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (properties != null ? !properties.equals(packet.properties)
|
||||||
|
: packet.properties != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (to != null ? !to.equals(packet.to) : packet.to != null) { return false; }
|
||||||
|
return !(xmlns != null ? !xmlns.equals(packet.xmlns) : packet.xmlns != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int hashCode() {
|
||||||
|
int result;
|
||||||
|
result = (xmlns != null ? xmlns.hashCode() : 0);
|
||||||
|
result = 31 * result + (packetID != null ? packetID.hashCode() : 0);
|
||||||
|
result = 31 * result + (to != null ? to.hashCode() : 0);
|
||||||
|
result = 31 * result + (from != null ? from.hashCode() : 0);
|
||||||
|
result = 31 * result + packetExtensions.hashCode();
|
||||||
|
result = 31 * result + properties.hashCode();
|
||||||
|
result = 31 * result + (error != null ? error.hashCode() : 0);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -62,6 +62,7 @@ public class Presence extends Packet {
|
||||||
private String status = null;
|
private String status = null;
|
||||||
private int priority = Integer.MIN_VALUE;
|
private int priority = Integer.MIN_VALUE;
|
||||||
private Mode mode = null;
|
private Mode mode = null;
|
||||||
|
private String language;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new presence update. Status, priority, and mode are left un-set.
|
* Creates a new presence update. Status, priority, and mode are left un-set.
|
||||||
|
@ -69,7 +70,7 @@ public class Presence extends Packet {
|
||||||
* @param type the type.
|
* @param type the type.
|
||||||
*/
|
*/
|
||||||
public Presence(Type type) {
|
public Presence(Type type) {
|
||||||
this.type = type;
|
setType(type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,10 +82,10 @@ public class Presence extends Packet {
|
||||||
* @param mode the mode type for this presence update.
|
* @param mode the mode type for this presence update.
|
||||||
*/
|
*/
|
||||||
public Presence(Type type, String status, int priority, Mode mode) {
|
public Presence(Type type, String status, int priority, Mode mode) {
|
||||||
this.type = type;
|
setType(type);
|
||||||
this.status = status;
|
setStatus(status);
|
||||||
this.priority = priority;
|
setPriority(priority);
|
||||||
this.mode = mode;
|
setMode(mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -128,6 +129,9 @@ public class Presence extends Packet {
|
||||||
* @param type the type of the presence packet.
|
* @param type the type of the presence packet.
|
||||||
*/
|
*/
|
||||||
public void setType(Type type) {
|
public void setType(Type type) {
|
||||||
|
if(type == null) {
|
||||||
|
throw new NullPointerException("Type cannot be null");
|
||||||
|
}
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,9 +200,35 @@ public class Presence extends Packet {
|
||||||
this.mode = mode;
|
this.mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the xml:lang of this Presence, or null if one has not been set.
|
||||||
|
*
|
||||||
|
* @return the xml:lang of this Presence, or null if one has not been set.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
private String getLanguage() {
|
||||||
|
return language;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the xml:lang of this Presence.
|
||||||
|
*
|
||||||
|
* @param language the xml:lang of this Presence.
|
||||||
|
* @since 3.0.2
|
||||||
|
*/
|
||||||
|
public void setLanguage(String language) {
|
||||||
|
this.language = language;
|
||||||
|
}
|
||||||
|
|
||||||
public String toXML() {
|
public String toXML() {
|
||||||
StringBuilder buf = new StringBuilder();
|
StringBuilder buf = new StringBuilder();
|
||||||
buf.append("<presence");
|
buf.append("<presence");
|
||||||
|
if(getXmlns() != null) {
|
||||||
|
buf.append(" xmlns=\"").append(getXmlns()).append("\"");
|
||||||
|
}
|
||||||
|
if (language != null) {
|
||||||
|
buf.append(" xml:lang=\"").append(getLanguage()).append("\"");
|
||||||
|
}
|
||||||
if (getPacketID() != null) {
|
if (getPacketID() != null) {
|
||||||
buf.append(" id=\"").append(getPacketID()).append("\"");
|
buf.append(" id=\"").append(getPacketID()).append("\"");
|
||||||
}
|
}
|
||||||
|
@ -241,8 +271,8 @@ public class Presence extends Packet {
|
||||||
if (mode != null) {
|
if (mode != null) {
|
||||||
buf.append(": ").append(mode);
|
buf.append(": ").append(mode);
|
||||||
}
|
}
|
||||||
if (status != null) {
|
if (getStatus() != null) {
|
||||||
buf.append(" (").append(status).append(")");
|
buf.append(" (").append(getStatus()).append(")");
|
||||||
}
|
}
|
||||||
return buf.toString();
|
return buf.toString();
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,13 +61,17 @@ public class PacketParserUtils {
|
||||||
message.setTo(parser.getAttributeValue("", "to"));
|
message.setTo(parser.getAttributeValue("", "to"));
|
||||||
message.setFrom(parser.getAttributeValue("", "from"));
|
message.setFrom(parser.getAttributeValue("", "from"));
|
||||||
message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
|
message.setType(Message.Type.fromString(parser.getAttributeValue("", "type")));
|
||||||
|
String language = getLanguageAttribute(parser);
|
||||||
|
if (language != null && !"".equals(language.trim())) {
|
||||||
|
message.setLanguage(language);
|
||||||
|
}
|
||||||
|
|
||||||
// Parse sub-elements. We include extra logic to make sure the values
|
// Parse sub-elements. We include extra logic to make sure the values
|
||||||
// are only read once. This is because it's possible for the names to appear
|
// are only read once. This is because it's possible for the names to appear
|
||||||
// in arbitrary sub-elements.
|
// in arbitrary sub-elements.
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
String subject = null;
|
String subject = null;
|
||||||
String body = null;
|
String body;
|
||||||
String thread = null;
|
String thread = null;
|
||||||
Map<String, Object> properties = null;
|
Map<String, Object> properties = null;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
|
@ -81,9 +85,9 @@ public class PacketParserUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (elementName.equals("body")) {
|
else if (elementName.equals("body")) {
|
||||||
if (body == null) {
|
String xmlLang = getLanguageAttribute(parser);
|
||||||
body = parser.nextText();
|
body = parser.nextText();
|
||||||
}
|
message.addBody(xmlLang, body);
|
||||||
}
|
}
|
||||||
else if (elementName.equals("thread")) {
|
else if (elementName.equals("thread")) {
|
||||||
if (thread == null) {
|
if (thread == null) {
|
||||||
|
@ -111,7 +115,6 @@ public class PacketParserUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
message.setSubject(subject);
|
message.setSubject(subject);
|
||||||
message.setBody(body);
|
|
||||||
message.setThread(thread);
|
message.setThread(thread);
|
||||||
// Set packet properties.
|
// Set packet properties.
|
||||||
if (properties != null) {
|
if (properties != null) {
|
||||||
|
@ -140,13 +143,18 @@ public class PacketParserUtils {
|
||||||
System.err.println("Found invalid presence type " + typeString);
|
System.err.println("Found invalid presence type " + typeString);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Presence presence = new Presence(type);
|
Presence presence = new Presence(type);
|
||||||
presence.setTo(parser.getAttributeValue("", "to"));
|
presence.setTo(parser.getAttributeValue("", "to"));
|
||||||
presence.setFrom(parser.getAttributeValue("", "from"));
|
presence.setFrom(parser.getAttributeValue("", "from"));
|
||||||
String id = parser.getAttributeValue("", "id");
|
String id = parser.getAttributeValue("", "id");
|
||||||
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
||||||
|
|
||||||
|
String language = getLanguageAttribute(parser);
|
||||||
|
if (language != null && !"".equals(language.trim())) {
|
||||||
|
presence.setLanguage(language);
|
||||||
|
}
|
||||||
|
presence.setPacketID(id == null ? Packet.ID_NOT_AVAILABLE : id);
|
||||||
|
|
||||||
// Parse sub-elements
|
// Parse sub-elements
|
||||||
boolean done = false;
|
boolean done = false;
|
||||||
while (!done) {
|
while (!done) {
|
||||||
|
@ -403,6 +411,18 @@ public class PacketParserUtils {
|
||||||
return extension;
|
return extension;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getLanguageAttribute(XmlPullParser parser) {
|
||||||
|
for (int i = 0; i < parser.getAttributeCount(); i++) {
|
||||||
|
String attributeName = parser.getAttributeName(i);
|
||||||
|
if ( "xml:lang".equals(attributeName) ||
|
||||||
|
("lang".equals(attributeName) &&
|
||||||
|
"xml".equals(parser.getAttributePrefix(i)))) {
|
||||||
|
return parser.getAttributeValue(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static Object parseWithIntrospection(String elementName,
|
public static Object parseWithIntrospection(String elementName,
|
||||||
Class objectClass, XmlPullParser parser) throws Exception
|
Class objectClass, XmlPullParser parser) throws Exception
|
||||||
{
|
{
|
||||||
|
|
244
test-unit/org/jivesoftware/smack/packet/MessageTest.java
Normal file
244
test-unit/org/jivesoftware/smack/packet/MessageTest.java
Normal file
|
@ -0,0 +1,244 @@
|
||||||
|
/**
|
||||||
|
* $Revision:$
|
||||||
|
* $Date:$
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Jive Software. All rights reserved.
|
||||||
|
* This software is the proprietary information of Jive Software. Use is subject to license terms.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
import static org.custommonkey.xmlunit.XMLAssert.*;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class MessageTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setMessageTypeTest() throws IOException, SAXException {
|
||||||
|
Message.Type type = Message.Type.chat;
|
||||||
|
Message.Type type2 = Message.Type.headline;
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message")
|
||||||
|
.append(" type=\"")
|
||||||
|
.append(type)
|
||||||
|
.append("\">")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message messageTypeInConstructor = new Message(null, Message.Type.chat);
|
||||||
|
messageTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
|
||||||
|
assertEquals(type, messageTypeInConstructor.getType());
|
||||||
|
assertXMLEqual(control, messageTypeInConstructor.toXML());
|
||||||
|
|
||||||
|
controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message")
|
||||||
|
.append(" type=\"")
|
||||||
|
.append(type2)
|
||||||
|
.append("\">")
|
||||||
|
.append("</message>");
|
||||||
|
control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message messageTypeSet = getNewMessage();
|
||||||
|
messageTypeSet.setType(type2);
|
||||||
|
assertEquals(type2, messageTypeSet.getType());
|
||||||
|
assertXMLEqual(control, messageTypeSet.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void setMessageTypeNullTest() {
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setType(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=NullPointerException.class)
|
||||||
|
public void setNullMessageBodyTest() {
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.addBody(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setMessageSubjectTest() throws IOException, SAXException {
|
||||||
|
final String messageSubject = "This is a test of the emergency broadcast system.";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message>")
|
||||||
|
.append("<subject>")
|
||||||
|
.append(messageSubject)
|
||||||
|
.append("</subject>")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setSubject(messageSubject);
|
||||||
|
|
||||||
|
assertEquals(messageSubject, message.getSubject());
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void oneMessageBodyTest() throws IOException, SAXException {
|
||||||
|
final String messageBody = "This is a test of the emergency broadcast system.";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message>")
|
||||||
|
.append("<body>")
|
||||||
|
.append(messageBody)
|
||||||
|
.append("</body>")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setBody(messageBody);
|
||||||
|
|
||||||
|
assertEquals(messageBody, message.getBody());
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void multipleMessageBodiesTest() throws IOException, SAXException {
|
||||||
|
final String messageBody1 = "This is a test of the emergency broadcast system, 1.";
|
||||||
|
final String lang2 = "ru";
|
||||||
|
final String messageBody2 = "This is a test of the emergency broadcast system, 2.";
|
||||||
|
final String lang3 = "sp";
|
||||||
|
final String messageBody3 = "This is a test of the emergency broadcast system, 3.";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message>")
|
||||||
|
.append("<body>")
|
||||||
|
.append(messageBody1)
|
||||||
|
.append("</body>")
|
||||||
|
.append("<body xml:lang=\"")
|
||||||
|
.append(lang2)
|
||||||
|
.append("\">")
|
||||||
|
.append(messageBody2)
|
||||||
|
.append("</body>")
|
||||||
|
.append("<body xml:lang=\"")
|
||||||
|
.append(lang3)
|
||||||
|
.append("\">")
|
||||||
|
.append(messageBody3)
|
||||||
|
.append("</body>")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.addBody(null, messageBody1);
|
||||||
|
message.addBody(lang2, messageBody2);
|
||||||
|
message.addBody(lang3, messageBody3);
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
|
||||||
|
Collection<String> languages = message.getBodyLanguages();
|
||||||
|
List<String> controlLanguages = new ArrayList<String>();
|
||||||
|
controlLanguages.add(lang2);
|
||||||
|
controlLanguages.add(lang3);
|
||||||
|
controlLanguages.removeAll(languages);
|
||||||
|
assertTrue(controlLanguages.size() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void removeMessageBodyTest() {
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setBody("test");
|
||||||
|
assertTrue(message.getBodies().size() == 1);
|
||||||
|
|
||||||
|
message.setBody(null);
|
||||||
|
assertTrue(message.getBodies().size() == 0);
|
||||||
|
|
||||||
|
assertFalse(message.removeBody("sp"));
|
||||||
|
|
||||||
|
Message.Body body = message.addBody("es", "test");
|
||||||
|
assertTrue(message.getBodies().size() == 1);
|
||||||
|
|
||||||
|
message.removeBody(body);
|
||||||
|
assertTrue(message.getBodies().size() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setMessageThreadTest() throws IOException, SAXException {
|
||||||
|
final String messageThread = "1234";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message>")
|
||||||
|
.append("<thread>")
|
||||||
|
.append(messageThread)
|
||||||
|
.append("</thread>")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setThread(messageThread);
|
||||||
|
|
||||||
|
assertEquals(messageThread, message.getThread());
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void messageXmlLangTest() throws IOException, SAXException {
|
||||||
|
final String lang = "sp";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message")
|
||||||
|
.append(" xml:lang=\"")
|
||||||
|
.append(lang)
|
||||||
|
.append("\">")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Message message = getNewMessage();
|
||||||
|
message.setLanguage(lang);
|
||||||
|
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void messageEqualityTest() {
|
||||||
|
Message message = getNewMessage();
|
||||||
|
assertTrue(message.equals(message));
|
||||||
|
//noinspection ObjectEqualsNull
|
||||||
|
assertFalse(message.equals(null));
|
||||||
|
assertFalse(message.equals("test"));
|
||||||
|
Message message2 = getNewMessage();
|
||||||
|
|
||||||
|
assertTrue(message.equals(message2));
|
||||||
|
|
||||||
|
message.setTo("joe@shmoe.com");
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setTo("joe@shmoe.com");
|
||||||
|
|
||||||
|
message.setSubject("subject");
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setSubject("subject");
|
||||||
|
|
||||||
|
message.setThread("thread");
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setThread("thread");
|
||||||
|
|
||||||
|
message.setBody("body1");
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setBody("body1");
|
||||||
|
|
||||||
|
message.setLanguage("language");
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setLanguage("language");
|
||||||
|
|
||||||
|
message.setType(Message.Type.chat);
|
||||||
|
assertFalse(message.equals(message2));
|
||||||
|
message2.setType(Message.Type.chat);
|
||||||
|
|
||||||
|
assertTrue(message.equals(message2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Message getNewMessage() {
|
||||||
|
Message message = new Message();
|
||||||
|
message.setPacketID(Packet.ID_NOT_AVAILABLE);
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
186
test-unit/org/jivesoftware/smack/packet/PresenceTest.java
Normal file
186
test-unit/org/jivesoftware/smack/packet/PresenceTest.java
Normal file
|
@ -0,0 +1,186 @@
|
||||||
|
/**
|
||||||
|
* $Revision:$
|
||||||
|
* $Date:$
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Jive Software. All rights reserved.
|
||||||
|
* This software is the proprietary information of Jive Software. Use is subject to license terms.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.packet;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||||
|
import org.xml.sax.SAXException;
|
||||||
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class PresenceTest {
|
||||||
|
@Test
|
||||||
|
public void setPresenceTypeTest() throws IOException, SAXException {
|
||||||
|
Presence.Type type = Presence.Type.unavailable;
|
||||||
|
Presence.Type type2 = Presence.Type.subscribe;
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence")
|
||||||
|
.append(" type=\"")
|
||||||
|
.append(type)
|
||||||
|
.append("\">")
|
||||||
|
.append("</presence>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presenceTypeInConstructor = new Presence(type);
|
||||||
|
presenceTypeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
|
||||||
|
assertEquals(type, presenceTypeInConstructor.getType());
|
||||||
|
assertXMLEqual(control, presenceTypeInConstructor.toXML());
|
||||||
|
|
||||||
|
controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence")
|
||||||
|
.append(" type=\"")
|
||||||
|
.append(type2)
|
||||||
|
.append("\">")
|
||||||
|
.append("</presence>");
|
||||||
|
control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presenceTypeSet = getNewPresence();
|
||||||
|
presenceTypeSet.setType(type2);
|
||||||
|
assertEquals(type2, presenceTypeSet.getType());
|
||||||
|
assertXMLEqual(control, presenceTypeSet.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=NullPointerException.class)
|
||||||
|
public void setNullPresenceTypeTest() {
|
||||||
|
getNewPresence().setType(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isPresenceAvailableTest() {
|
||||||
|
Presence presence = getNewPresence();
|
||||||
|
presence.setType(Presence.Type.available);
|
||||||
|
assertTrue(presence.isAvailable());
|
||||||
|
|
||||||
|
presence.setType(Presence.Type.unavailable);
|
||||||
|
assertFalse(presence.isAvailable());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setPresenceStatusTest() throws IOException, SAXException {
|
||||||
|
final String status = "This is a test of the emergency broadcast system.";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence>")
|
||||||
|
.append("<status>")
|
||||||
|
.append(status)
|
||||||
|
.append("</status>")
|
||||||
|
.append("</presence>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presence = getNewPresence();
|
||||||
|
presence.setStatus(status);
|
||||||
|
|
||||||
|
assertEquals(status, presence.getStatus());
|
||||||
|
assertXMLEqual(control, presence.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setPresencePriorityTest() throws IOException, SAXException {
|
||||||
|
final int priority = 10;
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence>")
|
||||||
|
.append("<priority>")
|
||||||
|
.append(priority)
|
||||||
|
.append("</priority>")
|
||||||
|
.append("</presence>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presence = getNewPresence();
|
||||||
|
presence.setPriority(priority);
|
||||||
|
|
||||||
|
assertEquals(priority, presence.getPriority());
|
||||||
|
assertXMLEqual(control, presence.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=IllegalArgumentException.class)
|
||||||
|
public void setIllegalPriorityTest() {
|
||||||
|
getNewPresence().setPriority(Integer.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void setPresenceModeTest() throws IOException, SAXException {
|
||||||
|
Presence.Mode mode1 = Presence.Mode.dnd;
|
||||||
|
final int priority = 10;
|
||||||
|
final String status = "This is a test of the emergency broadcast system.";
|
||||||
|
Presence.Mode mode2 = Presence.Mode.chat;
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence>")
|
||||||
|
.append("<status>")
|
||||||
|
.append(status)
|
||||||
|
.append("</status>")
|
||||||
|
.append("<priority>")
|
||||||
|
.append(priority)
|
||||||
|
.append("</priority>")
|
||||||
|
.append("<show>")
|
||||||
|
.append(mode1)
|
||||||
|
.append("</show>")
|
||||||
|
.append("</presence>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presenceModeInConstructor = new Presence(Presence.Type.available, status, priority,
|
||||||
|
mode1);
|
||||||
|
presenceModeInConstructor.setPacketID(Packet.ID_NOT_AVAILABLE);
|
||||||
|
assertEquals(mode1, presenceModeInConstructor.getMode());
|
||||||
|
assertXMLEqual(control, presenceModeInConstructor.toXML());
|
||||||
|
|
||||||
|
controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence>")
|
||||||
|
.append("<show>")
|
||||||
|
.append(mode2)
|
||||||
|
.append("</show>")
|
||||||
|
.append("</presence>");
|
||||||
|
control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presenceModeSet = getNewPresence();
|
||||||
|
presenceModeSet.setMode(mode2);
|
||||||
|
assertEquals(mode2, presenceModeSet.getMode());
|
||||||
|
assertXMLEqual(control, presenceModeSet.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void isModeAwayTest() {
|
||||||
|
Presence presence = getNewPresence();
|
||||||
|
presence.setMode(Presence.Mode.away);
|
||||||
|
assertTrue(presence.isAway());
|
||||||
|
|
||||||
|
presence.setMode(Presence.Mode.chat);
|
||||||
|
assertFalse(presence.isAway());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void presenceXmlLangTest() throws IOException, SAXException {
|
||||||
|
final String lang = "sp";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<presence")
|
||||||
|
.append(" xml:lang=\"")
|
||||||
|
.append(lang)
|
||||||
|
.append("\">")
|
||||||
|
.append("</presence>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Presence presence = getNewPresence();
|
||||||
|
presence.setLanguage(lang);
|
||||||
|
|
||||||
|
assertXMLEqual(control, presence.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Presence getNewPresence() {
|
||||||
|
Presence presence = new Presence(Presence.Type.available);
|
||||||
|
presence.setPacketID(Packet.ID_NOT_AVAILABLE);
|
||||||
|
return presence;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,63 @@
|
||||||
|
/**
|
||||||
|
* $Revision:$
|
||||||
|
* $Date:$
|
||||||
|
*
|
||||||
|
* Copyright (C) 2007 Jive Software. All rights reserved.
|
||||||
|
* This software is the proprietary information of Jive Software. Use is subject to license terms.
|
||||||
|
*/
|
||||||
|
package org.jivesoftware.smack.util;
|
||||||
|
|
||||||
|
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
||||||
|
import org.jivesoftware.smack.packet.Packet;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.xmlpull.mxp1.MXParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParser;
|
||||||
|
import org.xmlpull.v1.XmlPullParserException;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.StringReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class PacketParserUtilsTest {
|
||||||
|
@Test
|
||||||
|
public void multipleMessageBodiesParsingTest() throws Exception {
|
||||||
|
final String messageBody1 = "This is a test of the emergency broadcast system, 1.";
|
||||||
|
final String lang2 = "ru";
|
||||||
|
final String messageBody2 = "This is a test of the emergency broadcast system, 2.";
|
||||||
|
final String lang3 = "sp";
|
||||||
|
final String messageBody3 = "This is a test of the emergency broadcast system, 3.";
|
||||||
|
|
||||||
|
StringBuilder controlBuilder = new StringBuilder();
|
||||||
|
controlBuilder.append("<message xml:lang=\"en\">")
|
||||||
|
.append("<body>")
|
||||||
|
.append(messageBody1)
|
||||||
|
.append("</body>")
|
||||||
|
.append("<body xml:lang=\"")
|
||||||
|
.append(lang2)
|
||||||
|
.append("\">")
|
||||||
|
.append(messageBody2)
|
||||||
|
.append("</body>")
|
||||||
|
.append("<body xml:lang=\"")
|
||||||
|
.append(lang3)
|
||||||
|
.append("\">")
|
||||||
|
.append(messageBody3)
|
||||||
|
.append("</body>")
|
||||||
|
.append("</message>");
|
||||||
|
String control = controlBuilder.toString();
|
||||||
|
|
||||||
|
Packet message = PacketParserUtils.parseMessage(getParser(control));
|
||||||
|
assertXMLEqual(control, message.toXML());
|
||||||
|
}
|
||||||
|
|
||||||
|
private XmlPullParser getParser(String control) throws XmlPullParserException, IOException {
|
||||||
|
XmlPullParser parser = new MXParser();
|
||||||
|
parser.setInput(new StringReader(control));
|
||||||
|
while(true) {
|
||||||
|
if(parser.next() == XmlPullParser.START_TAG
|
||||||
|
&& parser.getName().equals("message")) { break; }
|
||||||
|
}
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue