xref: /trunk/main/bridges/test/com/sun/star/lib/uno/bridges/java_remote/Bug107753_Test.java (revision 3309286857f19787ae62bd793a98b5af4edd2ad3)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.bridges.javaremote;
25 
26 import com.sun.star.bridge.XInstanceProvider;
27 import com.sun.star.lib.TestBed;
28 import com.sun.star.lib.uno.typeinfo.MethodTypeInfo;
29 import com.sun.star.lib.uno.typeinfo.TypeInfo;
30 import com.sun.star.uno.UnoRuntime;
31 import com.sun.star.uno.XComponentContext;
32 import com.sun.star.uno.XInterface;
33 
34 import org.junit.Test;
35 import static org.junit.Assert.*;
36 
37 /**
38  * Test case for bug #107753#.
39  *
40  * <p>Bug #107753# "Java UNO: Proxies should implement intuitive semantics of
41  * equals and hashCode" requests that two proxies are equal iff they represent
42  * the same UNO object.  This implies that if two proxies repsent the same UNO
43  * object, they must have the same hash code.</p>
44  */
45 public final class Bug107753_Test {
46     @Test
test()47     public void test() throws Exception {
48         TestBed t = new TestBed();
49         assertTrue("test", t.execute(new Provider(t), false, Client.class, 0));
50     }
51 
52     public static final class Client extends TestBed.Client {
main(String[] args)53         public static void main(String[] args) {
54             new Client().execute();
55         }
56 
run(XComponentContext context)57         protected boolean run(XComponentContext context) throws Throwable {
58             boolean success = true;
59             XTransport transport = UnoRuntime.queryInterface(
60                 XTransport.class, getBridge(context).getInstance("Transport"));
61 
62             Object obj1a = new XType1() {};
63             XType1 obj1b = UnoRuntime.queryInterface(XType1.class, obj1a);
64             success &= test("obj1a == obj1b", obj1a == obj1b);
65 
66             Object obj2a = new XType2() {};
67             XType2 obj2b = UnoRuntime.queryInterface(XType2.class, obj2a);
68             success &= test("obj2a == obj2b", obj2a == obj2b);
69 
70             Object obj3a = transport.getType1();
71             XType1 obj3b = UnoRuntime.queryInterface(XType1.class, obj3a);
72             success &= test(
73                 "obj3a != obj3b; only meaningful as long as different proxy"
74                 + " instances are used for different UNO interfaces of one UNO"
75                 + " object",
76                 obj3a != obj3b);
77 
78             Object obj4a = transport.getType2();
79             XType2 obj4b = UnoRuntime.queryInterface(XType2.class, obj4a);
80             success &= test(
81                 "obj4a != obj4b; only meaningful as long as different proxy"
82                 + " instances are used for different UNO interfaces of one UNO"
83                 + " object",
84                 obj4a != obj4b);
85 
86             success &= test("UnoRuntime.areSame(null, null)",
87                             UnoRuntime.areSame(null, null));
88             success &= test("!UnoRuntime.areSame(null, obj1a)",
89                             !UnoRuntime.areSame(null, obj1a));
90             success &= test("!UnoRuntime.areSame(null, obj1b)",
91                             !UnoRuntime.areSame(null, obj1b));
92             success &= test("!UnoRuntime.areSame(null, obj2a)",
93                             !UnoRuntime.areSame(null, obj2a));
94             success &= test("!UnoRuntime.areSame(null, obj2b)",
95                             !UnoRuntime.areSame(null, obj2b));
96             success &= test("!UnoRuntime.areSame(null, obj3a)",
97                             !UnoRuntime.areSame(null, obj3a));
98             success &= test("!UnoRuntime.areSame(null, obj3b)",
99                             !UnoRuntime.areSame(null, obj3b));
100             success &= test("!UnoRuntime.areSame(null, obj4a)",
101                             !UnoRuntime.areSame(null, obj4a));
102             success &= test("!UnoRuntime.areSame(null, obj4b)",
103                             !UnoRuntime.areSame(null, obj4b));
104 
105             success &= test("!obj1a.equals(null)", !obj1a.equals(null));
106             success &= test("!UnoRuntime.areSame(obj1a, null)",
107                             !UnoRuntime.areSame(obj1a, null));
108             success &= test("obj1a.equals(obj1a)", obj1a.equals(obj1a));
109             success &= test("UnoRuntime.areSame(obj1a, obj1a)",
110                             UnoRuntime.areSame(obj1a, obj1a));
111             success &= test("obj1a.equals(obj1b)", obj1a.equals(obj1b));
112             success &= test("UnoRuntime.areSame(obj1a, obj1b)",
113                             UnoRuntime.areSame(obj1a, obj1b));
114             success &= test("!obj1a.equals(obj2a)", !obj1a.equals(obj2a));
115             success &= test("!UnoRuntime.areSame(obj1a, obj2a)",
116                             !UnoRuntime.areSame(obj1a, obj2a));
117             success &= test("!obj1a.equals(obj2b)", !obj1a.equals(obj2b));
118             success &= test("!UnoRuntime.areSame(obj1a, obj2b)",
119                             !UnoRuntime.areSame(obj1a, obj2b));
120             success &= test("!obj1a.equals(obj3a)", !obj1a.equals(obj3a));
121             success &= test("!UnoRuntime.areSame(obj1a, obj3a)",
122                             !UnoRuntime.areSame(obj1a, obj3a));
123             success &= test("!obj1a.equals(obj3b)", !obj1a.equals(obj3b));
124             success &= test("!UnoRuntime.areSame(obj1a, obj3b)",
125                             !UnoRuntime.areSame(obj1a, obj3b));
126             success &= test("!obj1a.equals(obj4a)", !obj1a.equals(obj4a));
127             success &= test("!UnoRuntime.areSame(obj1a, obj4a)",
128                             !UnoRuntime.areSame(obj1a, obj4a));
129             success &= test("!obj1a.equals(obj4b)", !obj1a.equals(obj4b));
130             success &= test("!UnoRuntime.areSame(obj1a, obj4b)",
131                             !UnoRuntime.areSame(obj1a, obj4b));
132 
133             success &= test("!obj1b.equals(null)", !obj1b.equals(null));
134             success &= test("!UnoRuntime.areSame(obj1b, null)",
135                             !UnoRuntime.areSame(obj1b, null));
136             success &= test("obj1b.equals(obj1a)", obj1b.equals(obj1a));
137             success &= test("UnoRuntime.areSame(obj1b, obj1a)",
138                             UnoRuntime.areSame(obj1b, obj1a));
139             success &= test("obj1b.equals(obj1b)", obj1b.equals(obj1b));
140             success &= test("UnoRuntime.areSame(obj1b, obj1b)",
141                             UnoRuntime.areSame(obj1b, obj1b));
142             success &= test("!obj1b.equals(obj2a)", !obj1b.equals(obj2a));
143             success &= test("!UnoRuntime.areSame(obj1b, obj2a)",
144                             !UnoRuntime.areSame(obj1b, obj2a));
145             success &= test("!obj1b.equals(obj2b)", !obj1b.equals(obj2b));
146             success &= test("!UnoRuntime.areSame(obj1b, obj2b)",
147                             !UnoRuntime.areSame(obj1b, obj2b));
148             success &= test("!obj1b.equals(obj3a)", !obj1b.equals(obj3a));
149             success &= test("!UnoRuntime.areSame(obj1b, obj3a)",
150                             !UnoRuntime.areSame(obj1b, obj3a));
151             success &= test("!obj1b.equals(obj3b)", !obj1b.equals(obj3b));
152             success &= test("!UnoRuntime.areSame(obj1b, obj3b)",
153                             !UnoRuntime.areSame(obj1b, obj3b));
154             success &= test("!obj1b.equals(obj4a)", !obj1b.equals(obj4a));
155             success &= test("!UnoRuntime.areSame(obj1b, obj4a)",
156                             !UnoRuntime.areSame(obj1b, obj4a));
157             success &= test("!obj1b.equals(obj4b)", !obj1b.equals(obj4b));
158             success &= test("!UnoRuntime.areSame(obj1b, obj4b)",
159                             !UnoRuntime.areSame(obj1b, obj4b));
160 
161             success &= test("!obj2a.equals(null)", !obj2a.equals(null));
162             success &= test("!UnoRuntime.areSame(obj2a, null)",
163                             !UnoRuntime.areSame(obj2a, null));
164             success &= test("!obj2a.equals(obj1a)", !obj2a.equals(obj1a));
165             success &= test("!UnoRuntime.areSame(obj2a, obj1a)",
166                             !UnoRuntime.areSame(obj2a, obj1a));
167             success &= test("!obj2a.equals(obj1b)", !obj2a.equals(obj1b));
168             success &= test("!UnoRuntime.areSame(obj2a, obj1b)",
169                             !UnoRuntime.areSame(obj2a, obj1b));
170             success &= test("obj2a.equals(obj2a)", obj2a.equals(obj2a));
171             success &= test("UnoRuntime.areSame(obj2a, obj2a)",
172                             UnoRuntime.areSame(obj2a, obj2a));
173             success &= test("obj2a.equals(obj2b)", obj2a.equals(obj2b));
174             success &= test("UnoRuntime.areSame(obj2a, obj2b)",
175                             UnoRuntime.areSame(obj2a, obj2b));
176             success &= test("!obj2a.equals(obj3a)", !obj2a.equals(obj3a));
177             success &= test("!UnoRuntime.areSame(obj2a, obj3a)",
178                             !UnoRuntime.areSame(obj2a, obj3a));
179             success &= test("!obj2a.equals(obj3b)", !obj2a.equals(obj3b));
180             success &= test("!UnoRuntime.areSame(obj2a, obj3b)",
181                             !UnoRuntime.areSame(obj2a, obj3b));
182             success &= test("!obj2a.equals(obj4a)", !obj2a.equals(obj4a));
183             success &= test("!UnoRuntime.areSame(obj2a, obj4a)",
184                             !UnoRuntime.areSame(obj2a, obj4a));
185             success &= test("!obj2a.equals(obj4b)", !obj2a.equals(obj4b));
186             success &= test("!UnoRuntime.areSame(obj2a, obj4b)",
187                             !UnoRuntime.areSame(obj2a, obj4b));
188 
189             success &= test("!obj2b.equals(null)", !obj2b.equals(null));
190             success &= test("!UnoRuntime.areSame(obj2b, null)",
191                             !UnoRuntime.areSame(obj2b, null));
192             success &= test("!obj2b.equals(obj1a)", !obj2b.equals(obj1a));
193             success &= test("!UnoRuntime.areSame(obj2b, obj1a)",
194                             !UnoRuntime.areSame(obj2b, obj1a));
195             success &= test("!obj2b.equals(obj1b)", !obj2b.equals(obj1b));
196             success &= test("!UnoRuntime.areSame(obj2b, obj1b)",
197                             !UnoRuntime.areSame(obj2b, obj1b));
198             success &= test("obj2b.equals(obj2a)", obj2b.equals(obj2a));
199             success &= test("UnoRuntime.areSame(obj2b, obj2a)",
200                             UnoRuntime.areSame(obj2b, obj2a));
201             success &= test("obj2b.equals(obj2b)", obj2b.equals(obj2b));
202             success &= test("UnoRuntime.areSame(obj2b, obj2b)",
203                             UnoRuntime.areSame(obj2b, obj2b));
204             success &= test("!obj2b.equals(obj3a)", !obj2b.equals(obj3a));
205             success &= test("!UnoRuntime.areSame(obj2b, obj3a)",
206                             !UnoRuntime.areSame(obj2b, obj3a));
207             success &= test("!obj2b.equals(obj3b)", !obj2b.equals(obj3b));
208             success &= test("!UnoRuntime.areSame(obj2b, obj3b)",
209                             !UnoRuntime.areSame(obj2b, obj3b));
210             success &= test("!obj2b.equals(obj4a)", !obj2b.equals(obj4a));
211             success &= test("!UnoRuntime.areSame(obj2b, obj4a)",
212                             !UnoRuntime.areSame(obj2b, obj4a));
213             success &= test("!obj2b.equals(obj4b)", !obj2b.equals(obj4b));
214             success &= test("!UnoRuntime.areSame(obj2b, obj4b)",
215                             !UnoRuntime.areSame(obj2b, obj4b));
216 
217             success &= test("!obj3a.equals(null)", !obj3a.equals(null));
218             success &= test("!UnoRuntime.areSame(obj3a, null)",
219                             !UnoRuntime.areSame(obj3a, null));
220             success &= test("!obj3a.equals(obj1a)", !obj3a.equals(obj1a));
221             success &= test("!UnoRuntime.areSame(obj3a, obj1a)",
222                             !UnoRuntime.areSame(obj3a, obj1a));
223             success &= test("!obj3a.equals(obj1b)", !obj3a.equals(obj1b));
224             success &= test("!UnoRuntime.areSame(obj3a, obj1b)",
225                             !UnoRuntime.areSame(obj3a, obj1b));
226             success &= test("!obj3a.equals(obj2a)", !obj3a.equals(obj2a));
227             success &= test("!UnoRuntime.areSame(obj3a, obj2a)",
228                             !UnoRuntime.areSame(obj3a, obj2a));
229             success &= test("!obj3a.equals(obj2b)", !obj3a.equals(obj2b));
230             success &= test("!UnoRuntime.areSame(obj3a, obj2b)",
231                             !UnoRuntime.areSame(obj3a, obj2b));
232             success &= test("obj3a.equals(obj3a)", obj3a.equals(obj3a));
233             success &= test("UnoRuntime.areSame(obj3a, obj3a)",
234                             UnoRuntime.areSame(obj3a, obj3a));
235             success &= test("obj3a.equals(obj3b)", obj3a.equals(obj3b));
236             success &= test("UnoRuntime.areSame(obj3a, obj3b)",
237                             UnoRuntime.areSame(obj3a, obj3b));
238             success &= test("!obj3a.equals(obj4a)", !obj3a.equals(obj4a));
239             success &= test("!UnoRuntime.areSame(obj3a, obj4a)",
240                             !UnoRuntime.areSame(obj3a, obj4a));
241             success &= test("!obj3a.equals(obj4b)", !obj3a.equals(obj4b));
242             success &= test("!UnoRuntime.areSame(obj3a, obj4b)",
243                             !UnoRuntime.areSame(obj3a, obj4b));
244 
245             success &= test("!obj3b.equals(null)", !obj3b.equals(null));
246             success &= test("!UnoRuntime.areSame(obj3b, null)",
247                             !UnoRuntime.areSame(obj3b, null));
248             success &= test("!obj3b.equals(obj1a)", !obj3b.equals(obj1a));
249             success &= test("!UnoRuntime.areSame(obj3b, obj1a)",
250                             !UnoRuntime.areSame(obj3b, obj1a));
251             success &= test("!obj3b.equals(obj1b)", !obj3b.equals(obj1b));
252             success &= test("!UnoRuntime.areSame(obj3b, obj1b)",
253                             !UnoRuntime.areSame(obj3b, obj1b));
254             success &= test("!obj3b.equals(obj2a)", !obj3b.equals(obj2a));
255             success &= test("!UnoRuntime.areSame(obj3b, obj2a)",
256                             !UnoRuntime.areSame(obj3b, obj2a));
257             success &= test("!obj3b.equals(obj2b)", !obj3b.equals(obj2b));
258             success &= test("!UnoRuntime.areSame(obj3b, obj2b)",
259                             !UnoRuntime.areSame(obj3b, obj2b));
260             success &= test("obj3b.equals(obj3a)", obj3b.equals(obj3a));
261             success &= test("UnoRuntime.areSame(obj3b, obj3a)",
262                             UnoRuntime.areSame(obj3b, obj3a));
263             success &= test("obj3b.equals(obj3b)", obj3b.equals(obj3b));
264             success &= test("UnoRuntime.areSame(obj3b, obj3b)",
265                             UnoRuntime.areSame(obj3b, obj3b));
266             success &= test("!obj3b.equals(obj4a)", !obj3b.equals(obj4a));
267             success &= test("!UnoRuntime.areSame(obj3b, obj4a)",
268                             !UnoRuntime.areSame(obj3b, obj4a));
269             success &= test("!obj3b.equals(obj4b)", !obj3b.equals(obj4b));
270             success &= test("!UnoRuntime.areSame(obj3b, obj4b)",
271                             !UnoRuntime.areSame(obj3b, obj4b));
272 
273             success &= test("!obj4a.equals(null)", !obj4a.equals(null));
274             success &= test("!UnoRuntime.areSame(obj4a, null)",
275                             !UnoRuntime.areSame(obj4a, null));
276             success &= test("!obj4a.equals(obj1a)", !obj4a.equals(obj1a));
277             success &= test("!UnoRuntime.areSame(obj4a, obj1a)",
278                             !UnoRuntime.areSame(obj4a, obj1a));
279             success &= test("!obj4a.equals(obj1b)", !obj4a.equals(obj1b));
280             success &= test("!UnoRuntime.areSame(obj4a, obj1b)",
281                             !UnoRuntime.areSame(obj4a, obj1b));
282             success &= test("!obj4a.equals(obj2a)", !obj4a.equals(obj2a));
283             success &= test("!UnoRuntime.areSame(obj4a, obj2a)",
284                             !UnoRuntime.areSame(obj4a, obj2a));
285             success &= test("!obj4a.equals(obj2b)", !obj4a.equals(obj2b));
286             success &= test("!UnoRuntime.areSame(obj4a, obj2b)",
287                             !UnoRuntime.areSame(obj4a, obj2b));
288             success &= test("!obj4a.equals(obj3a)", !obj4a.equals(obj3a));
289             success &= test("!UnoRuntime.areSame(obj4a, obj3a)",
290                             !UnoRuntime.areSame(obj4a, obj3a));
291             success &= test("!obj4a.equals(obj3b)", !obj4a.equals(obj3b));
292             success &= test("!UnoRuntime.areSame(obj4a, obj3b)",
293                             !UnoRuntime.areSame(obj4a, obj3b));
294             success &= test("obj4a.equals(obj4a)", obj4a.equals(obj4a));
295             success &= test("UnoRuntime.areSame(obj4a, obj4a)",
296                             UnoRuntime.areSame(obj4a, obj4a));
297             success &= test("obj4a.equals(obj4b)", obj4a.equals(obj4b));
298             success &= test("UnoRuntime.areSame(obj4a, obj4b)",
299                             UnoRuntime.areSame(obj4a, obj4b));
300 
301             success &= test("!obj4b.equals(null)", !obj4b.equals(null));
302             success &= test("!UnoRuntime.areSame(obj4b, null)",
303                             !UnoRuntime.areSame(obj4b, null));
304             success &= test("!obj4b.equals(obj1a)", !obj4b.equals(obj1a));
305             success &= test("!UnoRuntime.areSame(obj4b, obj1a)",
306                             !UnoRuntime.areSame(obj4b, obj1a));
307             success &= test("!obj4b.equals(obj1b)", !obj4b.equals(obj1b));
308             success &= test("!UnoRuntime.areSame(obj4b, obj1b)",
309                             !UnoRuntime.areSame(obj4b, obj1b));
310             success &= test("!obj4b.equals(obj2a)", !obj4b.equals(obj2a));
311             success &= test("!UnoRuntime.areSame(obj4b, obj2a)",
312                             !UnoRuntime.areSame(obj4b, obj2a));
313             success &= test("!obj4b.equals(obj2b)", !obj4b.equals(obj2b));
314             success &= test("!UnoRuntime.areSame(obj4b, obj2b)",
315                             !UnoRuntime.areSame(obj4b, obj2b));
316             success &= test("!obj4b.equals(obj3a)", !obj4b.equals(obj3a));
317             success &= test("!UnoRuntime.areSame(obj4b, obj3a)",
318                             !UnoRuntime.areSame(obj4b, obj3a));
319             success &= test("!obj4b.equals(obj3b)", !obj4b.equals(obj3b));
320             success &= test("!UnoRuntime.areSame(obj4b, obj3b)",
321                             !UnoRuntime.areSame(obj4b, obj3b));
322             success &= test("obj4b.equals(obj4a)", obj4b.equals(obj4a));
323             success &= test("UnoRuntime.areSame(obj4b, obj4a)",
324                             UnoRuntime.areSame(obj4b, obj4a));
325             success &= test("obj4b.equals(obj4b)", obj4b.equals(obj4b));
326             success &= test("UnoRuntime.areSame(obj4b, obj4b)",
327                             UnoRuntime.areSame(obj4b, obj4b));
328 
329             success &= test("obj1a.hashCode() == obj1b.hashCode()",
330                             obj1a.hashCode() == obj1b.hashCode());
331             success &= test("obj2a.hashCode() == obj2b.hashCode()",
332                             obj2a.hashCode() == obj2b.hashCode());
333             success &= test("obj3a.hashCode() == obj3b.hashCode()",
334                             obj3a.hashCode() == obj3b.hashCode());
335             success &= test("obj4a.hashCode() == obj4b.hashCode()",
336                             obj4a.hashCode() == obj4b.hashCode());
337 
338             return success;
339         }
340 
test(String message, boolean condition)341         private static boolean test(String message, boolean condition) {
342             if (!condition) {
343                 System.err.println("Failed: " + message);
344             }
345             return condition;
346         }
347     }
348 
349     private static final class Provider implements XInstanceProvider {
Provider(TestBed testBed)350         public Provider(TestBed testBed) {
351             this.testBed = testBed;
352         }
353 
getInstance(String instanceName)354         public Object getInstance(String instanceName) {
355             return new XTransport() {
356                     public Object getType1() {
357                         return new XType1() {};
358                     }
359 
360                     public Object getType2() {
361                         return new XType2() {};
362                     }
363                 };
364         }
365 
366         private final TestBed testBed;
367     }
368 
369     public interface XTransport extends XInterface {
370         Object getType1();
371 
372         Object getType2();
373 
374         TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("getType1", 0, 0),
375                                    new MethodTypeInfo("getType2", 1, 0) };
376     }
377 
378     public interface XType1 extends XInterface {
379         TypeInfo[] UNOTYPEINFO = null;
380     }
381 
382     public interface XType2 extends XInterface {
383         TypeInfo[] UNOTYPEINFO = null;
384     }
385 }
386