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