KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > smackx > FormTest


1 /**
2 * $RCSfile$
3 * $Revision: 2446 $
4 * $Date: 2005-01-12 00:01:43 -0300 (Wed, 12 Jan 2005) $
5 *
6 * Copyright (C) 2002-2003 Jive Software. All rights reserved.
7 * ====================================================================
8 * The Jive Software License (based on Apache Software License, Version 1.1)
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 *
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 *
22 * 3. The end-user documentation included with the redistribution,
23 * if any, must include the following acknowledgment:
24 * "This product includes software developed by
25 * Jive Software (http://www.jivesoftware.com)."
26 * Alternately, this acknowledgment may appear in the software itself,
27 * if and wherever such third-party acknowledgments normally appear.
28 *
29 * 4. The names "Smack" and "Jive Software" must not be used to
30 * endorse or promote products derived from this software without
31 * prior written permission. For written permission, please
32 * contact webmaster@jivesoftware.com.
33 *
34 * 5. Products derived from this software may not be called "Smack",
35 * nor may "Smack" appear in their name, without prior written
36 * permission of Jive Software.
37 *
38 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
39 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
40 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
46 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
48 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49 * SUCH DAMAGE.
50 * ====================================================================
51 */

52
53 package org.jivesoftware.smackx;
54
55 import org.jivesoftware.smack.Chat;
56 import org.jivesoftware.smack.XMPPException;
57 import org.jivesoftware.smack.packet.Message;
58 import org.jivesoftware.smack.test.SmackTestCase;
59
60 /**
61  * Tests the DataForms extensions.
62  *
63  * @author Gaston Dombiak
64  */

65 public class FormTest extends SmackTestCase {
66
67     /**
68      * Constructor for FormTest.
69      * @param arg0
70      */

71     public FormTest(String JavaDoc arg0) {
72         super(arg0);
73     }
74
75     /**
76      * 1. Create a form to fill out and send it to the other user
77      * 2. Retrieve the form to fill out, complete it and return it to the requestor
78      * 3. Retrieve the completed form and check that everything is OK
79      */

80     public void testFilloutForm() {
81         Form formToSend = new Form(Form.TYPE_FORM);
82         formToSend.setInstructions(
83             "Fill out this form to report your case.\nThe case will be created automatically.");
84         formToSend.setTitle("Case configurations");
85         // Add a hidden variable
86
FormField field = new FormField("hidden_var");
87         field.setType(FormField.TYPE_HIDDEN);
88         field.addValue("Some value for the hidden variable");
89         formToSend.addField(field);
90         // Add a fixed variable
91
field = new FormField();
92         field.addValue("Section 1: Case description");
93         formToSend.addField(field);
94         // Add a text-single variable
95
field = new FormField("name");
96         field.setLabel("Enter a name for the case");
97         field.setType(FormField.TYPE_TEXT_SINGLE);
98         formToSend.addField(field);
99         // Add a text-multi variable
100
field = new FormField("description");
101         field.setLabel("Enter a description");
102         field.setType(FormField.TYPE_TEXT_MULTI);
103         formToSend.addField(field);
104         // Add a boolean variable
105
field = new FormField("time");
106         field.setLabel("Is this your first case?");
107         field.setType(FormField.TYPE_BOOLEAN);
108         formToSend.addField(field);
109         // Add a text variable where an int value is expected
110
field = new FormField("age");
111         field.setLabel("How old are you?");
112         field.setType(FormField.TYPE_TEXT_SINGLE);
113         formToSend.addField(field);
114
115         // Create the chats between the two participants
116
Chat chat = getConnection(0).createChat(getBareJID(1));
117         Chat chat2 = new Chat(getConnection(1), getBareJID(0), chat.getThreadID());
118
119         Message msg = chat.createMessage();
120         msg.setBody("To enter a case please fill out this form and send it back to me");
121         msg.addExtension(formToSend.getDataFormToSend());
122
123         try {
124             // Send the message with the form to fill out
125
chat.sendMessage(msg);
126
127             // Get the message with the form to fill out
128
Message msg2 = chat2.nextMessage(2000);
129             // Retrieve the form to fill out
130
Form formToRespond = Form.getFormFrom(msg2);
131             assertNotNull(formToRespond);
132             assertNotNull(formToRespond.getField("name"));
133             assertNotNull(formToRespond.getField("description"));
134             // Obtain the form to send with the replies
135
Form completedForm = formToRespond.createAnswerForm();
136             assertNotNull(completedForm.getField("hidden_var"));
137             // Check that a field of type String does not accept booleans
138
try {
139                 completedForm.setAnswer("name", true);
140                 fail("A boolean value was set to a field of type String");
141             }
142             catch (IllegalArgumentException JavaDoc e) {
143             }
144             completedForm.setAnswer("name", "Credit card number invalid");
145             completedForm.setAnswer(
146                 "description",
147                 "The ATM says that my credit card number is invalid. What's going on?");
148             completedForm.setAnswer("time", true);
149             completedForm.setAnswer("age", 20);
150             // Create a new message to send with the completed form
151
msg2 = chat2.createMessage();
152             msg2.setBody("To enter a case please fill out this form and send it back to me");
153             // Add the completed form to the message
154
msg2.addExtension(completedForm.getDataFormToSend());
155             // Send the message with the completed form
156
chat2.sendMessage(msg2);
157
158             // Get the message with the completed form
159
Message msg3 = chat.nextMessage(2000);
160             // Retrieve the completed form
161
completedForm = Form.getFormFrom(msg3);
162             assertNotNull(completedForm);
163             assertNotNull(completedForm.getField("name"));
164             assertNotNull(completedForm.getField("description"));
165             assertEquals(
166                 completedForm.getField("name").getValues().next(),
167                 "Credit card number invalid");
168             assertNotNull(completedForm.getField("time"));
169             assertNotNull(completedForm.getField("age"));
170             assertEquals("The age is bad", "20", completedForm.getField("age").getValues().next());
171
172         }
173         catch (XMPPException ex) {
174             fail(ex.getMessage());
175         }
176     }
177
178     protected int getMaxConnections() {
179         return 2;
180     }
181
182 }
183
Popular Tags