KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > system > configuration > ServerOverrideTest


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.geronimo.system.configuration;
18
19 import junit.framework.TestCase;
20 import org.apache.geronimo.gbean.AbstractNameQuery;
21 import org.apache.geronimo.gbean.ReferencePatterns;
22 import org.apache.geronimo.kernel.repository.Artifact;
23 import org.apache.geronimo.kernel.util.XmlUtil;
24 import org.w3c.dom.Document JavaDoc;
25 import org.w3c.dom.Element JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.OutputKeys JavaDoc;
35 import javax.xml.transform.stream.StreamResult JavaDoc;
36 import javax.xml.transform.dom.DOMSource JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.InputStream JavaDoc;
40 import java.util.Arrays JavaDoc;
41 import java.util.Collections JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.LinkedHashSet JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.Set JavaDoc;
46
47 import org.apache.commons.logging.Log;
48 import org.apache.commons.logging.LogFactory;
49
50 /**
51  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
52  */

53 public class ServerOverrideTest extends TestCase {
54     private static final Log log = LogFactory.getLog(ServerOverrideTest.class);
55     
56     public void testBasics() throws Exception JavaDoc {
57         GBeanOverride pizza = new GBeanOverride("Pizza", true);
58         assertTrue(pizza.isLoad());
59
60         pizza.setLoad(false);
61         assertFalse(pizza.isLoad());
62
63         pizza.setAttribute("cheese", "mozzarella");
64         assertEquals("mozzarella", pizza.getAttribute("cheese"));
65
66         AbstractNameQuery pizzaOvenQuery = getAbstractNameQuery(":name=PizzaOven");
67         ReferencePatterns pizzaOvenPatterns = new ReferencePatterns(Collections.singleton(pizzaOvenQuery));
68         pizza.setReferencePatterns("oven", pizzaOvenPatterns);
69         assertEquals(pizzaOvenPatterns, pizza.getReferencePatterns("oven"));
70
71         AbstractNameQuery toasterOvenQuery = getAbstractNameQuery(":name=ToasterOven,*");
72         AbstractNameQuery[] queries = new AbstractNameQuery[]{pizzaOvenQuery, toasterOvenQuery};
73         ReferencePatterns ovenPatterns = getReferencePatterns(queries);
74         pizza.setReferencePatterns("oven", ovenPatterns);
75         assertEquals(ovenPatterns, pizza.getReferencePatterns("oven"));
76
77         ConfigurationOverride dinnerMenu = new ConfigurationOverride(new Artifact("test","Dinner Menu","1.0","car"), true);
78         assertTrue(dinnerMenu.isLoad());
79
80         dinnerMenu.setLoad(false);
81         assertFalse(dinnerMenu.isLoad());
82
83         dinnerMenu.addGBean(pizza);
84         assertSame(pizza, dinnerMenu.getGBean("Pizza"));
85
86         ServerOverride restaurant = new ServerOverride();
87         restaurant.addConfiguration(dinnerMenu);
88         assertSame(dinnerMenu, restaurant.getConfiguration(new Artifact("test","Dinner Menu","1.0","car")));
89     }
90
91     private ReferencePatterns getReferencePatterns(AbstractNameQuery[] queries) {
92         Set JavaDoc querySet = new LinkedHashSet JavaDoc(Arrays.asList(queries));
93         return new ReferencePatterns(querySet);
94     }
95
96     private AbstractNameQuery getAbstractNameQuery(String JavaDoc pizzaOvenString) throws MalformedObjectNameException JavaDoc {
97         ObjectName JavaDoc pizzaOvenPattern = new ObjectName JavaDoc(pizzaOvenString);
98         return new AbstractNameQuery(null, pizzaOvenPattern.getKeyPropertyList(), Collections.EMPTY_SET);
99     }
100
101     public void testGBeanXml() throws Exception JavaDoc {
102         GBeanOverride pizza = new GBeanOverride("Pizza", true);
103         assertCopyIdentical(pizza);
104
105         pizza.setLoad(false);
106         assertCopyIdentical(pizza);
107
108         pizza.setAttribute("cheese", "mozzarella");
109         assertCopyIdentical(pizza);
110
111         pizza.setAttribute("size", "x-large");
112         assertCopyIdentical(pizza);
113
114         AbstractNameQuery pizzaOvenQuery = getAbstractNameQuery(":name=PizzaOven");
115         ReferencePatterns pizzaOvenPatterns = new ReferencePatterns(Collections.singleton(pizzaOvenQuery));
116         pizza.setReferencePatterns("oven", pizzaOvenPatterns);
117         assertCopyIdentical(pizza);
118
119         AbstractNameQuery toasterOvenQuery = getAbstractNameQuery(":name=ToasterOven,*");
120         AbstractNameQuery[] queries = new AbstractNameQuery[]{pizzaOvenQuery, toasterOvenQuery};
121         ReferencePatterns ovenPatterns = getReferencePatterns(queries);
122         pizza.setReferencePatterns("oven", ovenPatterns);
123         assertCopyIdentical(pizza);
124     }
125
126     public void testConfigurationXml() throws Exception JavaDoc {
127         ConfigurationOverride dinnerMenu = new ConfigurationOverride(new Artifact("test","Dinner Menu","1.0","car"), true);
128         assertCopyIdentical(dinnerMenu);
129
130         dinnerMenu.setLoad(false);
131         assertCopyIdentical(dinnerMenu);
132
133         GBeanOverride pizza = new GBeanOverride("Pizza", false);
134         pizza.setAttribute("cheese", "mozzarella");
135         pizza.setAttribute("size", "x-large");
136         pizza.setAttribute("emptyString", "");
137         pizza.setClearAttribute("greenPeppers");
138         pizza.setNullAttribute("pineapple");
139
140         AbstractNameQuery pizzaOvenQuery = getAbstractNameQuery(":name=PizzaOven");
141         AbstractNameQuery toasterOvenQuery = getAbstractNameQuery(":name=ToasterOven,*");
142         AbstractNameQuery[] queries = new AbstractNameQuery[]{pizzaOvenQuery, toasterOvenQuery};
143         ReferencePatterns ovenPatterns = getReferencePatterns(queries);
144         pizza.setReferencePatterns("oven", ovenPatterns);
145         pizza.setClearReference("microwave");
146
147         assertCopyIdentical(dinnerMenu);
148
149         dinnerMenu.addGBean(pizza);
150         assertCopyIdentical(dinnerMenu);
151
152         GBeanOverride garlicCheeseBread = new GBeanOverride("Garlic Cheese Bread", true);
153         ReferencePatterns toasterOvenPatterns = new ReferencePatterns(Collections.singleton(toasterOvenQuery));
154         garlicCheeseBread.setReferencePatterns("oven", toasterOvenPatterns);
155         dinnerMenu.addGBean(garlicCheeseBread);
156         assertCopyIdentical(dinnerMenu);
157     }
158
159     public void testServerXml() throws Exception JavaDoc {
160         ServerOverride restaurant = new ServerOverride();
161         assertCopyIdentical(restaurant);
162
163         ConfigurationOverride dinnerMenu = new ConfigurationOverride(new Artifact("test","Dinner Menu","1.0","car"), false);
164         restaurant.addConfiguration(dinnerMenu);
165         GBeanOverride pizza = new GBeanOverride("Pizza", false);
166         pizza.setAttribute("cheese", "mozzarella");
167         pizza.setAttribute("size", "x-large");
168         pizza.setAttribute("emptyString", "");
169         pizza.setClearAttribute("greenPeppers");
170         pizza.setNullAttribute("pineapple");
171         AbstractNameQuery pizzaOvenQuery = getAbstractNameQuery(":name=PizzaOven");
172         AbstractNameQuery toasterOvenQuery = getAbstractNameQuery(":name=ToasterOven,*");
173         AbstractNameQuery[] queries = new AbstractNameQuery[]{pizzaOvenQuery, toasterOvenQuery};
174         ReferencePatterns ovenPatterns = getReferencePatterns(queries);
175         pizza.setReferencePatterns("oven", ovenPatterns);
176         pizza.setClearReference("microwave");
177         dinnerMenu.addGBean(pizza);
178         GBeanOverride garlicCheeseBread = new GBeanOverride("Garlic Cheese Bread", true);
179         ReferencePatterns toasterOvenPatterns = new ReferencePatterns(Collections.singleton(toasterOvenQuery));
180         garlicCheeseBread.setReferencePatterns("oven", toasterOvenPatterns);
181         dinnerMenu.addGBean(garlicCheeseBread);
182         assertCopyIdentical(restaurant);
183
184         ConfigurationOverride drinkMenu = new ConfigurationOverride(new Artifact("test","Drink Menu","1.0","car"), false);
185         restaurant.addConfiguration(drinkMenu);
186         GBeanOverride beer = new GBeanOverride("Beer", true);
187         pizza.setReferencePatterns("glass", getReferencePatterns(new AbstractNameQuery[] {
188             getAbstractNameQuery(":name=PintGlass"),
189             getAbstractNameQuery(":name=BeerStein"),
190             getAbstractNameQuery(":name=BeerBottle"),
191             getAbstractNameQuery(":name=BeerCan")
192         }));
193         drinkMenu.addGBean(beer);
194         GBeanOverride wine = new GBeanOverride("Wine", true);
195         wine.setReferencePatterns("glass", getReferencePatterns(new AbstractNameQuery[] {
196             getAbstractNameQuery(":name=WineGlass"),
197             getAbstractNameQuery(":name=WineBottle"),
198             getAbstractNameQuery(":name=BoxWine")
199         }));
200         drinkMenu.addGBean(wine);
201         assertCopyIdentical(restaurant);
202     }
203
204     private static final String JavaDoc REFERENCE_XML =
205             " <gbean name=\"EJBBuilder\">\n" +
206                     " <attribute name=\"listener\">?name=JettyWebContainer</attribute>\n" +
207                     " <reference name=\"ServiceBuilders\">\n" +
208                     " <pattern>\n" +
209                     " <name>GBeanBuilder</name>\n" +
210                     " </pattern>\n" +
211                     " <pattern>\n" +
212                     " <name>PersistenceUnitBuilder</name>\n" +
213                     " </pattern>\n" +
214                     " </reference>\n" +
215                     " <reference name=\"WebServiceBuilder\">\n" +
216                     " <pattern>\n" +
217                     " <name>CXFBuilder</name>\n" +
218                     " </pattern>\n" +
219                     " </reference>\n" +
220                     " </gbean>";
221
222     public void testReferenceXml() throws Exception JavaDoc {
223         InputStream JavaDoc in = new ByteArrayInputStream JavaDoc(REFERENCE_XML.getBytes());
224         Element JavaDoc gbeanElement = parseXml(in, "gbean");
225         GBeanOverride gbean = new GBeanOverride(gbeanElement);
226         assertCopyIdentical(gbean);
227     }
228
229     private void assertCopyIdentical(ServerOverride server) throws Exception JavaDoc {
230         ServerOverride copy = copy(server);
231         assertIdentical(server, copy);
232     }
233
234     private void assertCopyIdentical(ConfigurationOverride configuration) throws Exception JavaDoc {
235         ConfigurationOverride copy = copy(configuration);
236         assertIdentical(configuration, copy);
237     }
238
239     private void assertCopyIdentical(GBeanOverride gbean) throws Exception JavaDoc {
240         GBeanOverride copy = copy(gbean);
241         assertIdentical(gbean, copy);
242     }
243
244     private void assertIdentical(ServerOverride expected, ServerOverride actual) {
245         assertNotNull(expected);
246         assertNotNull(actual);
247         assertNotSame(expected, actual);
248
249         Map JavaDoc expectedGBeans = expected.getConfigurations();
250         Map JavaDoc actualGBeans = actual.getConfigurations();
251         assertEquals(expectedGBeans.size(), actualGBeans.size());
252
253         for (Iterator JavaDoc iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();) {
254             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
255             Object JavaDoc name = entry.getKey();
256             ConfigurationOverride expectedConfiguration = (ConfigurationOverride) entry.getValue();
257             ConfigurationOverride actualConfiguration = (ConfigurationOverride) actualGBeans.get(name);
258             assertIdentical(expectedConfiguration, actualConfiguration);
259         }
260     }
261
262     private void assertIdentical(ConfigurationOverride expected, ConfigurationOverride actual) {
263         assertNotNull(expected);
264         assertNotNull(actual);
265         assertNotSame(expected, actual);
266         assertEquals(expected.getName(), actual.getName());
267         assertEquals(expected.isLoad(), actual.isLoad());
268
269         Map JavaDoc expectedGBeans = expected.getGBeans();
270         Map JavaDoc actualGBeans = actual.getGBeans();
271         assertEquals(expectedGBeans.size(), actualGBeans.size());
272
273         for (Iterator JavaDoc iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();) {
274             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
275             Object JavaDoc name = entry.getKey();
276             GBeanOverride expectedGBean = (GBeanOverride) entry.getValue();
277             GBeanOverride actualGBean = (GBeanOverride) actualGBeans.get(name);
278             assertIdentical(expectedGBean, actualGBean);
279         }
280     }
281
282     private void assertIdentical(GBeanOverride expected, GBeanOverride actual) {
283         assertNotNull(expected);
284         assertNotNull(actual);
285         assertNotSame(expected, actual);
286         assertEquals(expected.getName(), actual.getName());
287         assertEquals(expected.isLoad(), actual.isLoad());
288         assertEquals(expected.getAttributes(), actual.getAttributes());
289         assertEquals(expected.getClearAttributes(), actual.getClearAttributes());
290         assertEquals(expected.getNullAttributes(), actual.getNullAttributes());
291         assertEquals(expected.getReferences(), actual.getReferences());
292         assertEquals(expected.getClearReferences(), actual.getClearReferences());
293     }
294
295     private ServerOverride copy(ServerOverride server) throws Exception JavaDoc {
296         Document JavaDoc doc = createDocument();
297         return new ServerOverride(readElement(server.writeXml(doc), "attributes"));
298     }
299
300     private ConfigurationOverride copy(ConfigurationOverride configuration) throws Exception JavaDoc {
301         Document JavaDoc doc = createDocument();
302         Element JavaDoc root = doc.createElement("temp");
303         doc.appendChild(root);
304         return new ConfigurationOverride(readElement(configuration.writeXml(doc, root), "module"));
305     }
306
307     private GBeanOverride copy(GBeanOverride gbean) throws Exception JavaDoc {
308         Document JavaDoc doc = createDocument();
309         Element JavaDoc root = doc.createElement("temp");
310         doc.appendChild(root);
311         return new GBeanOverride(readElement(gbean.writeXml(doc, root), "gbean"));
312     }
313
314     private Element JavaDoc parseXml(InputStream JavaDoc in, String JavaDoc name) throws Exception JavaDoc {
315         DocumentBuilderFactory JavaDoc documentBuilderFactory = XmlUtil.newDocumentBuilderFactory();
316         Document JavaDoc doc = documentBuilderFactory.newDocumentBuilder().parse(in);
317         Element JavaDoc elem = doc.getDocumentElement();
318         if(elem.getNodeName().equals(name)) {
319             return elem;
320         }
321         NodeList JavaDoc list = elem.getElementsByTagName(name);
322         return (Element JavaDoc) list.item(0);
323     }
324
325     private Document JavaDoc createDocument() throws ParserConfigurationException JavaDoc {
326         DocumentBuilderFactory JavaDoc dFactory = XmlUtil.newDocumentBuilderFactory();
327         dFactory.setValidating(false);
328         return dFactory.newDocumentBuilder().newDocument();
329     }
330
331     private Element JavaDoc readElement(Element JavaDoc e, String JavaDoc name) throws Exception JavaDoc {
332         ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
333         TransformerFactory JavaDoc xfactory = XmlUtil.newTransformerFactory();
334         Transformer JavaDoc xform = xfactory.newTransformer();
335         xform.setOutputProperty(OutputKeys.INDENT, "yes");
336         xform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
337         xform.transform(new DOMSource JavaDoc(e), new StreamResult JavaDoc(out));
338         log.debug(new String JavaDoc(out.toByteArray()));
339         ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(out.toByteArray());
340         return parseXml(in, name);
341     }
342 }
343
Popular Tags