mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-12-13 22:41:08 +01:00
Support for XEP-0122: Data Forms Validation.
Data Forms Validation are a part of Data Fields and implemented as extensions, added to a Datafield. Data validation extensions are validated before adding to the message, using the consistency rules as described in the XEP. Fixes SMACK-621. Minor modifications done by Florian Schmaus <flo@geekplace.eu>
This commit is contained in:
parent
019b9dc5d4
commit
b08dbc1dbc
13 changed files with 943 additions and 66 deletions
|
|
@ -20,8 +20,6 @@ import static org.junit.Assert.assertEquals;
|
|||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.jivesoftware.smack.SmackException;
|
||||
import org.jivesoftware.smack.packet.Element;
|
||||
|
|
@ -32,6 +30,9 @@ import org.jivesoftware.smackx.xdatalayout.packet.DataLayout;
|
|||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Fieldref;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Section;
|
||||
import org.jivesoftware.smackx.xdatalayout.packet.DataLayout.Text;
|
||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
|
||||
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.xmlpull.v1.XmlPullParser;
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
|
@ -45,11 +46,10 @@ import org.xmlpull.v1.XmlPullParserException;
|
|||
public class DataFormTest {
|
||||
private static final String TEST_OUTPUT_1 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field></x>";
|
||||
private static final String TEST_OUTPUT_2 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'></field><page xmlns='http://jabber.org/protocol/xdata-layout' label='Label'><fieldref var='testField1'/><section label='section Label'><text>SectionText</text></section><text>PageText</text></page></x>";
|
||||
private static Logger logger = Logger.getLogger(DataFormTest.class.getName());
|
||||
private static final String TEST_OUTPUT_3 = "<x xmlns='jabber:x:data' type='SUBMIT'><instructions>InstructionTest1</instructions><field var='testField1'><validate xmlns='http://jabber.org/protocol/xdata-validate' datatype='xs:integer'><range min='1111' max='9999'/></validate></field></x>";
|
||||
|
||||
@Test
|
||||
public void test() throws XmlPullParserException, IOException, SmackException {
|
||||
|
||||
//Build a Form
|
||||
DataForm df = new DataForm("SUBMIT");
|
||||
String instruction = "InstructionTest1";
|
||||
|
|
@ -59,12 +59,11 @@ public class DataFormTest {
|
|||
|
||||
assertNotNull( df.toXML());
|
||||
String output = df.toXML().toString();
|
||||
logger.finest(output);
|
||||
assertEquals(TEST_OUTPUT_1, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = getParser(output);
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
||||
|
|
@ -75,15 +74,11 @@ public class DataFormTest {
|
|||
|
||||
assertNotNull( df.toXML());
|
||||
output = df.toXML().toString();
|
||||
logger.finest(output);
|
||||
assertEquals(TEST_OUTPUT_1, output);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLayout() throws XmlPullParserException, IOException, SmackException {
|
||||
|
||||
//Build a Form
|
||||
DataForm df = new DataForm("SUBMIT");
|
||||
String instruction = "InstructionTest1";
|
||||
|
|
@ -104,12 +99,11 @@ public class DataFormTest {
|
|||
|
||||
assertNotNull( df.toXML());
|
||||
String output = df.toXML().toString();
|
||||
logger.finest(output);
|
||||
assertEquals(TEST_OUTPUT_2, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = getParser(output);
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
||||
|
|
@ -124,23 +118,42 @@ public class DataFormTest {
|
|||
|
||||
assertNotNull( df.toXML());
|
||||
output = df.toXML().toString();
|
||||
logger.finest(output);
|
||||
assertEquals(TEST_OUTPUT_2, output);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param output
|
||||
* @return
|
||||
* @throws XmlPullParserException
|
||||
* @throws IOException
|
||||
*/
|
||||
private XmlPullParser getParser(String output) throws XmlPullParserException, IOException {
|
||||
logger.finest("getParser");
|
||||
XmlPullParser parser = PacketParserUtils.newXmppParser();
|
||||
parser.setInput(new StringReader(output));
|
||||
parser.next();
|
||||
return parser;
|
||||
@Test
|
||||
public void testValidation() throws XmlPullParserException, IOException, SmackException {
|
||||
//Build a Form
|
||||
DataForm df = new DataForm("SUBMIT");
|
||||
String instruction = "InstructionTest1";
|
||||
df.addInstruction(instruction);
|
||||
FormField field = new FormField("testField1");
|
||||
df.addField(field);
|
||||
|
||||
ValidateElement dv = new RangeValidateElement("xs:integer","1111", "9999");
|
||||
field.setValidateElement(dv);
|
||||
|
||||
assertNotNull( df.toXML());
|
||||
String output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_3, output);
|
||||
|
||||
DataFormProvider pr = new DataFormProvider();
|
||||
|
||||
XmlPullParser parser = PacketParserUtils.getParserFor(output);
|
||||
|
||||
df = pr.parse(parser);
|
||||
|
||||
assertNotNull(df);
|
||||
assertNotNull(df.getFields());
|
||||
assertEquals(1 , df.getFields().size() );
|
||||
Element element = df.getFields().get(0).getValidateElement();
|
||||
assertNotNull(element);
|
||||
dv = (ValidateElement) element;
|
||||
|
||||
assertEquals("xs:integer" , dv.getDatatype());
|
||||
|
||||
assertNotNull( df.toXML());
|
||||
output = df.toXML().toString();
|
||||
assertEquals(TEST_OUTPUT_3, output);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue