1 15 package org.apache.hivemind.lib.factory; 16 17 import java.io.Serializable ; 18 import java.util.ArrayList ; 19 import java.util.Collections ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.ErrorLog; 25 import org.apache.hivemind.Registry; 26 import org.apache.hivemind.ServiceImplementationFactoryParameters; 27 import org.apache.hivemind.lib.BeanFactory; 28 import org.apache.hivemind.xml.XmlTestCase; 29 import org.easymock.MockControl; 30 31 37 public class TestBeanFactoryImpl extends XmlTestCase 38 { 39 private BeanFactoryContribution build(String name, Class objectClass) 40 { 41 return build(name, objectClass, null); 42 } 43 44 private BeanFactoryContribution build(String name, Class objectClass, Boolean cacheable) 45 { 46 BeanFactoryContribution result = new BeanFactoryContribution(); 47 result.setName(name); 48 result.setBeanClass(objectClass); 49 result.setCacheable(cacheable); 50 51 return result; 52 } 53 54 private void executeNonClassContribution(String name, Class objectClass, String message) 55 { 56 List l = Collections.singletonList(build(name, objectClass)); 57 58 ErrorLog el = (ErrorLog) newMock(ErrorLog.class); 59 60 el.error(message, null, null); 61 62 replayControls(); 63 64 BeanFactoryImpl f = new BeanFactoryImpl(el, Object .class, l, true); 65 66 try 67 { 68 f.get(name); 69 unreachable(); 70 } 71 catch (ApplicationRuntimeException ex) 72 { 73 assertEquals(FactoryMessages.unknownContribution(name), ex.getMessage()); 74 } 75 76 verifyControls(); 77 } 78 79 public void testInterfaceContribution() 80 { 81 executeNonClassContribution( 82 "serializable", 83 Serializable .class, 84 "Contribution 'serializable' is for java.io.Serializable which is inappropriate for an object factory. The contribution has been ignored."); 85 } 86 87 public void testArrayContribution() 88 { 89 executeNonClassContribution( 90 "array", 91 String [].class, 92 "Contribution 'array' is for java.lang.String[] which is inappropriate for an object factory. The contribution has been ignored."); 93 } 94 95 public void testPrimitiveContribution() 96 { 97 executeNonClassContribution( 98 "primitive", 99 double.class, 100 "Contribution 'primitive' is for double which is inappropriate for an object factory. The contribution has been ignored."); 101 } 102 103 public void testIncorrectType() 104 { 105 List l = Collections.singletonList(build("array-list", ArrayList .class)); 106 107 ErrorLog el = (ErrorLog) newMock(ErrorLog.class); 108 109 el 110 .error( 111 "Contribution 'array-list' (class java.util.ArrayList) is not assignable to interface java.util.Map and has been ignored.", 112 null, 113 null); 114 115 replayControls(); 116 117 BeanFactoryImpl f = new BeanFactoryImpl(el, Map .class, l, true); 118 119 try 120 { 121 f.get("array-list"); 122 unreachable(); 123 } 124 catch (ApplicationRuntimeException ex) 125 { 126 assertEquals(FactoryMessages.unknownContribution("array-list"), ex.getMessage()); 127 } 128 129 verifyControls(); 130 } 131 132 public void testTranslator() 133 { 134 List l = Collections.singletonList(build("string", String .class)); 135 136 BeanFactoryImpl f = new BeanFactoryImpl(null, Object .class, l, true); 137 138 String s = (String ) f.get("string,locator"); 139 140 assertEquals("locator", s); 141 } 142 143 public void testPlain() 144 { 145 List l = Collections.singletonList(build("string", String .class)); 146 147 BeanFactoryImpl f = new BeanFactoryImpl(null, Object .class, l, true); 148 149 String s1 = (String ) f.get("string"); 150 String s2 = (String ) f.get("string"); 151 152 assertSame(s1, s2); 153 } 154 155 public void testNonCache() 156 { 157 List l = Collections.singletonList(build("buffer", StringBuffer .class, Boolean.FALSE)); 158 159 BeanFactoryImpl f = new BeanFactoryImpl(null, Object .class, l, true); 160 161 StringBuffer s1 = (StringBuffer ) f.get("buffer"); 162 StringBuffer s2 = (StringBuffer ) f.get("buffer"); 163 164 assertNotSame(s1, s2); 165 } 166 167 public void testConstructFailure() 168 { 169 List l = Collections.singletonList(build("integer", Integer .class)); 170 171 BeanFactoryImpl f = new BeanFactoryImpl(null, Number .class, l, true); 172 173 try 174 { 175 f.get("integer"); 176 unreachable(); 177 } 178 catch (ApplicationRuntimeException ex) 179 { 180 assertEquals( 181 "Unable to instantiate instance of class java.lang.Integer: java.lang.Integer", 182 ex.getMessage()); 183 } 184 185 } 186 187 public void testBuilder() 188 { 189 BeanFactoryParameter p = new BeanFactoryParameter(); 190 191 List l = Collections.singletonList(build("integer", Integer .class)); 192 193 p.setContributionsList(l); 194 195 MockControl fpc = newControl(ServiceImplementationFactoryParameters.class); 196 ServiceImplementationFactoryParameters fp = (ServiceImplementationFactoryParameters) fpc 197 .getMock(); 198 199 fp.getFirstParameter(); 200 fpc.setReturnValue(p); 201 202 fp.getErrorLog(); 203 fpc.setReturnValue(newMock(ErrorLog.class)); 204 205 replayControls(); 206 207 BeanFactoryBuilder b = new BeanFactoryBuilder(); 208 209 BeanFactory f = (BeanFactory) b.createCoreServiceImplementation(fp); 210 211 Integer i = (Integer ) f.get("integer,5"); 212 213 assertEquals(new Integer (5), i); 214 215 verifyControls(); 216 } 217 218 221 public void testIntegration() throws Exception 222 { 223 Registry r = buildFrameworkRegistry("NumberFactory.xml"); 224 225 BeanFactory f = (BeanFactory) r.getService( 226 "hivemind.lib.test.NumberFactory", 227 BeanFactory.class); 228 229 assertEquals(new Integer (27), f.get("int,27")); 230 assertEquals(new Double (-22.5), f.get("double,-22.5")); 231 } 232 233 public void testContains() 234 { 235 List l = Collections.singletonList(build("integer", Integer .class)); 236 237 BeanFactoryImpl f = new BeanFactoryImpl(null, Integer .class, l, true); 238 239 boolean contains = f.contains("integer"); 240 241 assertTrue(contains); 242 } 243 244 public void testContainsFailure() 245 { 246 List l = Collections.singletonList(build("integer", Integer .class)); 247 248 BeanFactoryImpl f = new BeanFactoryImpl(null, Integer .class, l, true); 249 250 boolean contains = f.contains("not_found"); 251 252 assertTrue(!contains); 253 } 254 } | Popular Tags |