Author: ron.sigal(a)jboss.com
Date: 2010-09-07 15:44:50 -0400 (Tue, 07 Sep 2010)
New Revision: 6089
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ServiceRegistrationTestCase.java
Log:
JBREM-1228: Extends RemotingRootTestBase instead of RemotingTestBase; added new tests.
Modified:
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ServiceRegistrationTestCase.java
===================================================================
---
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ServiceRegistrationTestCase.java 2010-09-07
19:37:19 UTC (rev 6088)
+++
remoting3/trunk/jboss-remoting/src/test/java/org/jboss/remoting3/test/ServiceRegistrationTestCase.java 2010-09-07
19:44:50 UTC (rev 6089)
@@ -25,6 +25,7 @@
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertSame;
+import static org.testng.Assert.fail;
import java.io.IOException;
import java.util.HashMap;
@@ -38,6 +39,7 @@
import org.jboss.remoting3.RemotingOptions;
import org.jboss.remoting3.RequestContext;
import org.jboss.remoting3.RequestListener;
+import org.jboss.remoting3.ServiceRegistrationException;
import org.jboss.remoting3.ServiceRegistrationListener;
import org.jboss.remoting3.Endpoint.ServiceBuilder;
import org.jboss.xnio.OptionMap;
@@ -58,7 +60,7 @@
* @param <O>
*/
@Test(suiteName = "ServiceRegistration")
-public class ServiceRegistrationTestCase<O> extends RemotingTestBase {
+public class ServiceRegistrationTestCase<O> extends RemotingRootTestBase {
private static final Logger log =
Logger.getLogger(ServiceRegistrationTestCase.class);
private static Object lock = new Object();
@@ -71,12 +73,50 @@
public void tearDown() throws IOException {
}
+ @SuppressWarnings("unchecked")
@Test
+ public void testRegistrationErrors() throws Exception {
+ enter();
+
+ Endpoint endpoint = Remoting.getConfiguredEndpoint();
+ final ServiceBuilder<?, ?> sb = endpoint.serviceBuilder();
+ sb.setInstanceName("instance");
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ sb.setServiceType("service");
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.setRequestType(null);}});
+ sb.setRequestType(RequestType.class);
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.setReplyType(null);}});
+ sb.setReplyType(ReplyType.class);
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.setOptionMap(null);}});
+ final ServiceBuilder<RequestType, ReplyType> sb2 =
(ServiceBuilder<RequestType, ReplyType>) sb;
+ sb2.setClientListener(new TestClientListener());
+ OptionMap optionMap = OptionMap.builder().set(RemotingOptions.METRIC,
-1).getMap();
+ sb2.setOptionMap(optionMap);
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ sb.setOptionMap(OptionMap.EMPTY);
+ sb.setInstanceName(":instance");
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ sb.setInstanceName("instance");
+ sb.setServiceType("0service");
+ testIllegalArgumentException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ sb.setServiceType("service");
+ sb.register();
+ testServiceRegistrationException(sb, new TestAction() {public void run() throws
Exception {sb.register();}});
+ }
+
+ @Test
public void testRegistration() throws Exception {
enter();
Endpoint endpoint = Remoting.getConfiguredEndpoint();
- TestServiceRegistrationListener registrationListener = new
TestServiceRegistrationListener();
- endpoint.addServiceRegistrationListener(registrationListener, null);
+
+ // Register listener before registering a service.
+ TestServiceRegistrationListener registrationListener1 = new
TestServiceRegistrationListener();
+ Registration serviceListenerRegistration =
endpoint.addServiceRegistrationListener(registrationListener1, null);
+
+ // Register a service.
TestClassLoader testClassLoader = new TestClassLoader();
ClientListener<RequestType, ReplyType> testClientListener = new
TestClientListener();
Registration registration = null;
@@ -92,21 +132,41 @@
sb.setOptionMap(optionMap);
sb.setClientListener(testClientListener);
registration = sb.register();
+
+ // Test listener.
+ synchronized (lock) {
+ lock.wait();
+ }
+ assertNotNull(registrationListener1.info);
+ assertSame(testClassLoader,
registrationListener1.info.getServiceClassLoader());
+ assertEquals("instance",
registrationListener1.info.getInstanceName());
+ assertEquals("service", registrationListener1.info.getServiceType());
+ assertEquals(SuperRequestType.class,
registrationListener1.info.getRequestClass());
+ assertEquals(SubReplyType.class, registrationListener1.info.getReplyClass());
+ assertSame(optionMap, registrationListener1.info.getOptionMap());
+ // Register a listener after registering a service.
+ TestServiceRegistrationListener registrationListener2 = new
TestServiceRegistrationListener();
+ endpoint.addServiceRegistrationListener(registrationListener2, null);
+
+ // Test listener.
synchronized (lock) {
lock.wait();
}
- assertNotNull(registrationListener.info);
- assertSame(testClassLoader, registrationListener.info.getServiceClassLoader());
- assertEquals("instance",
registrationListener.info.getInstanceName());
- assertEquals("service", registrationListener.info.getServiceType());
- assertEquals(SuperRequestType.class,
registrationListener.info.getRequestClass());
- assertEquals(SubReplyType.class, registrationListener.info.getReplyClass());
- assertSame(optionMap, registrationListener.info.getOptionMap());
+ assertNotNull(registrationListener2.info);
+ assertSame(testClassLoader,
registrationListener2.info.getServiceClassLoader());
+ assertEquals("instance",
registrationListener2.info.getInstanceName());
+ assertEquals("service", registrationListener2.info.getServiceType());
+ assertEquals(SuperRequestType.class,
registrationListener2.info.getRequestClass());
+ assertEquals(SubReplyType.class, registrationListener2.info.getReplyClass());
+ assertSame(optionMap, registrationListener2.info.getOptionMap());
- log.info(getName() + " PASSES");
-
+ log.debug(getName() + " PASSES");
+
} finally {
+ if (serviceListenerRegistration != null) {
+ serviceListenerRegistration.close();
+ }
if (registration != null) {
registration.close();
}
@@ -116,7 +176,40 @@
exit();
}
}
-
+
+ protected void testIllegalArgumentException(ServiceBuilder<?, ?> sb, TestAction
action) {
+ try {
+ action.run();
+ fail("expected IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ // good
+ } catch (Exception e) {
+ fail("expected IllegalArgumentException");
+ }
+ }
+
+ protected void testServiceRegistrationException(ServiceBuilder<?, ?> sb,
TestAction action) {
+ try {
+ action.run();
+ fail("expected ServiceRegistrationException");
+ } catch (ServiceRegistrationException e) {
+ // good
+ } catch (Exception e) {
+ fail("expected ServiceRegistrationException");
+ }
+ }
+
+ protected void testForNullPointerException(ServiceBuilder<RequestType,
ReplyType> sb) {
+ try {
+ sb.register();
+ fail("expected NullPointerException");
+ } catch (NullPointerException e) {
+ // good
+ } catch (Exception e) {
+ fail("expected NullPointerException");
+ }
+ }
+
protected OptionMap buildOptionMap() {
Builder builder = OptionMap.builder();
builder.set(RemotingOptions.METRIC, 1);
@@ -127,7 +220,11 @@
builder.add(map);
return builder.getMap();
}
-
+
+ interface TestAction {
+ void run() throws Exception;
+ }
+
static class TestClassLoader extends ClassLoader {
}
@@ -158,6 +255,7 @@
public ServiceInfo info;
public void serviceRegistered(Registration listenerHandle, ServiceInfo info) {
+ log.info(this + ".serviceRegistered() entered");
this.info = info;
synchronized (lock) {
lock.notify();