package test.crispy.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import junit.framework.TestCase;
import net.sf.crispy.Property;
import net.sf.crispy.impl.JBossRemotingExecutor;
import net.sf.crispy.impl.MiniServer;
import net.sf.crispy.impl.ServiceManager;
import net.sf.crispy.impl.jboss.MiniJBossRemotingServer;
import net.sf.crispy.interceptor.StopWatchInterceptor;
import net.sf.crispy.strategy.NameSpacePlusMethodInvocationStrategy;
import test.crispy.JUnitTestException2;
import test.crispy.compatibility.CompatibilityKit;
import test.crispy.concurrent.AsynchronousCallbackForTests;
import test.crispy.example.model.Kunde;
import test.crispy.example.model.Primitive;
import test.crispy.example.model.ValidationError;
import test.crispy.example.service.Calculator;
import test.crispy.example.service.Echo;
public class JBossRemotingServiceTest extends TestCase {
private static MiniServer miniServer = null;
private CompatibilityKit compatibilityKit = new CompatibilityKit();
/**
* Init <code>static</code> MiniServer for all tests.
*/
public JBossRemotingServiceTest() {
if (miniServer == null) {
miniServer = new MiniJBossRemotingServer();
miniServer.addService("test.crispy.example.service.Echo", "test.crispy.example.service.EchoImpl");
miniServer.addService("test.crispy.example.service.Calculator", "test.crispy.example.service.CalculatorImpl");
miniServer.start();
}
compatibilityKit.addProperty(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
}
protected void setUp() throws Exception {
super.setUp();
compatibilityKit.removeProperty(Property.DYNAMIC_PROXY_CLASS);
}
public void testEchoPrimitive() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvServiceManager = new ServiceManager(prop);
Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
Primitive lvPrimitive = Primitive.createPrimitiveExample();
Primitive lvPrimitiveAfter = lvEcho.echoPrimitive(lvPrimitive);
assertEquals(lvPrimitive, lvPrimitiveAfter);
assertNotSame(lvPrimitive, lvPrimitiveAfter);
}
public void testSimpleRemotePing() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
String lvPing = echo.ping();
assertEquals(lvPing, Echo.PING_STRING);
}
public void testSimpleRemoteInvocation() throws Exception {
compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
}
public void testSimpleRemoteInvocationWithDynamicCglibProxy() throws Exception {
compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_CGLIB_DYNAMIC_PROXY);
compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
}
public void testSimpleRemoteInvocationWithDynamicJdkProxy() throws Exception {
compatibilityKit.addProperty(Property.DYNAMIC_PROXY_CLASS, Property.VALUE_FOR_JDK_DYNAMIC_PROXY);
compatibilityKit.makeSimpleEchoTest(compatibilityKit.createServiceManager());
}
public void testSimpleDoubleRemoteInvocation() throws Exception {
compatibilityKit.makeMultipleServiceTest();
}
public void testComplexRemoteInvocation() throws Exception {
compatibilityKit.makeComplexEchoTest(compatibilityKit.createServiceManager());
}
public void testCycleDetectionTest() throws Exception {
compatibilityKit.makeCycleDetectionTest(compatibilityKit.createServiceManager());
}
public void testAddNode() throws Exception {
compatibilityKit.makeAddNodeTest(compatibilityKit.createServiceManager());
}
public void testProxyInterceptor() throws Exception {
Properties prop = new Properties();
prop.put(Property.INTERCEPTOR_CLASS, StopWatchInterceptor.class.getName());
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvServiceManager = new ServiceManager(prop);
Calculator lvCalculator = (Calculator) lvServiceManager.createService(Calculator.class);
assertNotNull(lvCalculator);
int lvResult = lvCalculator.add(2, 3);
assertEquals(lvResult, 5);
lvResult = lvCalculator.subtract(8, 2);
assertEquals(lvResult, 6);
Echo lvEcho = (Echo) lvServiceManager.createService(Echo.class);
assertNotNull(lvEcho);
String lvEchoStr = "Hallo Echo";
assertEquals(lvEcho.echo(lvEchoStr), lvEchoStr);
}
public void testModifyService() throws Exception {
compatibilityKit.makeModifyServiceTest(compatibilityKit.createServiceManager());
}
public void testAddLongs() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Calculator calc = (Calculator) manager.createService(Calculator.class);
assertEquals(calc.addLong(new Long(123l), new Long(124l)), new Long(247));
}
public void testOverloading() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Calculator lvCalculator = (Calculator) manager.createService(Calculator.class);
int lvIntResult = lvCalculator.add(2, 3);
assertEquals(lvIntResult, 5);
long lvLongResult = lvCalculator.add(21, 32);
assertEquals(lvLongResult, 53);
double lvDoubleResult = lvCalculator.add(2.3, 3.2);
assertEquals(lvDoubleResult, 5.5, 0);
}
public void testOverloadingInteger() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
Integer i[] = echo.echoArray(new Integer[] {new Integer(1), new Integer(3), new Integer(5)});
assertTrue(i.length == 3);
assertEquals(i[0], new Integer(1));
assertEquals(i[1], new Integer(3));
assertEquals(i[2], new Integer(5));
}
public void testOverloadingLong() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
Long l[] = echo.echoArray(new Long[] {new Long(1), new Long(5)});
assertTrue(l.length == 2);
assertEquals(l[0], new Long(1));
assertEquals(l[1], new Long(5));
}
public void testOverloadingObject() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
Object o[] = echo.echoArray(new Object[] {"a", new Integer(3), new Long(5)});
assertTrue(o.length == 3);
assertEquals(o[0], "a");
assertEquals(o[1], new Integer(3));
assertEquals(o[2], new Long(5));
}
public void testCreateService() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Calculator lvCalculator1 = (Calculator) lvManager.createService(Calculator.class);
Calculator lvCalculator2 = (Calculator) lvManager.createService(Calculator.class);
assertNotNull(lvCalculator1);
assertNotNull(lvCalculator2);
assertNotSame(lvCalculator1, lvCalculator2);
}
public void testThrowException() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Echo lvEcho = (Echo) lvManager.createService(Echo.class);
boolean throwException = false;
try {
lvEcho.throwException("A Test-Excption.");
} catch (Exception e) {
throwException = true;
}
assertTrue("No Exception thrown: " + throwException, throwException);
}
public void testNullValueParamsWithException() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Echo lvEcho = (Echo) lvManager.createService(Echo.class);
String s = lvEcho.echo(null);
assertNull(s);
}
public void testNullLongValueParams() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Calculator lvCalculator = (Calculator) lvManager.createService(Calculator.class);
Long lvLong = lvCalculator.addLong(null, null);
assertNull(lvLong);
}
public void testNullComplexValueParams() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Echo lvEcho = (Echo) lvManager.createService(Echo.class);
Kunde k = lvEcho.renameKunde(null, null);
assertNull(k);
}
public void testTransferDate() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo lvEcho = (Echo) manager.createService(Echo.class);
Kunde k = new Kunde("JUnit-Test-Name");
Date d = new Date();
k.setDate(d);
Kunde k2 = lvEcho.renameKunde(k, "Rename-Test");
assertNotNull(k2.getDate());
assertEquals(k.getDate(), k2.getDate());
}
public void testInvalidInvocationStrategy() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
prop.put(Property.INVOCATION_STRATEGY, NameSpacePlusMethodInvocationStrategy.class.getName());
ServiceManager lvManager = new ServiceManager(prop);
Echo lvEcho = (Echo) lvManager.createService(Echo.class);
try {
lvEcho.echo("Bla");
fail("Must be thrown Exception. It is set a bad InvocationStrategy");
} catch (Exception e) {
assertTrue(true);
}
}
public void testAsynchronousInvocation() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
String lvPing = echo.ping();
assertNull(lvPing);
assertTrue(manager.isInvocationAsynchronous(Echo.class));
Thread.sleep(300);
AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
assertNotNull(lvAsynchronousCallbackForTests);
assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult());
assertNull(lvAsynchronousCallbackForTests.getThrowable());
}
public void testAsynchronousInvocationWithMultyThreaded() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
prop.put(Property.ASYNCHRONOUS_CALLBACK_CLASS, AsynchronousCallbackForTests.class.getName());
prop.put(Property.ASYNCHRONOUS_MAX_SIZE_OF_THREADS, "3");
ServiceManager manager = new ServiceManager(prop);
Echo echo = (Echo) manager.createService(Echo.class);
String lvPing = echo.ping();
assertNull(lvPing);
assertTrue(manager.isInvocationAsynchronous(Echo.class));
Thread.sleep(300);
AsynchronousCallbackForTests lvAsynchronousCallbackForTests = (AsynchronousCallbackForTests) manager.getAsynchronousCallback(Echo.class);
assertNotNull(lvAsynchronousCallbackForTests);
assertEquals("ping", lvAsynchronousCallbackForTests.getMethodName());
assertEquals(Echo.PING_STRING, lvAsynchronousCallbackForTests.getResult());
assertNull(lvAsynchronousCallbackForTests.getThrowable());
}
public void testIndivualAsynchronousInvocationWithMultyThreaded() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
String lvEchoStr = null;
String lvLongExecution = null;
for (int i=0;i<3;i++) {
lvLongExecution = e.doLongExecution("Hello");
assertNull(lvLongExecution);
lvEchoStr = e.echo("Hello");
assertNull(lvEchoStr);
}
}
public void testIndivualAsynchronousInvocationWithMultyThreadedWith2Services() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
Echo e = (Echo) manager.createService(Echo.class, lvCallback, null, 8);
Calculator c = (Calculator) manager.createService(Calculator.class, lvCallback, null, 8);
String lvLongExecution = null;
Long lvAddResult = null;
for (int i=0;i<3;i++) {
lvLongExecution = e.doLongExecution("Hello");
assertNull(lvLongExecution);
lvAddResult = c.addLong(new Long(123), new Long(456));
assertNull(lvAddResult);
}
}
public void testIndivualAsynchronousInvocationWithMethodFilter() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4);
String lvEchoStr = null;
String lvLongExecution = null;
for (int i=0;i<3;i++) {
lvLongExecution = e.doLongExecution("Hello");
assertNotNull(lvLongExecution);
assertEquals("Hello", lvLongExecution);
lvEchoStr = e.echo("Hello");
assertNull(lvEchoStr);
}
}
public void testAddAndRemoveAsynchronousCallback() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
AsynchronousCallbackForTests lvCallback = new AsynchronousCallbackForTests();
Echo e = (Echo) manager.createService(Echo.class, lvCallback, new String[] { "doLongExecution" }, 4);
Calculator c = (Calculator) manager.createService(Calculator.class, lvCallback, null, 4);
String lvEchoStr = e.echo("Hello");
assertNull(lvEchoStr);
Long lvAddResult = c.addLong(new Long(1), new Long(3));
assertNull(lvAddResult);
assertTrue(manager.isInvocationAsynchronous(Calculator.class));
manager.removeAsynchronousCallback(Calculator.class);
assertFalse(manager.isInvocationAsynchronous(Calculator.class));
lvEchoStr = e.echo("Hello2");
assertNull(lvEchoStr);
lvAddResult = c.addLong(new Long(2), new Long(3));
assertNotNull(lvAddResult);
assertEquals(new Long(5), lvAddResult);
manager.removeAsynchronousCallback(Echo.class);
lvEchoStr = e.echo("Hello3");
assertNotNull(lvEchoStr);
assertEquals("Hello3", lvEchoStr);
}
public void testRemoteMethodWithThrownExceptionWithValidationErrors() throws Exception {
Properties prop = new Properties();
prop.put(Property.EXECUTOR_CLASS, JBossRemotingExecutor.class.getName());
ServiceManager manager = new ServiceManager(prop);
Echo lvEcho = (Echo) manager.createService(Echo.class);
List lvValidationErrors = new ArrayList();
ValidationError lvError1 = new ValidationError(1, "Error 1");
ValidationError lvError2 = new ValidationError(2, "Error 2");
lvValidationErrors.add(lvError1);
lvValidationErrors.add(lvError2);
try {
lvEcho.throwComplexException("JUnit-Test", lvValidationErrors);
fail("The method throwComplexEception must be thrown a Exception");
} catch (JUnitTestException2 e) {
assertEquals(lvValidationErrors.size(), e.getValidationErrors().size());
}
}
}
|