1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2024-11-26 14:02:06 +01:00

Created new chat manager which handles the creation of Chats.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@6213 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Alex Wenckus 2006-11-22 22:55:37 +00:00 committed by alex
parent 18a35e4e8f
commit ae6065d7cc
36 changed files with 3964 additions and 714 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<module version="4" relativePaths="true" type="JAVA_MODULE"> <module version="4" relativePaths="true" type="JAVA_MODULE">
<component name="ModuleRootManager" /> <component name="ModuleRootManager" />
<component name="NewModuleRootManager"> <component name="NewModuleRootManager" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/../../classes" /> <output url="file://$MODULE_DIR$/../../classes" />
<exclude-output /> <exclude-output />
<exclude-exploded /> <exclude-exploded />

View file

@ -12,6 +12,13 @@
<properties /> <properties />
</buildFile> </buildFile>
</component> </component>
<component name="BuildJarProjectSettings">
<option name="BUILD_JARS_ON_MAKE" value="false" />
</component>
<component name="CodeStyleProjectProfileManger">
<option name="PROJECT_PROFILE" />
<option name="USE_PROJECT_LEVEL_SETTINGS" value="false" />
</component>
<component name="CodeStyleSettingsManager"> <component name="CodeStyleSettingsManager">
<option name="PER_PROJECT_SETTINGS"> <option name="PER_PROJECT_SETTINGS">
<value> <value>
@ -29,8 +36,10 @@
</component> </component>
<component name="CompilerConfiguration"> <component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" /> <option name="DEFAULT_COMPILER" value="Javac" />
<option name="CLEAR_OUTPUT_DIRECTORY" value="false" />
<option name="DEPLOY_AFTER_MAKE" value="0" /> <option name="DEPLOY_AFTER_MAKE" value="0" />
<excludeFromCompile>
<file url="file://$PROJECT_DIR$/../../../SmackTester/src/LeakTest.java" />
</excludeFromCompile>
<resourceExtensions> <resourceExtensions>
<entry name=".+\.(properties|xml|html|dtd|tld)" /> <entry name=".+\.(properties|xml|html|dtd|tld)" />
<entry name=".+\.(gif|png|jpeg|jpg)" /> <entry name=".+\.(gif|png|jpeg|jpg)" />
@ -52,6 +61,20 @@
<option name="myForwardDirection" value="false" /> <option name="myForwardDirection" value="false" />
</component> </component>
<component name="DependencyValidationManager" /> <component name="DependencyValidationManager" />
<component name="EclipseCompilerSettings">
<option name="DEBUGGING_INFO" value="true" />
<option name="GENERATE_NO_WARNINGS" value="true" />
<option name="DEPRECATION" value="false" />
<option name="ADDITIONAL_OPTIONS_STRING" value="" />
<option name="MAXIMUM_HEAP_SIZE" value="128" />
</component>
<component name="EclipseEmbeddedCompilerSettings">
<option name="DEBUGGING_INFO" value="true" />
<option name="GENERATE_NO_WARNINGS" value="true" />
<option name="DEPRECATION" value="false" />
<option name="ADDITIONAL_OPTIONS_STRING" value="" />
<option name="MAXIMUM_HEAP_SIZE" value="128" />
</component>
<component name="EntryPointsManager"> <component name="EntryPointsManager">
<entry_points /> <entry_points />
</component> </component>
@ -61,6 +84,48 @@
<option name="OUTPUT_DIRECTORY" /> <option name="OUTPUT_DIRECTORY" />
</component> </component>
<component name="GUI Designer component loader factory" /> <component name="GUI Designer component loader factory" />
<component name="IdProvider" IDEtalkID="A0560E1D64D0B56EB83CBCB1C84992E6" />
<component name="InspectionProjectProfileManager">
<option name="PROJECT_PROFILE" value="Project Default" />
<option name="USE_PROJECT_LEVEL_SETTINGS" value="false" />
<scopes />
<profiles>
<profile version="1.0" is_locked="false">
<option name="myName" value="Project Default" />
<option name="myLocal" value="false" />
<used_levels>
<error>
<option name="myName" value="ERROR" />
<option name="myVal" value="400" />
</error>
<warning>
<option name="myName" value="WARNING" />
<option name="myVal" value="300" />
</warning>
<information>
<option name="myName" value="INFO" />
<option name="myVal" value="200" />
</information>
<server>
<option name="myName" value="SERVER PROBLEM" />
<option name="myVal" value="100" />
</server>
</used_levels>
<inspection_tool class="CloneCallsSuperClone" level="WARNING" enabled="false" />
<inspection_tool class="ForCanBeForeach" level="WARNING" enabled="false" />
<inspection_tool class="UnnecessaryBoxing" level="WARNING" enabled="false" />
<inspection_tool class="UnnecessaryUnboxing" level="WARNING" enabled="false" />
<inspection_tool class="WhileCanBeForeach" level="WARNING" enabled="false" />
<inspection_tool class="SuspiciousMethodCalls" level="WARNING" enabled="false" />
<inspection_tool class="ConstantConditions" level="WARNING" enabled="false">
<option name="SUGGEST_NULLABLE_ANNOTATIONS" value="false" />
</inspection_tool>
<inspection_tool class="PointlessBooleanExpression" level="WARNING" enabled="false">
<option name="m_ignoreExpressionsContainingConstants" value="false" />
</inspection_tool>
</profile>
</profiles>
</component>
<component name="JavacSettings"> <component name="JavacSettings">
<option name="DEBUGGING_INFO" value="true" /> <option name="DEBUGGING_INFO" value="true" />
<option name="GENERATE_NO_WARNINGS" value="false" /> <option name="GENERATE_NO_WARNINGS" value="false" />
@ -82,6 +147,7 @@
<option name="OPTION_DEPRECATED_LIST" value="true" /> <option name="OPTION_DEPRECATED_LIST" value="true" />
<option name="OTHER_OPTIONS" value="" /> <option name="OTHER_OPTIONS" value="" />
<option name="HEAP_SIZE" /> <option name="HEAP_SIZE" />
<option name="LOCALE" />
<option name="OPEN_IN_BROWSER" value="true" /> <option name="OPEN_IN_BROWSER" value="true" />
</component> </component>
<component name="JikesSettings"> <component name="JikesSettings">
@ -92,116 +158,141 @@
<option name="IS_EMACS_ERRORS_MODE" value="true" /> <option name="IS_EMACS_ERRORS_MODE" value="true" />
<option name="ADDITIONAL_OPTIONS_STRING" value="" /> <option name="ADDITIONAL_OPTIONS_STRING" value="" />
</component> </component>
<component name="LogConsolePreferences">
<option name="FILTER_ERRORS" value="false" />
<option name="FILTER_WARNINGS" value="false" />
<option name="FILTER_INFO" value="true" />
<option name="CUSTOM_FILTER" />
</component>
<component name="Palette2"> <component name="Palette2">
<group name="Swing"> <group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false"> <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item> </item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false"> <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item> </item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false"> <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item> </item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false"> <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item> </item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false"> <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values> <initial-values>
<property name="text" value="Button" /> <property name="text" value="Button" />
</initial-values> </initial-values>
</item> </item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false"> <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values> <initial-values>
<property name="text" value="RadioButton" /> <property name="text" value="RadioButton" />
</initial-values> </initial-values>
</item> </item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false"> <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values> <initial-values>
<property name="text" value="CheckBox" /> <property name="text" value="CheckBox" />
</initial-values> </initial-values>
</item> </item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false"> <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values> <initial-values>
<property name="text" value="Label" /> <property name="text" value="Label" />
</initial-values> </initial-values>
</item> </item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false"> <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" /> <preferred-size width="150" height="-1" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false"> <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" /> <preferred-size width="150" height="-1" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false"> <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" /> <preferred-size width="150" height="-1" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false"> <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false"> <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false"> <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false"> <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item> </item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false"> <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false"> <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false"> <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" /> <preferred-size width="150" height="50" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false"> <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" /> <preferred-size width="200" height="200" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false"> <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" /> <preferred-size width="200" height="200" />
</default-constraints> </default-constraints>
</item> </item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false"> <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item> </item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false"> <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item> </item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group> </group>
</component> </component>
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<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$/../../../SmackTester/SmackTester.iml" filepath="$PROJECT_DIR$/../../../SmackTester/SmackTester.iml" />
</modules> </modules>
</component> </component>
<component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" /> <component name="ProjectRootManager" version="2" assert-keyword="true" jdk-15="true" project-jdk-name="JDK 1.5.0" />
<component name="ProjectRunConfigurationManager" />
<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" />
@ -209,6 +300,15 @@
<option name="GENERATE_IIOP_STUBS" value="false" /> <option name="GENERATE_IIOP_STUBS" value="false" />
<option name="ADDITIONAL_OPTIONS_STRING" value="" /> <option name="ADDITIONAL_OPTIONS_STRING" value="" />
</component> </component>
<component name="StarteamVcsAdapter" />
<component name="VssVcs" />
<component name="com.intellij.jsf.UserDefinedFacesConfigs">
<option name="USER_DEFINED_CONFIGS">
<value>
<list size="0" />
</value>
</option>
</component>
<component name="libraryTable"> <component name="libraryTable">
<library name="Smack"> <library name="Smack">
<CLASSES> <CLASSES>
@ -224,6 +324,7 @@
<component name="uidesigner-configuration"> <component name="uidesigner-configuration">
<option name="INSTRUMENT_CLASSES" value="true" /> <option name="INSTRUMENT_CLASSES" value="true" />
<option name="COPY_FORMS_RUNTIME_TO_OUTPUT" value="true" /> <option name="COPY_FORMS_RUNTIME_TO_OUTPUT" value="true" />
<option name="DEFAULT_LAYOUT_MANAGER" value="GridLayoutManager" />
</component> </component>
<UsedPathMacros /> <UsedPathMacros />
</project> </project>

View file

@ -20,15 +20,12 @@
package org.jivesoftware.smack; package org.jivesoftware.smack;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.util.StringUtils;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
/** /**
* A chat is a series of messages sent between two users. Each chat has a unique * A chat is a series of messages sent between two users. Each chat has a unique
@ -38,72 +35,27 @@ import java.util.Set;
* arrives it is routed to the most recently created Chat with the message * arrives it is routed to the most recently created Chat with the message
* sender. * sender.
* *
* @see XMPPConnection#createChat(String)
* @author Matt Tucker * @author Matt Tucker
*/ */
public class Chat { public class Chat {
/** private ChatManager chatManager;
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(5);
/**
* Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID.
*/
private static long id = 0;
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
private static synchronized String nextID() {
return prefix + Long.toString(id++);
}
private XMPPConnection connection;
private String threadID; private String threadID;
private String participant; private String participant;
private PacketFilter messageFilter;
private PacketCollector messageCollector;
private final Set<WeakReference<PacketListener>> listeners = private final Set<WeakReference<PacketListener>> listeners =
new HashSet<WeakReference<PacketListener>>(); new CopyOnWriteArraySet<WeakReference<PacketListener>>();
/**
* Creates a new chat with the specified user.
*
* @param connection the connection the chat will use.
* @param participant the user to chat with.
*/
public Chat(XMPPConnection connection, String participant) {
// Automatically assign the next chat ID.
this(connection, participant, nextID());
}
/** /**
* Creates a new chat with the specified user and thread ID. * Creates a new chat with the specified user and thread ID.
* *
* @param connection the connection the chat will use. * @param chatManager the chatManager the chat will use.
* @param participant the user to chat with. * @param participant the user to chat with.
* @param threadID the thread ID to use. * @param threadID the thread ID to use.
*/ */
public Chat(XMPPConnection connection, String participant, String threadID) { Chat(ChatManager chatManager, String participant, String threadID) {
this.connection = connection; this.chatManager = chatManager;
this.participant = participant; this.participant = participant;
this.threadID = threadID; this.threadID = threadID;
// Register with the map of chats so that messages with no thread ID
// set will be delivered to this Chat.
connection.chats.put(StringUtils.parseBareAddress(participant),
new WeakReference<Chat>(this));
// Filter the messages whose thread equals Chat's id
messageFilter = new ThreadFilter(threadID);
messageCollector = connection.createPacketCollector(messageFilter);
} }
/** /**
@ -140,30 +92,15 @@ public class Chat {
* @throws XMPPException if sending the message fails. * @throws XMPPException if sending the message fails.
*/ */
public void sendMessage(String text) throws XMPPException { public void sendMessage(String text) throws XMPPException {
Message message = createMessage(); Message message = new Message(participant, Message.Type.chat);
message.setBody(text);
connection.sendPacket(message);
}
/**
* Creates a new Message to the chat participant. The message returned
* will have its thread property set with this chat ID.
*
* @return a new message addressed to the chat participant and
* using the correct thread value.
* @see #sendMessage(Message)
*/
public Message createMessage() {
Message message = new Message(participant, Message.Type.CHAT);
message.setThread(threadID); message.setThread(threadID);
return message; message.setBody(text);
chatManager.sendMessage(this, message);
} }
/** /**
* Sends a message to the other chat participant. The thread ID, recipient, * Sends a message to the other chat participant. The thread ID, recipient,
* and message type of the message will automatically set to those of this chat * and message type of the message will automatically set to those of this chat.
* in case the Message was not created using the {@link #createMessage() createMessage}
* method.
* *
* @param message the message to send. * @param message the message to send.
* @throws XMPPException if an error occurs sending the message. * @throws XMPPException if an error occurs sending the message.
@ -172,47 +109,9 @@ public class Chat {
// Force the recipient, message type, and thread ID since the user elected // Force the recipient, message type, and thread ID since the user elected
// to send the message through this chat object. // to send the message through this chat object.
message.setTo(participant); message.setTo(participant);
message.setType(Message.Type.CHAT); message.setType(Message.Type.chat);
message.setThread(threadID); message.setThread(threadID);
connection.sendPacket(message); chatManager.sendMessage(this, message);
}
/**
* Polls for and returns the next message, or <tt>null</tt> if there isn't
* a message immediately available. This method provides significantly different
* functionalty than the {@link #nextMessage()} method since it's non-blocking.
* In other words, the method call will always return immediately, whereas the
* nextMessage method will return only when a message is available (or after
* a specific timeout).
*
* @return the next message if one is immediately available and
* <tt>null</tt> otherwise.
*/
public Message pollMessage() {
return (Message)messageCollector.pollResult();
}
/**
* Returns the next available message in the chat. The method call will block
* (not return) until a message is available.
*
* @return the next message.
*/
public Message nextMessage() {
return (Message)messageCollector.nextResult();
}
/**
* Returns the next available message in the chat. The method call will block
* (not return) until a packet is available or the <tt>timeout</tt> has elapased.
* If the timeout elapses without a result, <tt>null</tt> will be returned.
*
* @param timeout the maximum amount of time to wait for the next message.
* @return the next message, or <tt>null</tt> if the timeout elapses without a
* message becoming available.
*/
public Message nextMessage(long timeout) {
return (Message)messageCollector.nextResult(timeout);
} }
/** /**
@ -222,12 +121,21 @@ public class Chat {
* @param listener a packet listener. * @param listener a packet listener.
*/ */
public void addMessageListener(PacketListener listener) { public void addMessageListener(PacketListener listener) {
connection.addPacketListener(listener, messageFilter); if(listener == null) {
// Keep track of the listener so that we can manually deliver extra return;
// messages to it later if needed. }
synchronized (listeners) {
listeners.add(new WeakReference<PacketListener>(listener)); listeners.add(new WeakReference<PacketListener>(listener));
} }
/**
* Creates a {@link org.jivesoftware.smack.PacketCollector} which will accumulate the Messages
* for this chat. Always cancel PacketCollectors when finished with them as they will accumulate
* messages indefinitely.
*
* @return the PacketCollector which returns Messages for this chat.
*/
public PacketCollector createCollector() {
return chatManager.createPacketCollector(this);
} }
/** /**
@ -244,8 +152,6 @@ public class Chat {
// probably never had one. // probably never had one.
message.setThread(threadID); message.setThread(threadID);
messageCollector.processPacket(message);
synchronized (listeners) {
for (Iterator<WeakReference<PacketListener>> i = listeners.iterator(); i.hasNext();) { for (Iterator<WeakReference<PacketListener>> i = listeners.iterator(); i.hasNext();) {
WeakReference<PacketListener> listenerRef = i.next(); WeakReference<PacketListener> listenerRef = i.next();
PacketListener listener; PacketListener listener;
@ -259,16 +165,3 @@ public class Chat {
} }
} }
} }
public void finalize() throws Throwable {
super.finalize();
try {
if (messageCollector != null) {
messageCollector.cancel();
}
}
catch (Exception e) {
// Ignore.
}
}
}

View file

@ -0,0 +1,25 @@
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.smack;
/**
* A listener for chat related events.
*
* @author Alexander Wenckus
*/
public interface ChatListener {
/**
* Event fired when a new chat is created.
*
* @param chat the chat that was created.
* @param createdLocally true if the chat was created by the local user and false if it wasn't.
*/
void chatCreated(Chat chat, boolean createdLocally);
}

View file

@ -0,0 +1,188 @@
/**
* $RCSfile: $
* $Revision: $
* $Date: $
*
* Copyright (C) 2006 Jive Software. All rights reserved.
* This software is the proprietary information of Jive Software. Use is subject to license terms.
*/
package org.jivesoftware.smack;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smack.util.collections.ReferenceMap;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.filter.*;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* The chat manager keeps track of references to all current chats. It will not hold any references
* in memory on its own so it is neccesary to keep a reference to the chat object itself. To be
* made aware of new chats, register a listener by calling {@link #addChatListener(ChatListener)}.
*
* @author Alexander Wenckus
*/
public class ChatManager {
/**
* Returns the next unique id. Each id made up of a short alphanumeric
* prefix along with a unique numeric value.
*
* @return the next id.
*/
private static synchronized String nextID() {
return prefix + Long.toString(id++);
}
/**
* A prefix helps to make sure that ID's are unique across mutliple instances.
*/
private static String prefix = StringUtils.randomString(5);
/**
* Keeps track of the current increment, which is appended to the prefix to
* forum a unique ID.
*/
private static long id = 0;
/**
* Maps thread ID to chat.
*/
private Map<String, Chat> threadChats = new ReferenceMap<String, Chat>(ReferenceMap.HARD,
ReferenceMap.WEAK);
/**
* Maps jids to chats
*/
private Map<String, Chat> jidChats = new ReferenceMap<String, Chat>(ReferenceMap.HARD,
ReferenceMap.WEAK);
private Set<ChatListener> chatListeners = new CopyOnWriteArraySet<ChatListener>();
private XMPPConnection connection;
ChatManager(XMPPConnection connection) {
this.connection = connection;
PacketFilter filter = new AndFilter(new PacketTypeFilter(Message.class),
new PacketFilter() {
public boolean accept(Packet packet) {
if (!(packet instanceof Message)) {
return false;
}
Message.Type messageType = ((Message) packet).getType();
return messageType != Message.Type.groupchat &&
messageType != Message.Type.headline;
}
});
// Add a listener for all message packets so that we can deliver errant
// messages to the best Chat instance available.
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
Chat chat;
if (message.getThread() == null) {
chat = getUserChat(StringUtils.parseBareAddress(message.getFrom()));
}
else {
chat = getThreadChat(message.getThread());
}
if(chat == null) {
chat = createChat(message);
}
deliverMessage(chat, message);
}
}, filter);
}
/**
* Creates a new chat and returns it.
*
* @param userJID the user this chat is with.
* @param listener the listener which will listen for new messages from this chat.
* @return the created chat.
*/
public Chat createChat(String userJID, PacketListener listener) {
String threadID = nextID();
Chat chat = createChat(userJID, threadID, true);
chat.addMessageListener(listener);
return chat;
}
private Chat createChat(String userJID, String threadID, boolean createdLocally) {
Chat chat = new Chat(this, userJID, threadID);
threadChats.put(threadID, chat);
jidChats.put(userJID, chat);
for(ChatListener listener : chatListeners) {
listener.chatCreated(chat, createdLocally);
}
return chat;
}
private Chat createChat(Message message) {
String threadID = message.getThread();
String userJID = message.getFrom();
return createChat(userJID, threadID, false);
}
private Chat getUserChat(String userJID) {
return jidChats.get(userJID);
}
private Chat getThreadChat(String thread) {
return threadChats.get(thread);
}
/**
* Register a new listener with the ChatManager to recieve events related to chats.
*
* @param listener the listener.
*/
public void addChatListener(ChatListener listener) {
chatListeners.add(listener);
}
/**
* Removes a listener, it will no longer be notified of new events related to chats.
*
* @param listener the listener that is being removed
*/
public void removeChatListener(ChatListener listener) {
chatListeners.remove(listener);
}
/**
* Returns an unmodifiable collection of all chat listeners currently registered with this
* manager.
*
* @return an unmodifiable collection of all chat listeners currently registered with this
* manager.
*/
public Collection<ChatListener> getChatListeners() {
return Collections.unmodifiableCollection(chatListeners);
}
private void deliverMessage(Chat chat, Message message) {
// Here we will run any interceptors
chat.deliver(message);
}
void sendMessage(Chat chat, Message message) {
// Here we will run any interceptors
connection.sendPacket(message);
}
PacketCollector createPacketCollector(Chat chat) {
return connection.createPacketCollector(new AndFilter(new ThreadFilter(chat.getThreadID()),
new FromContainsFilter(chat.getParticipant())));
}
}

View file

@ -22,8 +22,6 @@ package org.jivesoftware.smack;
import org.jivesoftware.smack.debugger.SmackDebugger; import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.filter.PacketFilter; import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Presence; import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.packet.XMPPError; import org.jivesoftware.smack.packet.XMPPError;
@ -34,15 +32,12 @@ import javax.net.SocketFactory;
import javax.net.ssl.SSLContext; import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocket;
import java.io.*; import java.io.*;
import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.Socket; import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.util.Collection; import java.util.Collection;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.CopyOnWriteArraySet;
/** /**
@ -147,14 +142,6 @@ public class XMPPConnection {
Writer writer; Writer writer;
Reader reader; Reader reader;
/**
* A map between JIDs and the most recently created Chat object with that JID.
* Reference to the Chat is stored via a WeakReference so that the map
* does not interfere with garbage collection. The map of chats must be stored
* with each connection.
*/
Map<String, WeakReference<Chat>> chats = new ConcurrentHashMap<String, WeakReference<Chat>>();
/** /**
* Collection of available stream compression methods offered by the server. * Collection of available stream compression methods offered by the server.
*/ */
@ -167,6 +154,7 @@ public class XMPPConnection {
* Holds the initial configuration used while creating the connection. * Holds the initial configuration used while creating the connection.
*/ */
private ConnectionConfiguration configuration; private ConnectionConfiguration configuration;
private ChatManager chatManager;
/** /**
* Creates a new connection to the specified XMPP server. A DNS SRV lookup will be * Creates a new connection to the specified XMPP server. A DNS SRV lookup will be
@ -564,18 +552,16 @@ public class XMPPConnection {
} }
/** /**
* Creates a new chat with the specified participant. The participant should * Returns a chat manager instance for this connection. The ChatManager manages all incoming and
* be a valid XMPP user such as <tt>jdoe@jivesoftware.com</tt> or * outgoing chats on the current connection.
* <tt>jdoe@jivesoftware.com/work</tt>.
* *
* @param participant the person to start the conversation with. * @return a chat manager instance for this connection.
* @return a new Chat object.
*/ */
public Chat createChat(String participant) { public synchronized ChatManager getChatManager() {
if (!isConnected()) { if(this.chatManager == null) {
throw new IllegalStateException("Not connected to server."); this.chatManager = new ChatManager(this);
} }
return new Chat(this, participant); return this.chatManager;
} }
/** /**
@ -934,34 +920,6 @@ public class XMPPConnection {
for (ConnectionCreationListener listener : connectionEstablishedListeners) { for (ConnectionCreationListener listener : connectionEstablishedListeners) {
listener.connectionCreated(this); listener.connectionCreated(this);
} }
// Add a listener for all message packets so that we can deliver errant
// messages to the best Chat instance available.
addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
// Ignore any messages with a thread ID, as they will likely
// already be associated with a Chat. This will miss messages
// with new thread ID values, but we can only assume that a
// listener is registered to deal with this case.
if (message.getThread() == null &&
message.getType() != Message.Type.GROUP_CHAT &&
message.getType() != Message.Type.HEADLINE) {
WeakReference<Chat> chatRef =
chats.get(StringUtils.parseBareAddress(message.getFrom()));
if (chatRef != null) {
// Do some extra clean-up if the reference was cleared.
Chat chat = chatRef.get();
if (chat == null) {
chats.remove(message.getFrom());
}
else {
chat.deliver(message);
}
}
}
}
}, new PacketTypeFilter(Message.class));
} }
else { else {
packetReader.notifyReconnection(); packetReader.notifyReconnection();

View file

@ -22,6 +22,9 @@ package org.jivesoftware.smack.filter;
import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.packet.Packet;
import java.util.List;
import java.util.ArrayList;
/** /**
* Implements the logical AND operation over two or more packet filters. * Implements the logical AND operation over two or more packet filters.
* In other words, packets pass this filter if they pass <b>all</b> of the filters. * In other words, packets pass this filter if they pass <b>all</b> of the filters.
@ -30,39 +33,34 @@ import org.jivesoftware.smack.packet.Packet;
*/ */
public class AndFilter implements PacketFilter { public class AndFilter implements PacketFilter {
/**
* The current number of elements in the filter.
*/
private int size;
/** /**
* The list of filters. * The list of filters.
*/ */
private PacketFilter [] filters; private List<PacketFilter> filters = new ArrayList<PacketFilter>();
/** /**
* Creates an empty AND filter. Filters should be added using the * Creates an empty AND filter. Filters should be added using the
* {@link #addFilter(PacketFilter)} method. * {@link #addFilter(PacketFilter)} method.
*/ */
public AndFilter() { public AndFilter() {
size = 0;
filters = new PacketFilter[3];
} }
/** /**
* Creates an AND filter using the two specified filters. * Creates an AND filter using the specified filters.
* *
* @param filter1 the first packet filter. * @param filters the filters to add.
* @param filter2 the second packet filter.
*/ */
public AndFilter(PacketFilter filter1, PacketFilter filter2) { public AndFilter(PacketFilter... filters) {
if (filter1 == null || filter2 == null) { if (filters == null) {
throw new IllegalArgumentException("Parameters cannot be null."); throw new IllegalArgumentException("Parameter cannot be null.");
}
for(PacketFilter filter : filters) {
if(filter == null) {
throw new IllegalArgumentException("Parameter cannot be null.");
}
this.filters.add(filter);
} }
size = 2;
filters = new PacketFilter[2];
filters[0] = filter1;
filters[1] = filter2;
} }
/** /**
@ -75,22 +73,12 @@ public class AndFilter implements PacketFilter {
if (filter == null) { if (filter == null) {
throw new IllegalArgumentException("Parameter cannot be null."); throw new IllegalArgumentException("Parameter cannot be null.");
} }
// If there is no more room left in the filters array, expand it. filters.add(filter);
if (size == filters.length) {
PacketFilter [] newFilters = new PacketFilter[filters.length+2];
for (int i=0; i<filters.length; i++) {
newFilters[i] = filters[i];
}
filters = newFilters;
}
// Add the new filter to the array.
filters[size] = filter;
size++;
} }
public boolean accept(Packet packet) { public boolean accept(Packet packet) {
for (int i=0; i<size; i++) { for (PacketFilter filter : filters) {
if (!filters[i].accept(packet)) { if (!filter.accept(packet)) {
return false; return false;
} }
} }

View file

@ -45,11 +45,6 @@ public class ThreadFilter implements PacketFilter {
} }
public boolean accept(Packet packet) { public boolean accept(Packet packet) {
if (packet instanceof Message) { return packet instanceof Message && thread.equals(((Message) packet).getThread());
return thread.equals(((Message)packet).getThread());
}
else {
return false;
}
} }
} }

View file

@ -48,7 +48,7 @@ import org.jivesoftware.smack.util.StringUtils;
*/ */
public class Message extends Packet { 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 body = null;
private String thread = null; private String thread = null;
@ -138,7 +138,8 @@ public class Message extends Packet {
/** /**
* Sets the body of the message. The body is the main message contents. * Sets the body of the message. The body is the main message contents.
* @param body *
* @param body the body of the message.
*/ */
public void setBody(String body) { public void setBody(String body) {
this.body = body; this.body = body;
@ -176,7 +177,7 @@ public class Message extends Packet {
if (getFrom() != null) { if (getFrom() != null) {
buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\""); buf.append(" from=\"").append(StringUtils.escapeForXML(getFrom())).append("\"");
} }
if (type != Type.NORMAL) { if (type != Type.normal) {
buf.append(" type=\"").append(type).append("\""); buf.append(" type=\"").append(type).append("\"");
} }
buf.append(">"); buf.append(">");
@ -190,7 +191,7 @@ public class Message extends Packet {
buf.append("<thread>").append(thread).append("</thread>"); buf.append("<thread>").append(thread).append("</thread>");
} }
// Append the error subpacket if the message type is an error. // Append the error subpacket if the message type is an error.
if (type == Type.ERROR) { if (type == Type.error) {
XMPPError error = getError(); XMPPError error = getError();
if (error != null) { if (error != null) {
buf.append(error.toXML()); buf.append(error.toXML());
@ -205,69 +206,41 @@ public class Message extends Packet {
/** /**
* Represents the type of a message. * Represents the type of a message.
*/ */
public static class Type { public enum Type {
/** /**
* (Default) a normal text message used in email like interface. * (Default) a normal text message used in email like interface.
*/ */
public static final Type NORMAL = new Type("normal"); normal,
/** /**
* Typically short text message used in line-by-line chat interfaces. * Typically short text message used in line-by-line chat interfaces.
*/ */
public static final Type CHAT = new Type("chat"); chat,
/** /**
* Chat message sent to a groupchat server for group chats. * Chat message sent to a groupchat server for group chats.
*/ */
public static final Type GROUP_CHAT = new Type("groupchat"); groupchat,
/** /**
* Text message to be displayed in scrolling marquee displays. * Text message to be displayed in scrolling marquee displays.
*/ */
public static final Type HEADLINE = new Type("headline"); headline,
/** /**
* indicates a messaging error. * indicates a messaging error.
*/ */
public static final Type ERROR = new Type("error"); error;
/** public static Type fromString(String name) {
* Converts a String value into its Type representation. try {
* return Type.valueOf(name);
* @param type the String value.
* @return the Type corresponding to the String.
*/
public static Type fromString(String type) {
if (type == null) {
return NORMAL;
} }
type = type.toLowerCase(); catch (Exception e) {
if (CHAT.toString().equals(type)) { return normal;
return CHAT;
}
else if (GROUP_CHAT.toString().equals(type)) {
return GROUP_CHAT;
}
else if (HEADLINE.toString().equals(type)) {
return HEADLINE;
}
else if (ERROR.toString().equals(type)) {
return ERROR;
}
else {
return NORMAL;
} }
} }
private String value;
private Type(String value) {
this.value = value;
}
public String toString() {
return value;
}
} }
} }

View file

@ -0,0 +1,89 @@
// GenericsNote: Converted.
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.NoSuchElementException;
/**
* Provides an implementation of an empty iterator.
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:24 $
* @since Commons Collections 3.1
*/
abstract class AbstractEmptyIterator <E> {
/**
* Constructor.
*/
protected AbstractEmptyIterator() {
super();
}
public boolean hasNext() {
return false;
}
public E next() {
throw new NoSuchElementException("Iterator contains no elements");
}
public boolean hasPrevious() {
return false;
}
public E previous() {
throw new NoSuchElementException("Iterator contains no elements");
}
public int nextIndex() {
return 0;
}
public int previousIndex() {
return -1;
}
public void add(E obj) {
throw new UnsupportedOperationException("add() not supported for empty Iterator");
}
public void set(E obj) {
throw new IllegalStateException("Iterator contains no elements");
}
public void remove() {
throw new IllegalStateException("Iterator contains no elements");
}
public E getKey() {
throw new IllegalStateException("Iterator contains no elements");
}
public E getValue() {
throw new IllegalStateException("Iterator contains no elements");
}
public E setValue(E value) {
throw new IllegalStateException("Iterator contains no elements");
}
public void reset() {
// do nothing
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,80 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
/**
* Abstract pair class to assist with creating KeyValue and MapEntry implementations.
*
* @author James Strachan
* @author Michael A. Smith
* @author Neil O'Toole
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:32 $
* @since Commons Collections 3.0
*/
public abstract class AbstractKeyValue <K,V> implements KeyValue<K, V> {
/**
* The key
*/
protected K key;
/**
* The value
*/
protected V value;
/**
* Constructs a new pair with the specified key and given value.
*
* @param key the key for the entry, may be null
* @param value the value for the entry, may be null
*/
protected AbstractKeyValue(K key, V value) {
super();
this.key = key;
this.value = value;
}
/**
* Gets the key from the pair.
*
* @return the key
*/
public K getKey() {
return key;
}
/**
* Gets the value from the pair.
*
* @return the value
*/
public V getValue() {
return value;
}
/**
* Gets a debugging String view of the pair.
*
* @return a String view of the entry
*/
public String toString() {
return new StringBuffer().append(getKey()).append('=').append(getValue()).toString();
}
}

View file

@ -0,0 +1,89 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Map;
/**
* Abstract Pair class to assist with creating correct Map Entry implementations.
*
* @author James Strachan
* @author Michael A. Smith
* @author Neil O'Toole
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:32 $
* @since Commons Collections 3.0
*/
public abstract class AbstractMapEntry <K,V> extends AbstractKeyValue<K, V> implements Map.Entry<K, V> {
/**
* Constructs a new entry with the given key and given value.
*
* @param key the key for the entry, may be null
* @param value the value for the entry, may be null
*/
protected AbstractMapEntry(K key, V value) {
super(key, value);
}
// Map.Entry interface
//-------------------------------------------------------------------------
/**
* Sets the value stored in this Map Entry.
* <p/>
* This Map Entry is not connected to a Map, so only the local data is changed.
*
* @param value the new value
* @return the previous value
*/
public V setValue(V value) {
V answer = this.value;
this.value = value;
return answer;
}
/**
* Compares this Map Entry with another Map Entry.
* <p/>
* Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
*
* @param obj the object to compare to
* @return true if equal key and value
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Map.Entry == false) {
return false;
}
Map.Entry other = (Map.Entry) obj;
return (getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) && (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue()));
}
/**
* Gets a hashCode compatible with the equals method.
* <p/>
* Implemented per API documentation of {@link java.util.Map.Entry#hashCode()}
*
* @return a suitable hash code
*/
public int hashCode() {
return (getKey() == null ? 0 : getKey().hashCode()) ^ (getValue() == null ? 0 : getValue().hashCode());
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,65 @@
// GenericsNote: Converted.
/*
* Copyright 2001-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Map;
/**
* A restricted implementation of {@link java.util.Map.Entry} that prevents
* the MapEntry contract from being broken.
*
* @author James Strachan
* @author Michael A. Smith
* @author Neil O'Toole
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:32 $
* @since Commons Collections 3.0
*/
public final class DefaultMapEntry <K,V> extends AbstractMapEntry<K, V> {
/**
* Constructs a new entry with the specified key and given value.
*
* @param key the key for the entry, may be null
* @param value the value for the entry, may be null
*/
public DefaultMapEntry(final K key, final V value) {
super(key, value);
}
/**
* Constructs a new entry from the specified KeyValue.
*
* @param pair the pair to copy, must not be null
* @throws NullPointerException if the entry is null
*/
public DefaultMapEntry(final KeyValue<K, V> pair) {
super(pair.getKey(), pair.getValue());
}
/**
* Constructs a new entry from the specified MapEntry.
*
* @param entry the entry to copy, must not be null
* @throws NullPointerException if the entry is null
*/
public DefaultMapEntry(final Map.Entry<K, V> entry) {
super(entry.getKey(), entry.getValue());
}
}

View file

@ -0,0 +1,58 @@
// GenericsNote: Converted.
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Iterator;
/**
* Provides an implementation of an empty iterator.
* <p/>
* This class provides an implementation of an empty iterator.
* This class provides for binary compatability between Commons Collections
* 2.1.1 and 3.1 due to issues with <code>IteratorUtils</code>.
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:24 $
* @since Commons Collections 2.1.1 and 3.1
*/
public class EmptyIterator <E> extends AbstractEmptyIterator<E> implements ResettableIterator<E> {
/**
* Singleton instance of the iterator.
*
* @since Commons Collections 3.1
*/
public static final ResettableIterator RESETTABLE_INSTANCE = new EmptyIterator();
/**
* Singleton instance of the iterator.
*
* @since Commons Collections 2.1.1 and 3.1
*/
public static final Iterator INSTANCE = RESETTABLE_INSTANCE;
public static <T> Iterator<T> getInstance() {
return INSTANCE;
}
/**
* Constructor.
*/
protected EmptyIterator() {
super();
}
}

View file

@ -0,0 +1,42 @@
// GenericsNote: Converted.
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.smack.util.collections;
/**
* Provides an implementation of an empty map iterator.
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:24 $
* @since Commons Collections 3.1
*/
public class EmptyMapIterator extends AbstractEmptyIterator implements MapIterator, ResettableIterator {
/**
* Singleton instance of the iterator.
*
* @since Commons Collections 3.1
*/
public static final MapIterator INSTANCE = new EmptyMapIterator();
/**
* Constructor.
*/
protected EmptyMapIterator() {
super();
}
}

View file

@ -0,0 +1,61 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Map;
/**
* Defines a map that can be iterated directly without needing to create an entry set.
* <p/>
* A map iterator is an efficient way of iterating over maps.
* There is no need to access the entry set or cast to Map Entry objects.
* <pre>
* IterableMap map = new HashedMap();
* MapIterator it = map.mapIterator();
* while (it.hasNext()) {
* Object key = it.next();
* Object value = it.getValue();
* it.setValue("newValue");
* }
* </pre>
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:19 $
* @since Commons Collections 3.0
*/
public interface IterableMap <K,V> extends Map<K, V> {
/**
* Obtains a <code>MapIterator</code> over the map.
* <p/>
* A map iterator is an efficient way of iterating over maps.
* There is no need to access the entry set or cast to Map Entry objects.
* <pre>
* IterableMap map = new HashedMap();
* MapIterator it = map.mapIterator();
* while (it.hasNext()) {
* Object key = it.next();
* Object value = it.getValue();
* it.setValue("newValue");
* }
* </pre>
*
* @return a map iterator
*/
MapIterator<K, V> mapIterator();
}

View file

@ -0,0 +1,46 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
/**
* Defines a simple key value pair.
* <p/>
* A Map Entry has considerable additional semantics over and above a simple
* key-value pair. This interface defines the minimum key value, with just the
* two get methods.
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:19 $
* @since Commons Collections 3.0
*/
public interface KeyValue <K,V> {
/**
* Gets the key from the pair.
*
* @return the key
*/
K getKey();
/**
* Gets the value from the pair.
*
* @return the value
*/
V getValue();
}

View file

@ -0,0 +1,109 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Iterator;
/**
* Defines an iterator that operates over a <code>Map</code>.
* <p/>
* This iterator is a special version designed for maps. It can be more
* efficient to use this rather than an entry set iterator where the option
* is available, and it is certainly more convenient.
* <p/>
* A map that provides this interface may not hold the data internally using
* Map Entry objects, thus this interface can avoid lots of object creation.
* <p/>
* In use, this iterator iterates through the keys in the map. After each call
* to <code>next()</code>, the <code>getValue()</code> method provides direct
* access to the value. The value can also be set using <code>setValue()</code>.
* <pre>
* MapIterator it = map.mapIterator();
* while (it.hasNext()) {
* Object key = it.next();
* Object value = it.getValue();
* it.setValue(newValue);
* }
* </pre>
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:19 $
* @since Commons Collections 3.0
*/
public interface MapIterator <K,V> extends Iterator<K> {
/**
* Checks to see if there are more entries still to be iterated.
*
* @return <code>true</code> if the iterator has more elements
*/
boolean hasNext();
/**
* Gets the next <em>key</em> from the <code>Map</code>.
*
* @return the next key in the iteration
* @throws java.util.NoSuchElementException
* if the iteration is finished
*/
K next();
//-----------------------------------------------------------------------
/**
* Gets the current key, which is the key returned by the last call
* to <code>next()</code>.
*
* @return the current key
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
K getKey();
/**
* Gets the current value, which is the value associated with the last key
* returned by <code>next()</code>.
*
* @return the current value
* @throws IllegalStateException if <code>next()</code> has not yet been called
*/
V getValue();
//-----------------------------------------------------------------------
/**
* Removes the last returned key from the underlying <code>Map</code> (optional operation).
* <p/>
* This method can be called once per call to <code>next()</code>.
*
* @throws UnsupportedOperationException if remove is not supported by the map
* @throws IllegalStateException if <code>next()</code> has not yet been called
* @throws IllegalStateException if <code>remove()</code> has already been called
* since the last call to <code>next()</code>
*/
void remove();
/**
* Sets the value associated with the current key (optional operation).
*
* @param value the new value
* @return the previous value
* @throws UnsupportedOperationException if setValue is not supported by the map
* @throws IllegalStateException if <code>next()</code> has not yet been called
* @throws IllegalStateException if <code>remove()</code> has been called since the
* last call to <code>next()</code>
*/
V setValue(V value);
}

View file

@ -0,0 +1,161 @@
// GenericsNote: Converted.
/*
* Copyright 2002-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* A <code>Map</code> implementation that allows mappings to be
* removed by the garbage collector.
* <p/>
* When you construct a <code>ReferenceMap</code>, you can specify what kind
* of references are used to store the map's keys and values.
* If non-hard references are used, then the garbage collector can remove
* mappings if a key or value becomes unreachable, or if the JVM's memory is
* running low. For information on how the different reference types behave,
* see {@link java.lang.ref.Reference}.
* <p/>
* Different types of references can be specified for keys and values.
* The keys can be configured to be weak but the values hard,
* in which case this class will behave like a
* <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/WeakHashMap.html">
* <code>WeakHashMap</code></a>. However, you can also specify hard keys and
* weak values, or any other combination. The default constructor uses
* hard keys and soft values, providing a memory-sensitive cache.
* <p/>
* This map is similar to ReferenceIdentityMap.
* It differs in that keys and values in this class are compared using <code>equals()</code>.
* <p/>
* This {@link java.util.Map} implementation does <i>not</i> allow null elements.
* Attempting to add a null key or value to the map will raise a <code>NullPointerException</code>.
* <p/>
* This implementation is not synchronized.
* You can use {@link java.util.Collections#synchronizedMap} to
* provide synchronized access to a <code>ReferenceMap</code>.
* Remember that synchronization will not stop the garbage collecter removing entries.
* <p/>
* All the available iterators can be reset back to the start by casting to
* <code>ResettableIterator</code> and calling <code>reset()</code>.
* <p/>
* NOTE: As from Commons Collections 3.1 this map extends <code>AbstractReferenceMap</code>
* (previously it extended AbstractMap). As a result, the implementation is now
* extensible and provides a <code>MapIterator</code>.
*
* @author Paul Jack
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:32 $
* @see java.lang.ref.Reference
* @since Commons Collections 3.0 (previously in main package v2.1)
*/
public class ReferenceMap <K,V> extends AbstractReferenceMap<K, V> implements Serializable {
/**
* Serialization version
*/
private static final long serialVersionUID = 1555089888138299607L;
/**
* Constructs a new <code>ReferenceMap</code> that will
* use hard references to keys and soft references to values.
*/
public ReferenceMap() {
super(HARD, SOFT, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
}
/**
* Constructs a new <code>ReferenceMap</code> that will
* use the specified types of references.
*
* @param keyType the type of reference to use for keys;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
*/
public ReferenceMap(int keyType, int valueType) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, false);
}
/**
* Constructs a new <code>ReferenceMap</code> that will
* use the specified types of references.
*
* @param keyType the type of reference to use for keys;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
public ReferenceMap(int keyType, int valueType, boolean purgeValues) {
super(keyType, valueType, DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, purgeValues);
}
/**
* Constructs a new <code>ReferenceMap</code> with the
* specified reference types, load factor and initial
* capacity.
*
* @param keyType the type of reference to use for keys;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param capacity the initial capacity for the map
* @param loadFactor the load factor for the map
*/
public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor) {
super(keyType, valueType, capacity, loadFactor, false);
}
/**
* Constructs a new <code>ReferenceMap</code> with the
* specified reference types, load factor and initial
* capacity.
*
* @param keyType the type of reference to use for keys;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param valueType the type of reference to use for values;
* must be {@link #HARD}, {@link #SOFT}, {@link #WEAK}
* @param capacity the initial capacity for the map
* @param loadFactor the load factor for the map
* @param purgeValues should the value be automatically purged when the
* key is garbage collected
*/
public ReferenceMap(int keyType, int valueType, int capacity, float loadFactor, boolean purgeValues) {
super(keyType, valueType, capacity, loadFactor, purgeValues);
}
//-----------------------------------------------------------------------
/**
* Write the map out using a custom routine.
*/
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
doWriteObject(out);
}
/**
* Read the map in using a custom routine.
*/
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
doReadObject(in);
}
}

View file

@ -0,0 +1,38 @@
// GenericsNote: Converted.
/*
* Copyright 2003-2004 The Apache Software Foundation
*
* 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.smack.util.collections;
import java.util.Iterator;
/**
* Defines an iterator that can be reset back to an initial state.
* <p/>
* This interface allows an iterator to be repeatedly reused.
*
* @author Matt Hall, John Watkinson, Stephen Colebourne
* @version $Revision: 1.1 $ $Date: 2005/10/11 17:05:19 $
* @since Commons Collections 3.0
*/
public interface ResettableIterator <E> extends Iterator<E> {
/**
* Resets the iterator back to the position at which the iterator
* was created.
*/
public void reset();
}

View file

@ -56,6 +56,7 @@ import java.util.Date;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.filter.ThreadFilter;
/** /**
@ -65,20 +66,17 @@ import org.jivesoftware.smack.test.SmackTestCase;
*/ */
public class ChatTest extends SmackTestCase { public class ChatTest extends SmackTestCase {
/**
* Constructor for ChatTest.
* @param arg0
*/
public ChatTest(String arg0) { public ChatTest(String arg0) {
super(arg0); super(arg0);
} }
public void testProperties() { public void testProperties() {
try { try {
Chat newChat = getConnection(0).createChat(getFullJID(1)); Chat newChat = getConnection(0).getChatManager().createChat(getFullJID(1), null);
Chat newChat2 = new Chat(getConnection(1), getFullJID(0), newChat.getThreadID()); PacketCollector collector = getConnection(1)
.createPacketCollector(new ThreadFilter(newChat.getThreadID()));
Message msg = newChat.createMessage(); Message msg = new Message();
msg.setSubject("Subject of the chat"); msg.setSubject("Subject of the chat");
msg.setBody("Body of the chat"); msg.setBody("Body of the chat");
@ -90,7 +88,8 @@ public class ChatTest extends SmackTestCase {
msg.setProperty("birthdate", new Date()); msg.setProperty("birthdate", new Date());
newChat.sendMessage(msg); newChat.sendMessage(msg);
Message msg2 = newChat2.nextMessage(2000); Message msg2 = (Message) collector.nextResult(2000);
assertNotNull("No message was received", msg2); assertNotNull("No message was received", msg2);
assertEquals("Subjects are different", msg.getSubject(), msg2.getSubject()); assertEquals("Subjects are different", msg.getSubject(), msg2.getSubject());
assertEquals("Bodies are different", msg.getBody(), msg2.getBody()); assertEquals("Bodies are different", msg.getBody(), msg2.getBody());

View file

@ -21,6 +21,7 @@
package org.jivesoftware.smack; package org.jivesoftware.smack;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smack.filter.ThreadFilter;
/** /**
* Simple test to measure server performance. * Simple test to measure server performance.
@ -35,14 +36,17 @@ public class FloodTest extends SmackTestCase {
public void testMessageFlood() { public void testMessageFlood() {
try { try {
Chat chat11 = getConnection(0).createChat(getBareJID(1)); Chat chat11 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
Chat chat12 = new Chat(getConnection(1), getBareJID(0), chat11.getThreadID()); PacketCollector chat12 = getConnection(1).createPacketCollector(
new ThreadFilter(chat11.getThreadID()));
Chat chat21 = getConnection(0).createChat(getBareJID(2)); Chat chat21 = getConnection(0).getChatManager().createChat(getBareJID(2), null);
Chat chat22 = new Chat(getConnection(2), getBareJID(0), chat21.getThreadID()); PacketCollector chat22 = getConnection(2).createPacketCollector(
new ThreadFilter(chat21.getThreadID()));
Chat chat31 = getConnection(0).createChat(getBareJID(3)); Chat chat31 = getConnection(0).getChatManager().createChat(getBareJID(3), null);
Chat chat32 = new Chat(getConnection(3), getBareJID(0), chat31.getThreadID()); PacketCollector chat32 = getConnection(3).createPacketCollector(
new ThreadFilter(chat31.getThreadID()));
for (int i=0; i<500; i++) { for (int i=0; i<500; i++) {
chat11.sendMessage("Hello_1" + i); chat11.sendMessage("Hello_1" + i);
@ -50,9 +54,9 @@ public class FloodTest extends SmackTestCase {
chat31.sendMessage("Hello_3" + i); chat31.sendMessage("Hello_3" + i);
} }
for (int i=0; i<500; i++) { for (int i=0; i<500; i++) {
assertNotNull("Some message was lost (" + i + ")", chat12.nextMessage(1000)); assertNotNull("Some message was lost (" + i + ")", chat12.nextResult(1000));
assertNotNull("Some message was lost (" + i + ")", chat22.nextMessage(1000)); assertNotNull("Some message was lost (" + i + ")", chat22.nextResult(1000));
assertNotNull("Some message was lost (" + i + ")", chat32.nextMessage(1000)); assertNotNull("Some message was lost (" + i + ")", chat32.nextResult(1000));
} }
} }
catch (Exception e) { catch (Exception e) {

View file

@ -48,14 +48,16 @@ public class MessageTest extends SmackTestCase {
Thread.sleep(500); Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment // User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).createChat(getBareJID(1)); Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test 1"); chat.sendMessage("Test 1");
chat.sendMessage("Test 2"); chat.sendMessage("Test 2");
Thread.sleep(500); Thread.sleep(500);
// User2 becomes available again // User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.CHAT));
getConnection(1).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available // Check that offline messages are retrieved by user2 which is now available
@ -66,7 +68,8 @@ public class MessageTest extends SmackTestCase {
message = (Message) collector.nextResult(1000); message = (Message) collector.nextResult(1000);
assertNull(message); assertNull(message);
} catch (Exception e) { }
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
} }
@ -84,14 +87,16 @@ public class MessageTest extends SmackTestCase {
Thread.sleep(500); Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment // User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).createChat(getBareJID(1)); Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
chat.sendMessage("Test \f 1"); chat.sendMessage("Test \f 1");
chat.sendMessage("Test \r 1"); chat.sendMessage("Test \r 1");
Thread.sleep(500); Thread.sleep(500);
// User2 becomes available again // User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.CHAT));
getConnection(1).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that offline messages are retrieved by user2 which is now available // Check that offline messages are retrieved by user2 which is now available
@ -102,7 +107,8 @@ public class MessageTest extends SmackTestCase {
message = (Message) collector.nextResult(1000); message = (Message) collector.nextResult(1000);
assertNull(message); assertNull(message);
} catch (Exception e) { }
catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
} }
@ -114,10 +120,11 @@ public class MessageTest extends SmackTestCase {
*/ */
public void testHugeMessage() { public void testHugeMessage() {
// User2 becomes available again // User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.CHAT)); PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.chat));
// Create message with a body of 4K characters // Create message with a body of 4K characters
Message msg = new Message(getFullJID(1), Message.Type.CHAT); Message msg = new Message(getFullJID(1), Message.Type.chat);
StringBuilder sb = new StringBuilder(5000); StringBuilder sb = new StringBuilder(5000);
for (int i = 0; i <= 4000; i++) { for (int i = 0; i <= 4000; i++) {
sb.append("X"); sb.append("X");

View file

@ -63,7 +63,7 @@ public class AndFilterTest extends TestCase {
public void testNullArgs() { public void testNullArgs() {
PacketFilter filter = null; PacketFilter filter = null;
try { try {
AndFilter and = new AndFilter(filter, filter); new AndFilter(filter, filter);
fail("Should have thrown IllegalArgumentException"); fail("Should have thrown IllegalArgumentException");
} }
catch (IllegalArgumentException e) { catch (IllegalArgumentException e) {

View file

@ -54,6 +54,8 @@ package org.jivesoftware.smackx;
import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
@ -64,10 +66,6 @@ import org.jivesoftware.smack.test.SmackTestCase;
*/ */
public class FormTest extends SmackTestCase { public class FormTest extends SmackTestCase {
/**
* Constructor for FormTest.
* @param arg0
*/
public FormTest(String arg0) { public FormTest(String arg0) {
super(arg0); super(arg0);
} }
@ -113,10 +111,13 @@ public class FormTest extends SmackTestCase {
formToSend.addField(field); formToSend.addField(field);
// Create the chats between the two participants // Create the chats between the two participants
Chat chat = getConnection(0).createChat(getBareJID(1)); Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat.getThreadID()); PacketCollector collector = getConnection(0).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
PacketCollector collector2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
Message msg = chat.createMessage(); Message msg = new Message();
msg.setBody("To enter a case please fill out this form and send it back to me"); msg.setBody("To enter a case please fill out this form and send it back to me");
msg.addExtension(formToSend.getDataFormToSend()); msg.addExtension(formToSend.getDataFormToSend());
@ -125,7 +126,7 @@ public class FormTest extends SmackTestCase {
chat.sendMessage(msg); chat.sendMessage(msg);
// Get the message with the form to fill out // Get the message with the form to fill out
Message msg2 = chat2.nextMessage(2000); Message msg2 = (Message)collector2.nextResult(2000);
// Retrieve the form to fill out // Retrieve the form to fill out
Form formToRespond = Form.getFormFrom(msg2); Form formToRespond = Form.getFormFrom(msg2);
assertNotNull(formToRespond); assertNotNull(formToRespond);
@ -148,15 +149,18 @@ public class FormTest extends SmackTestCase {
completedForm.setAnswer("time", true); completedForm.setAnswer("time", true);
completedForm.setAnswer("age", 20); completedForm.setAnswer("age", 20);
// Create a new message to send with the completed form // Create a new message to send with the completed form
msg2 = chat2.createMessage(); msg2 = new Message();
msg2.setTo(msg.getFrom());
msg2.setThread(msg.getThread());
msg2.setType(Message.Type.chat);
msg2.setBody("To enter a case please fill out this form and send it back to me"); msg2.setBody("To enter a case please fill out this form and send it back to me");
// Add the completed form to the message // Add the completed form to the message
msg2.addExtension(completedForm.getDataFormToSend()); msg2.addExtension(completedForm.getDataFormToSend());
// Send the message with the completed form // Send the message with the completed form
chat2.sendMessage(msg2); getConnection(1).sendPacket(msg2);
// Get the message with the completed form // Get the message with the completed form
Message msg3 = chat.nextMessage(2000); Message msg3 = (Message) collector.nextResult(2000);
// Retrieve the completed form // Retrieve the completed form
completedForm = Form.getFormFrom(msg3); completedForm = Form.getFormFrom(msg3);
assertNotNull(completedForm); assertNotNull(completedForm);
@ -173,6 +177,10 @@ public class FormTest extends SmackTestCase {
catch (XMPPException ex) { catch (XMPPException ex) {
fail(ex.getMessage()); fail(ex.getMessage());
} }
finally {
collector.cancel();
collector2.cancel();
}
} }
protected int getMaxConnections() { protected int getMaxConnections() {

View file

@ -66,10 +66,6 @@ import org.jivesoftware.smack.test.SmackTestCase;
*/ */
public class MessageEventManagerTest extends SmackTestCase { public class MessageEventManagerTest extends SmackTestCase {
/**
* Constructor for MessageEventManagerTest.
* @param name
*/
public MessageEventManagerTest(String name) { public MessageEventManagerTest(String name) {
super(name); super(name);
} }
@ -83,10 +79,10 @@ public class MessageEventManagerTest extends SmackTestCase {
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here..."); msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed, // Add to the message all the notifications requests (offline, delivered, displayed,
@ -112,7 +108,7 @@ public class MessageEventManagerTest extends SmackTestCase {
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
MessageEventManager messageEventManager = new MessageEventManager(getConnection(0)); MessageEventManager messageEventManager = new MessageEventManager(getConnection(0));
messageEventManager messageEventManager
@ -139,7 +135,7 @@ public class MessageEventManagerTest extends SmackTestCase {
}); });
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here..."); msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed, // Add to the message all the notifications requests (offline, delivered, displayed,
@ -166,8 +162,8 @@ public class MessageEventManagerTest extends SmackTestCase {
* 5. User_2 will simulate that he/she has cancelled the reply * 5. User_2 will simulate that he/she has cancelled the reply
*/ */
public void testRequestsAndNotifications() { public void testRequestsAndNotifications() {
final ArrayList results = new ArrayList(); final ArrayList<String> results = new ArrayList<String>();
ArrayList resultsExpected = new ArrayList(); ArrayList<String> resultsExpected = new ArrayList<String>();
resultsExpected.add("deliveredNotificationRequested"); resultsExpected.add("deliveredNotificationRequested");
resultsExpected.add("composingNotificationRequested"); resultsExpected.add("composingNotificationRequested");
resultsExpected.add("displayedNotificationRequested"); resultsExpected.add("displayedNotificationRequested");
@ -178,7 +174,7 @@ public class MessageEventManagerTest extends SmackTestCase {
resultsExpected.add("cancelledNotification"); resultsExpected.add("cancelledNotification");
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0)); MessageEventManager messageEventManager1 = new MessageEventManager(getConnection(0));
messageEventManager1 messageEventManager1
@ -241,7 +237,7 @@ public class MessageEventManagerTest extends SmackTestCase {
}); });
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here..."); msg.setBody("An interesting body comes here...");
// Add to the message all the notifications requests (offline, delivered, displayed, // Add to the message all the notifications requests (offline, delivered, displayed,

View file

@ -49,11 +49,11 @@ public class MultipleRecipientManagerTest extends SmackTestCase {
public void testSending() throws XMPPException { public void testSending() throws XMPPException {
PacketCollector collector1 = PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 = PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 = PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
Message message = new Message(); Message message = new Message();
message.setBody("Hola"); message.setBody("Hola");
@ -114,13 +114,13 @@ public class MultipleRecipientManagerTest extends SmackTestCase {
*/ */
public void testReplying() throws XMPPException { public void testReplying() throws XMPPException {
PacketCollector collector0 = PacketCollector collector0 =
getConnection(0).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(0).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector1 = PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 = PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 = PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
// Send the intial message with multiple recipients // Send the intial message with multiple recipients
Message message = new Message(); Message message = new Message();
@ -196,11 +196,11 @@ public class MultipleRecipientManagerTest extends SmackTestCase {
*/ */
public void testNoReply() throws XMPPException { public void testNoReply() throws XMPPException {
PacketCollector collector1 = PacketCollector collector1 =
getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(1).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector2 = PacketCollector collector2 =
getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(2).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
PacketCollector collector3 = PacketCollector collector3 =
getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.NORMAL)); getConnection(3).createPacketCollector(new MessageTypeFilter(Message.Type.normal));
// Send the intial message with multiple recipients // Send the intial message with multiple recipients
Message message = new Message(); Message message = new Message();

View file

@ -64,7 +64,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
Thread.sleep(500); Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment // User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).createChat(getBareJID(1)); Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1"); chat.sendMessage("Test 1");
chat.sendMessage("Test 2"); chat.sendMessage("Test 2");
@ -76,7 +76,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
// Check the message headers // Check the message headers
Iterator headers = offlineManager.getHeaders(); Iterator headers = offlineManager.getHeaders();
assertTrue("No message header was found", headers.hasNext()); assertTrue("No message header was found", headers.hasNext());
List stamps = new ArrayList(); List<String> stamps = new ArrayList<String>();
while (headers.hasNext()) { while (headers.hasNext()) {
OfflineMessageHeader header = (OfflineMessageHeader) headers.next(); OfflineMessageHeader header = (OfflineMessageHeader) headers.next();
assertEquals("Incorrect sender", getFullJID(0), header.getJid()); assertEquals("Incorrect sender", getFullJID(0), header.getJid());
@ -87,7 +87,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
// Get the offline messages // Get the offline messages
Iterator messages = offlineManager.getMessages(stamps); Iterator messages = offlineManager.getMessages(stamps);
assertTrue("No message was found", messages.hasNext()); assertTrue("No message was found", messages.hasNext());
stamps = new ArrayList(); stamps = new ArrayList<String>();
while (messages.hasNext()) { while (messages.hasNext()) {
Message message = (Message) messages.next(); Message message = (Message) messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline",
@ -102,7 +102,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
// User2 becomes available again // User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector( PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.CHAT)); new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user // Check that no offline messages was sent to the user
@ -133,7 +133,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
Thread.sleep(500); Thread.sleep(500);
// User1 sends some messages to User2 which is not available at the moment // User1 sends some messages to User2 which is not available at the moment
Chat chat = getConnection(0).createChat(getBareJID(1)); Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
chat.sendMessage("Test 1"); chat.sendMessage("Test 1");
chat.sendMessage("Test 2"); chat.sendMessage("Test 2");
@ -145,7 +145,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
// Get all offline messages // Get all offline messages
Iterator messages = offlineManager.getMessages(); Iterator messages = offlineManager.getMessages();
assertTrue("No message was found", messages.hasNext()); assertTrue("No message was found", messages.hasNext());
List stamps = new ArrayList(); List<String> stamps = new ArrayList<String>();
while (messages.hasNext()) { while (messages.hasNext()) {
Message message = (Message) messages.next(); Message message = (Message) messages.next();
OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline", OfflineMessageInfo info = (OfflineMessageInfo) message.getExtension("offline",
@ -160,7 +160,7 @@ public class OfflineMessageManagerTest extends SmackTestCase {
// User2 becomes available again // User2 becomes available again
PacketCollector collector = getConnection(1).createPacketCollector( PacketCollector collector = getConnection(1).createPacketCollector(
new MessageTypeFilter(Message.Type.CHAT)); new MessageTypeFilter(Message.Type.chat));
getConnection(1).sendPacket(new Presence(Presence.Type.available)); getConnection(1).sendPacket(new Presence(Presence.Type.available));
// Check that no offline messages was sent to the user // Check that no offline messages was sent to the user

View file

@ -66,10 +66,6 @@ import org.jivesoftware.smackx.packet.DiscoverInfo;
*/ */
public class ServiceDiscoveryManagerTest extends SmackTestCase { public class ServiceDiscoveryManagerTest extends SmackTestCase {
/**
* Constructor for ServiceDiscoveryManagerTest.
* @param arg0
*/
public ServiceDiscoveryManagerTest(String arg0) { public ServiceDiscoveryManagerTest(String arg0) {
super(arg0); super(arg0);
} }

View file

@ -55,6 +55,7 @@ package org.jivesoftware.smackx;
import java.util.Iterator; import java.util.Iterator;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.ThreadFilter;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
@ -83,10 +84,10 @@ public class XHTMLManagerTest extends SmackTestCase {
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2 // User1 creates a chat with user2
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!"); msg.setBody("Hey John, this is my new green!!!!");
@ -123,39 +124,12 @@ public class XHTMLManagerTest extends SmackTestCase {
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a listener for the chat that will check if the received message includes
// an XHTML extension. Answer an ACK if everything is ok
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
String body = (String) it.next();
System.out.println(body);
}
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
try {
chat2.sendMessage("ok");
} catch (Exception e) {
fail("An error occured sending ack " + e.getMessage());
}
}
};
chat2.addMessageListener(packetListener);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!"); msg.setBody("Hey John, this is my new green!!!!");
@ -179,8 +153,24 @@ public class XHTMLManagerTest extends SmackTestCase {
} catch (Exception e) { } catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
// Wait for 1 second for a reply
msg = chat1.nextMessage(1000); Packet packet = chat2.nextResult(2000);
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
String body = (String) it.next();
System.out.println(body);
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
assertNotNull("No reply received", msg); assertNotNull("No reply received", msg);
} }
@ -194,37 +184,12 @@ public class XHTMLManagerTest extends SmackTestCase {
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a listener for the chat that will check if the received message includes
// an XHTML extension. Answer an ACK if everything is ok
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
int received = 0;
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
received++;
String body = (String) it.next();
System.out.println(body);
}
bodiesReceived = received;
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
}
};
chat2.addMessageListener(packetListener);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody( msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds."); "awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
@ -266,11 +231,31 @@ public class XHTMLManagerTest extends SmackTestCase {
bodiesSent = 2; bodiesSent = 2;
bodiesReceived = 0; bodiesReceived = 0;
chat1.sendMessage(msg); chat1.sendMessage(msg);
// Wait half second so that the complete test can run
Thread.sleep(300);
} catch (Exception e) { } catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
Packet packet = chat2.nextResult(2000);
int received = 0;
Message message = (Message) packet;
assertTrue(
"The received message is not an XHTML Message",
XHTMLManager.isXHTMLMessage(message));
try {
assertTrue(
"Message without XHTML bodies",
XHTMLManager.getBodies(message).hasNext());
for (Iterator it = XHTMLManager.getBodies(message); it.hasNext();) {
received++;
String body = (String) it.next();
System.out.println(body);
}
bodiesReceived = received;
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers" +
"is misconfigured");
}
assertEquals( assertEquals(
"Number of sent and received XHTMP bodies does not match", "Number of sent and received XHTMP bodies does not match",
bodiesSent, bodiesSent,

View file

@ -53,9 +53,6 @@
package org.jivesoftware.smackx.muc; package org.jivesoftware.smackx.muc;
import org.jivesoftware.smack.*; import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.*; import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smack.test.SmackTestCase; import org.jivesoftware.smack.test.SmackTestCase;
import org.jivesoftware.smackx.Form; import org.jivesoftware.smackx.Form;
@ -78,10 +75,6 @@ public class MultiUserChatTest extends SmackTestCase {
private MultiUserChat muc; private MultiUserChat muc;
/**
* Constructor for MultiUserChatTest.
* @param arg0
*/
public MultiUserChatTest(String arg0) { public MultiUserChatTest(String arg0) {
super(arg0); super(arg0);
} }
@ -440,8 +433,8 @@ public class MultiUserChatTest extends SmackTestCase {
assertFalse("No room was found", rooms.isEmpty()); assertFalse("No room was found", rooms.isEmpty());
// Check that we have discovered the room used by this test // Check that we have discovered the room used by this test
boolean found = false; boolean found = false;
for (Iterator it = rooms.iterator(); it.hasNext();) { for (Object room1 : rooms) {
HostedRoom hostedRoom = (HostedRoom) it.next(); HostedRoom hostedRoom = (HostedRoom) room1;
if (room.equals(hostedRoom.getJid())) { if (room.equals(hostedRoom.getJid())) {
found = true; found = true;
break; break;
@ -461,14 +454,12 @@ public class MultiUserChatTest extends SmackTestCase {
MultiUserChat muc2 = new MultiUserChat(getConnection(1), room); MultiUserChat muc2 = new MultiUserChat(getConnection(1), room);
muc2.join("testbot2"); muc2.join("testbot2");
getConnection(0).addPacketListener(new PacketListener() { getConnection(0).getChatManager().addChatListener(new ChatListener() {
public void processPacket(Packet packet) { public void chatCreated(Chat chat2, boolean createdLocally) {
Message message = (Message) packet;
Chat chat2 = new Chat(getConnection(0), message.getFrom(), message.getThread());
assertEquals( assertEquals(
"Sender of chat is incorrect", "Sender of chat is incorrect",
room + "/testbot2", room + "/testbot2",
message.getFrom()); chat2.getParticipant());
try { try {
chat2.sendMessage("ACK"); chat2.sendMessage("ACK");
} }
@ -476,16 +467,15 @@ public class MultiUserChatTest extends SmackTestCase {
fail(e.getMessage()); fail(e.getMessage());
} }
} }
}, });
new AndFilter(
new MessageTypeFilter(Message.Type.CHAT),
new PacketTypeFilter(Message.class)));
// Start a private chat with another participant // Start a private chat with another participant
Chat chat = muc2.createPrivateChat(room + "/testbot"); Chat chat = muc2.createPrivateChat(room + "/testbot", null);
PacketCollector collector = chat.createCollector();
chat.sendMessage("Hello there"); chat.sendMessage("Hello there");
Message response = chat.nextMessage(2000); Message response = (Message) collector.nextResult(2000);
assertNotNull("No response", response);
assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom()); assertEquals("Sender of response is incorrect", room + "/testbot", response.getFrom());
assertEquals("Body of response is incorrect", "ACK", response.getBody()); assertEquals("Body of response is incorrect", "ACK", response.getBody());
@ -1561,8 +1551,8 @@ public class MultiUserChatTest extends SmackTestCase {
// Check that the owner list is correct // Check that the owner list is correct
Collection affiliates = muc.getOwners(); Collection affiliates = muc.getOwners();
assertEquals("Room does not have 2 owners", 2, affiliates.size()); assertEquals("Room does not have 2 owners", 2, affiliates.size());
for (Iterator it =affiliates.iterator(); it.hasNext();) { for (Object affiliate1 : affiliates) {
Affiliate affiliate = (Affiliate)it.next(); Affiliate affiliate = (Affiliate) affiliate1;
if (getBareJID(0).equals(affiliate.getJid())) { if (getBareJID(0).equals(affiliate.getJid())) {
assertEquals("Wrong affiliation", "owner", affiliate.getAffiliation()); assertEquals("Wrong affiliation", "owner", affiliate.getAffiliation());
assertEquals("Wrong role", "moderator", affiliate.getRole()); assertEquals("Wrong role", "moderator", affiliate.getRole());
@ -1600,8 +1590,8 @@ public class MultiUserChatTest extends SmackTestCase {
// Check that the moderator list is correct // Check that the moderator list is correct
Collection occupants = muc.getModerators(); Collection occupants = muc.getModerators();
assertEquals("Room does not have 2 moderators", 2, occupants.size()); assertEquals("Room does not have 2 moderators", 2, occupants.size());
for (Iterator it =occupants.iterator(); it.hasNext();) { for (Object occupant1 : occupants) {
Occupant occupant = (Occupant)it.next(); Occupant occupant = (Occupant) occupant1;
if (getFullJID(0).equals(occupant.getJid())) { if (getFullJID(0).equals(occupant.getJid())) {
assertEquals("Wrong affiliation", "owner", occupant.getAffiliation()); assertEquals("Wrong affiliation", "owner", occupant.getAffiliation());
assertEquals("Wrong role", "moderator", occupant.getRole()); assertEquals("Wrong role", "moderator", occupant.getRole());
@ -1854,7 +1844,7 @@ public class MultiUserChatTest extends SmackTestCase {
answerForm.setAnswer("muc#roomconfig_moderatedroom", true); answerForm.setAnswer("muc#roomconfig_moderatedroom", true);
// Keep the room owner // Keep the room owner
try { try {
List owners = new ArrayList(); List<String> owners = new ArrayList<String>();
owners.add(getBareJID(0)); owners.add(getBareJID(0));
answerForm.setAnswer("muc#roomconfig_roomowners", owners); answerForm.setAnswer("muc#roomconfig_roomowners", owners);
} }
@ -1884,6 +1874,7 @@ public class MultiUserChatTest extends SmackTestCase {
muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT)); muc.sendConfigurationForm(new Form(Form.TYPE_SUBMIT));
} }
catch (Exception e) { catch (Exception e) {
e.printStackTrace();
fail(e.getMessage()); fail(e.getMessage());
} }
} }

View file

@ -65,10 +65,6 @@ import org.jivesoftware.smack.test.SmackTestCase;
*/ */
public class MessageEventTest extends SmackTestCase { public class MessageEventTest extends SmackTestCase {
/**
* Constructor for MessageEventTest.
* @param name
*/
public MessageEventTest(String name) { public MessageEventTest(String name) {
super(name); super(name);
} }
@ -82,10 +78,10 @@ public class MessageEventTest extends SmackTestCase {
*/ */
public void testSendMessageEventRequest() { public void testSendMessageEventRequest() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here..."); msg.setBody("An interesting body comes here...");
// Create a MessageEvent Package and add it to the message // Create a MessageEvent Package and add it to the message
@ -118,7 +114,7 @@ public class MessageEventTest extends SmackTestCase {
*/ */
public void testSendMessageEventRequestAndDisplayNotifications() { public void testSendMessageEventRequestAndDisplayNotifications() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create a Listener that listens for Messages with the extension "jabber:x:roster" // Create a Listener that listens for Messages with the extension "jabber:x:roster"
// This listener will listen on the conn2 and answer an ACK if everything is ok // This listener will listen on the conn2 and answer an ACK if everything is ok
@ -143,7 +139,7 @@ public class MessageEventTest extends SmackTestCase {
getConnection(0).addPacketListener(packetListener, packetFilter); getConnection(0).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("An interesting body comes here..."); msg.setBody("An interesting body comes here...");
// Create a MessageEvent Package and add it to the message // Create a MessageEvent Package and add it to the message

View file

@ -20,10 +20,6 @@ import org.jivesoftware.smackx.*;
*/ */
public class RosterExchangeTest extends SmackTestCase { public class RosterExchangeTest extends SmackTestCase {
/**
* Constructor for RosterExchangeTest.
* @param arg0
*/
public RosterExchangeTest(String arg0) { public RosterExchangeTest(String arg0) {
super(arg0); super(arg0);
} }
@ -35,10 +31,10 @@ public class RosterExchangeTest extends SmackTestCase {
*/ */
public void testSendRosterEntries() { public void testSendRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
@ -62,40 +58,12 @@ public class RosterExchangeTest extends SmackTestCase {
*/ */
public void testSendAndReceiveRosterEntries() { public void testSendAndReceiveRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension "jabber:x:roster"
// This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster");
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
RosterExchange rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
}
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
try {
chat2.sendMessage("ok");
} catch (Exception e) {
fail("An error occured sending ack " + e.getMessage());
}
}
};
getConnection(1).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
@ -110,8 +78,20 @@ public class RosterExchangeTest extends SmackTestCase {
fail("An error occured sending the message with the roster"); fail("An error occured sending the message with the roster");
} }
// Wait for 2 seconds for a reply // Wait for 2 seconds for a reply
msg = chat1.nextMessage(2000); Packet packet = chat2.nextResult(2000);
assertNotNull("No reply received", msg); Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
} }
/** /**
@ -122,47 +102,12 @@ public class RosterExchangeTest extends SmackTestCase {
*/ */
public void testSendAndAcceptRosterEntries() { public void testSendAndAcceptRosterEntries() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension "jabber:x:roster"
// This listener will listen on the conn2, save the roster entries and answer an ACK if everything is ok
PacketFilter packetFilter = new PacketExtensionFilter("x", "jabber:x:roster");
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
RosterExchange rosterExchange =
(RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
// Add the roster entries to user2's roster
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
}
} catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
} catch (Exception e) {
fail(e.toString());
}
try {
chat2.sendMessage("ok");
} catch (Exception e) {
fail("An error occured sending ack " + e.getMessage());
}
}
};
getConnection(1).addPacketListener(packetListener, packetFilter);
// Create the message to send with the roster // Create the message to send with the roster
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("This message contains roster items."); msg.setBody("This message contains roster items.");
// Create a RosterExchange Package and add it to the message // Create a RosterExchange Package and add it to the message
@ -177,12 +122,32 @@ public class RosterExchangeTest extends SmackTestCase {
fail("An error occured sending the message with the roster"); fail("An error occured sending the message with the roster");
} }
// Wait for 10 seconds for a reply // Wait for 10 seconds for a reply
msg = chat1.nextMessage(5000); Packet packet = chat2.nextResult(5000);
assertNotNull("No reply received", msg); Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try { try {
Thread.sleep(200); rosterExchange =
} catch (Exception e) { (RosterExchange) message.getExtension("x", "jabber:x:roster");
assertNotNull("Message without extension \"jabber:x:roster\"", rosterExchange);
assertTrue(
"Roster without entries",
rosterExchange.getRosterEntries().hasNext());
// Add the roster entries to user2's roster
for (Iterator it = rosterExchange.getRosterEntries(); it.hasNext();) {
RemoteRosterEntry remoteRosterEntry = (RemoteRosterEntry) it.next();
getConnection(1).getRoster().createEntry(
remoteRosterEntry.getUser(),
remoteRosterEntry.getName(),
remoteRosterEntry.getGroupArrayNames());
} }
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
catch (Exception e) {
fail(e.toString());
}
assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0); assertTrue("Roster2 has no entries", getConnection(1).getRoster().getEntryCount() > 0);
} }

View file

@ -69,10 +69,6 @@ public class XHTMLExtensionTest extends SmackTestCase {
private int bodiesSent; private int bodiesSent;
private int bodiesReceived; private int bodiesReceived;
/**
* Constructor for XHTMLExtensionTest.
* @param name
*/
public XHTMLExtensionTest(String name) { public XHTMLExtensionTest(String name) {
super(name); super(name);
} }
@ -84,10 +80,10 @@ public class XHTMLExtensionTest extends SmackTestCase {
*/ */
public void testSendSimpleXHTMLMessage() { public void testSendSimpleXHTMLMessage() {
// User1 creates a chat with user2 // User1 creates a chat with user2
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!"); msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message // Create a XHTMLExtension Package and add it to the message
@ -116,47 +112,12 @@ public class XHTMLExtensionTest extends SmackTestCase {
*/ */
public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendSimpleXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im"
// This listener will listen on the conn2 and answer an ACK if everything is ok
PacketFilter packetFilter =
new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
XHTMLExtension xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
String body = (String) it.next();
System.out.println(body);
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
try {
chat2.sendMessage("ok");
}
catch (Exception e) {
fail("An error occured sending ack " + e.getMessage());
}
}
};
getConnection(1).addPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody("Hey John, this is my new green!!!!"); msg.setBody("Hey John, this is my new green!!!!");
// Create a XHTMLExtension Package and add it to the message // Create a XHTMLExtension Package and add it to the message
@ -172,9 +133,26 @@ public class XHTMLExtensionTest extends SmackTestCase {
catch (Exception e) { catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
// Wait for 2 seconds for a reply Packet packet = chat2.nextResult(2000);
msg = chat1.nextMessage(1000); Message message = (Message) packet;
assertNotNull("No reply received", msg); assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
String body = (String) it.next();
System.out.println(body);
}
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
} }
/** /**
@ -187,8 +165,9 @@ public class XHTMLExtensionTest extends SmackTestCase {
*/ */
public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() { public void testSendComplexXHTMLMessageAndDisplayReceivedXHTMLMessage() {
// Create a chat for each connection // Create a chat for each connection
Chat chat1 = getConnection(0).createChat(getBareJID(1)); Chat chat1 = getConnection(0).getChatManager().createChat(getBareJID(1), null);
final Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat1.getThreadID()); final PacketCollector chat2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat1.getThreadID()));
// Create a Listener that listens for Messages with the extension // Create a Listener that listens for Messages with the extension
//"http://jabber.org/protocol/xhtml-im" //"http://jabber.org/protocol/xhtml-im"
@ -197,33 +176,13 @@ public class XHTMLExtensionTest extends SmackTestCase {
new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im"); new PacketExtensionFilter("html", "http://jabber.org/protocol/xhtml-im");
PacketListener packetListener = new PacketListener() { PacketListener packetListener = new PacketListener() {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
int received = 0;
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
XHTMLExtension xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
received++;
System.out.println((String) it.next());
}
bodiesReceived = received;
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is misconfigured");
}
} }
}; };
getConnection(1).addPacketListener(packetListener, packetFilter); getConnection(1).addPacketListener(packetListener, packetFilter);
// User1 creates a message to send to user2 // User1 creates a message to send to user2
Message msg = chat1.createMessage(); Message msg = new Message();
msg.setSubject("Any subject you want"); msg.setSubject("Any subject you want");
msg.setBody( msg.setBody(
"awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds."); "awesome! As Emerson once said: A foolish consistency is the hobgoblin of little minds.");
@ -240,11 +199,33 @@ public class XHTMLExtensionTest extends SmackTestCase {
bodiesSent = xhtmlExtension.getBodiesCount(); bodiesSent = xhtmlExtension.getBodiesCount();
bodiesReceived = 0; bodiesReceived = 0;
chat1.sendMessage(msg); chat1.sendMessage(msg);
Thread.sleep(300);
} }
catch (Exception e) { catch (Exception e) {
fail("An error occured sending the message with XHTML"); fail("An error occured sending the message with XHTML");
} }
Packet packet = chat2.nextResult(2000);
int received = 0;
Message message = (Message) packet;
assertNotNull("Body is null", message.getBody());
try {
xhtmlExtension =
(XHTMLExtension) message.getExtension(
"html",
"http://jabber.org/protocol/xhtml-im");
assertNotNull(
"Message without extension \"http://jabber.org/protocol/xhtml-im\"",
xhtmlExtension);
assertTrue("Message without XHTML bodies", xhtmlExtension.getBodiesCount() > 0);
for (Iterator it = xhtmlExtension.getBodies(); it.hasNext();) {
received++;
System.out.println((String) it.next());
}
bodiesReceived = received;
}
catch (ClassCastException e) {
fail("ClassCastException - Most probable cause is that smack providers is " +
"misconfigured");
}
// Wait half second so that the complete test can run // Wait half second so that the complete test can run
assertEquals( assertEquals(
"Number of sent and received XHTMP bodies does not match", "Number of sent and received XHTMP bodies does not match",