1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.uno;
29 
30 import com.sun.star.beans.Optional;
31 import complexlib.ComplexTestCase;
32 
33 public final class UnoRuntime_Test extends ComplexTestCase {
34     public String getTestObjectName() {
35         return getClass().getName();
36     }
37 
38     public String[] getTestMethodNames() {
39         return new String[] {
40             "test_generateOid", "test_queryInterface", "test_areSame",
41             "test_completeValue", "test_currentContext" };
42     }
43 
44     public void test_generateOid() {
45         // Test if UnoRuntime generates an OID for a simple class:
46         assure("Test1", UnoRuntime.generateOid(new Test1()) != null);
47 
48         // Test if UnoRuntime generates an OID for a class implementing
49         // IQueryInterface and returning null from getOid:
50         assure("Test2", UnoRuntime.generateOid(new Test2()) != null);
51 
52         // Test if a delegator object has the same OID as its creator:
53         Test4 test4 = new Test4();
54         Ifc ifc = UnoRuntime.queryInterface(Ifc.class, test4);
55         assure(
56             "Test4",
57             UnoRuntime.generateOid(test4).equals(UnoRuntime.generateOid(ifc)));
58     }
59 
60     public void test_queryInterface() {
61         // Test if a query for an interface which is not supported returns null:
62         assure(
63             "Test1",
64             UnoRuntime.queryInterface(Ifc.class, new Test1()) == null);
65 
66         // Test if a query for an interface which is supported through
67         // IQueryInterface succeeds:
68         assure(
69             "Test2",
70             UnoRuntime.queryInterface(Ifc.class, new Test2()) != null);
71 
72         // Test if a query for an interface which is directly supported (through
73         // inheritance) succeeds:
74         assure(
75             "Test3",
76             UnoRuntime.queryInterface(Ifc.class, new Test3()) != null);
77     }
78 
79     public void test_areSame() {
80         assure(
81             UnoRuntime.areSame(
82                 new Any(Type.UNSIGNED_LONG, new Integer(3)),
83                 new Any(Type.UNSIGNED_LONG, new Integer(3))));
84         assure(
85             !UnoRuntime.areSame(
86                 new Any(Type.UNSIGNED_LONG, new Integer(3)), new Integer(3)));
87         assure(!UnoRuntime.areSame(new int[] { 1 }, new int[] { 1, 2 }));
88         assure(
89             UnoRuntime.areSame(
90                 TypeClass.UNSIGNED_LONG,
91                 new Any(new Type(TypeClass.class), TypeClass.UNSIGNED_LONG)));
92         assure(
93             UnoRuntime.areSame(
94                 new Any(
95                     new Type("com.sun.star.beans.Optional<unsigned long>"),
96                     new Optional()),
97                 new Any(
98                     new Type("com.sun.star.beans.Optional<unsigned long>"),
99                     new Optional(false, new Integer(0)))));
100         assure(!UnoRuntime.areSame(new Test1(), new Test2()));
101         Test2 test2 = new Test2();
102         assure(
103             "Test2",
104             UnoRuntime.areSame(
105                 UnoRuntime.queryInterface(Ifc.class, test2), test2));
106     }
107 
108     public void test_completeValue() {
109         assure(
110             UnoRuntime.completeValue(Type.UNSIGNED_LONG, null).equals(
111                 new Integer(0)));
112         Object v = UnoRuntime.completeValue(
113             new Type("[][]unsigned long"), null);
114         assure(v instanceof int[][]);
115         assure(((int[][]) v).length == 0);
116         assure(
117             UnoRuntime.completeValue(new Type(TypeClass.class), null) ==
118             TypeClass.VOID);
119         v = UnoRuntime.completeValue(
120             new Type("com.sun.star.beans.Optional<unsigned long>"), null);
121         assure(v instanceof Optional);
122         assure(!((Optional) v).IsPresent);
123         assure(((Optional) v).Value == null);
124     }
125 
126     public void test_currentContext() throws InterruptedException {
127         TestThread t1 = new TestThread();
128         TestThread t2 = new TestThread();
129         t1.start();
130         t2.start();
131         t1.join();
132         t2.join();
133         Object v1 = t1.context.getValueByName("");
134         Object v2 = t2.context.getValueByName("");
135         assure("", t1.context != t2.context);
136         assure("", v1 == t1);
137         assure("", v2 == t2);
138         assure("", v1 != v2);
139     }
140 
141     private interface Ifc extends XInterface {}
142 
143     private static class Test1 {}
144 
145     private static class Test2 implements XInterface, IQueryInterface {
146         public String getOid() {
147             return null;
148         }
149 
150         public Object queryInterface(Type type) {
151             return type.equals(new Type(Ifc.class)) ? t2 : null;
152         }
153 
154         public boolean isSame(Object object) {
155             return object == t2;
156         }
157 
158         private static final class T2 implements Ifc {}
159 
160         private final T2 t2 = new T2();
161     }
162 
163     private static class Test3 implements Ifc {}
164 
165     private static class Test4 implements XInterface, IQueryInterface {
166         public String getOid() {
167             return null;
168         }
169 
170         public Object queryInterface(Type type) {
171             return type.equals(new Type(Ifc.class)) ? t4 : null;
172         }
173 
174         public boolean isSame(Object object) {
175             return object == t4;
176         }
177 
178         private final class T4 implements Ifc, IQueryInterface {
179             public String getOid() {
180                 return UnoRuntime.generateOid(Test4.this);
181             }
182 
183             public Object queryInterface(Type type) {
184                 return Test4.this.queryInterface(type);
185             }
186 
187             public boolean isSame(Object object) {
188                 return UnoRuntime.areSame(Test4.this, object);
189             }
190         }
191 
192         private final T4 t4 = new T4();
193     }
194 
195     private final class TestThread extends Thread {
196         public void run() {
197             assure("", UnoRuntime.getCurrentContext() == null);
198             context = new TestCurrentContext();
199             UnoRuntime.setCurrentContext(context);
200             assure("", UnoRuntime.getCurrentContext() == context);
201             assure("", context.getValueByName("") == this);
202             UnoRuntime.setCurrentContext(null);
203             assure("", UnoRuntime.getCurrentContext() == null);
204         }
205 
206         public XCurrentContext context = null;
207     }
208 
209     private static final class TestCurrentContext implements XCurrentContext {
210         public Object getValueByName(String name) {
211             return value;
212         }
213 
214         private final Object value = Thread.currentThread();
215     }
216 }
217