1 15 package org.apache.hivemind.lib.pipeline; 16 17 import java.io.Serializable ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ErrorLog; 21 import org.apache.hivemind.service.ClassFactory; 22 import org.apache.hivemind.service.impl.ClassFactoryImpl; 23 import org.apache.hivemind.test.HiveMindTestCase; 24 25 30 31 public class TestBridgeBuilder extends HiveMindTestCase 32 { 33 private ClassFactory _classFactory = new ClassFactoryImpl(); 34 35 public void testStandard() 36 { 37 replayControls(); 38 39 BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", StandardService.class, 40 StandardFilter.class, _classFactory); 41 42 StandardFilter sf = new StandardFilter() 43 { 44 public int run(int i, StandardService ss) 45 { 46 return ss.run(i + 1); 47 } 48 }; 49 50 StandardService ss = new StandardService() 51 { 52 public int run(int i) 53 { 54 return i * 3; 55 } 56 }; 57 58 StandardService bridge = (StandardService) bb.instantiateBridge(ss, sf); 59 60 63 assertEquals(18, bridge.run(5)); 64 65 68 assertEquals( 69 "<PipelineBridge for service foo.bar(org.apache.hivemind.lib.pipeline.StandardService)>", 70 bridge.toString()); 71 72 verifyControls(); 73 } 74 75 public void testToString() 76 { 77 replayControls(); 78 79 BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", ToStringService.class, 80 ToStringFilter.class, _classFactory); 81 82 ToStringFilter f = new ToStringFilter() 83 { 84 public String toString(ToStringService s) 85 { 86 return s.toString().toUpperCase(); 87 } 88 }; 89 90 ToStringService s = new ToStringService() 91 { 92 public String toString() 93 { 94 return "Service"; 95 } 96 }; 97 98 ToStringService bridge = (ToStringService) bb.instantiateBridge(s, f); 99 100 assertEquals("SERVICE", bridge.toString()); 101 102 verifyControls(); 103 } 104 105 public void testExtraServiceMethod() 106 { 107 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 108 109 log 110 .error( 111 "Service interface method void extraServiceMethod() has no match in filter interface java.io.Serializable.", 112 null, 113 null); 114 115 replayControls(); 116 117 BridgeBuilder bb = new BridgeBuilder(log, "foo.bar", ExtraServiceMethod.class, 118 Serializable .class, _classFactory); 119 120 ExtraServiceMethod esm = (ExtraServiceMethod) bb.instantiateBridge(null, null); 121 122 try 123 { 124 esm.extraServiceMethod(); 125 unreachable(); 126 } 127 catch (ApplicationRuntimeException ex) 128 { 129 assertEquals( 130 "Service interface method void extraServiceMethod() has no match in filter interface java.io.Serializable.", 131 ex.getMessage()); 132 } 133 134 verifyControls(); 135 } 136 137 public void testExtraFilterMethod() 138 { 139 ErrorLog log = (ErrorLog) newMock(ErrorLog.class); 140 141 log 142 .error( 143 "Method void extraFilterMethod() of filter interface " 144 + "org.apache.hivemind.lib.pipeline.ExtraFilterMethod does not have a matching service " 145 + "interface method (in interface java.io.Serializable, service foo.bar).", 146 null, 147 null); 148 149 replayControls(); 150 151 BridgeBuilder bb = new BridgeBuilder(log, "foo.bar", Serializable .class, 152 ExtraFilterMethod.class, _classFactory); 153 154 Object bridge = bb.instantiateBridge(null, null); 155 156 assertEquals(true, bridge instanceof Serializable ); 157 158 verifyControls(); 159 } 160 161 public void testServiceInTheMiddle() 162 { 163 replayControls(); 164 165 BridgeBuilder bb = new BridgeBuilder(null, "foo.bar", MiddleService.class, 166 MiddleFilter.class, _classFactory); 167 168 MiddleFilter mf = new MiddleFilter() 169 { 170 public void execute(int count, char ch, MiddleService service, StringBuffer buffer) 171 { 172 service.execute(count, ch, buffer); 173 174 buffer.append(' '); 175 176 service.execute(count + 1, Character.toUpperCase(ch), buffer); 177 178 } 179 }; 180 181 MiddleService ms = new MiddleService() 182 { 183 public void execute(int count, char ch, StringBuffer buffer) 184 { 185 for (int i = 0; i < count; i++) 186 buffer.append(ch); 187 } 188 }; 189 190 192 MiddleService bridge = (MiddleService) bb.instantiateBridge(ms, mf); 193 194 StringBuffer buffer = new StringBuffer ("CODE: "); 195 196 bridge.execute(3, 'a', buffer); 197 198 assertEquals("CODE: aaa AAAA", buffer.toString()); 199 200 verifyControls(); 201 } 202 } | Popular Tags |