KickJava   Java API By Example, From Geeks To Geeks.

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


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.StringReader JavaDoc;
27 import java.io.StringWriter JavaDoc;
28 import javax.xml.parsers.SAXParser JavaDoc;
29 import javax.xml.parsers.SAXParserFactory JavaDoc;
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 JavaDoc;
36 import org.xml.sax.XMLReader JavaDoc;
37 import org.xml.sax.helpers.DefaultHandler JavaDoc;
38 /**
39  *
40  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
41  * @version $Revision: 1.3 $
42  */

43
44 public class VariableExpanderTest extends TestCase {
45       // In this test we ensure that all characters collected so far
46
// are processed before a child element is processed.
47
public void testCharactersComeBeforeChild() throws Exception JavaDoc{
48         final String JavaDoc 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 JavaDoc {
60         VariableExpander ve = new VariableExpander();
61         ve.setFramer(new Framer());
62     }
63     
64                 
65     public void testSetFramer() throws Exception JavaDoc {
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 JavaDoc {
77         constructComplexFrames();
78        final String JavaDoc 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 JavaDoc 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 JavaDoc{
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 JavaDoc{
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       // Use this to construct a set of frames with inheritance
105
// etc. that's useful for testing with.
106
private void constructComplexFrames() {
107         Frame domain, config1, config2, cluster, server1, server2, server3;
108         String JavaDoc d = "d";
109         String JavaDoc dv1 = "dv1";
110         String JavaDoc c1 = "c1";
111         String JavaDoc cv1 = "cv1";
112         String JavaDoc c2 = "c2";
113         String JavaDoc cv2 = "cv2";
114         String JavaDoc cl1 = "cl1";
115         String JavaDoc clv1 = "clv1";
116         String JavaDoc s1 = "s1";
117         String JavaDoc sv1 = "sv1";
118         String JavaDoc s2 = "s2";
119         String JavaDoc sv2 = "sv2";
120         String JavaDoc s3 = "s3";
121         String JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc{
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 JavaDoc {
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 JavaDoc getInputSource(String JavaDoc s) {
188         return new InputSource JavaDoc(new StringReader JavaDoc(s));
189     }
190         
191                  
192     private XMLReader JavaDoc getXMLReader() throws Exception JavaDoc {
193         
194         final XMLReader JavaDoc 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 JavaDoc name){
203         super(name);
204     }
205
206     private FrameHolder frameHolder;
207     private VariableExpander ve;
208     private StringWriter JavaDoc out;
209     
210     
211     protected void setUp() throws Exception JavaDoc {
212         frameHolder = new FrameHolder();
213         out = new StringWriter JavaDoc();
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 JavaDoc 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 JavaDoc 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