1 15 package hivemind.test.lib; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Registry; 19 import org.apache.hivemind.ServiceImplementationFactoryParameters; 20 import org.apache.hivemind.lib.DefaultImplementationBuilder; 21 import org.apache.hivemind.lib.impl.DefaultImplementationBuilderImpl; 22 import org.apache.hivemind.lib.impl.PlaceholderFactory; 23 import org.apache.hivemind.service.impl.ClassFactoryImpl; 24 import org.apache.hivemind.xml.XmlTestCase; 25 import org.easymock.MockControl; 26 27 33 public class TestDefaultImplementationBuilder extends XmlTestCase 34 { 35 private DefaultImplementationBuilder _builder; 36 37 protected void setUp() throws Exception 38 { 39 super.setUp(); 40 41 DefaultImplementationBuilderImpl bi = new DefaultImplementationBuilderImpl(); 42 43 bi.setClassFactory(new ClassFactoryImpl()); 44 45 _builder = bi; 46 } 47 48 private Object create(Class interfaceType) 49 { 50 replayControls(); 51 52 return _builder.buildDefaultImplementation(interfaceType); 53 } 54 55 public void testSimple() 56 { 57 Runnable r = (Runnable ) create(Runnable .class); 58 59 r.run(); 60 61 assertEquals("<Default implementation of interface java.lang.Runnable>", r.toString()); 62 63 verifyControls(); 64 } 65 66 public void testComplex() 67 { 68 ValueHolder vh = (ValueHolder) create(ValueHolder.class); 69 70 assertNull(vh.getStringValue()); 71 assertEquals(false, vh.getBooleanValue()); 72 assertEquals(0, vh.getIntValue()); 73 74 verifyControls(); 75 } 76 77 public void testToStringInInterface() 78 { 79 ToString ts = (ToString) create(ToString.class); 80 81 assertNull(ts.toString()); 82 83 verifyControls(); 84 } 85 86 public void testCache() 87 { 88 Runnable r1 = (Runnable ) create(Runnable .class); 89 Runnable r2 = (Runnable ) _builder.buildDefaultImplementation(Runnable .class); 90 91 assertSame(r1, r2); 92 93 verifyControls(); 94 } 95 96 public void testNotInterface() 97 { 98 try 99 { 100 create(String .class); 101 unreachable(); 102 } 103 catch (ApplicationRuntimeException ex) 104 { 105 assertExceptionSubstring(ex, "Class java.lang.String is not an interface."); 106 } 107 } 108 109 public void testModuleDescriptor() throws Exception 110 { 111 Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml"); 112 113 DefaultImplementationBuilder dib = (DefaultImplementationBuilder) r.getService( 114 "hivemind.lib.DefaultImplementationBuilder", 115 DefaultImplementationBuilder.class); 116 117 replayControls(); 118 119 Runnable o = (Runnable ) dib.buildDefaultImplementation(Runnable .class); 120 121 o.run(); 122 123 verifyControls(); 124 } 125 126 public void testPlaceholderBuilderSimulated() throws Exception 127 { 128 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 129 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 130 .getMock(); 131 132 PlaceholderFactory db = new PlaceholderFactory(); 133 134 db.setBuilder(_builder); 135 136 fp.getServiceInterface(); 137 fpc.setReturnValue(Runnable .class); 138 139 replayControls(); 140 141 Runnable r = (Runnable ) db.createCoreServiceImplementation(fp); 142 143 r.run(); 144 145 verifyControls(); 146 } 147 148 public void testPlaceholderFactory() throws Exception 149 { 150 Registry r = buildFrameworkRegistry("DefaultImplementationBuilder.xml"); 151 152 Runnable o = (Runnable ) r.getService("hivemind.test.lib.Runnable", Runnable .class); 153 154 o.run(); 155 } 156 } | Popular Tags |