KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > config > serverbeans > validation > VariableResolverTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.config.serverbeans.validation;
25
26 import java.io.File JavaDoc;
27 import java.io.StringReader JavaDoc;
28 import java.io.StringWriter JavaDoc;
29 import java.net.MalformedURLException JavaDoc;
30 import java.net.URI JavaDoc;
31 import javax.xml.parsers.SAXParserFactory JavaDoc;
32 import junit.framework.*;
33 import org.xml.sax.ContentHandler JavaDoc;
34 import org.xml.sax.InputSource JavaDoc;
35 import org.xml.sax.XMLReader JavaDoc;
36 import com.sun.org.apache.xml.internal.serializer.ToXMLStream;
37 /**
38  *
39  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
40  * @version $Revision: 1.3 $
41  */

42
43 public class VariableResolverTest extends TestCase {
44     VariableResolver vr;
45     StringWriter JavaDoc out;
46
47     public void testXMLSerialization() throws Exception JavaDoc {
48         StringWriter JavaDoc w = new StringWriter JavaDoc();
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 JavaDoc{
59         vr.parse(new InputSource JavaDoc("test.xml"));
60         
61         String JavaDoc 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 JavaDoc{
83         final String JavaDoc uri = new File JavaDoc ("test.xml").toURI().toURL().toString();
84         vr.parse(new InputSource JavaDoc(uri));
85         
86         String JavaDoc 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       // test out the use of a default XML Reader
110
public void testUseOfDefaultReader() throws Exception JavaDoc {
111         VariableResolver myVr = new VariableResolver();
112         myVr.setContentHandler(getContentHandler(out));
113         String JavaDoc input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>";
114         String JavaDoc expected = "<foo att=\"value\"><system-property name=\"var\" value=\"value\"/></foo>";
115         myVr.parse(getInputSource(input));
116         assertEquals(expected, ""+out);
117     }
118     
119       // here we test that variable references are appropriate to
120
// their scope.
121
public void testAComplexExample() throws Exception JavaDoc {
122         String JavaDoc 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 JavaDoc 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 JavaDoc {
153         String JavaDoc 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 JavaDoc 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 JavaDoc {
173         String JavaDoc input = "<foo att=\"${var}\"><system-property name=\"var\" value=\"value\"/></foo>";
174         String JavaDoc 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 JavaDoc {
180         String JavaDoc 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 JavaDoc {
186         String JavaDoc doc = "<foo/>";
187         vr.parse(getInputSource(doc));
188         out.flush();
189         assertEquals("<foo/>", ""+out);
190     }
191     private InputSource JavaDoc getInputSource(String JavaDoc s) {
192         return new InputSource JavaDoc(new StringReader JavaDoc(s));
193     }
194
195     private ContentHandler JavaDoc getContentHandler(StringWriter JavaDoc w) throws Exception JavaDoc{
196         ToXMLStream xml = new ToXMLStream();
197         xml.setWriter(w);
198         xml.setOmitXMLDeclaration(true);
199         return xml.asContentHandler();
200     }
201 // private ContentHandler getContentHandler(StringWriter w) throws Exception{
202
// OutputFormat of = new OutputFormat();
203
// of.setOmitXMLDeclaration(true);
204
// Serializer ts = new XMLSerializer(of);
205
// ts.setOutputCharStream(w);
206
// return ts.asContentHandler();
207
// }
208

209         
210     private XMLReader JavaDoc getXMLReader() throws Exception JavaDoc {
211         final XMLReader JavaDoc 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 JavaDoc name){
218         super(name);
219     }
220
221     protected void setUp() throws Exception JavaDoc {
222         vr = new VariableResolver();
223         vr.setParent(getXMLReader());
224         out = new StringWriter JavaDoc();
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 JavaDoc 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 JavaDoc 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