KickJava   Java API By Example, From Geeks To Geeks.

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


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 junit.framework.*;
28 import org.xml.sax.Attributes JavaDoc;
29 import org.xml.sax.InputSource JavaDoc;
30 import org.xml.sax.XMLReader JavaDoc;
31 import org.xml.sax.helpers.AttributesImpl JavaDoc;
32 /**
33  *
34  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
35  * @version $Revision: 1.3 $
36  */

37
38 public class FrameHolderBuilderTest extends TestCase {
39
40       // In this test we'll have 1 domain, 2 configs (config1,
41
// config2), 1 cluster and three servers (s1, s2, s3). The
42
// inheritance paths from the tip to the root will be:
43
// sf1 config1 domain
44
// sf2 cluster config1 domain
45
// sf3 config2 domain
46
// Each entity will have a single key/value pair, being its own name.
47
public void testComplexInheritanceHierarchy() throws Exception JavaDoc {
48         final String JavaDoc domain = "domain";
49         final String JavaDoc config1 = "config1";
50         final String JavaDoc config2 = "config2";
51         final String JavaDoc cluster = "cluster";
52         final String JavaDoc s1 = "s1";
53         final String JavaDoc s2 = "s2";
54         final String JavaDoc s3 = "s3";
55
56         FrameHolder expected = new FrameHolder();
57           // First, the definitions
58
final Frame df = expected.getDomainFrame().put(domain, domain);
59         final Frame cf1 = expected.getConfigFrame(config1).put(config1, config1);
60         final Frame cf2 = expected.getConfigFrame(config2).put(config2, config2);
61         final Frame cl = expected.getClusterFrame(cluster).put(cluster, cluster);
62         final Frame sf1 = expected.getServerFrame(s1).put(s1, s1);
63         final Frame sf2 = expected.getServerFrame(s2).put(s2, s2);
64         final Frame sf3 = expected.getServerFrame(s3).put(s3, s3);
65           // Then the connections, in rank order
66
//Rank 1
67
cf1.inheritFrom(df);
68         cf2.inheritFrom(df);
69           //Rank 2
70
sf1.inheritFrom(cf1);
71         cl.inheritFrom(cf1);
72         sf3.inheritFrom(cf2);
73           //Rank 3
74
sf2.inheritFrom(cl);
75
76         final String JavaDoc input = "" +
77         "<doc>"+
78         " <config name='config1'>" +
79         " <system-property name='config1' value='config1'/>" +
80         " </config>" +
81         " <config name='config2'>" +
82         " <system-property name='config2' value='config2'/>" +
83         " </config>" +
84         " <server name='s1' config-ref='config1'>" +
85         " <system-property name='s1' value='s1'/>"+
86         " </server>" +
87         " <server name='s2'>" +
88         " <system-property name='s2' value='s2'/>" +
89         " </server>" +
90         " <server name='s3' config-ref='config2'>" +
91         " <system-property name='s3' value='s3'/>" +
92         " </server>" +
93         " <cluster name='cluster' config-ref='config1'>" +
94         " <server-ref name='s2'/>" +
95         " <system-property name='cluster' value='cluster'/>" +
96         " </cluster>" +
97         " <system-property name='domain' value='domain'/>" +
98         "</doc>"
99         ;
100
101         XMLReader JavaDoc r = XMLReaderFactory.newInstance(null);
102         r.setContentHandler(fb);
103         r.parse(new InputSource JavaDoc(new StringReader JavaDoc(input)));
104         
105         assertEquals(df, fb.getFrameHolder().getDomainFrame());
106         assertEquals(cf1, fb.getFrameHolder().getConfigFrame(config1));
107         assertEquals(cf2, fb.getFrameHolder().getConfigFrame(config2));
108         assertEquals(sf1, fb.getFrameHolder().getServerFrame(s1));
109         assertEquals(sf2, fb.getFrameHolder().getServerFrame(s2));
110         assertEquals(sf3, fb.getFrameHolder().getServerFrame(s3));
111         assertEquals(cl, fb.getFrameHolder().getClusterFrame(cluster));
112         assertEquals(expected, fb.getFrameHolder());
113     }
114         
115     public void testClusterPropertyEventHandling() throws Exception JavaDoc {
116         final String JavaDoc global = "global";
117         final String JavaDoc initialValue = "initialValue";
118         final String JavaDoc cluster1 = "cluster1";
119         final String JavaDoc myName = "myName";
120         final String JavaDoc myValue = "myValue";
121         final String JavaDoc overridden = "overridden";
122
123         FrameHolder expected = new FrameHolder();
124         expected.getDomainFrame().put(global, initialValue);
125         expected.getClusterFrame(cluster1).put(myName, myValue);
126         expected.getClusterFrame(cluster1).put(global, overridden);
127         fb.startDocument();
128         fireClusterStart(fb, cluster1, null, null);
129         fireSystemPropertyEvents(fb, myName, myValue);
130         fireSystemPropertyEvents(fb, global, overridden);
131         fb.endElement("", FrameHolderBuilder.CLUSTER, "");
132         fireSystemPropertyEvents(fb, global, initialValue);
133
134         assertEquals(myValue, expected.getClusterFrame(cluster1).lookup(myName));
135         assertEquals(initialValue, expected.getDomainFrame().lookup(global));
136         assertEquals(initialValue, fb.getFrameHolder().getDomainFrame().lookup(global));
137         assertEquals(expected.getDomainFrame(), fb.getFrameHolder().getDomainFrame());
138         assertEquals(expected.getClusterFrame(cluster1), fb.getFrameHolder().getClusterFrame(cluster1));
139         assertEquals(expected, fb.getFrameHolder());
140
141     }
142         
143     public void testServerInheritingFromConfig() throws Exception JavaDoc {
144         final String JavaDoc k1 = "k1";
145         final String JavaDoc v1 = "v1";
146         final String JavaDoc k2 = "k2";
147         final String JavaDoc v2 = "v2";
148         final String JavaDoc k3 = "k3";
149         final String JavaDoc v3 = "v3";
150         final String JavaDoc k4 = "k4";
151         final String JavaDoc v4 = "v4";
152         final String JavaDoc config = "config";
153         final String JavaDoc config1 = "config1";
154         final String JavaDoc server = "server";
155         final String JavaDoc server1 = "server1";
156
157         final String JavaDoc input = "" +
158         "<doc>" +
159         " <config name='config'>"+
160         " <system-property name='k2' value='v2'/>"+
161         " </config>"+
162         " <server name='server' config-ref='config'>"+
163         " <system-property name='k3 value='k3'/>"+
164         " </server>"+
165         " <system-property name='k1' value='v1'/>"+
166         " <config name='config1'/>"+
167         "</doc>"
168 ;
169         
170         
171         FrameHolder expected = new FrameHolder();
172         expected.getDomainFrame().put(k1, v1);
173         expected.getConfigFrame(config).put(k2, v2);
174         expected.getConfigFrame(config).inheritFrom(expected.getDomainFrame());
175         expected.getConfigFrame(config1).inheritFrom(expected.getDomainFrame());
176         expected.getServerFrame(server).put(k3, v3);
177         expected.getServerFrame(server).inheritFrom(expected.getConfigFrame(config));
178         expected.getServerFrame(server1).put(k4, v4);
179         expected.getServerFrame(server1).inheritFrom(expected.getConfigFrame(config));
180
181         fb.startDocument();
182         fireConfigStart(fb, config);
183         fireSystemPropertyEvents(fb, k2, v2);
184         fb.endElement("", FrameHolderBuilder.CONFIG, "");
185         fireServerStart(fb, server, config);
186         fireSystemPropertyEvents(fb, k3, v3);
187         fb.endElement("", FrameHolderBuilder.SERVER, "");
188         fireSystemPropertyEvents(fb, k1, v1);
189         fireConfigStart(fb, config1);
190         fb.endElement("", FrameHolderBuilder.CONFIG, "");
191         fireServerStart(fb, server1, config);
192         fireSystemPropertyEvents(fb, k4, v4);
193         fb.endElement("", FrameHolderBuilder.SERVER, "");
194         
195         assertEquals(expected.getDomainFrame(), fb.getFrameHolder().getDomainFrame());
196         assertEquals(expected.getConfigFrame(config), fb.getFrameHolder().getConfigFrame(config));
197         assertEquals(expected.getServerFrame(server), fb.getFrameHolder().getServerFrame(server));
198         assertEquals(expected, fb.getFrameHolder());
199     }
200     
201         
202
203         
204         
205     public void testServerSystemPropertyEventHandling() throws Exception JavaDoc {
206         final String JavaDoc global = "global";
207         final String JavaDoc initialValue = "initialValue";
208         final String JavaDoc server1 = "server1";
209         final String JavaDoc myName = "myName";
210         final String JavaDoc myValue = "myValue";
211         final String JavaDoc overridden = "overridden";
212         final String JavaDoc input = ""+
213         "<doc>" +
214         " <server name='server1'>"+
215         " <system-property name='myName' value='myValue'/>"+
216         " <system-property name='global' value='overridden'/>" +
217         " </server>"+
218         " <system-property name='global' value='initialValue'/>" +
219         "</doc>"
220         ;
221         
222         FrameHolder expected = new FrameHolder();
223         expected.getDomainFrame().put(global, initialValue);
224         expected.getServerFrame(server1).put(myName, myValue);
225         expected.getServerFrame(server1).put(global, overridden);
226
227         XMLReader JavaDoc r = XMLReaderFactory.newInstance(null);
228         r.setContentHandler(fb);
229         r.parse(new InputSource JavaDoc(new StringReader JavaDoc(input)));
230         
231         assertEquals(myValue, expected.getServerFrame(server1).lookup(myName));
232         assertEquals(initialValue, expected.getDomainFrame().lookup(global));
233         assertEquals(initialValue, fb.getFrameHolder().getDomainFrame().lookup(global));
234         assertEquals(expected.getDomainFrame(), fb.getFrameHolder().getDomainFrame());
235         assertEquals(expected.getServerFrame(server1), fb.getFrameHolder().getServerFrame(server1));
236         assertEquals(expected, fb.getFrameHolder());
237
238     }
239   
240     public void testConfigSystemPropertyEventHandling() throws Exception JavaDoc {
241         final String JavaDoc global = "global";
242         final String JavaDoc initialValue = "initialValue";
243         final String JavaDoc config1 = "config1";
244         final String JavaDoc myName = "myName";
245         final String JavaDoc myValue = "myValue";
246         final String JavaDoc overridden = "overridden";
247
248         final String JavaDoc input = "" +
249         "<doc>" +
250         " <config name='config1'>"+
251         " <system-property name='myName' value='myValue'/>"+
252         " <system-property name='global' value='overridden'/>" +
253         " </config>" +
254         " <system-property name='global' value='initialValue'/>"+
255         "</doc>"
256         ;
257         
258         FrameHolder expected = new FrameHolder();
259         expected.getDomainFrame().put(global, initialValue);
260         expected.getConfigFrame(config1).put(myName, myValue);
261         expected.getConfigFrame(config1).put(global, overridden);
262         expected.getConfigFrame(config1).inheritFrom(expected.getDomainFrame());
263
264         XMLReader JavaDoc r = XMLReaderFactory.newInstance(null);
265         r.setContentHandler(fb);
266         r.parse(new InputSource JavaDoc(new StringReader JavaDoc(input)));
267         
268         assertEquals(myValue, expected.getConfigFrame(config1).lookup(myName));
269         assertEquals(initialValue, expected.getDomainFrame().lookup(global));
270         assertEquals(initialValue, fb.getFrameHolder().getDomainFrame().lookup(global));
271         assertEquals(expected.getDomainFrame(), fb.getFrameHolder().getDomainFrame());
272         assertEquals(expected.getConfigFrame(config1), fb.getFrameHolder().getConfigFrame(config1));
273         assertEquals(expected, fb.getFrameHolder());
274     }
275
276     private void fireClusterStart(FrameHolderBuilder fb, String JavaDoc name, String JavaDoc configRef, String JavaDoc serverRef) throws Exception JavaDoc {
277         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
278         atts.addAttribute("", FrameHolderBuilder.NAME, "", "", name);
279         if (configRef != null){
280             atts.addAttribute("", FrameHolderBuilder.CONFIG_REF, "", "", configRef);
281         }
282         if (serverRef != null){
283             atts.addAttribute("", FrameHolderBuilder.SERVER_REF, "", "", serverRef);
284         }
285         fb.startElement("", FrameHolderBuilder.CLUSTER, "", atts);
286     }
287     private void fireServerStart(FrameHolderBuilder fb, String JavaDoc name, String JavaDoc configRef) throws Exception JavaDoc {
288         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
289         atts.addAttribute("", FrameHolderBuilder.NAME, "", "", name);
290         if (configRef != null){
291             atts.addAttribute("", FrameHolderBuilder.CONFIG_REF, "", "", configRef);
292         }
293         
294         fb.startElement("", FrameHolderBuilder.SERVER, "", atts);
295     }
296
297     private void fireConfigStart(FrameHolderBuilder fb, String JavaDoc name) throws Exception JavaDoc {
298         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
299         atts.addAttribute("", FrameHolderBuilder.NAME, "", "", name);
300         fb.startElement("", FrameHolderBuilder.CONFIG, "", atts);
301     }
302
303         
304     public void testBasicSystemPropertyEventHandling() throws Exception JavaDoc {
305         fb.startDocument();
306         fireSystemPropertyEvents(fb, "prop1", "val1");
307         FrameHolder expected = new FrameHolder();
308         expected.getDomainFrame().put("prop1", "val1");
309         assertEquals(expected, fb.getFrameHolder());
310         fireSystemPropertyEvents(fb, "prop2", "val2");
311         assertFalse(expected.equals(fb.getFrameHolder()));
312         expected.getDomainFrame().put("prop2", "val2");
313         assertEquals(expected, fb.getFrameHolder());
314     }
315
316     private void fireSystemPropertyEvents(FrameHolderBuilder fb, String JavaDoc name, String JavaDoc value) throws Exception JavaDoc {
317         AttributesImpl JavaDoc atts = new AttributesImpl JavaDoc();
318         atts.addAttribute("", FrameHolderBuilder.NAME, "", "", name);
319         atts.addAttribute("", FrameHolderBuilder.VALUE, "", "", value);
320         fb.startElement("", FrameHolderBuilder.SYSTEM_PROPERTY, "", atts);
321         fb.endElement("", FrameHolderBuilder.SYSTEM_PROPERTY, "");
322     }
323
324         
325     public void testStartEndDocumentEventHandling() throws Exception JavaDoc {
326         fb.startDocument();
327         fb.endDocument();
328         assertEquals(new FrameHolder(), fb.getFrameHolder());
329     }
330
331     public FrameHolderBuilderTest(String JavaDoc name){
332         super(name);
333     }
334
335     private FrameHolderBuilder fb;
336     
337     protected void setUp() {
338         fb = new FrameHolderBuilder();
339     }
340
341     protected void tearDown() {
342     }
343
344     private void nyi(){
345         fail("Not Yet Implemented");
346     }
347
348     public static void main(String JavaDoc args[]){
349         if (args.length == 0){
350             junit.textui.TestRunner.run(FrameHolderBuilderTest.class);
351         } else {
352             junit.textui.TestRunner.run(makeSuite(args));
353         }
354     }
355     private static TestSuite makeSuite(String JavaDoc args[]){
356         final TestSuite ts = new TestSuite();
357         for (int i = 0; i < args.length; i++){
358             ts.addTest(new FrameHolderBuilderTest(args[i]));
359         }
360         return ts;
361     }
362 }
363
Popular Tags