package wstxtest.msv;
import javax.xml.stream.*;
import org.codehaus.stax2.*;
import org.codehaus.stax2.validation.*;
import wstxtest.vstream.BaseValidationTest;
/**
* This is a simple base-line "smoke test" that checks that W3C Schema
* validation works at least minimally.
*/
public class TestW3CSchema
extends BaseValidationTest
{
/**
* Sample schema, using sample 'personal.xsd' found from the web
*/
final static String SIMPLE_NON_NS_SCHEMA = "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n"
+ "\n" + "\n" + "\n";
final static String SIMPLE_XML = ""
+ "" + " "
+ "FamilyFred" + " "
+ " " + " "
+ " "
+ " BlowJoe"
+ " " + " " + " " + "";
/**
* Test validation against a simple document valid according to a very
* simple W3C schema.
*/
public void testSimpleNonNs() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
XMLStreamReader2 sr = getReader(SIMPLE_XML);
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("personnel", sr.getLocalName());
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
assertTokenType(END_DOCUMENT, sr.getEventType());
}
public void testSimplePartialNonNs() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
XMLStreamReader2 sr = getReader(SIMPLE_XML);
assertTokenType(START_ELEMENT, sr.next());
assertEquals("personnel", sr.getLocalName());
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("person", sr.getLocalName());
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
assertTokenType(END_DOCUMENT, sr.getEventType());
}
/**
* Test validation of a simple document that is invalid according to the
* simple test schema.
*/
public void testSimpleNonNsMissingId() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
String XML = ""
+ "FG"
+ "";
verifyFailure(XML, schema, "missing id attribute",
"is missing \"id\" attribute");
}
public void testSimpleNonNsUndefinedId() throws XMLStreamException
{
XMLValidationSchema schema = parseW3CSchema(SIMPLE_NON_NS_SCHEMA);
String XML = ""
+ "FG"
+ "";
verifyFailure(XML, schema, "undefined referenced id ('m3')",
"Undefined ID 'm3'");
}
public void testSimpleDataTypes() throws XMLStreamException
{
// Another sample schema, from
String SCHEMA = "\n"
+ "\n"
+ " \n"
+ " \n"
+ " "
+ " "
+ " "
+ " "
+ ""
+ "";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// First, valid doc:
String XML = "- 3 \r\n4.05
";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
try {
assertTokenType(START_ELEMENT, sr.next());
assertEquals("item", sr.getLocalName());
assertTokenType(START_ELEMENT, sr.next());
assertEquals("quantity", sr.getLocalName());
String str = sr.getElementText();
assertEquals("3", str.trim());
assertTokenType(START_ELEMENT, sr.next());
assertEquals("price", sr.getLocalName());
str = sr.getElementText();
assertEquals("4.05", str.trim());
assertTokenType(END_ELEMENT, sr.next());
assertTokenType(END_DOCUMENT, sr.next());
} catch (XMLValidationException vex) {
fail("Did not expect validation exception, got: " + vex);
}
sr.close();
// Then invalid (wrong type for value)
XML = "- 34b1.00
";
sr.validateAgainst(schema);
verifyFailure(XML, schema, "invalid 'positive integer' datatype",
"does not satisfy the \"positiveInteger\"");
sr.close();
// Another invalid, empty value
XML = "- 1.00
";
sr.validateAgainst(schema);
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure
verifyFailure(XML, schema, "invalid (missing) positive integer value",
"does not satisfy the \"positiveInteger\"", false);
sr.close();
// Another invalid, missing value
XML = "- 1.00
";
sr.validateAgainst(schema);
// 12-Nov-2008, TSa: still having MSV bug here, need to suppress failure
verifyFailure(XML, schema, "invalid (missing) positive integer value",
"does not satisfy the \"positiveInteger\"", false);
sr.close();
}
public void testSimpleText() throws XMLStreamException
{
String SCHEMA = "\n"
+ "\n"
+ ""
+ "";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// First, 3 valid docs:
String XML = "xyz";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
XML = "";
sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
XML = "";
sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
// Then invalid?
XML = "";
sr = getReader(XML);
sr.validateAgainst(schema);
verifyFailure(XML, schema, "should warn about wrong root element",
"tag name \"foobar\" is not allowed", false);
}
/**
* Test for reproducing [WSTX-191]
*/
public void testConstrainedText() throws XMLStreamException
{
String SCHEMA = "\n"
+ "\n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n" + " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n"
+ " \n" + " \n"
+ " \n" + "\n";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// first cases where there is text, and 1 to 5 descs
_testValidDesc(schema, "Du Texte");
_testValidDesc(
schema,
"123");
_testValidDesc(schema,
"");
_testValidDesc(schema,
"??");
_testValidDesc(schema, "");
_testValidDesc(schema, "");
_testValidDesc(schema, "");
}
private void _testValidDesc(XMLValidationSchema schema, String descSnippet) throws XMLStreamException
{
// These should all be valid according to the schema
String XML = ""
+ descSnippet + "";
XMLStreamReader2 sr = getReader(XML);
sr.validateAgainst(schema);
streamThrough(sr);
sr.close();
}
public void testValidationHandler() throws XMLStreamException
{
String SCHEMA = "\n"
+ "\n"
+ ""
+ "";
XMLValidationSchema schema = parseW3CSchema(SCHEMA);
// Then invalid?
String XML = "";
XMLStreamReader2 sr = getReader(XML);
sr.setValidationProblemHandler(new ValidationProblemHandler() {
@Override
public void reportProblem(XMLValidationProblem problem)
throws XMLValidationException {
throw new LocalValidationError(problem);
}
});
sr.validateAgainst(schema);
boolean threw = false;
try {
while (sr.hasNext()) {
/* int type = */sr.next();
}
} catch (LocalValidationError lve) {
threw = true;
}
assertTrue(threw);
}
/*
///////////////////////////////////////////////////////////////////////
// Helper methods
///////////////////////////////////////////////////////////////////////
*/
XMLStreamReader2 getReader(String contents) throws XMLStreamException
{
XMLInputFactory2 f = getInputFactory();
setValidating(f, false);
return constructStreamReader(f, contents);
}
/*
///////////////////////////////////////////////////////////////////////
// Helper classes
///////////////////////////////////////////////////////////////////////
*/
public static class LocalValidationError extends RuntimeException
{
private static final long serialVersionUID = 1L;
protected XMLValidationProblem problem;
LocalValidationError(XMLValidationProblem problem) {
this.problem = problem;
}
public XMLValidationProblem getProblem() {
return problem;
}
}
}