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.uno;
25 
26 import com.sun.star.beans.Optional;
27 
28 import org.junit.Test;
29 import static org.junit.Assert.*;
30 
31 public final class UnoRuntime_Test {
32     @Test
test_generateOid()33     public void test_generateOid() {
34         // Test if UnoRuntime generates an OID for a simple class:
35         assertTrue("Test1", UnoRuntime.generateOid(new Test1()) != null);
36 
37         // Test if UnoRuntime generates an OID for a class implementing
38         // IQueryInterface and returning null from getOid:
39         assertTrue("Test2", UnoRuntime.generateOid(new Test2()) != null);
40 
41         // Test if a delegator object has the same OID as its creator:
42         Test4 test4 = new Test4();
43         Ifc ifc = UnoRuntime.queryInterface(Ifc.class, test4);
44         assertTrue(
45             "Test4",
46             UnoRuntime.generateOid(test4).equals(UnoRuntime.generateOid(ifc)));
47     }
48 
49     @Test
test_queryInterface()50     public void test_queryInterface() {
51         // Test if a query for an interface which is not supported returns null:
52         assertTrue(
53             "Test1",
54             UnoRuntime.queryInterface(Ifc.class, new Test1()) == null);
55 
56         // Test if a query for an interface which is supported through
57         // IQueryInterface succeeds:
58         assertTrue(
59             "Test2",
60             UnoRuntime.queryInterface(Ifc.class, new Test2()) != null);
61 
62         // Test if a query for an interface which is directly supported (through
63         // inheritance) succeeds:
64         assertTrue(
65             "Test3",
66             UnoRuntime.queryInterface(Ifc.class, new Test3()) != null);
67     }
68 
69     @Test
test_areSame()70     public void test_areSame() {
71         assertTrue(
72             UnoRuntime.areSame(
73                 new Any(Type.UNSIGNED_LONG, new Integer(3)),
74                 new Any(Type.UNSIGNED_LONG, new Integer(3))));
75         assertTrue(
76             !UnoRuntime.areSame(
77                 new Any(Type.UNSIGNED_LONG, new Integer(3)), new Integer(3)));
78         assertTrue(!UnoRuntime.areSame(new int[] { 1 }, new int[] { 1, 2 }));
79         assertTrue(
80             UnoRuntime.areSame(
81                 TypeClass.UNSIGNED_LONG,
82                 new Any(new Type(TypeClass.class), TypeClass.UNSIGNED_LONG)));
83         assertTrue(
84             UnoRuntime.areSame(
85                 new Any(
86                     new Type("com.sun.star.beans.Optional<unsigned long>"),
87                     new Optional()),
88                 new Any(
89                     new Type("com.sun.star.beans.Optional<unsigned long>"),
90                     new Optional(false, new Integer(0)))));
91         assertTrue(!UnoRuntime.areSame(new Test1(), new Test2()));
92         Test2 test2 = new Test2();
93         assertTrue(
94             "Test2",
95             UnoRuntime.areSame(
96                 UnoRuntime.queryInterface(Ifc.class, test2), test2));
97     }
98 
99     @Test
test_completeValue()100     public void test_completeValue() {
101         assertTrue(
102             UnoRuntime.completeValue(Type.UNSIGNED_LONG, null).equals(
103                 new Integer(0)));
104         Object v = UnoRuntime.completeValue(
105             new Type("[][]unsigned long"), null);
106         assertTrue(v instanceof int[][]);
107         assertTrue(((int[][]) v).length == 0);
108         assertTrue(
109             UnoRuntime.completeValue(new Type(TypeClass.class), null) ==
110             TypeClass.VOID);
111         v = UnoRuntime.completeValue(
112             new Type("com.sun.star.beans.Optional<unsigned long>"), null);
113         assertTrue(v instanceof Optional);
114         assertTrue(!((Optional) v).IsPresent);
115         assertTrue(((Optional) v).Value == null);
116     }
117 
118     @Test
test_currentContext()119     public void test_currentContext() throws InterruptedException {
120         TestThread t1 = new TestThread();
121         TestThread t2 = new TestThread();
122         t1.start();
123         t2.start();
124         t1.join();
125         t2.join();
126         Object v1 = t1.context.getValueByName("");
127         Object v2 = t2.context.getValueByName("");
128         assertTrue("", t1.context != t2.context);
129         assertTrue("", v1 == t1);
130         assertTrue("", v2 == t2);
131         assertTrue("", v1 != v2);
132     }
133 
134     private interface Ifc extends XInterface {}
135 
136     private static class Test1 {}
137 
138     private static class Test2 implements XInterface, IQueryInterface {
getOid()139         public String getOid() {
140             return null;
141         }
142 
queryInterface(Type type)143         public Object queryInterface(Type type) {
144             return type.equals(new Type(Ifc.class)) ? t2 : null;
145         }
146 
isSame(Object object)147         public boolean isSame(Object object) {
148             return object == t2;
149         }
150 
151         private static final class T2 implements Ifc {}
152 
153         private final T2 t2 = new T2();
154     }
155 
156     private static class Test3 implements Ifc {}
157 
158     private static class Test4 implements XInterface, IQueryInterface {
getOid()159         public String getOid() {
160             return null;
161         }
162 
queryInterface(Type type)163         public Object queryInterface(Type type) {
164             return type.equals(new Type(Ifc.class)) ? t4 : null;
165         }
166 
isSame(Object object)167         public boolean isSame(Object object) {
168             return object == t4;
169         }
170 
171         private final class T4 implements Ifc, IQueryInterface {
getOid()172             public String getOid() {
173                 return UnoRuntime.generateOid(Test4.this);
174             }
175 
queryInterface(Type type)176             public Object queryInterface(Type type) {
177                 return Test4.this.queryInterface(type);
178             }
179 
isSame(Object object)180             public boolean isSame(Object object) {
181                 return UnoRuntime.areSame(Test4.this, object);
182             }
183         }
184 
185         private final T4 t4 = new T4();
186     }
187 
188     private final class TestThread extends Thread {
run()189         public void run() {
190             assertTrue("", UnoRuntime.getCurrentContext() == null);
191             context = new TestCurrentContext();
192             UnoRuntime.setCurrentContext(context);
193             assertTrue("", UnoRuntime.getCurrentContext() == context);
194             assertTrue("", context.getValueByName("") == this);
195             UnoRuntime.setCurrentContext(null);
196             assertTrue("", UnoRuntime.getCurrentContext() == null);
197         }
198 
199         public XCurrentContext context = null;
200     }
201 
202     private static final class TestCurrentContext implements XCurrentContext {
getValueByName(String name)203         public Object getValueByName(String name) {
204             return value;
205         }
206 
207         private final Object value = Thread.currentThread();
208     }
209 }
210