1 16 package org.apache.commons.jelly.test.xml; 17 18 import java.io.StringWriter ; 19 import java.net.URL ; 20 21 import junit.framework.TestCase; 22 import junit.framework.TestSuite; 23 24 import org.apache.commons.jelly.Jelly; 25 import org.apache.commons.jelly.JellyContext; 26 import org.apache.commons.jelly.JellyException; 27 import org.apache.commons.jelly.Script; 28 import org.apache.commons.jelly.XMLOutput; 29 30 37 public class TestXMLValidation extends TestCase { 38 39 Jelly jelly = null; 40 JellyContext context = null; 41 XMLOutput xmlOutput = null; 42 43 public TestXMLValidation(String name) { 44 super(name); 45 } 46 47 public static TestSuite suite() throws Exception { 48 return new TestSuite(TestXMLValidation.class); 49 } 50 51 public void setUp(String scriptName) throws Exception { 52 context = new JellyContext(); 53 xmlOutput = XMLOutput.createXMLOutput(new StringWriter ()); 54 55 jelly = new Jelly(); 56 57 String script = scriptName; 58 URL url = this.getClass().getResource(script); 59 if ( url == null ) { 60 throw new Exception ( 61 "Could not find Jelly script: " + script 62 + " in package of class: " + this.getClass().getName() 63 ); 64 } 65 jelly.setUrl(url); 66 } 67 68 public void testInvalidXML1NoValidation() throws Exception { 69 setUp("invalidScript1.jelly"); 71 Script script = jelly.compileScript(); 72 script.run(context,xmlOutput); 73 assertTrue("should have set 'foo' variable to 'bar'", 74 context.getVariable("foo").equals("bar")); 75 76 setUp("invalidScript1.jelly"); 78 jelly.setValidateXML(false); 79 script = jelly.compileScript(); 80 script.run(context,xmlOutput); 81 assertTrue("should have set 'foo' variable to 'bar'", 82 context.getVariable("foo").equals("bar")); 83 } 84 85 public void testInvalidXML1Validation() throws Exception { 86 setUp("invalidScript1.jelly"); 88 jelly.setValidateXML(true); 89 try { 90 Script script = jelly.compileScript(); 91 fail("Invalid scripts should throw JellyException on parse"); 92 } catch (JellyException e) { 93 } 94 } 95 96 public void testValidXML1Validation()throws Exception { 97 setUp("validScript1.jelly"); 99 jelly.setValidateXML(true); 100 Script script = jelly.compileScript(); 101 script.run(context,xmlOutput); 102 assertTrue("should have set 'foo' variable to 'bar'", 103 context.getVariable("foo").equals("bar")); 104 } 105 106 } 107 | Popular Tags |