1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.File ; 27 import java.io.StringReader ; 28 import java.io.StringWriter ; 29 import java.net.MalformedURLException ; 30 import java.net.URI ; 31 import javax.xml.parsers.SAXParserFactory ; 32 import junit.framework.*; 33 import org.xml.sax.ContentHandler ; 34 import org.xml.sax.InputSource ; 35 import org.xml.sax.XMLReader ; 36 import com.sun.org.apache.xml.internal.serializer.ToXMLStream; 37 42 43 public class VariableResolverTest extends TestCase { 44 VariableResolver vr; 45 StringWriter out; 46 47 public void testXMLSerialization() throws Exception { 48 StringWriter w = new StringWriter (); 49 ToXMLStream xml = new ToXMLStream(); 50 xml.setWriter(w); 51 xml.setOmitXMLDeclaration(true); 52 xml.startElement("uri", "localName", "name", null); 53 xml.endElement("uri", "localName", "name"); 54 assertEquals("<name xmlns=\"uri\"/>", w.toString()); 55 } 56 57 58 public void testFile() throws Exception { 59 vr.parse(new InputSource ("test.xml")); 60 61 String expected = "<r>\n"+ 62 " <foo>\n"+ 63 " <bar>\n"+ 64 " <gargle>la di da</gargle>\n"+ 65 " </bar>\n"+ 66 " <bar>\n"+ 67 " hick\n"+ 68 " </bar>\n"+ 69 " <system-property name=\"k1\" value=\"this is the value of k1\"/>\n"+ 70 " A reference to k1 yields: \"this is the value of k1\"\n"+ 71 " </foo>\n"+ 72 " <config name=\"config1\">\n"+ 73 " k1: \"K1 VALUE\"\n"+ 74 " <system-property name=\"k1\" value=\"K1 VALUE\"/>\n"+ 75 " </config>\n"+ 76 " <config name=\"config2\">\n"+ 77 " K1: \"this is the value of k1\"\n"+ 78 " </config>\n"+ 79 "</r>"; 80 assertEquals(expected, ""+out); 81 } 82 public void testURI() throws Exception { 83 final String uri = new File ("test.xml").toURI().toURL().toString(); 84 vr.parse(new InputSource (uri)); 85 86 String expected = "<r>\n"+ 87 " <foo>\n"+ 88 " <bar>\n"+ 89 " <gargle>la di da</gargle>\n"+ 90 " </bar>\n"+ 91 " <bar>\n"+ 92 " hick\n"+ 93 " </bar>\n"+ 94 " <system-property name=\"k1\" value=\"this is the value of k1\"/>\n"+ 95 " A reference to k1 yields: \"this is the value of k1\"\n"+ 96 " </foo>\n"+ 97 " <config name=\"config1\">\n"+ 98 " k1: \"K1 VALUE\"\n"+ 99 " <system-property name=\"k1\" value=\"K1 VALUE\"/>\n"+ 100 " </config>\n"+ 101 " <config name=\"config2\">\n"+ 102 " K1: \"this is the value of k1\"\n"+ 103 " </config>\n"+ 104 "</r>"; 105 assertEquals(expected, ""+out); 106 } 107 108 109 public void testUseOfDefaultReader() throws Exception { 111 VariableResolver myVr = new VariableResolver(); 112 myVr.setContentHandler(getContentHandler(out)); 113 String input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>"; 114 String expected = "<foo att=\"value\"><system-property name=\"var\" value=\"value\"/></foo>"; 115 myVr.parse(getInputSource(input)); 116 assertEquals(expected, ""+out); 117 } 118 119 public void testAComplexExample() throws Exception { 122 String input = "<r>"+ 123 "<foo><bar><gargle/>la di da</bar><bar>hick</bar>"+ 124 "<system-property name=\"k1\" value=\"this is the value of k1\"/>"+ 125 "A reference to k1 yields: \"${k1}\""+ 126 "</foo>"+ 127 "<config name=\"config1\">" + 128 "k1: \"${k1}\""+ 129 "<system-property name=\"k1\" value=\"K1 VALUE\"/>"+ 130 "</config>"+ 131 "<config name=\"config2\">"+ 132 "k1: \"${k1}\""+ 133 "</config>"+ 134 "</r>"; 135 String expected = "<r>"+ 136 "<foo><bar><gargle/>la di da</bar><bar>hick</bar>"+ 137 "<system-property name=\"k1\" value=\"this is the value of k1\"/>"+ 138 "A reference to k1 yields: \"this is the value of k1\""+ 139 "</foo>"+ 140 "<config name=\"config1\">" + 141 "k1: \"K1 VALUE\""+ 142 "<system-property name=\"k1\" value=\"K1 VALUE\"/>"+ 143 "</config>"+ 144 "<config name=\"config2\">"+ 145 "k1: \"this is the value of k1\""+ 146 "</config>"+ 147 "</r>"; 148 vr.parse(getInputSource(input)); 149 assertEquals(expected, ""+out); 150 } 151 152 public void testConfigStuff() throws Exception { 153 String input = "<r>"+ 154 "<config name=\"c1\" att=\"${k1}\">"+ 155 "<child>${k2}</child>"+ 156 "<system-property name=\"k2\" value=\"v2\"/>"+ 157 "</config>"+ 158 "<system-property name=\"k1\" value=\"v1\"/>"+ 159 "</r>"; 160 String expected = "<r>"+ 161 "<config name=\"c1\" att=\"v1\">"+ 162 "<child>v2</child>"+ 163 "<system-property name=\"k2\" value=\"v2\"/>"+ 164 "</config>"+ 165 "<system-property name=\"k1\" value=\"v1\"/>"+ 166 "</r>"; 167 vr.parse(getInputSource(input)); 168 assertEquals(expected, ""+out); 169 } 170 171 172 public void testBasicSystemVariable() throws Exception { 173 String input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>"; 174 String expected = "<foo att=\"value\"><system-property name=\"var\" value=\"value\"/></foo>"; 175 vr.parse(getInputSource(input)); 176 assertEquals(expected, ""+out); 177 } 178 179 public void testSimpleExpansion() throws Exception { 180 String input = "<foo>${path.separator}</foo>"; 181 vr.parse(getInputSource(input)); 182 assertEquals("<foo>"+System.getProperty("path.separator")+"</foo>", ""+out); 183 } 184 185 public void testBasicOperation() throws Exception { 186 String doc = "<foo/>"; 187 vr.parse(getInputSource(doc)); 188 out.flush(); 189 assertEquals("<foo/>", ""+out); 190 } 191 private InputSource getInputSource(String s) { 192 return new InputSource (new StringReader (s)); 193 } 194 195 private ContentHandler getContentHandler(StringWriter w) throws Exception { 196 ToXMLStream xml = new ToXMLStream(); 197 xml.setWriter(w); 198 xml.setOmitXMLDeclaration(true); 199 return xml.asContentHandler(); 200 } 201 209 210 private XMLReader getXMLReader() throws Exception { 211 final XMLReader xr = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); 212 xr.setFeature("http://xml.org/sax/features/namespaces", true); 213 xr.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 214 return xr; 215 } 216 217 public VariableResolverTest(String name){ 218 super(name); 219 } 220 221 protected void setUp() throws Exception { 222 vr = new VariableResolver(); 223 vr.setParent(getXMLReader()); 224 out = new StringWriter (); 225 vr.setContentHandler(getContentHandler(out)); 226 } 227 228 protected void tearDown() { 229 } 230 231 private void nyi(){ 232 fail("Not Yet Implemented"); 233 } 234 235 public static void main(String args[]){ 236 if (args.length == 0){ 237 junit.textui.TestRunner.run(VariableResolverTest.class); 238 } else { 239 junit.textui.TestRunner.run(makeSuite(args)); 240 } 241 } 242 private static TestSuite makeSuite(String args[]){ 243 final TestSuite ts = new TestSuite(); 244 for (int i = 0; i < args.length; i++){ 245 ts.addTest(new VariableResolverTest(args[i])); 246 } 247 return ts; 248 } 249 } 250 | Popular Tags |