1 15 package org.apache.hivemind.lib.chain; 16 17 import java.lang.reflect.Modifier ; 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.hivemind.Registry; 22 import org.apache.hivemind.impl.RegistryBuilder; 23 import org.apache.hivemind.service.BodyBuilder; 24 import org.apache.hivemind.service.ClassFab; 25 import org.apache.hivemind.service.MethodFab; 26 import org.apache.hivemind.service.MethodSignature; 27 import org.apache.hivemind.xml.XmlTestCase; 28 import org.easymock.MockControl; 29 30 37 public class TestChainBuilder extends XmlTestCase 38 { 39 public void testDefaultforReturnType() 40 { 41 ChainBuilderImpl cb = new ChainBuilderImpl(); 42 43 assertEquals("null", cb.defaultForReturnType(Object .class)); 44 assertEquals("false", cb.defaultForReturnType(boolean.class)); 45 assertEquals("null", cb.defaultForReturnType(Boolean .class)); 46 assertEquals("null", cb.defaultForReturnType(boolean[].class)); 47 assertEquals("0", cb.defaultForReturnType(int.class)); 48 assertEquals("null", cb.defaultForReturnType(Integer .class)); 49 } 50 51 private MethodFab newMethodFab() 52 { 53 return (MethodFab) newMock(MethodFab.class); 54 } 55 56 59 public void testAddVoidMethod() 60 { 61 MockControl cfc = newControl(ClassFab.class); 62 ClassFab cf = (ClassFab) cfc.getMock(); 63 64 MethodSignature sig = new MethodSignature(void.class, "run", null, null); 65 66 BodyBuilder builder = new BodyBuilder(); 67 builder.begin(); 68 builder.addln("for (int i = 0; i < _commands.length; i++)"); 69 builder.addln("_commands[i].run($$);"); 70 builder.end(); 71 72 cf.addMethod(Modifier.PUBLIC, sig, builder.toString()); 73 cfc.setReturnValue(newMethodFab()); 74 75 replayControls(); 76 77 ChainBuilderImpl cb = new ChainBuilderImpl(); 78 79 cb.addMethod(cf, Runnable .class, sig); 80 81 verifyControls(); 82 } 83 84 public void testAddNonVoidMethod() 85 { 86 MockControl cfc = newControl(ClassFab.class); 87 ClassFab cf = (ClassFab) cfc.getMock(); 88 89 MethodSignature sig = new MethodSignature(boolean.class, "execute", new Class [] 90 { String .class }, null); 91 92 BodyBuilder builder = new BodyBuilder(); 93 builder.begin(); 94 builder.addln("boolean result = false;"); 95 builder.addln("for (int i = 0; i < _commands.length; i++)"); 96 builder.begin(); 97 builder.addln("result = _commands[i].execute($$);"); 98 builder.addln("if (result != false) break;"); 99 builder.end(); 100 builder.addln("return result;"); 101 builder.end(); 102 103 cf.addMethod(Modifier.PUBLIC, sig, builder.toString()); 104 cfc.setReturnValue(newMethodFab()); 105 106 replayControls(); 107 108 ChainBuilderImpl cb = new ChainBuilderImpl(); 109 110 cb.addMethod(cf, ChainInterface.class, sig); 111 112 verifyControls(); 113 } 114 115 118 119 private ChainInterface newCommand(String parameter, boolean returnValue) 120 { 121 MockControl control = newControl(ChainInterface.class); 122 ChainInterface chain = (ChainInterface) control.getMock(); 123 124 chain.execute(parameter); 125 control.setReturnValue(returnValue); 126 127 return chain; 128 } 129 130 public void testIntegration() 131 { 132 Registry r = RegistryBuilder.constructDefaultRegistry(); 133 134 ChainBuilder cb = (ChainBuilder) r.getService(ChainBuilder.class); 135 136 List commands = new ArrayList (); 137 138 commands.add(newCommand("fred", false)); 139 commands.add(newCommand("fred", false)); 140 141 ChainInterface chain = (ChainInterface) cb.buildImplementation( 142 ChainInterface.class, 143 commands, 144 "<Chain>"); 145 146 ChainInterface chain2 = (ChainInterface) cb.buildImplementation( 147 ChainInterface.class, 148 commands, 149 "<Chain2>"); 150 151 replayControls(); 152 153 assertEquals(false, chain.execute("fred")); 154 assertEquals("<Chain>", chain.toString()); 155 156 159 assertEquals("<Chain2>", chain2.toString()); 160 assertSame(chain.getClass(), chain2.getClass()); 161 162 verifyControls(); 163 } 164 165 169 170 public void testIntegrationWithCancel() 171 { 172 Registry r = RegistryBuilder.constructDefaultRegistry(); 173 174 ChainBuilder cb = (ChainBuilder) r.getService(ChainBuilder.class); 175 176 List commands = new ArrayList (); 177 178 commands.add(newCommand("barney", true)); 179 commands.add(newMock(ChainInterface.class)); 180 181 ChainInterface chain = (ChainInterface) cb.buildImplementation( 182 ChainInterface.class, 183 commands, 184 "<Chain>"); 185 186 replayControls(); 187 188 assertEquals(true, chain.execute("barney")); 189 190 verifyControls(); 191 } 192 193 public void testChainFactoryIntegration() throws Exception 194 { 195 Registry r = buildFrameworkRegistry("ChainFactoryIntegration.xml"); 196 197 ChainInterface chain = (ChainInterface) r.getService(ChainInterface.class); 198 199 assertEquals(true, chain.execute("whatever")); 200 } 201 202 } | Popular Tags |