| 1 |
package wstxtest.msv; |
| 2 |
|
| 3 |
import javax.xml.stream.*; |
| 4 |
|
| 5 |
import org.codehaus.stax2.*; |
| 6 |
import org.codehaus.stax2.validation.*; |
| 7 |
|
| 8 |
import wstxtest.vstream.BaseValidationTest; |
| 9 |
|
| 10 |
import java.io.StringWriter; |
| 11 |
|
| 12 |
/** |
| 13 |
* Simple testing of W3C Schema datatypes. |
| 14 |
* Added to test [WSTX-210]. |
| 15 |
* |
| 16 |
* @author Tatu Saloranta |
| 17 |
*/ |
| 18 |
public class TestW3CSchemaTypes |
| 19 |
extends BaseValidationTest |
| 20 |
{ |
| 21 |
final static String SCHEMA_INT = |
| 22 |
"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" |
| 23 |
+"<xsd:element name='price' type='xsd:int' />" |
| 24 |
+"\n</xsd:schema>" |
| 25 |
; |
| 26 |
|
| 27 |
final static String SCHEMA_FLOAT = |
| 28 |
"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" |
| 29 |
+"<xsd:element name='price' type='xsd:float' />" |
| 30 |
+"\n</xsd:schema>" |
| 31 |
; |
| 32 |
|
| 33 |
final static String SCHEMA_ATTRIBUTE = |
| 34 |
"<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'>\n" |
| 35 |
+"<xsd:element name='price'>\n" |
| 36 |
+"<xsd:complexType>\n" |
| 37 |
+"<xsd:attribute name='amount' type='xsd:int' use='required'/>\n" |
| 38 |
+"</xsd:complexType>\n" |
| 39 |
+"</xsd:element>\n" |
| 40 |
+"</xsd:schema>" |
| 41 |
; |
| 42 |
|
| 43 |
// // // First 'int' datatype |
| 44 |
|
| 45 |
public void testSimpleValidInt() throws Exception |
| 46 |
{ |
| 47 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT); |
| 48 |
XMLStreamReader2 sr = getReader("<price>129</price>"); |
| 49 |
sr.validateAgainst(schema); |
| 50 |
streamThrough(sr); |
| 51 |
} |
| 52 |
|
| 53 |
public void testSimpleInvalidInt() throws Exception |
| 54 |
{ |
| 55 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT); |
| 56 |
verifyFailure("<price>abc</price>", schema, "invalid 'int' value", |
| 57 |
"does not satisfy the \"int\" type"); |
| 58 |
} |
| 59 |
|
| 60 |
// // // Then 'float' datatype |
| 61 |
|
| 62 |
public void testSimpleValidFloat() throws Exception |
| 63 |
{ |
| 64 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_FLOAT); |
| 65 |
XMLStreamReader2 sr = getReader("<price>1.00</price>"); |
| 66 |
sr.validateAgainst(schema); |
| 67 |
streamThrough(sr); |
| 68 |
} |
| 69 |
|
| 70 |
public void testSimpleInvalidFloat() throws Exception |
| 71 |
{ |
| 72 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_FLOAT); |
| 73 |
verifyFailure("<price>abc</price>", schema, "invalid 'float' value", |
| 74 |
"does not satisfy the \"float\" type"); |
| 75 |
} |
| 76 |
|
| 77 |
// // // Writing |
| 78 |
|
| 79 |
public void testValidationWhenWritingAttribute() throws Exception |
| 80 |
{ |
| 81 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_ATTRIBUTE); |
| 82 |
|
| 83 |
XMLOutputFactory2 f = getOutputFactory(); |
| 84 |
final StringWriter stringWriter = new StringWriter(); |
| 85 |
XMLStreamWriter2 sw = (XMLStreamWriter2)f.createXMLStreamWriter(stringWriter); |
| 86 |
sw.validateAgainst(schema); |
| 87 |
|
| 88 |
sw.writeStartElement("price"); |
| 89 |
sw.writeIntAttribute(null, null, "amount", 129); |
| 90 |
sw.writeEndElement(); |
| 91 |
sw.flush(); |
| 92 |
|
| 93 |
final String XML = stringWriter.toString(); |
| 94 |
XMLStreamReader2 sr = getReader(XML); |
| 95 |
sr.validateAgainst(schema); |
| 96 |
streamThrough(sr); |
| 97 |
} |
| 98 |
|
| 99 |
public void testValdiationWhenWritingCharactersFromArray() throws Exception |
| 100 |
{ |
| 101 |
XMLValidationSchema schema = parseW3CSchema(SCHEMA_INT); |
| 102 |
|
| 103 |
XMLOutputFactory2 f = getOutputFactory(); |
| 104 |
XMLStreamWriter2 sw = (XMLStreamWriter2)f.createXMLStreamWriter(new StringWriter()); |
| 105 |
sw.validateAgainst(schema); |
| 106 |
|
| 107 |
String xml = "<price>129</price>"; |
| 108 |
|
| 109 |
sw.writeStartElement("price"); |
| 110 |
sw.writeCharacters(xml.toCharArray(), xml.indexOf("1"), 3); |
| 111 |
sw.writeEndElement(); |
| 112 |
sw.flush(); |
| 113 |
} |
| 114 |
|
| 115 |
/* |
| 116 |
/////////////////////////////////////////////////////////////////////// |
| 117 |
// Helper |
| 118 |
/////////////////////////////////////////////////////////////////////// |
| 119 |
*/ |
| 120 |
|
| 121 |
XMLStreamReader2 getReader(String contents) throws XMLStreamException |
| 122 |
{ |
| 123 |
XMLInputFactory2 f = getInputFactory(); |
| 124 |
setValidating(f, false); |
| 125 |
return constructStreamReader(f, contents); |
| 126 |
} |
| 127 |
} |