| 1 32 package net.sf.retrotranslator.runtime.java.rmi.server; 33 34 import java.io.*; 35 import java.lang.reflect.*; 36 import java.rmi.*; 37 import java.rmi.server.*; 38 import java.util.Arrays ; 39 import javax.naming.NamingException ; 40 import junit.framework.TestCase; 41 42 45 public class RemoteObjectInvocationHandler_TestCase extends TestCase { 46 47 private MockRemoteRef ref = new MockRemoteRef(new MyRemoteImpl()); 48 private MyRemote myRemote = (MyRemote) Proxy.newProxyInstance(MyRemote.class.getClassLoader(), 49 new Class []{MyRemote.class}, new RemoteObjectInvocationHandler(ref)); 50 51 public void testRemoteObjectInvocationHandler_() throws Exception { 52 try { 53 new RemoteObjectInvocationHandler(null); 54 fail(); 55 } catch (NullPointerException e) { 56 } 58 } 59 60 public void testInvoke_hashcode() { 61 assertEquals(1234567890, myRemote.hashCode()); 62 assertEquals("remoteHashCode", ref.log()); 63 } 64 65 public void testInvoke_equals() { 66 assertTrue(myRemote.equals(myRemote)); 67 assertEquals("", ref.log()); 68 69 assertTrue(myRemote.equals(Proxy.newProxyInstance(MyRemote.class.getClassLoader(), 70 new Class []{MyRemote.class}, new RemoteObjectInvocationHandler(ref)))); 71 assertEquals("remoteEquals", ref.log()); 72 73 assertFalse(myRemote.equals(Proxy.newProxyInstance(MyRemote.class.getClassLoader(), 74 new Class []{MyRemote.class}, new RemoteObjectInvocationHandler(new MockRemoteRef())))); 75 assertEquals("remoteEquals", ref.log()); 76 77 assertFalse(myRemote.equals(new MyRemoteImpl())); 78 assertEquals("", ref.log()); 79 80 assertFalse(myRemote.equals(null)); 81 assertEquals("", ref.log()); 82 } 83 84 public void testInvoke_toString() { 85 String handlerName = RemoteObjectInvocationHandler.class.getSimpleName(); 86 87 assertEquals("Proxy[RemoteObjectInvocationHandler_TestCase$MyRemote," + handlerName + "[<Mock>]]", 88 myRemote.toString()); 89 assertEquals("remoteToString", ref.log()); 90 91 Object noInterface = Proxy.newProxyInstance(getClass().getClassLoader(), 92 new Class []{}, new RemoteObjectInvocationHandler(ref)); 93 assertEquals("Proxy[" + handlerName + "[<Mock>]]", noInterface.toString()); 94 95 Object remoteInterface = Proxy.newProxyInstance(getClass().getClassLoader(), 96 new Class []{Remote.class}, new RemoteObjectInvocationHandler(ref)); 97 assertEquals("Proxy[Remote," + handlerName + "[<Mock>]]", remoteInterface.toString()); 98 99 Object comparableInterface = Proxy.newProxyInstance(getClass().getClassLoader(), 100 new Class []{Remote.class, Comparable .class}, new RemoteObjectInvocationHandler(ref)); 101 assertEquals("Proxy[Comparable," + handlerName + "[<Mock>]]", comparableInterface.toString()); 102 103 } 104 105 public void testInvoke_nonRemote() { 106 Runnable runnable = (Runnable ) Proxy.newProxyInstance(getClass().getClassLoader(), 107 new Class []{Runnable .class}, new RemoteObjectInvocationHandler(new MockRemoteRef(new Thread ()))); 108 try { 109 runnable.run(); 110 fail(); 111 } catch (IllegalArgumentException e) { 112 } 114 } 115 116 public void testInvoke_remote() throws Exception { 117 assertEquals("Hello, World!", myRemote.hello("World")); 118 assertEquals("invoke: hello [World] -5976794856777945295", ref.log()); 119 120 assertEquals(3, myRemote.add(1, 2)); 121 assertEquals("invoke: add [1, 2] -7734458262622125146", ref.log()); 122 } 123 124 public void testInvoke_exceptions() throws Exception { 125 InternalError internalError = new InternalError (); 126 try { 127 myRemote.exception(internalError); 128 fail(); 129 } catch (InternalError e) { 130 assertSame(e, internalError); 131 } 132 assertEquals("invoke: exception [java.lang.InternalError] 4977354682513654574", ref.log()); 133 ArithmeticException arithmeticException = new ArithmeticException (); 134 try { 135 myRemote.exception(arithmeticException); 136 fail(); 137 } catch (ArithmeticException e) { 138 assertSame(e, arithmeticException); 139 } 140 assertEquals("invoke: exception [java.lang.ArithmeticException] 4977354682513654574", ref.log()); 141 FileNotFoundException fileNotFoundException = new FileNotFoundException(); 142 try { 143 myRemote.exception(fileNotFoundException); 144 fail(); 145 } catch (FileNotFoundException e) { 146 assertSame(e, fileNotFoundException); 147 } 148 assertEquals("invoke: exception [java.io.FileNotFoundException] 4977354682513654574", ref.log()); 149 NamingException namingException = new NamingException (); 150 try { 151 myRemote.exception(namingException); 152 fail(); 153 } catch (UnexpectedException e) { 154 assertSame(e.getCause(), namingException); 155 } 156 assertEquals("invoke: exception [javax.naming.NamingException] 4977354682513654574", ref.log()); 157 } 158 159 private interface MyRemote extends Remote { 160 161 String hello(String name) throws RemoteException; 162 163 int add(int a, int b) throws RemoteException; 164 165 void exception(Throwable t) throws IOException; 166 } 167 168 private static class Thrower { 169 170 public static ThreadLocal <Throwable > result = new ThreadLocal <Throwable >(); 171 172 public Thrower() throws Throwable { 173 Throwable throwable = result.get(); 174 result.set(null); 175 throw throwable; 176 } 177 178 public static void rethrow(Throwable t) { 179 try { 180 Thrower.result.set(t); 181 Thrower.class.newInstance(); 182 throw new Error (t); 183 } catch (InstantiationException e) { 184 throw new Error (e); 185 } catch (IllegalAccessException e) { 186 throw new Error (e); 187 } 188 } 189 } 190 191 private class MyRemoteImpl implements MyRemote { 192 193 public String hello(String name) { 194 return "Hello, " + name + "!"; 195 } 196 197 public int add(int a, int b) { 198 return a + b; 199 } 200 201 public void exception(Throwable t) throws IOException { 202 Thrower.rethrow(t); 203 } 204 } 205 206 private class MockRemoteRef implements RemoteRef { 207 208 private Object delegate; 209 private StringBuilder log = new StringBuilder (); 210 211 public MockRemoteRef() { 212 } 213 214 public MockRemoteRef(Object delegate) { 215 this.delegate = delegate; 216 } 217 218 public String log() { 219 try { 220 return log.toString(); 221 } finally { 222 log.setLength(0); 223 } 224 } 225 226 public Object invoke(Remote obj, Method method, Object [] params, long opnum) throws Exception { 227 log.append("invoke: ").append(method.getName()).append(' '). 228 append(Arrays.toString(params)).append(' ').append(opnum); 229 try { 230 return method.invoke(delegate, params); 231 } catch (InvocationTargetException e) { 232 Thrower.rethrow(e.getTargetException()); 233 throw new Error (e); 234 } 235 } 236 237 public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException { 238 throw new UnsupportedOperationException (); 239 } 240 241 public void invoke(RemoteCall call) throws Exception { 242 throw new UnsupportedOperationException (); 243 } 244 245 public void done(RemoteCall call) throws RemoteException { 246 throw new UnsupportedOperationException (); 247 } 248 249 public String getRefClass(ObjectOutput out) { 250 throw new UnsupportedOperationException (); 251 } 252 253 public int remoteHashCode() { 254 log.append("remoteHashCode"); 255 return 1234567890; 256 } 257 258 public boolean remoteEquals(RemoteRef obj) { 259 log.append("remoteEquals"); 260 return obj == this; 261 } 262 263 public String remoteToString() { 264 log.append("remoteToString"); 265 return "<Mock>"; 266 } 267 268 public void writeExternal(ObjectOutput out) throws IOException { 269 throw new UnsupportedOperationException (); 270 } 271 272 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { 273 throw new UnsupportedOperationException (); 274 } 275 } 276 } | Popular Tags |