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