📄 rmisupporttests.java
字号:
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
factory.setServiceInterface(IBusinessBean.class);
factory.setServiceUrl("rmi://localhost:1090/test");
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IBusinessBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
assertFalse(proxy instanceof IRemoteBean);
try {
proxy.setName(rmiExceptionClass.getName());
fail("Should have thrown " + rmiExceptionClass.getName());
}
catch (Exception ex) {
if (springExceptionClass.isInstance(ex)) {
// expected
}
else {
throw ex;
}
}
assertEquals(1, factory.counter);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndRemoteExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
RemoteException.class, RemoteAccessException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
ConnectException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndConnectIOExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
ConnectIOException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnknownHostExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
UnknownHostException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndNoSuchObjectExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
NoSuchObjectException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndStubNotFoundExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
StubNotFoundException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndMarshalExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
MarshalException.class, RemoteConnectFailureException.class);
}
public void testRmiProxyFactoryBeanWithBusinessInterfaceAndUnmarshalExceptionAndRefresh() throws Exception {
doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
UnmarshalException.class, RemoteConnectFailureException.class);
}
private void doTestRmiProxyFactoryBeanWithBusinessInterfaceAndExceptionAndRefresh(
Class rmiExceptionClass, Class springExceptionClass) throws Exception {
CountingRmiProxyFactoryBean factory = new CountingRmiProxyFactoryBean();
factory.setServiceInterface(IBusinessBean.class);
factory.setServiceUrl("rmi://localhost:1090/test");
factory.setRefreshStubOnConnectFailure(true);
factory.afterPropertiesSet();
assertTrue(factory.getObject() instanceof IBusinessBean);
IBusinessBean proxy = (IBusinessBean) factory.getObject();
assertFalse(proxy instanceof IRemoteBean);
try {
proxy.setName(rmiExceptionClass.getName());
fail("Should have thrown " + rmiExceptionClass.getName());
}
catch (Exception ex) {
if (springExceptionClass.isInstance(ex)) {
// expected
}
else {
throw ex;
}
}
if (RemoteConnectFailureException.class.isAssignableFrom(springExceptionClass)) {
assertEquals(2, factory.counter);
}
else {
assertEquals(1, factory.counter);
}
}
public void testRmiClientInterceptorRequiresUrl() throws Exception{
RmiClientInterceptor client = new RmiClientInterceptor();
client.setServiceInterface(IRemoteBean.class);
try {
client.afterPropertiesSet();
fail("url isn't set, expected IllegalArgumentException");
}
catch(IllegalArgumentException e){
// expected
}
}
public void testRemoteInvocation() throws NoSuchMethodException {
// let's see if the remote invocation object works
RemoteBean rb = new RemoteBean();
MethodInvocation mi = new ReflectiveMethodInvocation(
rb, rb, rb.getClass().getDeclaredMethod("setName", new Class[] {String.class}),
new Object[] { "bla" }, RemoteBean.class, new ArrayList());
RemoteInvocation inv = new RemoteInvocation(mi);
assertEquals("setName", inv.getMethodName());
assertEquals("bla", inv.getArguments()[0]);
assertEquals(String.class, inv.getParameterTypes()[0]);
// this is a bit BS, but we need to test it
inv = new RemoteInvocation();
inv.setArguments(new Object[] { "bla" });
assertEquals("bla", inv.getArguments()[0]);
inv.setMethodName("setName");
assertEquals("setName", inv.getMethodName());
inv.setParameterTypes(new Class[] {String.class});
assertEquals(String.class, inv.getParameterTypes()[0]);
inv = new RemoteInvocation("setName", new Class[] {String.class}, new Object[] {"bla"});
assertEquals("bla", inv.getArguments()[0]);
assertEquals("setName", inv.getMethodName());
assertEquals(String.class, inv.getParameterTypes()[0]);
}
public void testRmiInvokerWithSpecialLocalMethods() throws Exception {
String serviceUrl = "rmi://localhost:1090/test";
RmiProxyFactoryBean factory = new RmiProxyFactoryBean() {
protected Remote lookupStub() throws Exception {
return new RmiInvocationHandler() {
public Object invoke(RemoteInvocation invocation) throws RemoteException {
throw new RemoteException();
}
};
}
};
factory.setServiceInterface(IBusinessBean.class);
factory.setServiceUrl(serviceUrl);
factory.afterPropertiesSet();
IBusinessBean proxy = (IBusinessBean) factory.getObject();
// shouldn't go through to remote service
assertTrue(proxy.toString().indexOf("RMI invoker") != -1);
assertTrue(proxy.toString().indexOf(serviceUrl) != -1);
assertEquals(proxy.hashCode(), proxy.hashCode());
assertTrue(proxy.equals(proxy));
// should go through
try {
proxy.setName("test");
fail("Should have thrown RemoteAccessException");
}
catch (RemoteAccessException ex) {
// expected
}
}
private static class CountingRmiProxyFactoryBean extends RmiProxyFactoryBean {
private int counter = 0;
protected Remote lookupStub() {
counter++;
return new RemoteBean();
}
}
public static interface IBusinessBean {
public void setName(String name);
}
public static interface IRemoteBean extends Remote {
public void setName(String name) throws RemoteException;
}
public static class RemoteBean implements IRemoteBean {
private static String name;
public void setName(String nam) throws RemoteException {
if (nam != null && nam.endsWith("Exception")) {
RemoteException rex = null;
try {
Class exClass = Class.forName(nam);
Constructor ctor = exClass.getConstructor(new Class[] {String.class});
rex = (RemoteException) ctor.newInstance(new Object[] {"myMessage"});
}
catch (Exception ex) {
throw new RemoteException("Illegal exception class name: " + nam, ex);
}
throw rex;
}
name = nam;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -