1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.StringReader ; 27 import java.io.StringWriter ; 28 import javax.xml.parsers.SAXParser ; 29 import javax.xml.parsers.SAXParserFactory ; 30 import junit.framework.*; 31 import com.sun.org.apache.xml.internal.serialize.OutputFormat; 32 import com.sun.org.apache.xml.internal.serialize.Serializer; 33 import com.sun.org.apache.xml.internal.serialize.TextSerializer; 34 import com.sun.org.apache.xml.internal.serialize.XMLSerializer; 35 import org.xml.sax.InputSource ; 36 import org.xml.sax.XMLReader ; 37 import org.xml.sax.helpers.DefaultHandler ; 38 43 44 public class VariableExpanderTest extends TestCase { 45 public void testCharactersComeBeforeChild() throws Exception { 48 final String input = "<r>"+ 49 "<parent>"+ 50 "some child characters that should appear before the child element"+ 51 "<child>this should come after the preceding characters</child>"+ 52 "some child characters that should come after the child element"+ 53 "</parent>"+ 54 "</r>"; 55 ve.parse(getInputSource(input)); 56 assertEquals(input, ""+out); 57 } 58 59 public void testSetFramerBasic() throws Exception { 60 VariableExpander ve = new VariableExpander(); 61 ve.setFramer(new Framer()); 62 } 63 64 65 public void testSetFramer() throws Exception { 66 frameHolder.getDomainFrame().put("domain", "domain from original frame holder"); 67 FrameHolder newFH = new FrameHolder(); 68 newFH.getDomainFrame().put("domain", "new"); 69 Framer framer = new Framer(newFH); 70 ve.setFramer(framer); 71 ve.parse(getInputSource("<foo a=\"${domain}\"/>")); 72 assertEquals("<foo a=\"new\"/>", ""+out); 73 } 74 75 76 public void testGeneralCharacterHandling() throws Exception { 77 constructComplexFrames(); 78 final String doc = "<doc>" + 79 "<config name=\"c1\" att1=\"${d}\" att2=\"att2\">foo far ${c1} ${d}</config>"+ 80 "<server name=\"s2\">gargle ${s2} ${d} ${cl1}</server>"+ 81 "</doc>"; 82 final String expected = "<doc>" + 83 "<config name=\"c1\" att1=\"dv1\" att2=\"att2\">foo far cv1 dv1</config>"+ 84 "<server name=\"s2\">gargle sv2 dv1 clv1</server>"+ 85 "</doc>"; 86 ve.parse(getInputSource(doc)); 87 assertEquals(expected, ""+out); 88 } 89 90 public void testAttributeAndCharacterHandling() throws Exception { 91 frameHolder.getDomainFrame().put("k", "v"); 92 frameHolder.getDomainFrame().put("k1", "v1"); 93 ve.parse(getInputSource("<foo attr1='fee' attr2='${k}'>foo far ${k} ${k1}</foo>")); 94 assertEquals("<foo attr1=\"fee\" attr2=\"v\">foo far v v1</foo>", ""+out); 95 } 96 97 public void testAttributeHandling() throws Exception { 98 frameHolder.getDomainFrame().put("k", "v"); 99 ve.parse(getInputSource("<foo attr1='fee' attr2='${k}'/>")); 100 assertEquals("<foo attr1=\"fee\" attr2=\"v\"/>", ""+out); 101 } 102 103 104 private void constructComplexFrames() { 107 Frame domain, config1, config2, cluster, server1, server2, server3; 108 String d = "d"; 109 String dv1 = "dv1"; 110 String c1 = "c1"; 111 String cv1 = "cv1"; 112 String c2 = "c2"; 113 String cv2 = "cv2"; 114 String cl1 = "cl1"; 115 String clv1 = "clv1"; 116 String s1 = "s1"; 117 String sv1 = "sv1"; 118 String s2 = "s2"; 119 String sv2 = "sv2"; 120 String s3 = "s3"; 121 String sv3 = "sv3"; 122 domain = frameHolder.getDomainFrame(); 123 domain.put(d, dv1); 124 config1 = frameHolder.getConfigFrame(c1); 125 config1.put(c1, cv1); 126 config1.inheritFrom(domain); 127 config2 = frameHolder.getConfigFrame(c2); 128 config2.put(c2, cv2); 129 config2.inheritFrom(domain); 130 server1 = frameHolder.getServerFrame(s1); 131 server1.put(s1, sv1); 132 server1.inheritFrom(config1); 133 server2 = frameHolder.getServerFrame(s2); 134 server2.put(s2, sv2); 135 server3 = frameHolder.getServerFrame(s3); 136 server3.put(s3, sv3); 137 server3.inheritFrom(config2); 138 cluster = frameHolder.getClusterFrame(cl1); 139 cluster.put(cl1, clv1); 140 cluster.inheritFrom(config1); 141 server2.inheritFrom(cluster); 142 } 143 144 145 public void testConfigCase() throws Exception { 146 frameHolder.getDomainFrame().put("domain", "domain_new"); 147 frameHolder.getConfigFrame("config1").put("config1", "config1_v"); 148 ve.parse(getInputSource("<foo><system-property name='config1' value='sys-property'/><config name='config1'>${config1}</config></foo>")); 149 assertEquals("<foo><system-property name=\"config1\" value=\"sys-property\"/><config name=\"config1\">config1_v</config></foo>", out.toString()); 150 } 151 152 153 public void testMultipleVariables() throws Exception { 154 Frame f = frameHolder.getDomainFrame(); 155 f.put("k1", "v1"); 156 f.put("k2", "v2"); 157 assertEquals("far lar lar v1 foo far v2", ve.eval("far lar lar ${k1} foo far ${k2}", f)); 158 } 159 160 public void testNestedReference() throws Exception { 161 Frame f = Frame.newFrame(); 162 f.put("k", "v"); 163 assertEquals("${fo${k}}", ve.eval("${fo${k}}", f)); 164 165 } 166 167 public void testPartialReference() throws Exception { 168 Frame f = Frame.newFrame(); 169 f.put("domain", "domain_new"); 170 assertEquals("${fo", ve.eval("${fo", f)); 171 172 } 173 174 public void testInvalidReference() throws Exception { 175 Frame f = Frame.newFrame(); 176 f.put("domain", "domain_new"); 177 assertEquals("${foo}", ve.eval("${foo}", f)); 178 } 179 180 public void testSimpleCase() throws Exception { 181 Frame f = Frame.newFrame(); 182 f.put("domain", "domain_new"); 183 assertEquals("domain_new", ve.eval("${domain}", f)); 184 } 185 186 187 private InputSource getInputSource(String s) { 188 return new InputSource (new StringReader (s)); 189 } 190 191 192 private XMLReader getXMLReader() throws Exception { 193 194 final XMLReader xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 195 xr.setFeature("http://xml.org/sax/features/namespaces", true); 196 xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 197 return xr; 198 199 } 200 201 202 public VariableExpanderTest(String name){ 203 super(name); 204 } 205 206 private FrameHolder frameHolder; 207 private VariableExpander ve; 208 private StringWriter out; 209 210 211 protected void setUp() throws Exception { 212 frameHolder = new FrameHolder(); 213 out = new StringWriter (); 214 ve = new VariableExpander(new Framer(frameHolder)); 215 ve.setParent(getXMLReader()); 216 OutputFormat of = new OutputFormat(); 217 of.setOmitXMLDeclaration(true); 218 Serializer ts = new XMLSerializer(of); 219 ts.setOutputCharStream(out); 220 ve.setContentHandler(ts.asContentHandler()); 221 } 222 223 protected void tearDown() { 224 } 225 226 private void nyi(){ 227 fail("Not Yet Implemented"); 228 } 229 230 public static void main(String args[]){ 231 if (args.length == 0){ 232 junit.textui.TestRunner.run(VariableExpanderTest.class); 233 } else { 234 junit.textui.TestRunner.run(makeSuite(args)); 235 } 236 } 237 private static TestSuite makeSuite(String args[]){ 238 final TestSuite ts = new TestSuite(); 239 for (int i = 0; i < args.length; i++){ 240 ts.addTest(new VariableExpanderTest(args[i])); 241 } 242 return ts; 243 } 244 } 245 | Popular Tags |