1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-15 03:59:38 +02: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:
Anno van Vliet 2014-11-22 23:16:45 +01:00 committed by Florian Schmaus
parent 019b9dc5d4
commit b08dbc1dbc
13 changed files with 943 additions and 66 deletions

View file

@ -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);
}
}

View file

@ -21,8 +21,6 @@ import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.logging.Logger;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.util.PacketParserUtils;
@ -47,12 +45,9 @@ public class DataLayoutTest {
private static final String TEST_OUTPUT_2 = "<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>";
private static final String TEST_OUTPUT_SPECIAL = "<page xmlns='http://jabber.org/protocol/xdata-layout' label='Label - &amp; \u00E9 \u00E1 '><fieldref var='testField1'/><section label='section Label - &amp; \u00E9 \u00E1 '><text>SectionText - &amp; \u00E9 \u00E1 </text></section><text>PageText - &amp; \u00E9 \u00E1 </text><section label='&lt;html&gt;Number of Persons by&lt;br/&gt; Nationality and Status&lt;/html&gt;'><reportedref/></section><text>&lt;html&gt;&lt;font color=&apos;red&apos;&gt;&lt;em&gt;DO NOT DELAY&lt;/em&gt;&lt;/font&gt;&lt;br/&gt;supply further information&lt;/html&gt;</text></page>";
private static final String TEST_INPUT_1 = "xdata-layout-sample.xml";
private static Logger logger = Logger.getLogger(DataLayoutTest.class.getName());
@Test
public void testLayout() throws XmlPullParserException, IOException, SmackException {
DataLayout layout = new DataLayout("Label");
Fieldref reffield = new Fieldref("testField1");
layout.getPageLayout().add(reffield);
@ -63,10 +58,9 @@ public class DataLayoutTest {
assertNotNull( layout.toXML());
String output = layout.toXML().toString();
logger.finest(output);
assertEquals(TEST_OUTPUT_2, output);
XmlPullParser parser = getParser(output);
XmlPullParser parser = PacketParserUtils.getParserFor(output);
layout = DataLayoutProvider.parse(parser);
@ -75,10 +69,7 @@ public class DataLayoutTest {
assertNotNull( layout.toXML());
output = layout.toXML().toString();
logger.finest(output);
assertEquals(TEST_OUTPUT_2, output);
}
@Test
@ -101,10 +92,9 @@ public class DataLayoutTest {
assertNotNull( layout.toXML());
String output = layout.toXML().toString();
logger.finest(output);
assertEquals(TEST_OUTPUT_SPECIAL, output);
XmlPullParser parser = getParser(output);
XmlPullParser parser = PacketParserUtils.getParserFor(output);
layout = DataLayoutProvider.parse(parser);
@ -122,16 +112,11 @@ public class DataLayoutTest {
assertNotNull( layout.toXML());
output = layout.toXML().toString();
logger.finest(output);
assertEquals(TEST_OUTPUT_SPECIAL, output);
}
@Test
public void testLayoutFromFile() throws XmlPullParserException, IOException, SmackException {
DataFormProvider pr = new DataFormProvider();
XmlPullParser parser = PacketParserUtils.newXmppParser();
@ -158,24 +143,6 @@ public class DataLayoutTest {
assertNotNull( layout.toXML());
String output = layout.toXML().toString();
logger.finest(output);
assertEquals(TEST_OUTPUT_SPECIAL, 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;
}
}

View file

@ -0,0 +1,120 @@
/**
*
* Copyright 2014 Anno van Vliet
*
* 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.smackx.xdatavalidation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.jivesoftware.smackx.xdata.FormField;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.BasicValidateElement;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.OpenValidateElement;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RegexValidateElement;
import org.junit.Test;
/**
*
* @author Anno van Vliet
*
*/
public class DataValidationHelperTest {
@Test
public void testCheckConsistencyFormFieldBasicValidateElement() {
FormField field = new FormField("var");
field.setType(FormField.Type.jid_single);
BasicValidateElement element = new BasicValidateElement(null);
try {
element.checkConsistency(field);
fail("No correct check on consistency");
}
catch (ValidationConsistencyException e) {
assertEquals("Field type 'jid-single' is not consistent with validation method 'basic'.", e.getMessage());
}
try {
new ListRange(-1L, 1L);
fail("No correct check on consistency");
}
catch (IllegalArgumentException e) {
assertEquals("min must not be negative", e.getMessage());
}
element.setListRange(new ListRange(10L, 100L));
try {
element.checkConsistency(field);
fail("No correct check on consistency");
}
catch (ValidationConsistencyException e) {
assertEquals("Field type is not of type 'list-multi' while a 'list-range' is defined.", e.getMessage());
}
field.setType(FormField.Type.list_multi);
try {
element.checkConsistency(field);
}
catch (ValidationConsistencyException e) {
fail("No correct check on consistency");
}
}
@Test
public void testCheckConsistencyFormFieldOpenValidateElement() {
FormField field = new FormField("var");
field.setType(FormField.Type.hidden);
OpenValidateElement element = new OpenValidateElement(null);
try {
element.checkConsistency(field);
fail("No correct check on consistency");
}
catch (ValidationConsistencyException e) {
assertEquals("Field type 'hidden' is not consistent with validation method 'open'.", e.getMessage());
}
}
@Test
public void testCheckConsistencyFormFieldRangeValidateElement() {
FormField field = new FormField("var");
field.setType(FormField.Type.text_multi);
RangeValidateElement element = new RangeValidateElement("xs:integer",null, "99");
try {
element.checkConsistency(field);
fail("No correct check on consistency");
}
catch (ValidationConsistencyException e) {
assertEquals("Field type 'text-multi' is not consistent with validation method 'range'.", e.getMessage());
}
}
@Test
public void testCheckConsistencyFormFieldRegexValidateElement() {
FormField field = new FormField("var");
field.setType(FormField.Type.list_multi);
RegexValidateElement element = new RegexValidateElement(null, ".*");
try {
element.checkConsistency(field);
fail("No correct check on consistency");
}
catch (ValidationConsistencyException e) {
assertEquals("Field type 'list-multi' is not consistent with validation method 'regex'.", e.getMessage());
}
}
}

View file

@ -0,0 +1,140 @@
/**
*
* Copyright 2014 Anno van Vliet
*
* 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.smackx.xdatavalidation.provider;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.test.util.TestUtils;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.EmptyValidateElement;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.ListRange;
import org.jivesoftware.smackx.xdatavalidation.packet.ValidateElement.RangeValidateElement;
import org.junit.Test;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
/**
*
* @author Anno van Vliet
*
*/
public class DataValidationTest {
private static final String TEST_OUTPUT_MIN = "<validate xmlns='http://jabber.org/protocol/xdata-validate'></validate>";
private static final String TEST_OUTPUT_RANGE = "<validate xmlns='http://jabber.org/protocol/xdata-validate' datatype='xs:string'><range min='min-val' max='max-val'/><list-range min='111' max='999'/></validate>";
private static final String TEST_OUTPUT_RANGE2 = "<validate xmlns='http://jabber.org/protocol/xdata-validate'><range/></validate>";
private static final String TEST_OUTPUT_FAIL = "<validate xmlns='http://jabber.org/protocol/xdata-validate'><list-range min='1-1-1' max='999'/></validate>";
@Test
public void testMin() throws XmlPullParserException, IOException, SmackException {
ValidateElement dv = new EmptyValidateElement(null);
assertNotNull( dv.toXML());
String output = dv.toXML().toString();
assertEquals(TEST_OUTPUT_MIN, output);
XmlPullParser parser = getParser(output);
dv = DataValidationProvider.parse(parser);
assertNotNull(dv);
assertEquals("xs:string", dv.getDatatype());
assertTrue( dv instanceof EmptyValidateElement);
assertNotNull( dv.toXML());
output = dv.toXML().toString();
assertEquals(TEST_OUTPUT_MIN, output);
}
@Test
public void testRange() throws XmlPullParserException, IOException, SmackException {
ValidateElement dv = new RangeValidateElement("xs:string", "min-val", "max-val");
ListRange listRange = new ListRange(111L, 999L);
dv.setListRange(listRange );
assertNotNull( dv.toXML());
String output = dv.toXML().toString();
assertEquals(TEST_OUTPUT_RANGE, output);
XmlPullParser parser = getParser(output);
dv = DataValidationProvider.parse(parser);
assertNotNull(dv);
assertEquals("xs:string", dv.getDatatype());
assertTrue(dv instanceof RangeValidateElement );
RangeValidateElement rdv = (RangeValidateElement) dv;
assertEquals("min-val", rdv.getMin());
assertEquals("max-val", rdv.getMax());
assertNotNull(rdv.getListRange());
assertEquals(new Long(111), rdv.getListRange().getMin());
assertEquals(999, rdv.getListRange().getMax().intValue());
assertNotNull( dv.toXML());
output = dv.toXML().toString();
assertEquals(TEST_OUTPUT_RANGE, output);
}
@Test
public void testRange2() throws XmlPullParserException, IOException, SmackException {
ValidateElement dv = new RangeValidateElement(null, null, null);
assertNotNull( dv.toXML());
String output = dv.toXML().toString();
assertEquals(TEST_OUTPUT_RANGE2, output);
XmlPullParser parser = getParser(output);
dv = DataValidationProvider.parse(parser);
assertNotNull(dv);
assertEquals("xs:string", dv.getDatatype());
assertTrue(dv instanceof RangeValidateElement );
RangeValidateElement rdv = (RangeValidateElement) dv;
assertEquals(null, rdv.getMin());
assertEquals(null, rdv.getMax());
assertNotNull( rdv.toXML());
output = rdv.toXML().toString();
assertEquals(TEST_OUTPUT_RANGE2, output);
}
@Test(expected=NumberFormatException.class)
public void testRangeFailure() throws IOException, SmackException, XmlPullParserException {
XmlPullParser parser = getParser(TEST_OUTPUT_FAIL);
DataValidationProvider.parse(parser);
}
/**
* @param output
* @return
* @throws XmlPullParserException
* @throws IOException
*/
private XmlPullParser getParser(String output) throws XmlPullParserException, IOException {
return TestUtils.getParser(output, "validate");
}
}