1 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 ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.NodeList ; 27 28 import javax.management.MalformedObjectNameException ; 29 import javax.management.ObjectName ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.OutputKeys ; 35 import javax.xml.transform.stream.StreamResult ; 36 import javax.xml.transform.dom.DOMSource ; 37 import java.io.ByteArrayInputStream ; 38 import java.io.ByteArrayOutputStream ; 39 import java.io.InputStream ; 40 import java.util.Arrays ; 41 import java.util.Collections ; 42 import java.util.Iterator ; 43 import java.util.LinkedHashSet ; 44 import java.util.Map ; 45 import java.util.Set ; 46 47 import org.apache.commons.logging.Log; 48 import org.apache.commons.logging.LogFactory; 49 50 53 public class ServerOverrideTest extends TestCase { 54 private static final Log log = LogFactory.getLog(ServerOverrideTest.class); 55 56 public void testBasics() throws Exception { 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 querySet = new LinkedHashSet (Arrays.asList(queries)); 93 return new ReferencePatterns(querySet); 94 } 95 96 private AbstractNameQuery getAbstractNameQuery(String pizzaOvenString) throws MalformedObjectNameException { 97 ObjectName pizzaOvenPattern = new ObjectName (pizzaOvenString); 98 return new AbstractNameQuery(null, pizzaOvenPattern.getKeyPropertyList(), Collections.EMPTY_SET); 99 } 100 101 public void testGBeanXml() throws Exception { 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 { 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 { 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 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 { 223 InputStream in = new ByteArrayInputStream (REFERENCE_XML.getBytes()); 224 Element gbeanElement = parseXml(in, "gbean"); 225 GBeanOverride gbean = new GBeanOverride(gbeanElement); 226 assertCopyIdentical(gbean); 227 } 228 229 private void assertCopyIdentical(ServerOverride server) throws Exception { 230 ServerOverride copy = copy(server); 231 assertIdentical(server, copy); 232 } 233 234 private void assertCopyIdentical(ConfigurationOverride configuration) throws Exception { 235 ConfigurationOverride copy = copy(configuration); 236 assertIdentical(configuration, copy); 237 } 238 239 private void assertCopyIdentical(GBeanOverride gbean) throws Exception { 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 expectedGBeans = expected.getConfigurations(); 250 Map actualGBeans = actual.getConfigurations(); 251 assertEquals(expectedGBeans.size(), actualGBeans.size()); 252 253 for (Iterator iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();) { 254 Map.Entry entry = (Map.Entry ) iterator.next(); 255 Object 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 expectedGBeans = expected.getGBeans(); 270 Map actualGBeans = actual.getGBeans(); 271 assertEquals(expectedGBeans.size(), actualGBeans.size()); 272 273 for (Iterator iterator = expectedGBeans.entrySet().iterator(); iterator.hasNext();) { 274 Map.Entry entry = (Map.Entry ) iterator.next(); 275 Object 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 { 296 Document doc = createDocument(); 297 return new ServerOverride(readElement(server.writeXml(doc), "attributes")); 298 } 299 300 private ConfigurationOverride copy(ConfigurationOverride configuration) throws Exception { 301 Document doc = createDocument(); 302 Element 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 { 308 Document doc = createDocument(); 309 Element root = doc.createElement("temp"); 310 doc.appendChild(root); 311 return new GBeanOverride(readElement(gbean.writeXml(doc, root), "gbean")); 312 } 313 314 private Element parseXml(InputStream in, String name) throws Exception { 315 DocumentBuilderFactory documentBuilderFactory = XmlUtil.newDocumentBuilderFactory(); 316 Document doc = documentBuilderFactory.newDocumentBuilder().parse(in); 317 Element elem = doc.getDocumentElement(); 318 if(elem.getNodeName().equals(name)) { 319 return elem; 320 } 321 NodeList list = elem.getElementsByTagName(name); 322 return (Element ) list.item(0); 323 } 324 325 private Document createDocument() throws ParserConfigurationException { 326 DocumentBuilderFactory dFactory = XmlUtil.newDocumentBuilderFactory(); 327 dFactory.setValidating(false); 328 return dFactory.newDocumentBuilder().newDocument(); 329 } 330 331 private Element readElement(Element e, String name) throws Exception { 332 ByteArrayOutputStream out = new ByteArrayOutputStream (); 333 TransformerFactory xfactory = XmlUtil.newTransformerFactory(); 334 Transformer 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 (e), new StreamResult (out)); 338 log.debug(new String (out.toByteArray())); 339 ByteArrayInputStream in = new ByteArrayInputStream (out.toByteArray()); 340 return parseXml(in, name); 341 } 342 } 343 | Popular Tags |