KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > retrotranslator > runtime > java > rmi > server > RemoteObjectInvocationHandler_TestCase


1 /***
2  * Retrotranslator: a Java bytecode transformer that translates Java classes
3  * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
4  *
5  * Copyright (c) 2005 - 2007 Taras Puchko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the copyright holders nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30  * THE POSSIBILITY OF SUCH DAMAGE.
31  */

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 JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import junit.framework.TestCase;
41
42 /**
43  * @author Taras Puchko
44  */

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 JavaDoc[]{MyRemote.class}, new RemoteObjectInvocationHandler(ref));
50
51     public void testRemoteObjectInvocationHandler_() throws Exception JavaDoc {
52         try {
53             new RemoteObjectInvocationHandler(null);
54             fail();
55         } catch (NullPointerException JavaDoc e) {
56             //ok
57
}
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 JavaDoc[]{MyRemote.class}, new RemoteObjectInvocationHandler(ref))));
71         assertEquals("remoteEquals", ref.log());
72
73         assertFalse(myRemote.equals(Proxy.newProxyInstance(MyRemote.class.getClassLoader(),
74                 new Class JavaDoc[]{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 JavaDoc handlerName = RemoteObjectInvocationHandler.class.getSimpleName();
86
87         assertEquals("Proxy[RemoteObjectInvocationHandler_TestCase$MyRemote," + handlerName + "[<Mock>]]",
88                 myRemote.toString());
89         assertEquals("remoteToString", ref.log());
90
91         Object JavaDoc noInterface = Proxy.newProxyInstance(getClass().getClassLoader(),
92                 new Class JavaDoc[]{}, new RemoteObjectInvocationHandler(ref));
93         assertEquals("Proxy[" + handlerName + "[<Mock>]]", noInterface.toString());
94
95         Object JavaDoc remoteInterface = Proxy.newProxyInstance(getClass().getClassLoader(),
96                 new Class JavaDoc[]{Remote.class}, new RemoteObjectInvocationHandler(ref));
97         assertEquals("Proxy[Remote," + handlerName + "[<Mock>]]", remoteInterface.toString());
98
99         Object JavaDoc comparableInterface = Proxy.newProxyInstance(getClass().getClassLoader(),
100                 new Class JavaDoc[]{Remote.class, Comparable JavaDoc.class}, new RemoteObjectInvocationHandler(ref));
101         assertEquals("Proxy[Comparable," + handlerName + "[<Mock>]]", comparableInterface.toString());
102
103     }
104
105     public void testInvoke_nonRemote() {
106         Runnable JavaDoc runnable = (Runnable JavaDoc) Proxy.newProxyInstance(getClass().getClassLoader(),
107                 new Class JavaDoc[]{Runnable JavaDoc.class}, new RemoteObjectInvocationHandler(new MockRemoteRef(new Thread JavaDoc())));
108         try {
109             runnable.run();
110             fail();
111         } catch (IllegalArgumentException JavaDoc e) {
112             //ok
113
}
114     }
115
116     public void testInvoke_remote() throws Exception JavaDoc {
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 JavaDoc {
125         InternalError JavaDoc internalError = new InternalError JavaDoc();
126         try {
127             myRemote.exception(internalError);
128             fail();
129         } catch (InternalError JavaDoc e) {
130             assertSame(e, internalError);
131         }
132         assertEquals("invoke: exception [java.lang.InternalError] 4977354682513654574", ref.log());
133         ArithmeticException JavaDoc arithmeticException = new ArithmeticException JavaDoc();
134         try {
135             myRemote.exception(arithmeticException);
136             fail();
137         } catch (ArithmeticException JavaDoc 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 JavaDoc namingException = new NamingException JavaDoc();
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 JavaDoc hello(String JavaDoc name) throws RemoteException;
162
163         int add(int a, int b) throws RemoteException;
164
165         void exception(Throwable JavaDoc t) throws IOException;
166     }
167
168     private static class Thrower {
169
170         public static ThreadLocal JavaDoc<Throwable JavaDoc> result = new ThreadLocal JavaDoc<Throwable JavaDoc>();
171
172         public Thrower() throws Throwable JavaDoc {
173             Throwable JavaDoc throwable = result.get();
174             result.set(null);
175             throw throwable;
176         }
177
178         public static void rethrow(Throwable JavaDoc t) {
179             try {
180                 Thrower.result.set(t);
181                 Thrower.class.newInstance();
182                 throw new Error JavaDoc(t);
183             } catch (InstantiationException JavaDoc e) {
184                 throw new Error JavaDoc(e);
185             } catch (IllegalAccessException JavaDoc e) {
186                 throw new Error JavaDoc(e);
187             }
188         }
189     }
190
191     private class MyRemoteImpl implements MyRemote {
192
193         public String JavaDoc hello(String JavaDoc 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 JavaDoc t) throws IOException {
202             Thrower.rethrow(t);
203         }
204     }
205
206     private class MockRemoteRef implements RemoteRef {
207
208         private Object JavaDoc delegate;
209         private StringBuilder JavaDoc log = new StringBuilder JavaDoc();
210
211         public MockRemoteRef() {
212         }
213
214         public MockRemoteRef(Object JavaDoc delegate) {
215             this.delegate = delegate;
216         }
217
218         public String JavaDoc log() {
219             try {
220                 return log.toString();
221             } finally {
222                 log.setLength(0);
223             }
224         }
225
226         public Object JavaDoc invoke(Remote obj, Method method, Object JavaDoc[] params, long opnum) throws Exception JavaDoc {
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 JavaDoc(e);
234             }
235         }
236
237         public RemoteCall newCall(RemoteObject obj, Operation[] op, int opnum, long hash) throws RemoteException {
238             throw new UnsupportedOperationException JavaDoc();
239         }
240
241         public void invoke(RemoteCall call) throws Exception JavaDoc {
242             throw new UnsupportedOperationException JavaDoc();
243         }
244
245         public void done(RemoteCall call) throws RemoteException {
246             throw new UnsupportedOperationException JavaDoc();
247         }
248
249         public String JavaDoc getRefClass(ObjectOutput out) {
250             throw new UnsupportedOperationException JavaDoc();
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 JavaDoc remoteToString() {
264             log.append("remoteToString");
265             return "<Mock>";
266         }
267
268         public void writeExternal(ObjectOutput out) throws IOException {
269             throw new UnsupportedOperationException JavaDoc();
270         }
271
272         public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException JavaDoc {
273             throw new UnsupportedOperationException JavaDoc();
274         }
275     }
276 }
Popular Tags