1 15 package org.apache.hivemind.service.impl; 16 17 import hivemind.test.services.StringHolder; 18 import hivemind.test.services.impl.StringHolderImpl; 19 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.Location; 25 import org.apache.hivemind.impl.ModuleImpl; 26 import org.apache.hivemind.internal.Module; 27 import org.apache.hivemind.test.HiveMindTestCase; 28 import org.easymock.MockControl; 29 30 35 public class TestObjectProviders extends HiveMindTestCase 36 { 37 private Module newModule() 38 { 39 ModuleImpl result = new ModuleImpl(); 40 result.setClassResolver(getClassResolver()); 41 42 return result; 43 } 44 45 public void testServiceObjectProvider() 46 { 47 ServiceObjectProvider p = new ServiceObjectProvider(); 48 49 String expected = "EXPECTED RESULT"; 50 51 MockControl mc = newControl(Module.class); 52 Module m = (Module) mc.getMock(); 53 54 m.getService("fred", Object .class); 55 mc.setReturnValue(expected); 56 57 replayControls(); 58 59 Object actual = p.provideObject(m, Location.class, "fred", null); 60 61 assertSame(expected, actual); 62 63 assertNull(p.provideObject(m, Location.class, "", null)); 64 65 verifyControls(); 66 } 67 68 public void testConfigurationObjectProvider() 69 { 70 ConfigurationObjectProvider p = new ConfigurationObjectProvider(); 71 72 List expectedList = new ArrayList (); 73 74 MockControl mc = newControl(Module.class); 75 Module m = (Module) mc.getMock(); 76 77 m.getConfiguration("barney"); 78 mc.setReturnValue(expectedList); 79 80 replayControls(); 81 82 Object actual = p.provideObject(m, List .class, "barney", null); 83 84 assertSame(expectedList, actual); 85 86 verifyControls(); 87 } 88 89 public void testInstanceProvider() 90 { 91 ObjectInstanceObjectProvider p = new ObjectInstanceObjectProvider(); 92 93 Module m = newModule(); 94 95 Object actual = p.provideObject(m, List .class, "java.util.ArrayList", null); 96 97 assertTrue(actual.getClass().equals(ArrayList .class)); 98 99 } 100 101 public void testInstanceProviderFailure() 102 { 103 ObjectInstanceObjectProvider p = new ObjectInstanceObjectProvider(); 104 Module m = newModule(); 105 106 try 107 { 108 p.provideObject(m, List .class, "java.util.List", null); 109 unreachable(); 110 } 111 catch (ApplicationRuntimeException ex) 112 { 113 assertExceptionSubstring(ex, "Unable to instantiate instance of class java.util.List"); 114 } 115 } 116 117 public void testServicePropertyObjectProvider() 118 { 119 MockControl mc = newControl(Module.class); 120 Module m = (Module) mc.getMock(); 121 122 StringHolder h = new StringHolderImpl(); 123 124 h.setValue("abracadabra"); 125 126 m.getService("MyService", Object .class); 127 mc.setReturnValue(h); 128 129 replayControls(); 130 131 ServicePropertyObjectProvider p = new ServicePropertyObjectProvider(); 132 133 Object result = p.provideObject(m, String .class, "MyService:value", null); 134 135 assertEquals(h.getValue(), result); 136 137 verifyControls(); 138 } 139 140 public void testServicePropertyObjectProviderWithInvalidLocator() 141 { 142 ServicePropertyObjectProvider p = new ServicePropertyObjectProvider(); 143 Location l = newLocation(); 144 145 try 146 { 147 p.provideObject(null, null, "MyService", l); 148 unreachable(); 149 } 150 catch (ApplicationRuntimeException ex) 151 { 152 assertExceptionSubstring(ex, ServiceMessages.invalidServicePropertyLocator("MyService")); 153 assertSame(l, ex.getLocation()); 154 } 155 } 156 157 public void testClassProvider() 158 { 159 MockControl control = newControl(Module.class); 160 Module module = (Module) control.getMock(); 161 162 module.resolveType("List"); 163 control.setReturnValue(List .class); 164 165 replayControls(); 166 167 assertSame(List .class, new ClassObjectProvider().provideObject( 168 module, 169 Class .class, 170 "List", 171 null)); 172 173 verifyControls(); 174 } 175 176 } | Popular Tags |