View Javadoc

1   package test.crispy.compatibility;
2   
3   import java.math.BigDecimal;
4   import java.net.URL;
5   import java.util.Properties;
6   
7   import net.sf.crispy.IServiceManager;
8   import net.sf.crispy.Property;
9   import net.sf.crispy.impl.HttpExecutor;
10  import net.sf.crispy.impl.JBossRemotingExecutor;
11  import net.sf.crispy.impl.MiniServer;
12  import net.sf.crispy.impl.RestExecutor;
13  import net.sf.crispy.impl.RmiExecutor;
14  import net.sf.crispy.impl.ServiceManager;
15  import net.sf.crispy.impl.StaticBurlapProxy;
16  import net.sf.crispy.impl.StaticHessianProxy;
17  import net.sf.crispy.impl.XmlRpcExecutor;
18  import net.sf.crispy.impl.caucho.MiniCauchoServer;
19  import net.sf.crispy.impl.http.MiniHttpServer;
20  import net.sf.crispy.impl.jboss.MiniJBossRemotingServer;
21  import net.sf.crispy.impl.rest.MiniRestServer;
22  import net.sf.crispy.impl.rmi.MiniRmiServer;
23  import net.sf.crispy.impl.xmlrpc.MiniXmlRpcServer;
24  import net.sf.crispy.interceptor.StopWatchInterceptor;
25  import net.sf.crispy.util.Util;
26  import test.crispy.Run;
27  import test.crispy.example.interceptor.WaitInterceptor;
28  import test.crispy.example.model.Adresse;
29  import test.crispy.example.model.Kunde;
30  import test.crispy.example.model.Node;
31  import test.crispy.example.modify.EchoModifierAfter;
32  import test.crispy.example.modify.EchoModifierBefore;
33  import test.crispy.example.service.Calculator;
34  import test.crispy.example.service.CalculatorImpl;
35  import test.crispy.example.service.Echo;
36  import test.crispy.example.service.EchoImpl;
37  
38  /**
39   * 
40   * This class is the answer to the question: is my implementation from Proxy or from Executor
41   * compatible with this framework.
42   * 
43   * @author Linke
44   *
45   */
46  public class CompatibilityKit {
47  
48  	private Properties properties = null;
49  	private ServiceManager serviceManager = null;
50  	
51  	public CompatibilityKit() {
52  		createProperties();
53  	}
54  
55  	private void createProperties() {
56  		properties = new Properties();
57      	properties.put(Echo.class.getName(), EchoImpl.class.getName());
58      	properties.put(Calculator.class.getName(), CalculatorImpl.class.getName());
59          properties.put(Property.INTERCEPTOR_CLASS, WaitInterceptor.class.getName());
60          properties.put(Property.INTERCEPTOR_CLASS_2, StopWatchInterceptor.class.getName());		
61  	}
62  	
63  	public void addProperty(String pvKey, String pvValue) { properties.put(pvKey, pvValue); }
64  	public void removeProperty(String pvKey) { properties.remove(pvKey); }
65  	public String getProperty(String pvKey) { return (String) properties.get(pvKey); }
66  	
67  	public IServiceManager createServiceManager () {
68  		serviceManager = new ServiceManager(properties);
69  		return serviceManager; 
70  	}
71  	
72  	public void makeSimpleEchoTest (IServiceManager pvServiceManager) {
73  		Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
74  		if (lvEcho == null) { throw new CompatibilityException("The Echo service must be not null!"); }
75  		String lvEchoStrBefore = "Hallo Echo";
76  		String lvEchoStrAfter = lvEcho.echo(lvEchoStrBefore);
77  		if (!lvEchoStrAfter.equals(lvEchoStrBefore)) { 
78  			throw new CompatibilityException("The echo strings must be equals (" + lvEchoStrBefore + " != " + lvEchoStrAfter + ")"); 
79  		}
80  	}
81  	
82  	public void makeOverloadingTest (IServiceManager pvServiceManager) {
83  		Calculator lvCalculator = (Calculator) pvServiceManager.createService(Calculator.class);
84  		if (lvCalculator == null) { throw new CompatibilityException("The Calculator service must be not null!"); }
85  		int lvIntResult = lvCalculator.add(2, 3);
86  		if (lvIntResult != 5) { 
87  			throw new CompatibilityException("The int result must be equals (" + lvIntResult + " != 5)"); 
88  		}
89  		long lvLongResult = lvCalculator.add(21, 32);
90  		if (lvLongResult != 53) { 
91  			throw new CompatibilityException("The long result must be equals (" + lvLongResult + " != 53)"); 
92  		}
93  		double lvDoubleResult = lvCalculator.add(2.3, 3.2);
94  		if (lvDoubleResult != 5.5) { 
95  			throw new CompatibilityException("The double result must be equals (" + lvDoubleResult + " != 5.3)"); 
96  		}
97  	}
98  	
99  
100 	public void makeComplexEchoTest (IServiceManager pvServiceManager) {
101 		String lvNameVorher = "NameBefore";
102     	String lvNameDanach = "NameAfter";
103         Echo lvEchoRename = (Echo) pvServiceManager.createService(Echo.class);
104         Kunde k = new Kunde ();
105         k.setName(lvNameVorher); k.setVorname("Mario");
106         Adresse lvAdresse = new Adresse();
107         lvAdresse.setOrt("Magdeburg"); lvAdresse.setPlz("39106");
108         k.getAdressen().add(lvAdresse);
109         Kunde lvKundeDanach = lvEchoRename.renameKunde(k, lvNameDanach);
110         if (lvKundeDanach.getName().equals(k.getName())) {
111         	throw new CompatibilityException("The name must be not equals (" +lvKundeDanach.getName() + " != " + k.getName() + ")");
112         }
113         if (lvKundeDanach.getAdressen().size() != 1) {
114         	throw new CompatibilityException("The size from Adress must be 1 and not " + lvKundeDanach.getAdressen().size());
115         }
116 	}
117 	
118 	
119 	public void makeCalculatorTest (IServiceManager pvServiceManager) {
120 		Calculator lvCalculator = (Calculator) pvServiceManager.createService(Calculator.class);
121 		if (lvCalculator == null) {
122 			throw new CompatibilityException("The Calculator service must be not null");
123 		}
124 		int lvResult = lvCalculator.add(2, 3);
125 		if (lvResult != 5) {
126 			throw new CompatibilityException("The result from calculate (2+3) must be 5 and not: " + lvResult);
127 		}
128 		lvResult = lvCalculator.subtract(8, 2);
129 		if (lvResult != 6) {
130 			throw new CompatibilityException("The result from subtract (8-2) must be 6 and not: " + lvResult);
131 		}
132 	}
133 	
134 	public void makeMultipleServiceTest() {
135 		IServiceManager lvServiceManager = new ServiceManager(properties);
136 
137 		Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
138 		if (lvEcho == null) {
139 			throw new CompatibilityException("The Echo service must be not null");
140 		}
141 		String lvEchoStrBefore = "Hallo Echo";
142 		String lvEchoStrAfter = lvEcho.echo(lvEchoStrBefore);
143 		if (!lvEchoStrBefore.equals(lvEchoStrAfter)) {
144 			throw new CompatibilityException("The echo strings must be equals.");
145 		}
146 
147 		Calculator lvCalculator = (Calculator) lvServiceManager.createService(Calculator.class);
148 		if (lvCalculator == null) {
149 			throw new CompatibilityException("The Calculator service must be not null");
150 		}
151 		int lvResult = lvCalculator.add(2, 3);
152 		if (lvResult != 5) {
153 			throw new CompatibilityException("The result from calculate (2+3) must be 5 and not: " + lvResult);
154 		}
155 		lvResult = lvCalculator.subtract(8, 2);
156 		if (lvResult != 6) {
157 			throw new CompatibilityException("The result from subtract (8-2) must be 6 and not: " + lvResult);
158 		}
159 		
160 	}
161 	
162 	public void makeCycleDetectionTest (IServiceManager pvServiceManager) {
163 		Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
164 		if (lvEcho == null) {
165 			throw new CompatibilityException("The Echo service must be not null");
166 		}
167 		
168 		Kunde k = new Kunde("Mario");
169 		k.setGehalt(new BigDecimal(12.34));
170 		Adresse a = new Adresse();
171 		a.setOrt("Magdebrug");
172 		a.setPlz("39104");
173 		k.getAdressen().add(a);
174 		a.setKunde(k);
175 		
176 		String lvRenameName = "Nadine";
177 		Kunde kAfter = lvEcho.renameKunde(k, lvRenameName);
178 		if (!(kAfter.getName().equals(lvRenameName))) {
179 			throw new CompatibilityException("The rename must be: " + lvRenameName + " and not: " + kAfter.getName());
180 		}
181 		Kunde kTemp = ((Adresse) kAfter.getAdressen().get(0)).getKunde();
182 		if (!(kAfter.equals(kTemp))) {
183 			throw new CompatibilityException("Cycle detection works uncorrect: " + kAfter + " is not the same instance: " + kTemp);
184 		}
185 	}
186 	
187 	public void makeAddNodeTest (IServiceManager pvServiceManager) {
188 		Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
189 		if (lvEcho == null) {
190 			throw new CompatibilityException("The Echo service must be not null");
191 		}
192 		Node n1 = new Node("Node1");
193 		Node n2 = new Node("Node2");
194 		Node n3 = new Node("Node3");
195 		Node nA = new Node("NodeA");
196 		Node nB = new Node("NodeB");
197 		
198 		n1.getChildren().add(n2);
199 		n1.getChildren().add(n3);
200 		n2.getChildren().add(nA);
201 		n3.getChildren().add(nA);
202 		n3.getChildren().add(nB);
203 		
204 		Node nZZ = new Node("NodeZZ");
205 		
206 		Node nTarget1 = lvEcho.addNode(n1, nZZ);
207 		if (nTarget1.getChildren().size() != 3) {
208 			throw new CompatibilityException("The Node1 must 3 children and not: " + nTarget1.getChildren().size());
209 		}
210 		Node nTarget2 = (Node) nTarget1.getChildren().get(0);
211 		Node nTarget3 = (Node) nTarget1.getChildren().get(1);
212 		if (!(nTarget2.getChildren().get(0).equals(nTarget3.getChildren().get(0)))) {
213 			throw new CompatibilityException("The Node must be equals: " + 
214 					nTarget2.getChildren().get(0) + "==" + nTarget3.getChildren().get(0));
215 		}
216 	}
217 	
218     public void makeSimpleProxyInterceptorTest() {
219         ServiceManager lvServiceManager = new ServiceManager(properties);
220         makeSimpleEchoTest(lvServiceManager);
221         
222     	StopWatchInterceptor lvProxyInterceptor = (StopWatchInterceptor) lvServiceManager.getInterceptorByPos(1);
223     	if (lvProxyInterceptor.getStopTimeNewInstance() <= 0) {
224     		throw new CompatibilityException("The time must be greate then 0 and not: " + lvProxyInterceptor.getStopTimeNewInstance());
225     	}
226     	if (lvProxyInterceptor.getStopTimeInvokeMethod() <= 0) {
227     		throw new CompatibilityException("The time must be greate then 0 and not: " + lvProxyInterceptor.getStopTimeInvokeMethod());
228     	}
229     }
230 
231     public void makeMultiProxyInterceptorTest() {
232         ServiceManager lvServiceManager = new ServiceManager(properties);
233         if (lvServiceManager.getInterceptorSize() != 2) {
234         	throw new CompatibilityException("It must be 2 Interceptor register and not: " + lvServiceManager.getInterceptorSize());
235         }
236         
237         WaitInterceptor lvWaitInterceptor = (WaitInterceptor) lvServiceManager.getInterceptorByPos(0);
238     	StopWatchInterceptor lvProxyInterceptor = (StopWatchInterceptor) lvServiceManager.getInterceptorByPos(1);
239     	
240     	makeCalculatorTest (lvServiceManager);
241     	if (!(lvProxyInterceptor.getStopTimeNewInstance() >= lvWaitInterceptor.getWaitTime())) {
242     		throw new CompatibilityException("The Stop-Time from Interceptor for newInstance (Calculator) " +
243     				lvProxyInterceptor.getStopTimeNewInstance() + " - " + lvWaitInterceptor.getWaitTime());
244     	}
245     	if (!(lvProxyInterceptor.getStopTimeInvokeMethod() >= lvWaitInterceptor.getWaitTime())) {
246     		throw new CompatibilityException("The Stop-Time from Interceptor for InvokeMethod (Calculator)" +
247     				lvProxyInterceptor.getStopTimeInvokeMethod() + " - " + lvWaitInterceptor.getWaitTime());
248     	}
249     	
250         makeSimpleEchoTest(lvServiceManager);
251         if (!(lvProxyInterceptor.getStopTimeNewInstance() >= lvWaitInterceptor.getWaitTime())) {
252         	throw new CompatibilityException("The Stop-Time from Interceptor for newInstance (Echo)" +
253         			lvProxyInterceptor.getStopTimeNewInstance() + " - " +  lvWaitInterceptor.getWaitTime());
254         }
255         if (!(lvProxyInterceptor.getStopTimeInvokeMethod() >= lvWaitInterceptor.getWaitTime())) {
256         	throw new CompatibilityException("The Stop-Time from Interceptor for InvokeMethod (Echo)" +
257         			lvProxyInterceptor.getStopTimeInvokeMethod() + " - " + lvWaitInterceptor.getWaitTime());
258         }
259 	}
260 
261 	public void makeModifyServiceTest(IServiceManager pvServiceManager) {
262 		String lvResultStr = "- no result -";
263 		pvServiceManager.setModifier(new EchoModifierBefore());
264 		Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
265 		lvResultStr = lvEcho.echo("modify string");
266 		if (lvResultStr.equals(EchoModifierBefore.NEW_ECHO_STRING_BEFORE) == false) {
267 			throw new CompatibilityException("Invalid result in makeModifyServiceTest (BEFORE) - expected: " 
268 						+ EchoModifierBefore.NEW_ECHO_STRING_BEFORE + " and "
269 						+ " the result is: " + lvResultStr);
270 		}
271 
272 		pvServiceManager.setModifier(new EchoModifierAfter());
273 		lvResultStr = lvEcho.echo("modify string");
274 		if (lvResultStr.equals(EchoModifierAfter.NEW_ECHO_STRING_AFTER) == false) {
275 			throw new CompatibilityException("Invalid result in makeModifyServiceTest (AFTER) - expected: " 
276 						+ EchoModifierAfter.NEW_ECHO_STRING_AFTER + " and "
277 						+ " the result is: " + lvResultStr);
278 		}
279 	}
280 	
281 	public void makeCreateService(IServiceManager pvServiceManager) {
282 		Calculator lvCalculator1 = (Calculator) pvServiceManager.createService(Calculator.class);
283 		Calculator lvCalculator2 = (Calculator) pvServiceManager.createService(Calculator.class);
284 		if ((lvCalculator1 == null) || (lvCalculator2 == null)) {
285 			throw new CompatibilityException("The instance must be not null: " + lvCalculator1 + " and " + lvCalculator2);
286 		}
287 		if (lvCalculator1 == lvCalculator2) {
288 			throw new CompatibilityException("The instance must be different: " + lvCalculator1 + " and " + lvCalculator2);
289 		}
290 	}
291 	
292 	public void makeThrowException(IServiceManager pvServiceManager) {
293 		Echo lvEcho = (Echo) pvServiceManager.createService(Echo.class);
294 		boolean throwException = false;
295 		try {
296 			lvEcho.throwException("A Test-Excption.");
297 		} catch (Exception e) {
298 			throwException = true;
299 		}
300 		if (throwException == false) {
301 			throw new CompatibilityException("No Exception thrown!");
302 		}
303 	}
304 
305     /** 
306      * Invoke alle Test in this class and stop the time.
307      *  
308      */
309     public void makeAllTests(String pvDescription) {
310     	makeAllTests(pvDescription, null);
311     }
312     
313     /**
314      * Start and stop the Server and  
315      * invoke alle Test in this class and stop the time.
316      *  
317      */
318     public void makeAllTests(String pvDescription, MiniServer pvMiniServer) {
319     	if (pvMiniServer != null) {
320 			pvMiniServer.addService(Echo.class.getName(), EchoImpl.class.getName());
321 			pvMiniServer.addService(Calculator.class.getName(), CalculatorImpl.class.getName());
322 
323     		pvMiniServer.start();
324     	}
325     	long start = System.currentTimeMillis(); 
326     	IServiceManager lvServiceManager = createServiceManager();
327     	makeThrowException(lvServiceManager);
328     	makeCreateService(lvServiceManager);
329     	makeSimpleEchoTest(lvServiceManager);
330     	makeCalculatorTest(lvServiceManager);
331     	
332     	try {
333     		makeOverloadingTest(lvServiceManager);	
334     	} catch (CompatibilityException e) {
335     		System.out.println("===> Overloading is not supported by: " + pvDescription);
336     	}
337     	
338     	makeComplexEchoTest(lvServiceManager);
339     	makeCycleDetectionTest(lvServiceManager);
340     	makeAddNodeTest(lvServiceManager);
341     	makeMultipleServiceTest();
342     	makeSimpleProxyInterceptorTest();
343     	makeMultiProxyInterceptorTest();
344     	makeModifyServiceTest(lvServiceManager);
345     	System.out.println("--- Time all tests " + pvDescription + ": " + (System.currentTimeMillis() - start) + " ---");
346     	if (pvMiniServer != null) { pvMiniServer.stop();  }
347     }
348 
349     
350     /** 
351      * Two examples for CompatibilityKit - Test (XML-RPC and RMI)
352      * <b>Importend:</b> before you can call this main-method, you must start the XML-RPC and RMI Server. 
353      */
354     public static void main(String[] args) {
355     	
356 //    	Util.initJdkLogger();
357 		URL lvUrl = Run.class.getResource("/jdk14-no-logging.properties");		
358 	   	Util.initJdkLogger(lvUrl.getPath());
359 
360     	
361     	CompatibilityKit lvCompatibilityKit = new CompatibilityKit();
362     	lvCompatibilityKit.addProperty(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
363     	lvCompatibilityKit.makeAllTests("JBossRemoting", new MiniJBossRemotingServer());
364 
365     	
366     	lvCompatibilityKit = new CompatibilityKit();    	
367     	lvCompatibilityKit.addProperty(Property.EXECUTOR_CLASS, XmlRpcExecutor.class.getName());
368     	lvCompatibilityKit.makeAllTests("XmlRpc", new MiniXmlRpcServer());
369    	
370     	lvCompatibilityKit = new CompatibilityKit();
371     	lvCompatibilityKit.addProperty(Property.EXECUTOR_CLASS, RmiExecutor.class.getName());
372     	lvCompatibilityKit.makeAllTests("Rmi", new MiniRmiServer());
373     	
374 
375     	
376     	lvCompatibilityKit = new CompatibilityKit();
377     	lvCompatibilityKit.addProperty(Property.STATIC_PROXY_CLASS, StaticBurlapProxy.class.getName());
378     	lvCompatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
379     	lvCompatibilityKit.makeAllTests("Burlap", new MiniCauchoServer());
380 
381     	lvCompatibilityKit = new CompatibilityKit();
382     	lvCompatibilityKit.addProperty(Property.STATIC_PROXY_CLASS, StaticHessianProxy.class.getName());
383     	lvCompatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
384     	lvCompatibilityKit.makeAllTests("Hessian", new MiniCauchoServer());
385 
386 	   	lvCompatibilityKit = new CompatibilityKit();
387     	lvCompatibilityKit.addProperty(Property.EXECUTOR_CLASS, RestExecutor.class.getName());
388     	lvCompatibilityKit.makeAllTests("Rest", new MiniRestServer());
389     	
390     	lvCompatibilityKit = new CompatibilityKit();    	
391     	lvCompatibilityKit.addProperty(Property.EXECUTOR_CLASS, HttpExecutor.class.getName());
392     	lvCompatibilityKit.makeAllTests("Http", new MiniHttpServer());
393     	
394     	System.out.println("\nTHE END");
395     	System.exit(0);
396 	}
397 }