1 15 package hivemind.test.config; 16 17 import hivemind.test.config.impl.BooleanHolder; 18 import hivemind.test.config.impl.Child; 19 import hivemind.test.config.impl.Datum; 20 import hivemind.test.config.impl.DatumHolder; 21 import hivemind.test.config.impl.FrobableHolder; 22 import hivemind.test.config.impl.IntHolder; 23 import hivemind.test.config.impl.Parent; 24 import hivemind.test.config.impl.ResourceHolder; 25 26 import java.util.List ; 27 import java.util.Locale ; 28 29 import org.apache.hivemind.ApplicationRuntimeException; 30 import org.apache.hivemind.Element; 31 import org.apache.hivemind.Registry; 32 import org.apache.hivemind.Resource; 33 import org.apache.hivemind.impl.RegistryBuilder; 34 import org.apache.hivemind.impl.XmlModuleReader; 35 import org.apache.hivemind.util.ClasspathResource; 36 import org.apache.hivemind.xml.XmlTestCase; 37 38 43 public class TestConfigurationPoint extends XmlTestCase 44 { 45 46 public void testEmpty() throws Exception 47 { 48 Registry r = buildFrameworkRegistry("Empty.xml"); 49 50 List l = (List ) r.getConfiguration("hivemind.test.config.Empty"); 51 52 assertEquals(0, l.size()); 53 } 54 55 public void testSimple() throws Exception 56 { 57 Registry r = buildFrameworkRegistry("Simple.xml"); 58 59 List l = (List ) r.getConfiguration("hivemind.test.config.Simple"); 60 61 assertEquals(2, l.size()); 62 63 Datum d = (Datum) l.get(0); 64 65 assertEquals("key1", d.getKey()); 66 assertEquals("value1", d.getValue()); 67 assertNotNull(d.getLocation()); 68 69 d = (Datum) l.get(1); 70 71 assertEquals("key2", d.getKey()); 72 assertEquals("value2", d.getValue()); 73 } 74 75 public void testNullElement() throws Exception 76 { 77 Registry r = buildFrameworkRegistry("Null.xml"); 78 79 List l = (List ) r.getConfiguration("hivemind.test.config.Null"); 80 81 assertEquals(1, l.size()); 82 83 assertNull(l.get(0)); 84 } 85 86 public void testAttributeDefaults() throws Exception 87 { 88 Registry r = buildFrameworkRegistry("AttributeDefaults.xml"); 89 90 List l = (List ) r.getConfiguration("hivemind.test.config.AttributeDefaults"); 91 92 assertEquals(1, l.size()); 93 Datum d = (Datum) l.get(0); 94 95 assertEquals("DEFAULT_KEY", d.getKey()); 96 assertNull(d.getValue()); 97 } 98 99 public void testNested() throws Exception 100 { 101 Registry r = buildFrameworkRegistry("Nested.xml"); 102 103 List l = (List ) r.getConfiguration("hivemind.test.config.Nested"); 104 105 assertEquals(1, l.size()); 106 107 DatumHolder h = (DatumHolder) l.get(0); 108 109 assertListsEqual(new Object [] 110 { "fred", "wilma" }, h.getKeys()); 111 112 Datum d = h.getDatum("fred"); 113 assertNotNull(d.getLocation()); 114 assertEquals("barney", d.getValue()); 115 } 116 117 public void testStructured() throws Exception 118 { 119 Registry r = buildFrameworkRegistry("Structured.xml"); 120 121 List l = (List ) r.getConfiguration("hivemind.test.config.Structured"); 122 123 assertEquals(2, l.size()); 124 125 Datum d = (Datum) l.get(0); 126 127 assertEquals("key_1", d.getKey()); 128 assertEquals("value_1", d.getValue()); 129 assertNotNull(d.getLocation()); 130 131 d = (Datum) l.get(1); 132 133 assertEquals("key_2", d.getKey()); 134 assertEquals("value_2", d.getValue()); 135 } 136 137 public void testSetParent() throws Exception 138 { 139 Registry r = buildFrameworkRegistry("SetParent.xml"); 140 141 List l = (List ) r.getConfiguration("hivemind.test.config.SetParent"); 142 143 assertEquals(1, l.size()); 144 145 Parent p1 = (Parent) l.get(0); 146 147 assertEquals("key1", p1.getKey()); 148 assertEquals("value1", p1.getValue()); 149 150 l = p1.getChildren(); 151 assertEquals(2, l.size()); 152 153 Child c1 = (Child) l.get(0); 154 155 assertSame(p1, c1.getParent()); 156 157 assertEquals("detailkey1", c1.getKey()); 158 assertEquals("detailvalue1", c1.getValue()); 159 160 Child c2 = (Child) l.get(1); 161 162 assertSame(p1, c2.getParent()); 163 164 assertEquals("detailkey2", c2.getKey()); 165 assertEquals("detailvalue2", c2.getValue()); 166 } 167 168 public void testBooleanTranslator() throws Exception 169 { 170 interceptLogging(); 171 172 Registry r = buildFrameworkRegistry("BooleanTranslator.xml"); 173 174 List l = (List ) r.getConfiguration("hivemind.test.config.BooleanTranslator"); 175 176 178 l.size(); 179 180 assertLoggedMessagePattern("Unable to process attribute value \\(of element flag\\): " 181 + "'maybe' is not a boolean value \\(which should be either 'true' or 'false'\\)\\."); 182 183 assertEquals(3, l.size()); 184 185 BooleanHolder h = (BooleanHolder) l.get(0); 186 187 assertEquals(true, h.getValue()); 188 189 h = (BooleanHolder) l.get(1); 190 assertEquals(false, h.getValue()); 191 192 h = (BooleanHolder) l.get(2); 193 assertEquals(false, h.getValue()); 194 } 195 196 public void testIntTranslator() throws Exception 197 { 198 interceptLogging(); 199 200 Registry r = buildFrameworkRegistry("IntTranslator.xml"); 201 202 List l = (List ) r.getConfiguration("hivemind.test.config.IntTranslator"); 203 204 206 l.size(); 207 208 List events = getInterceptedLogEvents(); 209 210 assertLoggedMessagePattern("Unable to process attribute value \\(of element int\\): " 211 + "Value 2 is less than minimum value 5\\.", events); 212 assertLoggedMessagePattern("Value 12 is greater than maximum value 10\\.", events); 213 assertLoggedMessagePattern("'fred' is not an integer value\\.", events); 214 215 assertEquals(5, l.size()); 216 217 IntHolder h = (IntHolder) l.get(0); 218 219 assertEquals(7, h.getValue()); 220 221 h = (IntHolder) l.get(1); 222 assertEquals(0, h.getValue()); 223 224 h = (IntHolder) l.get(2); 225 226 assertEquals(0, h.getValue()); 227 228 h = (IntHolder) l.get(3); 229 230 assertEquals(0, h.getValue()); 231 232 h = (IntHolder) l.get(3); 233 234 assertEquals(0, h.getValue()); 235 236 } 237 238 public void testInstanceTranslator() throws Exception 239 { 240 Registry r = buildFrameworkRegistry("InstanceTranslator.xml"); 241 242 List l = (List ) r.getConfiguration("hivemind.test.config.InstanceTranslator"); 243 244 assertEquals(1, l.size()); 245 246 FrobableHolder h = (FrobableHolder) l.get(0); 247 248 Frobable f = h.getFrobable(); 249 250 assertNotNull(f); 251 assertEquals(true, f.frob()); 252 } 253 254 public void testSymbols() throws Exception 255 { 256 interceptLogging(); 257 258 Registry r = buildFrameworkRegistry("Symbols.xml"); 259 260 List l = (List ) r.getConfiguration("hivemind.test.config.Symbols"); 261 262 assertEquals(3, l.size()); 263 264 Datum d = (Datum) l.get(0); 265 266 assertEquals("wife", d.getKey()); 267 assertEquals("wilma", d.getValue()); 268 269 d = (Datum) l.get(1); 270 271 assertEquals("husband", d.getKey()); 272 assertEquals("fred", d.getValue()); 273 274 d = (Datum) l.get(2); 275 276 assertEquals("work", d.getKey()); 277 assertEquals("${work}", d.getValue()); 278 279 assertLoggedMessagePattern("No value available for symbol 'work'"); 280 } 281 282 public void testNoSchema() throws Exception 283 { 284 Registry r = buildFrameworkRegistry("NoSchema.xml"); 285 286 List l = (List ) r.getConfiguration("hivemind.test.config.NoSchema"); 287 288 assertEquals(2, l.size()); 289 290 Element e = (Element) l.get(0); 291 assertEquals("datum", e.getElementName()); 292 assertEquals("key1", e.getAttributeValue("key")); 293 assertEquals("value1", e.getAttributeValue("value")); 294 295 298 e = (Element) l.get(1); 299 assertEquals("datum", e.getElementName()); 300 assertEquals("key2", e.getAttributeValue("key")); 301 assertEquals("${value2}", e.getAttributeValue("value")); 302 } 303 304 public void testLocalized() throws Exception 305 { 306 Registry r = buildFrameworkRegistry("Localized.xml"); 307 308 List l = (List ) r.getConfiguration("hivemind.test.config.Localized"); 309 assertEquals(1, l.size()); 310 311 Datum d = (Datum) l.get(0); 312 313 assertEquals("message", d.getKey()); 314 assertEquals("Some Damn Thing", d.getValue()); 315 } 316 317 public void testElementsProxyList() throws Exception 318 { 319 Registry r = buildFrameworkRegistry("Simple.xml"); 320 321 List l = (List ) r.getConfiguration("hivemind.test.config.Simple"); 322 323 assertEquals("<InnerProxy for hivemind.test.config.Simple(java.util.List)>", l.toString()); 324 325 assertEquals(true, l.equals(l)); 326 assertEquals(false, l.equals(null)); 327 328 assertEquals(2, l.size()); 329 330 List l2 = (List ) r.getConfiguration("hivemind.test.config.Simple"); 331 332 assertNotSame(l, l2); 333 assertEquals(l, l2); 334 335 assertEquals(l2.toString(), l.toString()); 336 } 337 338 public void testTooFew() throws Exception 339 { 340 341 interceptLogging(RegistryBuilder.class.getName()); 342 343 Registry r = buildFrameworkRegistry("TooFew.xml"); 344 345 r.getConfiguration("hivemind.test.config.TooFew"); 346 347 assertLoggedMessage("Configuration point hivemind.test.config.TooFew contains no contributions but expects at least one contribution."); 348 349 } 350 351 public void testTooMany() throws Exception 352 { 353 interceptLogging(RegistryBuilder.class.getName()); 354 355 Registry r = buildFrameworkRegistry("TooMany.xml"); 356 357 r.getConfiguration("hivemind.test.config.TooMany"); 358 359 assertLoggedMessage("Configuration point hivemind.test.config.TooMany contains 2 contributions but expects an optional contribution."); 360 } 361 362 public void testBadAttributes() throws Exception 363 { 364 Registry r = buildFrameworkRegistry("BadAttributes.xml"); 365 366 List l = (List ) r.getConfiguration("hivemind.test.config.BadAttributes"); 367 try 368 { 369 l.size(); 370 371 unreachable(); 372 } 373 catch (ApplicationRuntimeException ex) 374 { 375 Throwable t = findNestedException(ex); 376 assertExceptionSubstring(t, "Element datum (at"); 377 assertExceptionSubstring( 378 t, 379 "Attribute 'xey' is not defined in the schema. Attribute 'key' is required but no value was provided."); 380 } 381 } 382 383 public void testBadElement() throws Exception 384 { 385 Registry r = buildFrameworkRegistry("BadElement.xml"); 386 387 interceptLogging("hivemind.test.config"); 388 389 List l = (List ) r.getConfiguration("hivemind.test.config.BadElement"); 390 391 assertEquals(1, l.size()); 392 393 assertLoggedMessagePattern("Error at .*?: Element xatum is not allowed here\\."); 394 } 395 396 public void testCustomRule() throws Exception 397 { 398 Registry r = buildFrameworkRegistry("CustomRule.xml"); 399 400 List l = (List ) r.getConfiguration("hivemind.test.config.CustomRule"); 401 402 Datum d = (Datum) l.get(0); 403 404 407 assertEquals(2, l.size()); 408 409 assertEquals("hivemind.test.config", d.getContributingModule().getModuleId()); 410 } 411 412 public void testCustomRuleFailure() throws Exception 413 { 414 try 415 { 416 parse("CustomRuleFailure.xml"); 417 418 unreachable(); 419 } 420 catch (ApplicationRuntimeException ex) 421 { 422 assertExceptionSubstring( 423 ex, 424 "Unable to create instance of Rule class hivemind.test.config.XetContributingModuleRule"); 425 } 426 } 427 428 public void testResourceTranslator() throws Exception 429 { 430 RegistryBuilder builder = new RegistryBuilder(); 431 builder.autoDetectModules(); 432 433 Resource moduleResource = new ClasspathResource(_resolver, 434 "/hivemind/test/config/ResourceTranslator.xml"); 435 436 XmlModuleReader reader = new XmlModuleReader(builder.getRegistryDefinition(), 437 _resolver, builder.getErrorHandler()); 438 reader.readModule(moduleResource); 439 440 Registry r = builder.constructRegistry(Locale.FRENCH); 441 442 interceptLogging(); 443 444 List l = (List ) r.getConfiguration("hivemind.test.config.ResourceTranslator"); 445 446 assertEquals(4, l.size()); 447 448 ResourceHolder h = (ResourceHolder) l.get(0); 449 450 assertEquals(moduleResource.getRelativeResource("Empty.xml"), h.getResource()); 451 452 h = (ResourceHolder) l.get(1); 453 454 assertEquals(moduleResource.getRelativeResource("Localized_fr.properties"), h.getResource()); 455 456 h = (ResourceHolder) l.get(2); 457 assertNull(h.getResource()); 458 459 h = (ResourceHolder) l.get(3); 460 assertNull(h.getResource()); 461 462 assertLoggedMessagePattern("Unable to process content of element resource: " 463 + "Unable to localize resource DoesNotExist\\.xml for module hivemind\\.test\\.config\\."); 464 } 465 466 public void testShutdown() throws Exception 467 { 468 Registry r = buildFrameworkRegistry("Simple.xml"); 469 470 List l = (List ) r.getConfiguration("hivemind.test.config.Simple"); 471 472 assertEquals(2, l.size()); 473 474 r.shutdown(); 475 476 try 477 { 478 l.size(); 479 } 480 catch (ApplicationRuntimeException ex) 481 { 482 assertExceptionSubstring(ex, "The HiveMind Registry has been shutdown."); 483 } 484 485 } 486 487 490 491 public void testUnknownContribution() throws Exception 492 { 493 interceptLogging(); 494 495 buildFrameworkRegistry("UnknownContribution.xml"); 496 497 assertLoggedMessagePattern("Module hivemind\\.test\\.config has contributed to unknown configuration point UnresolvedSchema\\. " 498 + "The contribution has been ignored\\."); 499 500 } 501 } | Popular Tags |