1*a046d00fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*a046d00fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*a046d00fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*a046d00fSAndrew Rist  * distributed with this work for additional information
6*a046d00fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*a046d00fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*a046d00fSAndrew Rist  * "License"); you may not use this file except in compliance
9*a046d00fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*a046d00fSAndrew Rist  *
11*a046d00fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*a046d00fSAndrew Rist  *
13*a046d00fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*a046d00fSAndrew Rist  * software distributed under the License is distributed on an
15*a046d00fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*a046d00fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*a046d00fSAndrew Rist  * specific language governing permissions and limitations
18*a046d00fSAndrew Rist  * under the License.
19*a046d00fSAndrew Rist  *
20*a046d00fSAndrew Rist  *************************************************************/
21*a046d00fSAndrew Rist 
22*a046d00fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package com.sun.star.uno;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import com.sun.star.beans.Optional;
27cdf0e10cSrcweir import complexlib.ComplexTestCase;
28cdf0e10cSrcweir 
29cdf0e10cSrcweir public final class UnoRuntime_Test extends ComplexTestCase {
getTestObjectName()30cdf0e10cSrcweir     public String getTestObjectName() {
31cdf0e10cSrcweir         return getClass().getName();
32cdf0e10cSrcweir     }
33cdf0e10cSrcweir 
getTestMethodNames()34cdf0e10cSrcweir     public String[] getTestMethodNames() {
35cdf0e10cSrcweir         return new String[] {
36cdf0e10cSrcweir             "test_generateOid", "test_queryInterface", "test_areSame",
37cdf0e10cSrcweir             "test_completeValue", "test_currentContext" };
38cdf0e10cSrcweir     }
39cdf0e10cSrcweir 
test_generateOid()40cdf0e10cSrcweir     public void test_generateOid() {
41cdf0e10cSrcweir         // Test if UnoRuntime generates an OID for a simple class:
42cdf0e10cSrcweir         assure("Test1", UnoRuntime.generateOid(new Test1()) != null);
43cdf0e10cSrcweir 
44cdf0e10cSrcweir         // Test if UnoRuntime generates an OID for a class implementing
45cdf0e10cSrcweir         // IQueryInterface and returning null from getOid:
46cdf0e10cSrcweir         assure("Test2", UnoRuntime.generateOid(new Test2()) != null);
47cdf0e10cSrcweir 
48cdf0e10cSrcweir         // Test if a delegator object has the same OID as its creator:
49cdf0e10cSrcweir         Test4 test4 = new Test4();
50cdf0e10cSrcweir         Ifc ifc = UnoRuntime.queryInterface(Ifc.class, test4);
51cdf0e10cSrcweir         assure(
52cdf0e10cSrcweir             "Test4",
53cdf0e10cSrcweir             UnoRuntime.generateOid(test4).equals(UnoRuntime.generateOid(ifc)));
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
test_queryInterface()56cdf0e10cSrcweir     public void test_queryInterface() {
57cdf0e10cSrcweir         // Test if a query for an interface which is not supported returns null:
58cdf0e10cSrcweir         assure(
59cdf0e10cSrcweir             "Test1",
60cdf0e10cSrcweir             UnoRuntime.queryInterface(Ifc.class, new Test1()) == null);
61cdf0e10cSrcweir 
62cdf0e10cSrcweir         // Test if a query for an interface which is supported through
63cdf0e10cSrcweir         // IQueryInterface succeeds:
64cdf0e10cSrcweir         assure(
65cdf0e10cSrcweir             "Test2",
66cdf0e10cSrcweir             UnoRuntime.queryInterface(Ifc.class, new Test2()) != null);
67cdf0e10cSrcweir 
68cdf0e10cSrcweir         // Test if a query for an interface which is directly supported (through
69cdf0e10cSrcweir         // inheritance) succeeds:
70cdf0e10cSrcweir         assure(
71cdf0e10cSrcweir             "Test3",
72cdf0e10cSrcweir             UnoRuntime.queryInterface(Ifc.class, new Test3()) != null);
73cdf0e10cSrcweir     }
74cdf0e10cSrcweir 
test_areSame()75cdf0e10cSrcweir     public void test_areSame() {
76cdf0e10cSrcweir         assure(
77cdf0e10cSrcweir             UnoRuntime.areSame(
78cdf0e10cSrcweir                 new Any(Type.UNSIGNED_LONG, new Integer(3)),
79cdf0e10cSrcweir                 new Any(Type.UNSIGNED_LONG, new Integer(3))));
80cdf0e10cSrcweir         assure(
81cdf0e10cSrcweir             !UnoRuntime.areSame(
82cdf0e10cSrcweir                 new Any(Type.UNSIGNED_LONG, new Integer(3)), new Integer(3)));
83cdf0e10cSrcweir         assure(!UnoRuntime.areSame(new int[] { 1 }, new int[] { 1, 2 }));
84cdf0e10cSrcweir         assure(
85cdf0e10cSrcweir             UnoRuntime.areSame(
86cdf0e10cSrcweir                 TypeClass.UNSIGNED_LONG,
87cdf0e10cSrcweir                 new Any(new Type(TypeClass.class), TypeClass.UNSIGNED_LONG)));
88cdf0e10cSrcweir         assure(
89cdf0e10cSrcweir             UnoRuntime.areSame(
90cdf0e10cSrcweir                 new Any(
91cdf0e10cSrcweir                     new Type("com.sun.star.beans.Optional<unsigned long>"),
92cdf0e10cSrcweir                     new Optional()),
93cdf0e10cSrcweir                 new Any(
94cdf0e10cSrcweir                     new Type("com.sun.star.beans.Optional<unsigned long>"),
95cdf0e10cSrcweir                     new Optional(false, new Integer(0)))));
96cdf0e10cSrcweir         assure(!UnoRuntime.areSame(new Test1(), new Test2()));
97cdf0e10cSrcweir         Test2 test2 = new Test2();
98cdf0e10cSrcweir         assure(
99cdf0e10cSrcweir             "Test2",
100cdf0e10cSrcweir             UnoRuntime.areSame(
101cdf0e10cSrcweir                 UnoRuntime.queryInterface(Ifc.class, test2), test2));
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir 
test_completeValue()104cdf0e10cSrcweir     public void test_completeValue() {
105cdf0e10cSrcweir         assure(
106cdf0e10cSrcweir             UnoRuntime.completeValue(Type.UNSIGNED_LONG, null).equals(
107cdf0e10cSrcweir                 new Integer(0)));
108cdf0e10cSrcweir         Object v = UnoRuntime.completeValue(
109cdf0e10cSrcweir             new Type("[][]unsigned long"), null);
110cdf0e10cSrcweir         assure(v instanceof int[][]);
111cdf0e10cSrcweir         assure(((int[][]) v).length == 0);
112cdf0e10cSrcweir         assure(
113cdf0e10cSrcweir             UnoRuntime.completeValue(new Type(TypeClass.class), null) ==
114cdf0e10cSrcweir             TypeClass.VOID);
115cdf0e10cSrcweir         v = UnoRuntime.completeValue(
116cdf0e10cSrcweir             new Type("com.sun.star.beans.Optional<unsigned long>"), null);
117cdf0e10cSrcweir         assure(v instanceof Optional);
118cdf0e10cSrcweir         assure(!((Optional) v).IsPresent);
119cdf0e10cSrcweir         assure(((Optional) v).Value == null);
120cdf0e10cSrcweir     }
121cdf0e10cSrcweir 
test_currentContext()122cdf0e10cSrcweir     public void test_currentContext() throws InterruptedException {
123cdf0e10cSrcweir         TestThread t1 = new TestThread();
124cdf0e10cSrcweir         TestThread t2 = new TestThread();
125cdf0e10cSrcweir         t1.start();
126cdf0e10cSrcweir         t2.start();
127cdf0e10cSrcweir         t1.join();
128cdf0e10cSrcweir         t2.join();
129cdf0e10cSrcweir         Object v1 = t1.context.getValueByName("");
130cdf0e10cSrcweir         Object v2 = t2.context.getValueByName("");
131cdf0e10cSrcweir         assure("", t1.context != t2.context);
132cdf0e10cSrcweir         assure("", v1 == t1);
133cdf0e10cSrcweir         assure("", v2 == t2);
134cdf0e10cSrcweir         assure("", v1 != v2);
135cdf0e10cSrcweir     }
136cdf0e10cSrcweir 
137cdf0e10cSrcweir     private interface Ifc extends XInterface {}
138cdf0e10cSrcweir 
139cdf0e10cSrcweir     private static class Test1 {}
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     private static class Test2 implements XInterface, IQueryInterface {
getOid()142cdf0e10cSrcweir         public String getOid() {
143cdf0e10cSrcweir             return null;
144cdf0e10cSrcweir         }
145cdf0e10cSrcweir 
queryInterface(Type type)146cdf0e10cSrcweir         public Object queryInterface(Type type) {
147cdf0e10cSrcweir             return type.equals(new Type(Ifc.class)) ? t2 : null;
148cdf0e10cSrcweir         }
149cdf0e10cSrcweir 
isSame(Object object)150cdf0e10cSrcweir         public boolean isSame(Object object) {
151cdf0e10cSrcweir             return object == t2;
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         private static final class T2 implements Ifc {}
155cdf0e10cSrcweir 
156cdf0e10cSrcweir         private final T2 t2 = new T2();
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     private static class Test3 implements Ifc {}
160cdf0e10cSrcweir 
161cdf0e10cSrcweir     private static class Test4 implements XInterface, IQueryInterface {
getOid()162cdf0e10cSrcweir         public String getOid() {
163cdf0e10cSrcweir             return null;
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir 
queryInterface(Type type)166cdf0e10cSrcweir         public Object queryInterface(Type type) {
167cdf0e10cSrcweir             return type.equals(new Type(Ifc.class)) ? t4 : null;
168cdf0e10cSrcweir         }
169cdf0e10cSrcweir 
isSame(Object object)170cdf0e10cSrcweir         public boolean isSame(Object object) {
171cdf0e10cSrcweir             return object == t4;
172cdf0e10cSrcweir         }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir         private final class T4 implements Ifc, IQueryInterface {
getOid()175cdf0e10cSrcweir             public String getOid() {
176cdf0e10cSrcweir                 return UnoRuntime.generateOid(Test4.this);
177cdf0e10cSrcweir             }
178cdf0e10cSrcweir 
queryInterface(Type type)179cdf0e10cSrcweir             public Object queryInterface(Type type) {
180cdf0e10cSrcweir                 return Test4.this.queryInterface(type);
181cdf0e10cSrcweir             }
182cdf0e10cSrcweir 
isSame(Object object)183cdf0e10cSrcweir             public boolean isSame(Object object) {
184cdf0e10cSrcweir                 return UnoRuntime.areSame(Test4.this, object);
185cdf0e10cSrcweir             }
186cdf0e10cSrcweir         }
187cdf0e10cSrcweir 
188cdf0e10cSrcweir         private final T4 t4 = new T4();
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     private final class TestThread extends Thread {
run()192cdf0e10cSrcweir         public void run() {
193cdf0e10cSrcweir             assure("", UnoRuntime.getCurrentContext() == null);
194cdf0e10cSrcweir             context = new TestCurrentContext();
195cdf0e10cSrcweir             UnoRuntime.setCurrentContext(context);
196cdf0e10cSrcweir             assure("", UnoRuntime.getCurrentContext() == context);
197cdf0e10cSrcweir             assure("", context.getValueByName("") == this);
198cdf0e10cSrcweir             UnoRuntime.setCurrentContext(null);
199cdf0e10cSrcweir             assure("", UnoRuntime.getCurrentContext() == null);
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         public XCurrentContext context = null;
203cdf0e10cSrcweir     }
204cdf0e10cSrcweir 
205cdf0e10cSrcweir     private static final class TestCurrentContext implements XCurrentContext {
getValueByName(String name)206cdf0e10cSrcweir         public Object getValueByName(String name) {
207cdf0e10cSrcweir             return value;
208cdf0e10cSrcweir         }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir         private final Object value = Thread.currentThread();
211cdf0e10cSrcweir     }
212cdf0e10cSrcweir }
213