1 15 package org.apache.hivemind.util; 16 17 import org.apache.hivemind.ApplicationRuntimeException; 18 import org.apache.hivemind.Location; 19 import org.apache.hivemind.impl.BaseLocatable; 20 import org.apache.hivemind.internal.Module; 21 import org.apache.hivemind.test.HiveMindTestCase; 22 import org.easymock.MockControl; 23 24 30 public class TestInstanceCreationUtils extends HiveMindTestCase 31 { 32 public static class Bean 33 { 34 private int _value; 35 36 public int getValue() 37 { 38 return _value; 39 } 40 41 public void setValue(int value) 42 { 43 _value = value; 44 } 45 } 46 47 private Module newModule(String name, Class returnValue) 48 { 49 MockControl control = newControl(Module.class); 50 Module module = (Module) control.getMock(); 51 52 module.resolveType(name); 53 control.setReturnValue(returnValue); 54 55 return module; 56 } 57 58 public void testSimple() 59 { 60 Module module = newModule("Bean", Bean.class); 61 62 replayControls(); 63 64 Bean bean = (Bean) InstanceCreationUtils.createInstance(module, "Bean", null); 65 66 assertNotNull(bean); 67 68 verifyControls(); 69 } 70 71 public void testComplex() 72 { 73 Module module = newModule("Bean", Bean.class); 74 75 replayControls(); 76 77 Bean bean = (Bean) InstanceCreationUtils.createInstance(module, "Bean,value=42", null); 78 79 assertEquals(42, bean.getValue()); 80 81 verifyControls(); 82 } 83 84 public void testSetLocation() 85 { 86 Location l = newLocation(); 87 Module module = newModule("Holder", BaseLocatable.class); 88 89 replayControls(); 90 91 BaseLocatable holder = (BaseLocatable) InstanceCreationUtils.createInstance( 92 module, 93 "Holder", 94 l); 95 96 assertSame(l, holder.getLocation()); 97 98 verifyControls(); 99 } 100 101 public void testFailure() 102 { 103 Location l = newLocation(); 104 Module module = newModule("Bean", Bean.class); 105 106 replayControls(); 107 108 try 109 { 110 InstanceCreationUtils.createInstance(module, "Bean,value=fred", l); 111 unreachable(); 112 } 113 catch (ApplicationRuntimeException ex) 114 { 115 assertExceptionSubstring( 116 ex, 117 "Unable to instantiate instance of class org.apache.hivemind.util.TestInstanceCreationUtils$Bean"); 118 assertSame(l, ex.getLocation()); 119 } 120 121 verifyControls(); 122 } 123 } | Popular Tags |