field.setType(FormField.TYPE_TEXT_SINGLE);
formToSend.addField(field);
// Create the chats between the two participants
Chat chat = getConnection(0).getChatManager().createChat(getBareJID(1), null);
PacketCollector collector = getConnection(0).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
PacketCollector collector2 = getConnection(1).createPacketCollector(
new ThreadFilter(chat.getThreadID()));
Message msg = new Message();
msg.setBody("To enter a case please fill out this form and send it back to me");
msg.addExtension(formToSend.getDataFormToSend());
try {
// Send the message with the form to fill out
chat.sendMessage(msg);
// Get the message with the form to fill out
Message msg2 = (Message)collector2.nextResult(2000);
assertNotNull("Messge not found", msg2);
// Retrieve the form to fill out
Form formToRespond = Form.getFormFrom(msg2);
assertNotNull(formToRespond);
assertNotNull(formToRespond.getField("name"));
assertNotNull(formToRespond.getField("description"));
// Obtain the form to send with the replies
Form completedForm = formToRespond.createAnswerForm();
assertNotNull(completedForm.getField("hidden_var"));
// Check that a field of type String does not accept booleans
try {
completedForm.setAnswer("name", true);
fail("A boolean value was set to a field of type String");
}
catch (IllegalArgumentException e) {
}
completedForm.setAnswer("name", "Credit card number invalid");
completedForm.setAnswer(
"description",
"The ATM says that my credit card number is invalid. What's going on?");
completedForm.setAnswer("time", true);
completedForm.setAnswer("age", 20);
// Create a new message to send with the completed form
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");
// Add the completed form to the message
msg2.addExtension(completedForm.getDataFormToSend());
// Send the message with the completed form
getConnection(1).sendPacket(msg2);
// Get the message with the completed form
Message msg3 = (Message) collector.nextResult(2000);
assertNotNull("Messge not found", msg3);
// Retrieve the completed form
completedForm = Form.getFormFrom(msg3);
assertNotNull(completedForm);
assertNotNull(completedForm.getField("name"));
assertNotNull(completedForm.getField("description"));
assertEquals(
completedForm.getField("name").getValues().next(),
"Credit card number invalid");
assertNotNull(completedForm.getField("time"));
assertNotNull(completedForm.getField("age"));
assertEquals("The age is bad", "20", completedForm.getField("age").getValues().next());
}
catch (XMPPException ex) {
fail(ex.getMessage());
}
finally {
collector.cancel();
collector2.cancel();
}
}