1 15 package org.apache.hivemind.test; 16 17 import java.net.URL ; 18 import java.util.ArrayList ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Locale ; 22 23 import junit.framework.AssertionFailedError; 24 import junit.framework.TestCase; 25 26 import org.apache.hivemind.ApplicationRuntimeException; 27 import org.apache.hivemind.ClassResolver; 28 import org.apache.hivemind.Location; 29 import org.apache.hivemind.Registry; 30 import org.apache.hivemind.Resource; 31 import org.apache.hivemind.definition.ModuleDefinition; 32 import org.apache.hivemind.definition.RegistryDefinition; 33 import org.apache.hivemind.definition.impl.RegistryDefinitionImpl; 34 import org.apache.hivemind.impl.DefaultClassResolver; 35 import org.apache.hivemind.impl.LocationImpl; 36 import org.apache.hivemind.impl.RegistryBuilder; 37 import org.apache.hivemind.internal.ser.ServiceSerializationHelper; 38 import org.apache.hivemind.util.ClasspathResource; 39 import org.apache.hivemind.util.PropertyUtils; 40 import org.apache.hivemind.util.URLResource; 41 import org.apache.log4j.Level; 42 import org.apache.log4j.LogManager; 43 import org.apache.log4j.Logger; 44 import org.apache.log4j.spi.LoggingEvent; 45 import org.apache.oro.text.regex.Pattern; 46 import org.apache.oro.text.regex.Perl5Compiler; 47 import org.apache.oro.text.regex.Perl5Matcher; 48 import org.easymock.MockControl; 49 import org.easymock.classextension.MockClassControl; 50 51 57 public abstract class HiveMindTestCase extends TestCase 58 { 59 61 64 65 private ClassResolver _classResolver; 66 67 protected String _interceptedLoggerName; 68 69 protected StoreAppender _appender; 70 71 private static Perl5Compiler _compiler; 72 73 private static Perl5Matcher _matcher; 74 75 76 77 private List _controls = new ArrayList (); 78 79 80 interface MockControlFactory 81 { 82 public MockControl newControl(Class mockClass); 83 } 84 85 86 private static class InterfaceMockControlFactory implements MockControlFactory 87 { 88 public MockControl newControl(Class mockClass) 89 { 90 return MockControl.createStrictControl(mockClass); 91 } 92 } 93 94 95 private static class ClassMockControlFactory implements MockControlFactory 96 { 97 public MockControl newControl(Class mockClass) 98 { 99 return MockClassControl.createStrictControl(mockClass); 100 } 101 } 102 103 104 static class PlaceholderClassMockControlFactory implements MockControlFactory 105 { 106 public MockControl newControl(Class mockClass) 107 { 108 throw new RuntimeException ( 109 "Unable to instantiate EasyMock control for " 110 + mockClass 111 + "; ensure that easymockclassextension-1.1.jar and cglib-full-2.0.1.jar are on the classpath."); 112 } 113 } 114 115 116 private static final MockControlFactory _interfaceMockControlFactory = new InterfaceMockControlFactory(); 117 118 119 private static MockControlFactory _classMockControlFactory; 120 121 static 122 { 123 try 124 { 125 _classMockControlFactory = new ClassMockControlFactory(); 126 } 127 catch (NoClassDefFoundError ex) 128 { 129 _classMockControlFactory = new PlaceholderClassMockControlFactory(); 130 } 131 } 132 133 137 protected Resource getResource(String file) 138 { 139 URL url = getClass().getResource(file); 140 141 if (url == null) 142 throw new NullPointerException ("No resource named '" + file + "'."); 143 144 return new URLResource(url); 145 } 146 147 151 protected static void assertListsEqual(Object [] expected, List actual) 152 { 153 assertListsEqual(expected, actual.toArray()); 154 } 155 156 160 protected static void assertListsEqual(Object [] expected, Object [] actual) 161 { 162 assertNotNull(actual); 163 164 int min = Math.min(expected.length, actual.length); 165 166 for (int i = 0; i < min; i++) 167 assertEquals("list[" + i + "]", expected[i], actual[i]); 168 169 assertEquals("list length", expected.length, actual.length); 170 } 171 172 176 protected static void unreachable() 177 { 178 throw new AssertionFailedError("This code should be unreachable."); 179 } 180 181 185 protected void interceptLogging(String loggerName) 186 { 187 Logger logger = LogManager.getLogger(loggerName); 188 189 logger.removeAllAppenders(); 190 191 _interceptedLoggerName = loggerName; 192 _appender = new StoreAppender(); 193 _appender.activateOptions(); 194 195 logger.setLevel(Level.DEBUG); 196 logger.setAdditivity(false); 197 logger.addAppender(_appender); 198 } 199 200 206 207 protected List getInterceptedLogEvents() 208 { 209 return _appender.getEvents(); 210 } 211 212 216 protected void tearDown() throws Exception 217 { 218 super.tearDown(); 219 220 if (_appender != null) 221 { 222 _appender = null; 223 224 Logger logger = LogManager.getLogger(_interceptedLoggerName); 225 logger.setLevel(null); 226 logger.setAdditivity(true); 227 logger.removeAllAppenders(); 228 } 229 230 PropertyUtils.clearCache(); 231 232 ServiceSerializationHelper.setServiceSerializationSupport(null); 233 } 234 235 238 protected void assertExceptionSubstring(Throwable ex, String substring) 239 { 240 String message = ex.getMessage(); 241 assertNotNull(message); 242 243 int pos = message.indexOf(substring); 244 245 if (pos < 0) 246 throw new AssertionFailedError("Exception message (" + message + ") does not contain [" 247 + substring + "]"); 248 } 249 250 253 254 protected void assertExceptionRegexp(Throwable ex, String pattern) throws Exception 255 { 256 String message = ex.getMessage(); 257 assertNotNull(message); 258 259 setupMatcher(); 260 261 Pattern compiled = _compiler.compile(pattern); 262 263 if (_matcher.contains(message, compiled)) 264 return; 265 266 throw new AssertionFailedError("Exception message (" + message 267 + ") does not contain regular expression [" + pattern + "]."); 268 } 269 270 protected void assertRegexp(String pattern, String actual) throws Exception 271 { 272 setupMatcher(); 273 274 Pattern compiled = _compiler.compile(pattern); 275 276 if (_matcher.contains(actual, compiled)) 277 return; 278 279 throw new AssertionFailedError("\"" + actual + "\" does not contain regular expression[" 280 + pattern + "]."); 281 } 282 283 287 protected Throwable findNestedException(ApplicationRuntimeException ex) 288 { 289 Throwable cause = ex.getRootCause(); 290 291 if (cause == null || cause == ex) 292 return ex; 293 294 if (cause instanceof ApplicationRuntimeException) 295 return findNestedException((ApplicationRuntimeException) cause); 296 297 return cause; 298 } 299 300 310 private void assertLoggedMessage(String message, List events, int index) 311 { 312 LoggingEvent e = (LoggingEvent) events.get(index); 313 314 assertEquals("Message", message, e.getMessage()); 315 } 316 317 320 protected void assertLoggedMessages(String [] messages) 321 { 322 List events = getInterceptedLogEvents(); 323 324 for (int i = 0; i < messages.length; i++) 325 { 326 assertLoggedMessage(messages[i], events, i); 327 } 328 } 329 330 333 protected void assertLoggedMessage(String message) 334 { 335 assertLoggedMessage(message, getInterceptedLogEvents()); 336 } 337 338 347 protected void assertLoggedMessage(String message, List events) 348 { 349 int count = events.size(); 350 351 for (int i = 0; i < count; i++) 352 { 353 LoggingEvent e = (LoggingEvent) events.get(i); 354 355 String eventMessage = String.valueOf(e.getMessage()); 356 357 if (eventMessage.indexOf(message) >= 0) 358 return; 359 } 360 361 throw new AssertionFailedError("Could not find logged message: " + message); 362 } 363 364 protected void assertLoggedMessagePattern(String pattern) throws Exception 365 { 366 assertLoggedMessagePattern(pattern, getInterceptedLogEvents()); 367 } 368 369 protected void assertLoggedMessagePattern(String pattern, List events) throws Exception 370 { 371 setupMatcher(); 372 373 Pattern compiled = null; 374 375 int count = events.size(); 376 377 for (int i = 0; i < count; i++) 378 { 379 LoggingEvent e = (LoggingEvent) events.get(i); 380 381 String eventMessage = e.getMessage().toString(); 382 383 if (compiled == null) 384 compiled = _compiler.compile(pattern); 385 386 if (_matcher.contains(eventMessage, compiled)) 387 return; 388 389 } 390 391 throw new AssertionFailedError("Could not find logged message with pattern: " + pattern); 392 } 393 394 private void setupMatcher() 395 { 396 if (_compiler == null) 397 _compiler = new Perl5Compiler(); 398 399 if (_matcher == null) 400 _matcher = new Perl5Matcher(); 401 } 402 403 protected Registry buildFrameworkRegistry(ModuleDefinition customModule) 404 { 405 return buildFrameworkRegistry(new ModuleDefinition[] {customModule}); 406 } 407 408 413 protected Registry buildFrameworkRegistry(ModuleDefinition[] customModules) 414 { 415 RegistryDefinition registryDefinition = new RegistryDefinitionImpl(); 416 for (int i = 0; i < customModules.length; i++) 417 { 418 ModuleDefinition module = customModules[i]; 419 registryDefinition.addModule(module); 420 } 421 422 RegistryBuilder builder = new RegistryBuilder(registryDefinition); 423 return builder.constructRegistry(Locale.getDefault()); 424 } 425 426 430 protected Registry buildMinimalRegistry(Resource l) throws Exception 431 { 432 RegistryBuilder builder = new RegistryBuilder(); 433 434 return builder.constructRegistry(Locale.getDefault()); 435 } 436 437 452 protected MockControl newControl(Class mockClass) 453 { 454 MockControlFactory factory = mockClass.isInterface() ? _interfaceMockControlFactory 455 : _classMockControlFactory; 456 457 MockControl result = factory.newControl(mockClass); 458 459 addControl(result); 460 461 return result; 462 } 463 464 475 476 protected MockControl getControl(Object mock) 477 { 478 Iterator i = _controls.iterator(); 479 while (i.hasNext()) 480 { 481 MockControl control = (MockControl) i.next(); 482 483 if (control.getMock() == mock) 484 return control; 485 } 486 487 throw new IllegalArgumentException (mock 488 + " is not a mock object controlled by any registered MockControl instance."); 489 } 490 491 501 protected void setThrowable(Object mock, Throwable t) 502 { 503 getControl(mock).setThrowable(t); 504 } 505 506 516 protected void setReturnValue(Object mock, Object returnValue) 517 { 518 getControl(mock).setReturnValue(returnValue); 519 } 520 521 531 protected void setReturnValue(Object mock, long returnValue) 532 { 533 getControl(mock).setReturnValue(returnValue); 534 } 535 536 546 protected void setReturnValue(Object mock, float returnValue) 547 { 548 getControl(mock).setReturnValue(returnValue); 549 } 550 551 561 protected void setReturnValue(Object mock, double returnValue) 562 { 563 getControl(mock).setReturnValue(returnValue); 564 } 565 566 576 protected void setReturnValue(Object mock, boolean returnValue) 577 { 578 getControl(mock).setReturnValue(returnValue); 579 } 580 581 585 protected void addControl(MockControl control) 586 { 587 _controls.add(control); 588 } 589 590 594 protected Object newMock(Class mockClass) 595 { 596 return newControl(mockClass).getMock(); 597 } 598 599 602 protected void replayControls() 603 { 604 Iterator i = _controls.iterator(); 605 while (i.hasNext()) 606 { 607 MockControl c = (MockControl) i.next(); 608 c.replay(); 609 } 610 } 611 612 616 617 protected void verifyControls() 618 { 619 Iterator i = _controls.iterator(); 620 while (i.hasNext()) 621 { 622 MockControl c = (MockControl) i.next(); 623 c.verify(); 624 c.reset(); 625 } 626 } 627 628 631 632 protected void resetControls() 633 { 634 Iterator i = _controls.iterator(); 635 while (i.hasNext()) 636 { 637 MockControl c = (MockControl) i.next(); 638 c.reset(); 639 } 640 } 641 642 645 protected Location fabricateLocation(int line) 646 { 647 String path = "/" + getClass().getName().replace('.', '/'); 648 649 Resource r = new ClasspathResource(getClassResolver(), path); 650 651 return new LocationImpl(r, line); 652 } 653 654 private int _line = 1; 655 656 663 protected Location newLocation() 664 { 665 return fabricateLocation(_line++); 666 } 667 668 674 675 protected ClassResolver getClassResolver() 676 { 677 if (_classResolver == null) 678 _classResolver = new DefaultClassResolver(); 679 680 return _classResolver; 681 } 682 683 protected boolean matches(String input, String pattern) throws Exception 684 { 685 setupMatcher(); 686 687 Pattern compiled = _compiler.compile(pattern); 688 689 return _matcher.matches(input, compiled); 690 } 691 692 } | Popular Tags |