1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import junit.framework.*; 27 import org.xml.sax.InputSource ; 28 import java.io.StringReader ; 29 import java.io.OutputStreamWriter ; 30 import java.io.StringWriter ; 31 import org.xml.sax.EntityResolver ; 32 import org.xml.sax.SAXException ; 33 import java.io.IOException ; 34 import org.xml.sax.SAXParseException ; 35 import java.io.InputStream ; 36 import java.io.Reader ; 37 import java.io.FileReader ; 38 import java.io.File ; 39 44 45 public class RNGValidatorTest extends TestCase { 46 public void testacceptanceTestNoEntityFile() throws Exception { 47 final RNGValidator r = new RNGValidator(); 48 final VariableResolver vr = new VariableResolver(); 49 vr.setEntityResolver(new MyEntityResolver()); 50 final InputStream schema = this.getClass().getResourceAsStream("/domain.rng"); 51 if (null ==schema ){ 52 throw new IOException ("couldn't get resource: /domain.rng"); 53 } 54 StringWriter out = new StringWriter (); 55 try { 56 r.validate(new InputSource (schema), 57 new InputSource ("domain.xml"), 58 vr, 59 out); 60 fail("Expected Error indicating no file"); 61 } 62 catch (SAXParseException spe){ 63 assertEquals("External entity not found: \"http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd\".", spe.getMessage()); 64 } 65 66 } 67 public void testacceptanceTestNoInputFile() throws Exception { 68 final RNGValidator r = new RNGValidator(); 69 final VariableResolver vr = new VariableResolver(); 70 vr.setEntityResolver(new MyEntityResolver(new File ("sun-domain_1_1.dtd"))); 71 final InputStream schema = this.getClass().getResourceAsStream("/domain.rng"); 72 if (null ==schema ){ 73 throw new IOException ("couldn't get resource: /domain.rng"); 74 } 75 StringWriter out = new StringWriter (); 76 try { 77 r.validate(new InputSource (schema), 78 new InputSource ("domain.xml.notHere"), 79 vr, 80 out); 81 fail("Expected Error indicating no file"); 82 } 83 catch (IOException ioe){ 84 assertEquals("/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml.notHere (No such file or directory)", ioe.getMessage()); 85 } 86 } 87 public void testacceptanceTest() throws Exception { 88 final RNGValidator r = new RNGValidator(); 89 final VariableResolver vr = new VariableResolver(); 90 vr.setEntityResolver(new MyEntityResolver(new File ("sun-domain_1_1.dtd"))); 91 final InputStream schema = this.getClass().getResourceAsStream("/domain.rng"); 92 if (null ==schema ){ 93 throw new IOException ("couldn't get resource: /domain.rng"); 94 } 95 StringWriter out = new StringWriter (); 96 r.validate(new InputSource (schema), 97 new InputSource ("domain.xml"), 98 vr, 99 out); 100 assertEquals("Error: URI=file:/opt/S1AS8/NewBuild/admin/validator/tests/com/sun/enterprise/config/serverbeans/validation/domain.xml Line=11: attribute \"load-order\" has a bad value: \"ABC\" does not satisfy the \"positiveInteger\" type\n", ""+out); 101 } 102 103 public void testEntityResolverWithError() throws Exception { 104 RNGValidator r = new RNGValidator(); 105 final String schema =""+ 106 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 107 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 108 "<define name='top'>"+ 109 "<element name='top'>"+ 110 "<empty/>"+ 111 "</element>"+ 112 "</define>"+ 113 "<start>"+ 114 "<ref name='top'/>"+ 115 "</start>"+ 116 "</grammar>"; 117 118 119 final String source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>"; 120 VariableResolver vr = new VariableResolver(); 121 StringWriter out = new StringWriter (); 122 123 vr.setEntityResolver(new MyEntityResolver()); 124 try { 125 126 r.validate(new InputSource (new StringReader (schema)), 127 new InputSource (new StringReader (source)), 128 vr, 129 out); 130 fail("Expected a SAX Exception indicating problems with the external entity"); 131 } 132 catch (SAXParseException spe){ 133 assertEquals("External parameter entity \"%[dtd];\" has characters after markup.", spe.getMessage()); 134 } 135 } 136 public void testEntityResolver() throws Exception { 137 RNGValidator r = new RNGValidator(); 138 final String schema =""+ 139 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 140 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 141 "<define name='top'>"+ 142 "<element name='top'>"+ 143 "<empty/>"+ 144 "</element>"+ 145 "</define>"+ 146 "<start>"+ 147 "<ref name='top'/>"+ 148 "</start>"+ 149 "</grammar>"; 150 151 final String dtd = ""+ 152 "<?xml version='1.0' encoding='UTF-8'?>"+ 153 "<!ELEMENT top (bottom)>"+ 154 "<!ELEMENT bottom EMPTY>"; 155 156 final String source ="<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE top PUBLIC '-//Sun Microsystems Inc.//DTD Application Server 8.0 Domain//EN' 'http://www.sun.com/software/appserver/dtds/sun-domain_1_1.dtd'><top/>"; 157 VariableResolver vr = new VariableResolver(); 158 StringWriter out = new StringWriter (); 159 160 vr.setEntityResolver(new MyEntityResolver(dtd)); 161 r.validate(new InputSource (new StringReader (schema)), 162 new InputSource (new StringReader (source)), 163 vr, 164 out); 165 assertEquals("", out+""); 166 } 167 public void testBasicWithSubstitutionError() throws Exception { 168 RNGValidator r = new RNGValidator(); 169 final String schema =""+ 170 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 171 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 172 "<define name='top'>"+ 173 "<element name='top'>"+ 174 "<ref name='system-property'/>"+ 175 "<attribute name='id'>"+ 176 "<value>VALUE</value>"+ 177 "</attribute>"+ 178 "</element>"+ 179 "</define>"+ 180 "<define name='system-property'>"+ 181 "<element name='system-property'>"+ 182 "<empty/>"+ 183 "<attribute name='name'/>"+ 184 "<attribute name='value'/>"+ 185 "</element>"+ 186 "</define>"+ 187 "<start>"+ 188 "<ref name='top'/>"+ 189 "</start>"+ 190 "</grammar>"; 191 192 final String source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='Not Expected Value'/></top>"; 193 StringWriter out = new StringWriter (); 194 VariableResolver vr = new VariableResolver(); 195 r.validate(new InputSource (new StringReader (schema)), 196 new InputSource (new StringReader (source)), 197 vr, 198 out); 199 assertEquals("Error: URI=null Line=1: attribute \"id\" has a bad value\n", ""+out); 200 201 } 202 public void testBasicWithSubstitution() throws Exception { 203 RNGValidator r = new RNGValidator(); 204 final String schema =""+ 205 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 206 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 207 "<define name='top'>"+ 208 "<element name='top'>"+ 209 "<ref name='system-property'/>"+ 210 "<attribute name='id'>"+ 211 "<value>VALUE</value>"+ 212 "</attribute>"+ 213 "</element>"+ 214 "</define>"+ 215 "<define name='system-property'>"+ 216 "<element name='system-property'>"+ 217 "<empty/>"+ 218 "<attribute name='name'/>"+ 219 "<attribute name='value'/>"+ 220 "</element>"+ 221 "</define>"+ 222 "<start>"+ 223 "<ref name='top'/>"+ 224 "</start>"+ 225 "</grammar>"; 226 227 final String source ="<?xml version='1.0'?> <top id='${var}'><system-property name='var' value='VALUE'/></top>"; 228 StringWriter out = new StringWriter (); 229 VariableResolver vr = new VariableResolver(); 230 r.validate(new InputSource (new StringReader (schema)), 231 new InputSource (new StringReader (source)), 232 vr, 233 out); 234 assertEquals("", ""+out); 235 236 } 237 238 public void testBasic() throws Exception { 239 RNGValidator r = new RNGValidator(); 240 final String schema =""+ 241 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 242 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 243 "<define name='top'>"+ 244 "<element name='top'>"+ 245 "<empty/>"+ 246 "</element>"+ 247 "</define>"+ 248 "<start>"+ 249 "<ref name='top'/>"+ 250 "</start>"+ 251 "</grammar>"; 252 253 final String source ="<?xml version='1.0'?> <top></top>"; 254 StringWriter out = new StringWriter (); 255 r.validate(new InputSource (new StringReader (schema)), 256 new InputSource (new StringReader (source)), 257 out); 258 assertEquals("", ""+out); 259 260 } 261 262 public void testBasicError() throws Exception { 263 RNGValidator r = new RNGValidator(); 264 final String schema =""+ 265 "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"+ 266 "<grammar xmlns='http://relaxng.org/ns/structure/1.0'>"+ 267 "<define name='top'>"+ 268 "<element name='top'>"+ 269 "<empty/>"+ 270 "</element>"+ 271 "</define>"+ 272 "<start>"+ 273 "<ref name='top'/>"+ 274 "</start>"+ 275 "</grammar>"; 276 277 final String source ="<?xml version='1.0'?> <top><bottom/></top>"; 278 StringWriter out = new StringWriter (); 279 r.validate(new InputSource (new StringReader (schema)), 280 new InputSource (new StringReader (source)), 281 out); 282 assertEquals("Error: URI=null Line=1: element \"bottom\" was found where no element may occur\n", ""+out); 283 284 } 285 286 public RNGValidatorTest(String name){ 287 super(name); 288 } 289 290 protected void setUp() { 291 } 292 293 protected void tearDown() { 294 } 295 296 private void nyi(){ 297 fail("Not Yet Implemented"); 298 } 299 300 public static void main(String args[]){ 301 if (args.length == 0){ 302 junit.textui.TestRunner.run(RNGValidatorTest.class); 303 } else { 304 junit.textui.TestRunner.run(makeSuite(args)); 305 } 306 } 307 private static TestSuite makeSuite(String args[]){ 308 final TestSuite ts = new TestSuite(); 309 for (int i = 0; i < args.length; i++){ 310 ts.addTest(new RNGValidatorTest(args[i])); 311 } 312 return ts; 313 } 314 315 private static class MyEntityResolver implements EntityResolver 316 { 317 String string; 318 File file; 319 320 MyEntityResolver(){} 321 322 323 MyEntityResolver(String dtdSrc){ 324 string = dtdSrc; 325 } 326 327 MyEntityResolver(File f) throws IOException { 328 file = f; 329 } 330 331 332 public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) 333 throws SAXException , java.io.IOException { 334 if (null != string){ 335 return new InputSource (new StringReader (string)); 336 } else if (null != file) { 337 return new InputSource (new FileReader (file)); 338 } else { 339 return null; 340 } 341 } 342 } 343 344 345 } 346 | Popular Tags |