1 23 24 package com.sun.enterprise.config.serverbeans.validation; 25 26 import java.io.StringReader ; 27 import junit.framework.*; 28 import org.xml.sax.Attributes ; 29 import org.xml.sax.InputSource ; 30 import org.xml.sax.XMLReader ; 31 import org.xml.sax.helpers.AttributesImpl ; 32 37 38 public class FrameHolderBuilderTest extends TestCase { 39 40 public void testComplexInheritanceHierarchy() throws Exception { 48 final String domain = "domain"; 49 final String config1 = "config1"; 50 final String config2 = "config2"; 51 final String cluster = "cluster"; 52 final String s1 = "s1"; 53 final String s2 = "s2"; 54 final String s3 = "s3"; 55 56 FrameHolder expected = new FrameHolder(); 57 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 cf1.inheritFrom(df); 68 cf2.inheritFrom(df); 69 sf1.inheritFrom(cf1); 71 cl.inheritFrom(cf1); 72 sf3.inheritFrom(cf2); 73 sf2.inheritFrom(cl); 75 76 final String 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 r = XMLReaderFactory.newInstance(null); 102 r.setContentHandler(fb); 103 r.parse(new InputSource (new StringReader (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 { 116 final String global = "global"; 117 final String initialValue = "initialValue"; 118 final String cluster1 = "cluster1"; 119 final String myName = "myName"; 120 final String myValue = "myValue"; 121 final String 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 { 144 final String k1 = "k1"; 145 final String v1 = "v1"; 146 final String k2 = "k2"; 147 final String v2 = "v2"; 148 final String k3 = "k3"; 149 final String v3 = "v3"; 150 final String k4 = "k4"; 151 final String v4 = "v4"; 152 final String config = "config"; 153 final String config1 = "config1"; 154 final String server = "server"; 155 final String server1 = "server1"; 156 157 final String 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 { 206 final String global = "global"; 207 final String initialValue = "initialValue"; 208 final String server1 = "server1"; 209 final String myName = "myName"; 210 final String myValue = "myValue"; 211 final String overridden = "overridden"; 212 final String 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 r = XMLReaderFactory.newInstance(null); 228 r.setContentHandler(fb); 229 r.parse(new InputSource (new StringReader (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 { 241 final String global = "global"; 242 final String initialValue = "initialValue"; 243 final String config1 = "config1"; 244 final String myName = "myName"; 245 final String myValue = "myValue"; 246 final String overridden = "overridden"; 247 248 final String 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 r = XMLReaderFactory.newInstance(null); 265 r.setContentHandler(fb); 266 r.parse(new InputSource (new StringReader (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 name, String configRef, String serverRef) throws Exception { 277 AttributesImpl atts = new AttributesImpl (); 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 name, String configRef) throws Exception { 288 AttributesImpl atts = new AttributesImpl (); 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 name) throws Exception { 298 AttributesImpl atts = new AttributesImpl (); 299 atts.addAttribute("", FrameHolderBuilder.NAME, "", "", name); 300 fb.startElement("", FrameHolderBuilder.CONFIG, "", atts); 301 } 302 303 304 public void testBasicSystemPropertyEventHandling() throws Exception { 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 name, String value) throws Exception { 317 AttributesImpl atts = new AttributesImpl (); 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 { 326 fb.startDocument(); 327 fb.endDocument(); 328 assertEquals(new FrameHolder(), fb.getFrameHolder()); 329 } 330 331 public FrameHolderBuilderTest(String 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 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 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 |