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