1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.StringReader ; 29 import java.io.FileReader ; 30 import java.io.StringWriter ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.TransformerException ; 33 import javax.xml.transform.sax.SAXSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 import junit.framework.TestCase; 36 import junit.framework.TestSuite; 37 import junit.textui.TestRunner; 38 import org.xml.sax.EntityResolver ; 39 import org.xml.sax.InputSource ; 40 import org.xml.sax.SAXException ; 41 42 47 48 public class SchematronTest extends TestCase { 49 public void testSimpleTransform() throws Exception { 50 Schematron s = new Schematron("/simple.xsl"); 51 StringWriter actual = new StringWriter (); 52 Result r = new StreamResult (actual); 53 VariableResolver vr = new VariableResolver(); 54 vr.setEntityResolver(new MyEntityResolver("/sun-domain_1_1.dtd")); 55 SAXSource src = new SAXSource (vr, 56 new InputSource ("domain.xml")); 57 s.analyze(src, r); 58 } 59 60 61 public void testWithSAXSourceWithVRAndER() throws Exception { 62 Schematron s = new Schematron("/domain.xsl"); 63 StringWriter actual = new StringWriter (); 64 Result r = new StreamResult (actual); 65 VariableResolver vr = new VariableResolver(); 66 vr.setEntityResolver(new MyEntityResolver("/sun-domain_1_1.dtd")); 67 SAXSource src = new SAXSource (vr, 68 new InputSource ("domain.xml.no.dtd")); 69 s.analyze(src, r); 70 assertEquals(1540, actual.toString().length()); 71 72 } 73 76 public void testWithSAXSourceWithVRNoER() throws Exception { 77 Schematron s = new Schematron("/domain.xsl"); 78 StringWriter actual = new StringWriter (); 79 Result r = new StreamResult (actual); 80 VariableResolver vr = new VariableResolver(); 81 SAXSource src = new SAXSource (vr, 82 new InputSource (new FileReader ("domain.xml.no.dtd"))); 83 s.analyze(src, r); 84 assertEquals(1540, actual.toString().length()); 85 } 86 public void testWithSAXSourceNoVR() throws Exception { 89 Schematron s = new Schematron("/domain.xsl"); 90 StringWriter actual = new StringWriter (); 91 Result r = new StreamResult (actual); 92 SAXSource src = new SAXSource (new InputSource ("domain.xml.no.dtd")); 93 s.analyze(src, r); 94 assertEquals(1540, actual.toString().length()); 95 } 96 97 public void testWithEntityResolverError() throws Exception { 100 Schematron s = new Schematron("/schema2.xsl"); 101 StringWriter actual = new StringWriter (); 102 Result r = new StreamResult (actual); 103 String input = "<?xml version='1.0'?><!DOCTYPE root PUBLIC 'DO NOT MATCH' 'http://www.google.com'><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>"; 104 VariableResolver vr = new VariableResolver(); 105 vr.setEntityResolver(new MyEntityResolver("/schema2.dtd")); 106 SAXSource src = new SAXSource (vr, 107 new InputSource (new StringReader (input))); 108 try { 109 s.analyze(src, r); 110 fail("Expected a Transformer Exception"); 111 } 112 catch (TransformerException te){ 113 assertEquals("javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: External parameter entity \"%[dtd];\" has characters after markup.", te.getMessage()); 114 } 115 116 } 117 public void testExternalWithEntityResolver() throws Exception { 120 Schematron s = new Schematron("/schema2.xsl"); 121 StringWriter actual = new StringWriter (); 122 Result r = new StreamResult (actual); 123 VariableResolver vr = new VariableResolver(); 124 vr.setEntityResolver(new MyEntityResolver("/schema2.dtd")); 125 SAXSource src = new SAXSource (vr, 126 new InputSource ("schema2.xml")); 127 128 s.analyze(src, r); 129 assertEquals("This is the id: value", ""+actual); 130 } 131 public void testExternal() throws Exception { 134 Schematron s = new Schematron("/schema2.xsl"); 135 StringWriter actual = new StringWriter (); 136 Result r = new StreamResult (actual); 137 SAXSource src = new SAXSource (new VariableResolver(), 138 new InputSource ("schema2.xml")); 139 140 s.analyze(src, r); 141 assertEquals("This is the id: value", ""+actual); 142 } 143 public void testWithEntityResolver() throws Exception { 146 Schematron s = new Schematron("/schema2.xsl"); 147 StringWriter actual = new StringWriter (); 148 Result r = new StreamResult (actual); 149 String input = "<?xml version='1.0'?><!DOCTYPE domain PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>"; 150 VariableResolver vr = new VariableResolver(); 151 vr.setEntityResolver(new MyEntityResolver("/schema2.dtd")); 152 SAXSource src = new SAXSource (vr, 153 new InputSource (new StringReader (input))); 154 155 s.analyze(src, r); 156 assertEquals("This is the id: value", ""+actual); 157 } 158 159 public void testComment() throws Exception { 160 Schematron s = new Schematron("/schema2.xsl"); 161 StringWriter actual = new StringWriter (); 162 Result r = new StreamResult (actual); 163 String input = "<?xml version='1.0'?><!-- comment --><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>"; 164 SAXSource src = new SAXSource (new VariableResolver(), 165 new InputSource (new StringReader (input))); 166 s.analyze(src, r); 167 assertEquals("This is the id: value", ""+actual); 168 } 169 170 public void testPipeline() throws Exception { 174 Schematron s = new Schematron("/schema2.xsl"); 175 StringWriter actual = new StringWriter (); 176 Result r = new StreamResult (actual); 177 String input = "<?xml version='1.0'?><root id=\"${var}\"><system-property name=\"var\" value=\"value\"/></root>"; 178 SAXSource src = new SAXSource (new VariableResolver(), 179 new InputSource (new StringReader (input))); 180 s.analyze(src, r); 181 assertEquals("This is the id: value", ""+actual); 182 } 183 184 public void testSchematron() throws Exception { 185 Schematron s = new Schematron("/schema.xsl"); 186 StringWriter actual = new StringWriter (); 187 Result r = new StreamResult (actual); 188 s.analyze(new SAXSource (new InputSource (new StringReader ("<top><root id ='foo'/></top>"))), r); 189 assertEquals("This is the result", ""+actual); 190 } 191 192 public SchematronTest(String name){ 193 super(name); 194 } 195 196 protected void setUp() { 197 } 198 199 protected void tearDown() { 200 } 201 202 private void nyi(){ 203 fail("Not Yet Implemented"); 204 } 205 206 public static void main(String args[]){ 207 if (args.length == 0){ 208 TestRunner.run(SchematronTest.class); 209 } else { 210 TestRunner.run(makeSuite(args)); 211 } 212 } 213 private static TestSuite makeSuite(String args[]){ 214 final TestSuite ts = new TestSuite(); 215 for (int i = 0; i < args.length; i++){ 216 ts.addTest(new SchematronTest(args[i])); 217 } 218 return ts; 219 } 220 221 private static class MyEntityResolver implements EntityResolver 222 { 223 private String myDtd = ""; 224 225 MyEntityResolver(){} 226 227 MyEntityResolver(String dtd){ 228 myDtd = dtd; 229 } 230 231 public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) throws SAXException , IOException { 232 if (null != publicId && publicId.equals("-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN")) { 233 final InputStream is = getClass().getResourceAsStream(myDtd); 234 return (null != is ) ? new InputSource (is) : null; 235 } else { 236 return null; 237 } 238 } 239 } 240 241 242 } 243 | Popular Tags |