| 1 |
package com.thaiopensource.relaxng.jaxp; |
| 2 |
|
| 3 |
import com.thaiopensource.validation.LSInputImpl; |
| 4 |
import com.thaiopensource.validation.SchemaFactory2; |
| 5 |
import com.thaiopensource.xml.sax.DraconianErrorHandler; |
| 6 |
import org.testng.Assert; |
| 7 |
import org.testng.annotations.DataProvider; |
| 8 |
import org.testng.annotations.Test; |
| 9 |
import org.w3c.dom.ls.LSInput; |
| 10 |
import org.w3c.dom.ls.LSResourceResolver; |
| 11 |
import org.xml.sax.InputSource; |
| 12 |
import org.xml.sax.SAXException; |
| 13 |
import org.xml.sax.SAXNotRecognizedException; |
| 14 |
import org.xml.sax.SAXNotSupportedException; |
| 15 |
import org.xml.sax.SAXParseException; |
| 16 |
|
| 17 |
import javax.xml.XMLConstants; |
| 18 |
import javax.xml.transform.Source; |
| 19 |
import javax.xml.transform.sax.SAXSource; |
| 20 |
import javax.xml.transform.stream.StreamSource; |
| 21 |
import javax.xml.validation.SchemaFactory; |
| 22 |
import javax.xml.validation.Validator; |
| 23 |
import javax.xml.validation.ValidatorHandler; |
| 24 |
import java.io.File; |
| 25 |
import java.io.FileOutputStream; |
| 26 |
import java.io.IOException; |
| 27 |
import java.io.OutputStreamWriter; |
| 28 |
import java.io.StringReader; |
| 29 |
import java.io.Writer; |
| 30 |
|
| 31 |
/** |
| 32 |
* Test SchemaFactoryImpl. |
| 33 |
*/ |
| 34 |
public abstract class SchemaFactoryImplTest { |
| 35 |
protected final Class<? extends SchemaFactory2> factoryClass; |
| 36 |
private static int filenameIndex = 0; |
| 37 |
|
| 38 |
protected SchemaFactoryImplTest(Class<? extends SchemaFactory2> factoryClass) { |
| 39 |
this.factoryClass = factoryClass; |
| 40 |
} |
| 41 |
|
| 42 |
protected SchemaFactory2 factory() { |
| 43 |
try { |
| 44 |
return factoryClass.newInstance(); |
| 45 |
} |
| 46 |
catch (InstantiationException e) { |
| 47 |
} |
| 48 |
catch (IllegalAccessException e) { |
| 49 |
} |
| 50 |
throw new AssertionError(); |
| 51 |
} |
| 52 |
|
| 53 |
@Test(dataProvider = "valid") |
| 54 |
public void testValidCharStream(String schemaString, String docString) throws SAXException, IOException { |
| 55 |
factory().newSchema(charStreamSource(schemaString)).newValidator().validate(charStreamSource(docString)); |
| 56 |
} |
| 57 |
|
| 58 |
@Test(dataProvider = "valid") |
| 59 |
public void testValidFile(String schemaString, String docString) throws SAXException, IOException { |
| 60 |
factory().newSchema(fileSource(schemaString)).newValidator().validate(fileSource(docString)); |
| 61 |
} |
| 62 |
@DataProvider(name = "valid") |
| 63 |
protected Object[][] valid() { |
| 64 |
return new Object[][] { |
| 65 |
{ createSchema("doc"), "<doc/>" }, |
| 66 |
{ element("doc", new String[] { attribute("att") }), |
| 67 |
"<doc att='val'/>" } |
| 68 |
}; |
| 69 |
} |
| 70 |
|
| 71 |
private static SAXSource charStreamSource(String s) { |
| 72 |
return new SAXSource(new InputSource(new StringReader(s))); |
| 73 |
} |
| 74 |
|
| 75 |
private static synchronized Source fileSource(String s) throws IOException { |
| 76 |
final File file = new File("t" + filenameIndex++); |
| 77 |
Writer w = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); |
| 78 |
w.write(s); |
| 79 |
w.close(); |
| 80 |
return new StreamSource(file); |
| 81 |
} |
| 82 |
|
| 83 |
static private class CountErrorHandler extends DraconianErrorHandler { |
| 84 |
int errorCount = 0; |
| 85 |
|
| 86 |
public void error(SAXParseException e) throws SAXException { |
| 87 |
++errorCount; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
@Test |
| 92 |
public void testErrorHandlerNoThrow() throws SAXException, IOException { |
| 93 |
SchemaFactory f = factory(); |
| 94 |
Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); |
| 95 |
CountErrorHandler eh = new CountErrorHandler() { |
| 96 |
public void error(SAXParseException e) throws SAXException { |
| 97 |
if (errorCount == 0) |
| 98 |
Assert.assertEquals(e.getLineNumber(), 2); |
| 99 |
super.error(e); |
| 100 |
} |
| 101 |
}; |
| 102 |
v.setErrorHandler(eh); |
| 103 |
Assert.assertSame(v.getErrorHandler(), eh); |
| 104 |
v.validate(charStreamSource("<doc>\n<bad/></doc>")); |
| 105 |
Assert.assertTrue(eh.errorCount > 0); |
| 106 |
} |
| 107 |
|
| 108 |
@Test(expectedExceptions = { RuntimeException.class }) |
| 109 |
public void testErrorHandlerThrowRuntime() throws SAXException, IOException { |
| 110 |
SchemaFactory f = factory(); |
| 111 |
Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); |
| 112 |
v.setErrorHandler(new DraconianErrorHandler() { |
| 113 |
public void error(SAXParseException e) throws SAXException { |
| 114 |
Assert.assertEquals(e.getLineNumber(), 2); |
| 115 |
throw new RuntimeException(); |
| 116 |
} |
| 117 |
}); |
| 118 |
v.validate(charStreamSource("<doc>\n<bad/></doc>")); |
| 119 |
throw new AssertionError(); |
| 120 |
} |
| 121 |
|
| 122 |
static class MySAXException extends SAXException { } |
| 123 |
|
| 124 |
@Test(expectedExceptions = { MySAXException.class }) |
| 125 |
public void testErrorHandlerThrowSAX() throws SAXException, IOException { |
| 126 |
SchemaFactory f = factory(); |
| 127 |
Validator v = f.newSchema(charStreamSource(createSchema("doc"))).newValidator(); |
| 128 |
v.setErrorHandler(new DraconianErrorHandler() { |
| 129 |
public void error(SAXParseException e) throws SAXException { |
| 130 |
Assert.assertEquals(e.getLineNumber(), 2); |
| 131 |
throw new MySAXException(); |
| 132 |
} |
| 133 |
}); |
| 134 |
v.validate(charStreamSource("<doc>\n<bad/></doc>")); |
| 135 |
throw new AssertionError(); |
| 136 |
} |
| 137 |
|
| 138 |
@Test |
| 139 |
public void testInstanceResourceResolver() throws SAXException, IOException { |
| 140 |
SchemaFactory f = factory(); |
| 141 |
Validator v = f.newSchema(charStreamSource(element("doc", element("inner")))).newValidator(); |
| 142 |
Assert.assertNull(v.getResourceResolver()); |
| 143 |
LSResourceResolver rr = new LSResourceResolver() { |
| 144 |
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { |
| 145 |
// In Java 5 Xerces absolutized the systemId relative to the current directory |
| 146 |
int slashIndex = systemId.lastIndexOf('/'); |
| 147 |
if (slashIndex >= 0) |
| 148 |
systemId = systemId.substring(slashIndex + 1); |
| 149 |
Assert.assertEquals(systemId, "e.xml"); |
| 150 |
Assert.assertEquals(type, "http://www.w3.org/TR/REC-xml"); |
| 151 |
LSInput in = new LSInputImpl(); |
| 152 |
in.setStringData("<inner/>"); |
| 153 |
return in; |
| 154 |
} |
| 155 |
}; |
| 156 |
v.setResourceResolver(rr); |
| 157 |
Assert.assertSame(v.getResourceResolver(), rr); |
| 158 |
v.validate(charStreamSource("<!DOCTYPE doc [ <!ENTITY e SYSTEM 'e.xml'> ]><doc>&e;</doc>")); |
| 159 |
} |
| 160 |
|
| 161 |
@Test |
| 162 |
public void testSchemaResourceResolver() throws SAXException, IOException { |
| 163 |
SchemaFactory f = factory(); |
| 164 |
Assert.assertNull(f.getResourceResolver()); |
| 165 |
LSResourceResolver rr = new LSResourceResolver() { |
| 166 |
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) { |
| 167 |
Assert.assertEquals(systemId, "myschema"); |
| 168 |
Assert.assertEquals(type, getLSType()); |
| 169 |
Assert.assertNull(baseURI); |
| 170 |
Assert.assertNull(namespaceURI); |
| 171 |
Assert.assertNull(publicId); |
| 172 |
LSInput in = new LSInputImpl(); |
| 173 |
in.setStringData(createSchema("doc")); |
| 174 |
return in; |
| 175 |
} |
| 176 |
}; |
| 177 |
f.setResourceResolver(rr); |
| 178 |
Assert.assertSame(f.getResourceResolver(), rr); |
| 179 |
Validator v = f.newSchema(charStreamSource(externalRef("myschema"))).newValidator(); |
| 180 |
v.validate(charStreamSource("<doc/>")); |
| 181 |
} |
| 182 |
|
| 183 |
@Test(expectedExceptions = { UnsupportedOperationException.class }) |
| 184 |
public void testNewSchemaNoArgs() throws SAXException { |
| 185 |
factory().newSchema(); |
| 186 |
} |
| 187 |
|
| 188 |
@DataProvider(name = "supportedFeatures") |
| 189 |
Object[][] createSupportedFeatures() { |
| 190 |
return new Object[][] { |
| 191 |
{ XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.FALSE } |
| 192 |
}; |
| 193 |
} |
| 194 |
@Test(dataProvider = "supportedFeatures") |
| 195 |
public void testSupportedFeatures(String feature, Boolean defaultValueObj) throws SAXNotRecognizedException, SAXNotSupportedException { |
| 196 |
SchemaFactory f = factory(); |
| 197 |
boolean defaultValue = defaultValueObj; |
| 198 |
Assert.assertEquals(f.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING), defaultValue); |
| 199 |
f.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, !defaultValue); |
| 200 |
Assert.assertEquals(f.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING), !defaultValue); |
| 201 |
} |
| 202 |
|
| 203 |
@Test(dataProvider = "supportedFeatures") |
| 204 |
public void testFeatureInheritance(String feature, Boolean defaultValueObj) |
| 205 |
throws SAXException, SAXNotRecognizedException, SAXNotSupportedException { |
| 206 |
SchemaFactory f = factory(); |
| 207 |
boolean defaultValue = defaultValueObj; |
| 208 |
Assert.assertEquals(f.getFeature(feature), defaultValue); |
| 209 |
f.setFeature(feature, !defaultValue); |
| 210 |
ValidatorHandler vh = f.newSchema(charStreamSource(createSchema("doc"))).newValidatorHandler(); |
| 211 |
// the docs say that only properties are inherited by the ValidatorHandler |
| 212 |
Assert.assertEquals(vh.getFeature(feature), defaultValue); |
| 213 |
} |
| 214 |
|
| 215 |
@Test(expectedExceptions = { SAXNotRecognizedException.class }) |
| 216 |
public void testUnrecognizedGetFeature() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 217 |
SchemaFactory f = factory(); |
| 218 |
f.getFeature("http://thaiopensource.com/features/no-such-feature"); |
| 219 |
throw new AssertionError(); |
| 220 |
} |
| 221 |
|
| 222 |
@Test(expectedExceptions = { SAXNotRecognizedException.class }) |
| 223 |
public void testUnrecognizedSetFeature() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 224 |
SchemaFactory f = factory(); |
| 225 |
f.setFeature("http://thaiopensource.com/features/no-such-feature", false); |
| 226 |
throw new AssertionError(); |
| 227 |
} |
| 228 |
|
| 229 |
@Test(expectedExceptions = { NullPointerException.class }) |
| 230 |
public void testNullGetFeature() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 231 |
SchemaFactory f = factory(); |
| 232 |
f.getFeature(null); |
| 233 |
throw new AssertionError(); |
| 234 |
} |
| 235 |
|
| 236 |
@Test(expectedExceptions = { NullPointerException.class }) |
| 237 |
public void testNullSetFeature() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 238 |
SchemaFactory f = factory(); |
| 239 |
f.setFeature(null, true); |
| 240 |
throw new AssertionError(); |
| 241 |
} |
| 242 |
|
| 243 |
@Test(expectedExceptions = { SAXNotRecognizedException.class }) |
| 244 |
public void testUnrecognizedSetProperty() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 245 |
SchemaFactory f = factory(); |
| 246 |
f.setProperty("http://thaiopensource.com/properties-no-such-property", null); |
| 247 |
throw new AssertionError(); |
| 248 |
} |
| 249 |
|
| 250 |
@Test(expectedExceptions = { SAXNotRecognizedException.class }) |
| 251 |
public void testUnrecognizedGetProperty() throws SAXNotRecognizedException, SAXNotSupportedException { |
| 252 |
SchemaFactory f = factory(); |
| 253 |
f.getProperty("http://thaiopensource.com/properties/no-such-property"); |
| 254 |
throw new AssertionError(); |
| 255 |
} |
| 256 |
|
| 257 |
private String createSchema(String rootElement) { |
| 258 |
return element(rootElement); |
| 259 |
} |
| 260 |
|
| 261 |
private String element(String name) { |
| 262 |
return element(name, new String[] { }); |
| 263 |
} |
| 264 |
|
| 265 |
private String element(String name, String contentPattern) { |
| 266 |
return element(name, new String[] { contentPattern }); |
| 267 |
} |
| 268 |
abstract protected String element(String name, String[] contentPatterns); |
| 269 |
abstract protected String attribute(String name); |
| 270 |
abstract protected String externalRef(String uri); |
| 271 |
abstract protected String getLSType(); |
| 272 |
} |