1 15 package org.apache.hivemind.impl; 16 17 import hivemind.test.services.StringHolder; 18 import hivemind.test.services.impl.StringHolderImpl; 19 20 import org.apache.hivemind.ApplicationRuntimeException; 21 import org.apache.hivemind.ClassResolver; 22 import org.apache.hivemind.ErrorLog; 23 import org.apache.hivemind.Locatable; 24 import org.apache.hivemind.internal.RegistryInfrastructure; 25 import org.apache.hivemind.test.HiveMindTestCase; 26 import org.easymock.MockControl; 27 28 33 public class TestModule extends HiveMindTestCase 34 { 35 36 public void testGetServiceByInterface() 37 { 38 MockControl rc = newControl(RegistryInfrastructure.class); 39 RegistryInfrastructure r = (RegistryInfrastructure) rc.getMock(); 40 41 ModuleImpl m = new ModuleImpl(); 42 m.setRegistry(r); 43 44 StringHolder h = new StringHolderImpl(); 45 46 r.getService(StringHolder.class, m); 47 rc.setReturnValue(h); 48 49 replayControls(); 50 51 Object result = m.getService(StringHolder.class); 52 53 assertEquals(h, result); 54 55 verifyControls(); 56 } 57 58 public void testResolveType() 59 { 60 ModuleImpl module = new ModuleImpl(); 61 module.setPackageName("org.apache.hivemind"); 62 module.setClassResolver(getClassResolver()); 63 64 assertSame(Locatable.class, module.resolveType("org.apache.hivemind.Locatable")); 65 assertSame(ErrorLog.class, module.resolveType("ErrorLog")); 66 assertSame(BaseLocatable.class, module.resolveType("impl.BaseLocatable")); 67 } 68 69 72 public void testResolveTypeCache() 73 { 74 Class expected = getClass(); 75 76 ClassResolver resolver = newClassResolver(); 77 78 trainCheckForClass(resolver, "FooBar", expected); 79 80 replayControls(); 81 82 ModuleImpl module = new ModuleImpl(); 83 module.setPackageName("org.apache.hivemind"); 84 module.setClassResolver(resolver); 85 86 assertSame(expected, module.resolveType("FooBar")); 87 88 90 assertSame(expected, module.resolveType("FooBar")); 91 92 verifyControls(); 93 } 94 95 private void trainCheckForClass(ClassResolver resolver, String type, Class returnClass) 96 { 97 resolver.checkForClass(type); 98 setReturnValue(resolver, returnClass); 99 } 100 101 private ClassResolver newClassResolver() 102 { 103 return (ClassResolver) newMock(ClassResolver.class); 104 } 105 106 public void testResolveTypeFailure() 107 { 108 ModuleImpl module = new ModuleImpl(); 109 module.setPackageName("org.apache.hivemind.order"); 110 module.setClassResolver(getClassResolver()); 111 112 try 113 { 114 module.resolveType("Qbert"); 115 unreachable(); 116 } 117 catch (ApplicationRuntimeException ex) 118 { 119 assertEquals( 120 "Unable to convert type 'Qbert' to a Java class, either as is, or in package org.apache.hivemind.order.", 121 ex.getMessage()); 122 } 123 } 124 } | Popular Tags |