1 15 package hivemind.test.rules; 16 17 import java.util.List ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.Location; 21 import org.apache.hivemind.Registry; 22 import org.apache.hivemind.impl.ElementImpl; 23 import org.apache.hivemind.impl.LocationImpl; 24 import org.apache.hivemind.impl.ModuleImpl; 25 import org.apache.hivemind.internal.Module; 26 import org.apache.hivemind.schema.rules.ClassTranslator; 27 import org.apache.hivemind.schema.rules.InstanceTranslator; 28 import org.apache.hivemind.xml.XmlTestCase; 29 30 36 public class TestInstanceTranslator extends XmlTestCase 37 { 38 39 public void testNull() 40 { 41 InstanceTranslator t = new InstanceTranslator(); 42 43 assertNull(t.translate(null, null, null, null)); 44 } 45 46 protected Module newModule() 47 { 48 return (Module) newMock(Module.class); 49 } 50 51 public void testBadClass() throws Exception 52 { 53 InstanceTranslator t = new InstanceTranslator(); 54 ElementImpl e = new ElementImpl(); 55 Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50); 56 e.setLocation(l); 57 58 Module m = newModule(); 59 60 m.resolveType("bad.class.Name"); 61 ApplicationRuntimeException cause = new ApplicationRuntimeException("failure"); 62 setThrowable(m, cause); 63 64 replayControls(); 65 66 try 67 { 68 t.translate(m, null, "bad.class.Name", null); 69 } 70 catch (ApplicationRuntimeException ex) 71 { 72 assertSame(cause, ex); 73 } 74 75 verifyControls(); 76 } 77 78 public void testInitializer() throws Exception 79 { 80 InstanceTranslator t = new InstanceTranslator(); 81 82 Module m = newModule(); 83 84 m.resolveType("Bean"); 85 setReturnValue(m, IntHolder.class); 86 87 replayControls(); 88 89 IntHolder ih = (IntHolder) t.translate(m, Object .class, "Bean,value=37", null); 90 91 assertEquals(37, ih.getValue()); 92 93 verifyControls(); 94 } 95 96 public void testPrivateObject() throws Exception 97 { 98 InstanceTranslator t = new InstanceTranslator(); 99 ElementImpl e = new ElementImpl(); 100 Location l = new LocationImpl(getResource("TestInstanceTranslator.class"), 50); 101 e.setLocation(l); 102 103 ModuleImpl m = new ModuleImpl(); 104 m.setClassResolver(getClassResolver()); 105 106 replayControls(); 107 108 try 109 { 110 t.translate(m, null, PrivateObject.class.getName(), null); 111 unreachable(); 112 } 113 catch (ApplicationRuntimeException ex) 114 { 115 assertExceptionSubstring( 116 ex, 117 "Unable to instantiate instance of class hivemind.test.rules.PrivateObject"); 118 } 119 120 verifyControls(); 121 } 122 123 public void testWrongType() throws Exception 124 { 125 Registry r = buildFrameworkRegistry("WrongType.xml"); 126 127 interceptLogging(); 128 129 List l = (List ) r.getConfiguration("hivemind.test.rules.WrongType"); 130 131 134 l.size(); 135 136 assertLoggedMessagePattern("Unable to update property value of object hivemind\\.test\\.config\\.impl\\.Datum@"); 137 } 138 139 public void testClassTranslator() throws Exception 140 { 141 ModuleImpl m = new ModuleImpl(); 142 m.setClassResolver(getClassResolver()); 143 144 replayControls(); 145 146 ClassTranslator t = new ClassTranslator(); 147 148 Class c = (Class ) t.translate(m, null, getClass().getName(), null); 149 150 assertEquals(getClass(), c); 151 152 verifyControls(); 153 } 154 } | Popular Tags |