diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 05:54:39 +0000 |
commit | 267c6f2ac71f92999e969232431ba04678e7437e (patch) | |
tree | 358c9467650e1d0a1d7227a21dac2e3d08b622b2 /ridljar/test/com/sun | |
parent | Initial commit. (diff) | |
download | libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.tar.xz libreoffice-267c6f2ac71f92999e969232431ba04678e7437e.zip |
Adding upstream version 4:24.2.0.upstream/4%24.2.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'ridljar/test/com/sun')
28 files changed, 4596 insertions, 0 deletions
diff --git a/ridljar/test/com/sun/star/comp/bridgefactory/BridgeFactory_Test.java b/ridljar/test/com/sun/star/comp/bridgefactory/BridgeFactory_Test.java new file mode 100644 index 0000000000..965653bf76 --- /dev/null +++ b/ridljar/test/com/sun/star/comp/bridgefactory/BridgeFactory_Test.java @@ -0,0 +1,92 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.comp.bridgefactory; + +import com.sun.star.bridge.BridgeExistsException; +import com.sun.star.bridge.XBridge; +import com.sun.star.comp.connections.PipedConnection; +import com.sun.star.lang.XComponent; +import com.sun.star.uno.UnoRuntime; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class BridgeFactory_Test { + @Test public void test() throws Exception { + PipedConnection rightSide = new PipedConnection(new Object[0]); + PipedConnection leftSide = new PipedConnection(new Object[]{rightSide}); + + BridgeFactory bridgeFactory = new BridgeFactory(); // create the needed bridgeFactory + + // create a bridge + XBridge xBridge = bridgeFactory.createBridge("testbridge", "urp", leftSide, null); + + // test that we get the same bridge + assertTrue( + UnoRuntime.areSame( + xBridge, bridgeFactory.getBridge("testbridge"))); + + // test that we can not create another bridge with same name + try { + bridgeFactory.createBridge("testbridge", "urp", leftSide, null); + + fail(); + } + catch(BridgeExistsException bridgeExistsException) { + } + + + // test getExistingBridges + XBridge xBridges[] = bridgeFactory.getExistingBridges(); + assertTrue(UnoRuntime.areSame(xBridge, xBridges[0])); + + // dispose the bridge + XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xBridge); + xComponent.dispose(); + + + // test that the bridge has been removed + assertTrue(bridgeFactory.getBridge("testbridge") == null); + + + + rightSide = new PipedConnection(new Object[0]); + leftSide = new PipedConnection(new Object[]{rightSide}); + + + // test that we really get a new bridge + XBridge xBridge_new = bridgeFactory.createBridge("testbridge", "urp", leftSide, null); + assertFalse(UnoRuntime.areSame(xBridge, xBridge_new)); + + for(int i = 0; i <10000; ++ i) { + Object x[] = new Object[100]; + } + + // test getExistingBridges + xBridges = bridgeFactory.getExistingBridges(); + assertEquals(1, xBridges.length); + assertTrue(UnoRuntime.areSame(xBridge_new, xBridges[0])); + + // dispose the new bridge + XComponent xComponent_new = UnoRuntime.queryInterface(XComponent.class, xBridge_new); + xComponent_new.dispose(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/comp/connections/PipedConnection_Test.java b/ridljar/test/com/sun/star/comp/connections/PipedConnection_Test.java new file mode 100644 index 0000000000..6cfef789d9 --- /dev/null +++ b/ridljar/test/com/sun/star/comp/connections/PipedConnection_Test.java @@ -0,0 +1,104 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.comp.connections; + +import com.sun.star.io.IOException; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class PipedConnection_Test { + private static final int ROUNDS = 2000; + + @Test public void test() throws Exception { + PipedConnection rightSide = new PipedConnection(new Object[0]); + PipedConnection leftSide = new PipedConnection(new Object[]{rightSide}); + + Reader reader = new Reader(rightSide); + Writer writer = new Writer(leftSide); + + reader.start(); + writer.start(); + + writer.join(); + reader.join(); + + assertTrue(writer._state); + assertTrue(reader._state); + assertEquals(ROUNDS, reader._rounds); + } + + private static class Reader extends Thread { + PipedConnection _pipedConnection; + boolean _state = false; + int _rounds = 0; + + Reader(PipedConnection pipedConnection) { + _pipedConnection = pipedConnection; + } + + @Override + public void run() { + try { + for (byte v = 0;; v++) { + byte[][] b = new byte[1][]; + int n = _pipedConnection.read(b, 1); + if (n == 0) { + break; + } + assertEquals(1, n); + assertEquals(1, b[0].length); + assertEquals(v, b[0][0]); + ++_rounds; + } + _pipedConnection.close(); + _state = true; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } + + private static class Writer extends Thread { + PipedConnection _pipedConnection; + boolean _state = false; + + Writer(PipedConnection pipedConnection) { + _pipedConnection = pipedConnection; + } + + @Override + public void run() { + try { + byte v = 0; + for (int i = 0; i != ROUNDS; ++i) { + byte[] b = new byte[] { v++ }; + _pipedConnection.write(b); + _pipedConnection.flush(); + } + _pipedConnection.close(); + _state = true; + } catch (IOException e) { + throw new RuntimeException(e); + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/BridgedObject_Test.java b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/BridgedObject_Test.java new file mode 100644 index 0000000000..e7dc9c0e19 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/BridgedObject_Test.java @@ -0,0 +1,64 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.bridges.java_remote; + +import com.sun.star.bridge.XBridge; +import com.sun.star.uno.Type; +import com.sun.star.uno.XInterface; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class BridgedObject_Test { + @Test public void test() { + RequestHandler handler = new RequestHandler() { + public Object sendRequest( + String oid, Type type, String operation, Object[] args) + { + return null; + } + }; + XBridge bridge1 = new TestBridge(); + ProxyFactory factory1 = new ProxyFactory(handler, bridge1); + XBridge bridge2 = new TestBridge(); + ProxyFactory factory2 = new ProxyFactory(handler, bridge2); + Object object0 = new Object(); + Object object1 = factory1.create("", new Type(XInterface.class)); + Object object2 = factory2.create("", new Type(XInterface.class)); + assertNull(BridgedObject.getBridge(object0)); + assertSame(bridge1, BridgedObject.getBridge(object1)); + assertSame(bridge2, BridgedObject.getBridge(object2)); + } + + private static final class TestBridge implements XBridge { + public Object getInstance(String instanceName) { + return null; + } + + public String getName() { + return null; + } + + public String getDescription() { + return null; + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory_Test.java b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory_Test.java new file mode 100644 index 0000000000..45fc02f899 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory_Test.java @@ -0,0 +1,131 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.bridges.java_remote; + +import com.sun.star.uno.IQueryInterface; +import com.sun.star.uno.MappingException; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XInterface; +import com.sun.star.uno.XNamingService; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.UndeclaredThrowableException; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class ProxyFactory_Test { + @Test public void testQueryInterface() { + TestRequestHandler handler = new TestRequestHandler(); + Type type = new Type(XNamingService.class); + Object proxy = new ProxyFactory(handler, null).create("TestOID", type); + assertSame(proxy, ((IQueryInterface) proxy).queryInterface(type)); + assertSame(proxy, UnoRuntime.queryInterface(type, proxy)); + } + + @Test public void testExceptionHandling() throws Exception { + TestRequestHandler handler = new TestRequestHandler(); + Object proxy = new ProxyFactory(handler, null).create( + "TestOID", new Type(XNamingService.class)); + testExceptions( + handler, + proxy.getClass().getMethod("queryInterface", + new Class[] { Type.class }), + proxy, new Object[] { new Type(XInterface.class) }, + new Class[] { null, MappingException.class, + com.sun.star.uno.RuntimeException.class, + UndeclaredThrowableException.class, + NullPointerException.class, + UndeclaredThrowableException.class }); + testExceptions( + handler, + proxy.getClass().getMethod("getRegisteredObject", + new Class[] { String.class }), + proxy, new Object[] { "TestName" }, + new Class[] { null, MappingException.class, + com.sun.star.uno.RuntimeException.class, + com.sun.star.uno.Exception.class, + NullPointerException.class, Exception.class }); + } + + private void testExceptions(TestRequestHandler handler, Method method, + Object obj, Object[] args, Class<?>[] exceptions) + throws Exception + { + for (int i = 0; i < exceptions.length; ++i) { + handler.setModus(i); + testExceptionType(method, obj, args, exceptions[i]); + } + } + + private void testExceptionType(Method method, Object obj, Object[] args, + Class<?> exception) throws Exception { + try { + method.invoke(obj, args); + assertNull(exception); + } catch (InvocationTargetException e) { + assertNotNull(exception); + assertTrue(exception.isInstance(e.getTargetException())); + // TODO check stack trace + } + } + + private static final class TestRequestHandler implements RequestHandler { + public Object sendRequest(String oid, Type type, String operation, + Object[] args) + throws Throwable + { + if (operation.equals("release")) { + return null; + } + int m; + synchronized (lock) { + m = modus; + } + switch (m) { + case 0: + return operation.equals("getInstance") ? "TestResult" : null; + case 1: + // TODO What is this test, with an obviously obsoleted + // MappingException, good for? + throw new MappingException(); + case 2: + throw new com.sun.star.uno.RuntimeException(); + case 3: + throw new com.sun.star.uno.Exception(); + case 4: + throw new NullPointerException(); + default: + throw new Throwable(); + } + } + + public void setModus(int modus) { + synchronized (lock) { + this.modus = modus; + } + } + + private final Object lock = new Object(); + private int modus = 0; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge_Test.java b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge_Test.java new file mode 100644 index 0000000000..53e1505271 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/bridges/java_remote/java_remote_bridge_Test.java @@ -0,0 +1,239 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.uno.bridges.java_remote; + +import com.sun.star.bridge.XBridge; +import com.sun.star.bridge.XInstanceProvider; +import com.sun.star.comp.connections.PipedConnection; +import com.sun.star.connection.XConnection; +import com.sun.star.container.NoSuchElementException; +import com.sun.star.lib.uno.environments.java.java_environment; +import com.sun.star.lib.uno.typeinfo.MethodTypeInfo; +import com.sun.star.lib.uno.typeinfo.TypeInfo; +import com.sun.star.uno.IQueryInterface; +import com.sun.star.uno.Type; +import com.sun.star.uno.UnoRuntime; +import com.sun.star.uno.XInterface; +import org.junit.Test; +import util.WaitUnreachable; +import static org.junit.Assert.*; + +public final class java_remote_bridge_Test { + @Test public void test() throws Exception { + String protocol = "urp"; + + XConnection connectionA = new PipedConnection(new Object[0]); + XConnection connectionB = new PipedConnection( + new Object[] { connectionA }); + java_remote_bridge bridgeA = new java_remote_bridge( + new java_environment(null), null, + new Object[] { protocol, connectionA, new TestInstanceProvider() }); + java_remote_bridge bridgeB = new java_remote_bridge( + new java_environment(null), null, + new Object[] { protocol, connectionB, null }); + + testGetInstance(bridgeA, bridgeB); + testLifeCycle(bridgeA, bridgeB); + } + + private void testGetInstance(XBridge bridgeA, XBridge bridgeB) { + assertNull(bridgeB.getInstance(TestInstanceProvider.NAME_NULL)); + + try { + bridgeB.getInstance(TestInstanceProvider.NAME_RUNTIME_EXCEPTION); + fail("throw RuntimeException"); + } catch (com.sun.star.uno.RuntimeException e) { + assertTrue( + e.getMessage().indexOf( + TestInstanceProvider.NAME_RUNTIME_EXCEPTION) + != -1); + } + + try { + bridgeB.getInstance( + TestInstanceProvider.NAME_NO_SUCH_ELEMENT_EXCEPTION); + fail("throw NoSuchElementException"); + } catch (com.sun.star.uno.RuntimeException e) { + assertTrue( + e.getMessage().indexOf( + TestInstanceProvider.NAME_NO_SUCH_ELEMENT_EXCEPTION) + != -1); + } + + try { + bridgeA.getInstance(TestInstanceProvider.NAME_ANYTHING); + fail("no instance provider"); + } catch (com.sun.star.uno.RuntimeException e) { + assertTrue(e.getMessage().startsWith("unknown OID ")); + } + } + + private void testLifeCycle(java_remote_bridge bridgeA, + java_remote_bridge bridgeB) + throws InterruptedException + { + // Repeatedly, objects are mapped from bridgeA to bridgeB, where proxies + // for those objects (for the XInterface and TestInterface facets) are + // created. The proxies at bridgeB keep both bridges alive; after those + // proxies have been garbage-collected, both bridges should be disposed. + // It does not work to map a local object from bridgeA to bridgeB, as + // bridgeB would find this object as a local one, too (via the shared, + // static localObjects Registry in java_environment): bridgeB would not + // create a proxy, would rather send back a "release" to bridgeA, and + // both bridges would be disposed while the first object is being + // mapped. Therefore, a HACK is used to install TestProxy objects + // (which behave as if they got mapped in to bridgeA from somewhere + // else) at bridgeA and map those. + + final int COUNT = 100; + XInterface[] proxyBXInterface = new XInterface[COUNT]; + TestInterface[] proxyBTestInterface = new TestInterface[COUNT]; + for (int i = 0; i < COUNT; ++i) { + String name = "TestOID" + i; + Object proxyA = new TestProxy(name); + bridgeA.getSourceEnvironment().registerInterface( + proxyA, new String[] { name }, new Type(XInterface.class)); + + proxyBXInterface[i] = (XInterface) bridgeB.getInstance(name); + + // map object: + proxyBTestInterface[i] = UnoRuntime.queryInterface( + TestInterface.class, proxyBXInterface[i]); + proxyBTestInterface[i].function(); + + // remap object once: + TestInterface remapped = UnoRuntime.queryInterface( + TestInterface.class, proxyBXInterface[i]); + remapped.function(); + + // remap object twice: + remapped = UnoRuntime.queryInterface( + TestInterface.class, proxyBXInterface[i]); + remapped.function(); + } + + assertEquals(3 * COUNT, TestProxy.getCount()); + + // The following checks rely on the implementation detail that mapping + // different facets of a UNO object (XInterface and TestInterface) leads + // to different proxies: + + assertEquals("bridge A life count", 2 * COUNT, bridgeA.getLifeCount()); + assertEquals("bridge B life count", 2 * COUNT, bridgeB.getLifeCount()); +/*TODO: below test fails with "expected:<200> but was:<204>": + assertEquals("proxy count", 2 * COUNT, ProxyFactory.getDebugCount()); +*/ + + System.out.println("waiting for proxies to become unreachable:"); + for (int i = 0; i < COUNT; ++i) { + WaitUnreachable u1 = new WaitUnreachable(proxyBXInterface[i]); + WaitUnreachable u2 = new WaitUnreachable(proxyBTestInterface[i]); + proxyBXInterface[i] = null; + proxyBTestInterface[i] = null; + u1.waitUnreachable(); + u2.waitUnreachable(); + } + // For whatever strange reason, this sleep seems to be necessary to + // reliably ensure that even the last proxy's finalization is over + // before the following assert is executed: + Thread.sleep(1000); + + assertEquals("proxy count", 0, ProxyFactory.getDebugCount()); + + System.out.println("waiting for pending messages to be done"); + while (bridgeA.getLifeCount() != 0 || bridgeB.getLifeCount() != 0) { + Thread.sleep(100); + } + + assertEquals("Zero bridge A life count", 0, bridgeA.getLifeCount()); + assertEquals("Zero bridge B life count", 0, bridgeB.getLifeCount()); + assertEquals("Zero proxy count", 0, ProxyFactory.getDebugCount()); + } + + public interface TestInterface extends XInterface { + void function(); + + TypeInfo[] UNOTYPEINFO = new TypeInfo[] { + new MethodTypeInfo("function", 0, 0) }; + } + + private static final class TestInstanceProvider + implements XInstanceProvider + { + public Object getInstance(String name) throws NoSuchElementException { + if (name.equals(NAME_NULL)) { + return null; + } else if (name.equals(NAME_RUNTIME_EXCEPTION)) { + throw new com.sun.star.uno.RuntimeException( + getClass().getName() + ", throwing: " + name); + } else if (name.equals(NAME_NO_SUCH_ELEMENT_EXCEPTION)) { + throw new NoSuchElementException( + getClass().getName() + ", throwing: " + name); + } else { + throw new IllegalStateException(); + } + } + + public static final String NAME_NULL = "return null"; + public static final String NAME_RUNTIME_EXCEPTION + = "throw RuntimeException"; + public static final String NAME_NO_SUCH_ELEMENT_EXCEPTION + = "throw NoSuchElementException"; + public static final String NAME_ANYTHING = "anything"; + } + + private static final class TestProxy + implements com.sun.star.lib.uno.Proxy, IQueryInterface, + TestInterface + { + public TestProxy(String oid) { + this.oid = oid; + } + + public Object queryInterface(Type type) { + // type should be either XInterface or TestInterface... + return this; + } + + public boolean isSame(Object object) { + return object instanceof TestProxy + && oid.equals(((TestProxy) object).oid); + } + + public String getOid() { + return oid; + } + + public void function() { + synchronized (getClass()) { + ++count; + } + } + + public static synchronized int getCount() { + return count; + } + + private final String oid; + + private static int count = 0; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/java/java_environment_Test.java b/ridljar/test/com/sun/star/lib/uno/environments/java/java_environment_Test.java new file mode 100644 index 0000000000..9381e2af43 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/java/java_environment_Test.java @@ -0,0 +1,55 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.java; + +import com.sun.star.uno.Type; +import com.sun.star.uno.XInterface; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class java_environment_Test { + @Test public void test() { + java_environment env = new java_environment(null); + + Object obj = Integer.valueOf(3); + String[] oid = new String[1]; + + Object obj2 = env.registerInterface(obj, oid, + new Type(XInterface.class)); + Object obj3 = env.registerInterface(obj, oid, + new Type(XInterface.class)); + // Register ordinary interface twice: + assertSame(obj, obj2); + assertSame(obj, obj3); + + // Ask for registered interface: + assertSame( + obj, + env.getRegisteredInterface(oid[0], new Type(XInterface.class))); + + env.revokeInterface(oid[0], new Type(XInterface.class)); + env.revokeInterface(oid[0], new Type(XInterface.class)); + // Revoke interface: + assertNull( + env.getRegisteredInterface(oid[0], new Type(XInterface.class))); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java new file mode 100644 index 0000000000..2d2264f060 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/JavaThreadPoolFactory_Test.java @@ -0,0 +1,48 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +import org.junit.Test; +import static org.junit.Assert.*; + +public final class JavaThreadPoolFactory_Test { + @Test public void test() throws InterruptedException { + ThreadId i1 = JavaThreadPoolFactory.getThreadId(); + assertEquals(i1, JavaThreadPoolFactory.getThreadId()); + final ThreadId[] i2 = new ThreadId[1]; + new Thread() { + @Override + public void run() { + synchronized (i2) { + i2[0] = JavaThreadPoolFactory.getThreadId(); + i2.notify(); + } + } + }.start(); + synchronized (i2) { + while (i2[0] == null) { + i2.wait(); + } + } + assertFalse(i1.equals(i2[0])); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java new file mode 100644 index 0000000000..a63c9c7ed6 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/JobQueue_Test.java @@ -0,0 +1,263 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +import com.sun.star.lib.uno.typedesc.TypeDescription; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class JobQueue_Test { + @Test public void testThreadLeavesJobQueueOnDispose0() + throws InterruptedException + { + testThreadLeavesJobQueueOnDispose(0); + } + + @Test public void testThreadLeavesJobQueueOnDispose5000() + throws InterruptedException + { + testThreadLeavesJobQueueOnDispose(5000); + } + + private void testThreadLeavesJobQueueOnDispose(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + String msg = "xcxxxxxxxx"; + t._jobQueue.dispose(t._disposeId, new RuntimeException (msg)); + t.waitToTerminate(); +/*TODO: below test fails with "expected:<xcxxxxxxxx> but was:<null>": + assertEquals(msg, t._message); +*/ + } + + @Test public void testThreadLeavesJobQueueOnReply0() + throws InterruptedException + { + testThreadLeavesJobQueueOnReply(0); + } + + @Test public void testThreadLeavesJobQueueOnReply5000() + throws InterruptedException + { + testThreadLeavesJobQueueOnReply(5000); + } + + private void testThreadLeavesJobQueueOnReply(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + // put reply job: + t._jobQueue.putJob( + new Job(null, __iReceiver, + new Message( + null, false, "oid", __workAt_td, null, false, null, + false, null, null)), + null); + t.waitToTerminate(); + assertTrue(true); // TODO! ??? + } + + @Test public void testStaticThreadExecutesJobs0() + throws InterruptedException + { + testStaticThreadExecutesJobs(0); + } + + @Test public void testStaticThreadExecutesJobs5000() + throws InterruptedException + { + testStaticThreadExecutesJobs(5000); + } + + private void testStaticThreadExecutesJobs(int waitTime) + throws InterruptedException + { + TestThread t = new TestThread(waitTime); + t.waitToStart(); + testExecuteJobs(t._jobQueue); + t._jobQueue.dispose(t._disposeId, + new RuntimeException("xxxxxxxxxxxxx")); + t.waitToTerminate(); + } + + @Test public void testDynamicThreadExecutesJob() throws InterruptedException + { + testExecuteJobs( + new JobQueue( + __javaThreadPoolFactory, ThreadId.createFresh(), true)); + } + + @Test public void testStaticThreadExecutesAsyncs() + throws InterruptedException + { + TestThread t = new TestThread(); + JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, + t._threadId); + assertEquals(1, async_jobQueue._ref_count); + t._jobQueue = __javaThreadPoolFactory.getJobQueue(t._threadId); + assertEquals(1, t._jobQueue._ref_count); + t.waitToStart(); + TestWorkAt workAt = new TestWorkAt(); + testAsyncJobQueue(workAt, async_jobQueue, t._threadId); + t._jobQueue.dispose(t._disposeId, + new RuntimeException("xxxxxxxxxxxxx")); + t.waitToTerminate(); + assertEquals(TestWorkAt.MESSAGES, workAt._async_counter); + assertEquals(TestWorkAt.MESSAGES, workAt._sync_counter); + } + + @Test public void testDynamicThreadExecutesAsyncs() + throws InterruptedException + { + ThreadId threadId = ThreadId.createFresh(); + JobQueue async_jobQueue = new JobQueue(__javaThreadPoolFactory, + threadId); + TestWorkAt workAt = new TestWorkAt(); + testAsyncJobQueue(workAt, async_jobQueue, threadId); + assertEquals(TestWorkAt.MESSAGES, workAt._async_counter); + assertEquals(TestWorkAt.MESSAGES, workAt._sync_counter); + } + + private void testExecuteJobs(JobQueue jobQueue) throws InterruptedException + { + TestWorkAt workAt = new TestWorkAt(); + testSendRequests(workAt, "increment", jobQueue); + synchronized (workAt) { + jobQueue.putJob(new Job(workAt, __iReceiver, + new Message( + null, true, "oid", __workAt_td, + __workAt_td.getMethodDescription( + "notifyme"), + true, null, false, null, null)), + null); + while (!workAt._notified) { + workAt.wait(); + } + } + assertEquals(TestWorkAt.MESSAGES, workAt._counter); + } + + private void testAsyncJobQueue(TestWorkAt workAt, JobQueue async_jobQueue, + ThreadId threadId) + throws InterruptedException + { + // put slow async calls first, followed by fast sync calls: + testSendRequests(workAt, "asyncCall", async_jobQueue); + testSendRequests(workAt, "syncCall", + __javaThreadPoolFactory.getJobQueue(threadId)); + synchronized (workAt) { + async_jobQueue._sync_jobQueue.putJob( + new Job(workAt, __iReceiver, + new Message( + null, true, "oid", __workAt_td, + __workAt_td.getMethodDescription("notifyme"), + true, null, false, null, null)), + null); + while (!workAt._notified) { + workAt.wait(); + } + } + assertTrue(workAt.passedAsyncTest()); + } + + private void testSendRequests(TestWorkAt workAt, String operation, + JobQueue jobQueue) { + Message iMessage = new Message( + null, true, "oid", __workAt_td, + __workAt_td.getMethodDescription(operation), + true, null, false, null, null); + for (int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + jobQueue.putJob(new Job(workAt, __iReceiver, iMessage), + new Object()); + } + } + + private static final class TestThread extends Thread { + public final ThreadId _threadId = JavaThreadPoolFactory.getThreadId(); + public final Object _disposeId = new Object(); + public JobQueue _jobQueue = null; + + public TestThread(int waitTime) { + this.waitTime = waitTime; + _jobQueue = new JobQueue(__javaThreadPoolFactory, _threadId, false); + } + + public TestThread() { + waitTime = 0; + } + + @Override + public void run() { + synchronized (lock) { + state = STATE_STARTED; + lock.notifyAll(); + } + try { + if (waitTime != 0) { + Thread.sleep(waitTime); + } + _jobQueue.enter(_disposeId); + } catch (Throwable e) { + } + synchronized (lock) { + state = STATE_DONE; + lock.notifyAll(); + } + } + + public void waitToStart() throws InterruptedException { + start(); + synchronized (lock) { + while (state == STATE_INITIAL) { + lock.wait(); + } + } + } + + public void waitToTerminate() throws InterruptedException { + synchronized (lock) { + while (state != STATE_DONE) { + lock.wait(); + } + } + join(); + } + + private final int waitTime; + + private final Object lock = new Object(); + private int state = STATE_INITIAL; + private static final int STATE_INITIAL = 0; + private static final int STATE_STARTED = 1; + private static final int STATE_DONE = 2; + } + + private static final JavaThreadPoolFactory __javaThreadPoolFactory + = new JavaThreadPoolFactory(); + private static final IReceiver __iReceiver = new TestReceiver(); + private static final TypeDescription __workAt_td + = TypeDescription.getTypeDescription(TestIWorkAt.class); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java new file mode 100644 index 0000000000..7bb71a80e9 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestIWorkAt.java @@ -0,0 +1,43 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + + +import com.sun.star.lib.uno.typeinfo.MethodTypeInfo; +import com.sun.star.lib.uno.typeinfo.TypeInfo; +import com.sun.star.uno.XInterface; + +public interface TestIWorkAt extends XInterface { + void syncCall() throws Throwable ; + void asyncCall() throws Throwable ; + + void increment() throws Throwable; + + void notifyme(); + + TypeInfo UNOTYPEINFO[] = { + new MethodTypeInfo("increment", 0, 0), + new MethodTypeInfo("notifyme", 1, 0), + new MethodTypeInfo("syncCall", 2, 0), + new MethodTypeInfo("asyncCall", 3, 0) + }; +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java new file mode 100644 index 0000000000..6c8f368857 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestReceiver.java @@ -0,0 +1,27 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +final class TestReceiver implements IReceiver { + public void sendReply(boolean exception, ThreadId threadId, Object result) { + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java new file mode 100644 index 0000000000..93297815cb --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/TestWorkAt.java @@ -0,0 +1,81 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +import static org.junit.Assert.*; + +class TestWorkAt implements TestIWorkAt { + /** + * When set to true, enables various debugging output. + */ + private static final boolean DEBUG = false; + + static final int MESSAGES = 35; + + + int _counter; + + int _sync_counter; + int _async_counter; + + private boolean _passedAsync = true; + boolean _notified = false; + + public synchronized void syncCall() throws Throwable { + ++ _sync_counter; + + // at least in currently run tests this should never fire, so don't + // defer the check until passedAsyncTest and assert here + assertEquals(MESSAGES, _async_counter); + if(_async_counter != MESSAGES) + _passedAsync = false; + + if(DEBUG) System.err.println("syncCall:" + _sync_counter + " " + _passedAsync + " " + Thread.currentThread()); + } + + public synchronized void asyncCall() throws Throwable { + ++ _async_counter; + + if(DEBUG) System.err.println("asyncCall:" + _async_counter + " " + Thread.currentThread()); + } + + public synchronized void increment() throws Throwable { + if(DEBUG) System.err.println("increment - " + Thread.currentThread()); + + ++ _counter; + notifyAll(); + } + + public synchronized void notifyme() { + if(DEBUG) System.err.println("\t\t\tnotifying me" + Thread.currentThread()); + + notifyAll(); + + _notified = true; + } + + public synchronized boolean passedAsyncTest() { + assertEquals(MESSAGES, _sync_counter); + assertTrue(_passedAsync); + return _passedAsync && (_sync_counter == MESSAGES); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java new file mode 100644 index 0000000000..8ee7f4a4c2 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadId_Test.java @@ -0,0 +1,57 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +import java.util.Arrays; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class ThreadId_Test { + @Test public void test() { + ThreadId i1 = ThreadId.createFresh(); + assertTrue(i1.equals(i1)); + assertFalse(i1.equals(null)); + assertFalse(i1.equals(new Object())); + assertEquals(i1.hashCode(), i1.hashCode()); + byte[] i1bytes = i1.getBytes(); + assertNotNull(i1bytes); + assertTrue(i1bytes.length >= 5); + assertEquals('j', i1bytes[0]); + assertEquals('a', i1bytes[1]); + assertEquals('v', i1bytes[2]); + assertEquals('a', i1bytes[3]); + assertEquals(':', i1bytes[4]); + assertArrayEquals(i1bytes, i1.getBytes()); + + ThreadId i2 = ThreadId.createFresh(); + assertFalse(i1.equals(i2)); + assertFalse(i2.equals(i1)); + assertFalse(Arrays.equals(i1bytes, i2.getBytes())); + + ThreadId i3 = new ThreadId(i1bytes); + assertTrue(i3.equals(i1)); + assertFalse(i3.equals(i2)); + assertTrue(i1.equals(i3)); + assertEquals(i1.hashCode(), i3.hashCode()); + assertArrayEquals(i1bytes, i3.getBytes()); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java b/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java new file mode 100644 index 0000000000..7da68db23f --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/environments/remote/ThreadPool_Test.java @@ -0,0 +1,437 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.environments.remote; + +import com.sun.star.lib.uno.typedesc.MethodDescription; +import com.sun.star.lib.uno.typedesc.TypeDescription; +import org.junit.Test; +import static org.junit.Assert.*; + +public class ThreadPool_Test { + @Test public void testDispose() throws InterruptedException { + IThreadPool iThreadPool = ThreadPoolManager.create(); + TestThread testThread = new TestThread(iThreadPool); + + ThreadId threadId = null; + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + + threadId = testThread._threadId; + + // let the thread attach and enter the threadpool + testThread.notifyAll(); + } + + String message = "blabla"; + + // terminate the test thread + synchronized(testThread) { + // put reply job + iThreadPool.dispose(new RuntimeException(message)); + + testThread.wait(); + } + + testThread.join(); + +/*TODO: below test fails with "expected:<blabla> but was:<null>": + assertEquals(message, testThread._message); +*/ + } + + @Test public void testThreadAsync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "increment"); + } + + synchronized(workAt) { + putJob(workAt, false, threadId, "notifyme"); + + while(!workAt._notified) { + workAt.wait(); + } + } + + assertEquals(TestWorkAt.MESSAGES, workAt._counter); + } + + @Test public void testDynamicThreadSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "increment"); + } + + synchronized(workAt) { + putJob(workAt, true, threadId, "notifyme"); + + while(!workAt._notified) { + workAt.wait(); + } + } + + assertEquals(TestWorkAt.MESSAGES, workAt._counter); + } + + @Test public void testStaticThreadSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + + TestThread testThread = new TestThread(); + + ThreadId threadId = null; + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + + threadId = testThread._threadId; + + // let the thread attach and enter the threadpool + testThread.notifyAll(); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "increment"); + } + + // terminate the test thread + synchronized(testThread) { + // put reply job + putJob(workAt, true, threadId, null); + + testThread.wait(); + } + + testThread.join(); + + assertEquals(TestWorkAt.MESSAGES, workAt._counter); + } + + @Test public void testDynamicThreadAsyncSyncOrder() + throws InterruptedException + { + TestWorkAt workAt = new TestWorkAt(); + + ThreadId threadId = ThreadId.createFresh(); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "asyncCall"); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "syncCall"); + } + + synchronized(workAt) { + putJob(workAt, true, threadId, "notifyme"); + + while(!workAt._notified) { + workAt.wait(); + } + } + + assertTrue(workAt.passedAsyncTest()); + } + + @Test public void testStaticThreadAsyncSyncOrder() + throws InterruptedException + { + TestWorkAt workAt = new TestWorkAt(); + + TestThread testThread = new TestThread(); + + // start the test thread + synchronized(testThread) { + testThread.start(); + + testThread.wait(); + } + + ThreadId threadId = testThread._threadId; + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, false, threadId, "asyncCall"); + } + + // let the thread attach and enter the threadpool + synchronized(testThread) { + testThread.notifyAll(); + } + + // queue syncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + Thread.yield(); // force scheduling + putJob(workAt, true, threadId, "syncCall"); + } + + // terminate the test thread + synchronized(testThread) { + // put reply job + putJob(workAt, true, threadId, null); + + testThread.wait(); + } + + testThread.join(); + + assertTrue(workAt.passedAsyncTest()); + } + + @Test public void testStress() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + for (int i = 0; i < TestWorkAt.MESSAGES; ++i) { + Thread.yield(); // force scheduling + ThreadId threadID = ThreadId.createFresh(); + putJob(workAt, true, threadID, "increment"); + putJob(workAt, false, threadID, "increment"); + } + synchronized (workAt) { + while (workAt._counter < 2 * TestWorkAt.MESSAGES) { + workAt.wait(); + } + } + + abstract class Stress extends Thread { + private Stress(int count) { + this.count = count; + } + + @Override + public void run() { + try { + for (int i = 0; i < count; ++i) { + runTest(); + } + } catch (Throwable e) { + e.printStackTrace(System.err); + } + } + + protected abstract void runTest() throws InterruptedException; + + private final int count; + } + + Stress stress1 = new Stress(50) { + @Override + protected void runTest() throws InterruptedException { + testThreadAsync(); + } + }; + stress1.start(); + + Stress stress2 = new Stress(50) { + @Override + protected void runTest() throws InterruptedException { + testDynamicThreadSync(); + } + }; + stress2.start(); + + Stress stress3 = new Stress(50) { + @Override + protected void runTest() throws InterruptedException { + testStaticThreadSync(); + } + }; + stress3.start(); + + Stress stress4 = new Stress(50) { + @Override + protected void runTest() throws InterruptedException { + testDynamicThreadAsyncSyncOrder(); + } + }; + stress4.start(); + + Stress stress5 = new Stress(50) { + @Override + protected void runTest() throws InterruptedException { + testStaticThreadAsyncSyncOrder(); + } + }; + stress5.start(); + + Stress stress6 = new Stress(500) { + @Override + protected void runTest() throws InterruptedException { + testDispose(); + } + }; + stress6.start(); + + stress1.join(); + stress2.join(); + stress3.join(); + stress4.join(); + stress5.join(); + stress6.join(); + } + + @Test public void testAsyncSync() throws InterruptedException { + TestWorkAt workAt = new TestWorkAt(); + ThreadId threadId = ThreadId.createFresh(); + MyWorkAt myWorkAt = new MyWorkAt( workAt ); + + // queue asyncs + for(int i = 0; i < TestWorkAt.MESSAGES; ++ i) { + if( i == 2 ) + { + putJob( myWorkAt, false , threadId, "asyncCall" ); + } + putJob(workAt, false, threadId, "asyncCall"); + } + + synchronized(workAt) { + putJob(workAt, false, threadId, "notifyme"); + + while(!workAt._notified) { + workAt.wait(); + } + } + + assertEquals(TestWorkAt.MESSAGES, workAt._async_counter); + assertTrue(myWorkAt._success); + } + + private static void putJob(TestIWorkAt iWorkAt, boolean synchron, + ThreadId threadId, String operation) { + __iThreadPool.putJob( + new Job(iWorkAt, __iReceiver, + new Message( + threadId, operation != null, "oid", __workAt_td, + (operation == null + ? null + : ((MethodDescription) + __workAt_td.getMethodDescription(operation))), + synchron, null, false, null, null))); + } + + private static final class TestThread extends Thread { + ThreadId _threadId; + IThreadPool _iThreadPool; + + TestThread() { + this(__iThreadPool); + } + + TestThread(IThreadPool iThreadPool) { + _iThreadPool = iThreadPool; + } + + @Override + public void run() { + _threadId = _iThreadPool.getThreadId(); + + + try { + synchronized(this) { + // notify that we are running + notify(); + + _iThreadPool.attach(); + + // wait until we should continue + wait(); + } + + _iThreadPool.enter(); + } + catch(Throwable throwable) { + } + + _iThreadPool.detach(); + + synchronized(this) { + // notify the listeners that we are dying + notifyAll(); + } + } + } + + private static final class MyWorkAt implements TestIWorkAt { + public MyWorkAt( TestWorkAt async_WorkAt ) { + _async_WorkAt = async_WorkAt; + } + + public void syncCall() throws Throwable + { + Message iMessage = new Message( + __iThreadPool.getThreadId(), false, "oid", __workAt_td, null, + false, null, false, null, null); + + // marshal reply + ThreadPool_Test.__iThreadPool.putJob( + new Job(this, ThreadPool_Test. __iReceiver, iMessage)); + } + + public void asyncCall() throws Throwable { + for (int i = 0 ; i < 5 ; ++i) { + ThreadPool_Test.__iThreadPool.attach(); + ThreadPool_Test.putJob(this, true, __iThreadPool.getThreadId(), + "syncCall"); + // wait for reply + ThreadPool_Test.__iThreadPool.enter(); + ThreadPool_Test.__iThreadPool.detach(); + } + // async must have waited for this call + _success = _async_WorkAt._async_counter == 2; + } + + public void increment() throws Throwable {} + + public void notifyme() {} + + public boolean _success = false; + + private final TestWorkAt _async_WorkAt; + } + + private static final IThreadPool __iThreadPool = ThreadPoolManager.create(); + private static final IReceiver __iReceiver = new TestReceiver(); + private static final TypeDescription __workAt_td + = TypeDescription.getTypeDescription(TestIWorkAt.class); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/Cache_Test.java b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Cache_Test.java new file mode 100644 index 0000000000..a7d93733d7 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Cache_Test.java @@ -0,0 +1,134 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.protocols.urp; + +import org.junit.Test; +import static org.junit.Assert.*; + +public final class Cache_Test { + @Test public void test0() { + Cache c = new Cache(0); + boolean[] f = new boolean[1]; + int i; + i = c.add(f, "a"); + assertTrue(i == Cache.NOT_CACHED && !f[0]); + i = c.add(f, "a"); + assertTrue(i == Cache.NOT_CACHED && !f[0]); + i = c.add(f, "b"); + assertTrue(i == Cache.NOT_CACHED && !f[0]); + i = c.add(f, "a"); + assertTrue(i == Cache.NOT_CACHED && !f[0]); + } + + @Test public void test1() { + Cache c = new Cache(1); + boolean[] f = new boolean[1]; + int i; + i = c.add(f, "a"); + assertTrue(i == 0 && !f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "b"); + assertTrue(i == 0 && !f[0]); + i = c.add(f, "b"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && !f[0]); + } + + @Test public void test2() { + Cache c = new Cache(2); + boolean[] f = new boolean[1]; + int i; + i = c.add(f, "a"); + assertTrue(i == 0 && !f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "b"); + assertTrue(i == 1 && !f[0]); + i = c.add(f, "b"); + assertTrue(i == 1 && f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "c"); + assertTrue(i == 1 && !f[0]); + i = c.add(f, "b"); + assertTrue(i == 0 && !f[0]); + } + + @Test public void test3() { + Cache c = new Cache(3); + boolean[] f = new boolean[1]; + int i; + i = c.add(f, "a"); + assertTrue(i == 0 && !f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "b"); + assertTrue(i == 1 && !f[0]); + i = c.add(f, "a"); + assertTrue(i == 0 && f[0]); + i = c.add(f, "c"); + assertTrue(i == 2 && !f[0]); + i = c.add(f, "d"); + assertTrue(i == 1 && !f[0]); + i = c.add(f, "d"); + assertTrue(i == 1 && f[0]); + } + + @Test public void testNothingLostFromLruList() { + // Regardless in what order arbitrary values from 0, ..., 3 are inserted + // into a size-4 cache, afterwards adding -1, ..., -4 must return each + // possible index in the range from 0, ..., 3 exactly once (so their sum + // must be 6); this code systematically tests all such arbitrary ways up + // to length 8 (the code arguably violates recommendations for writing + // good tests, but actually helped track down an error in the Cache + // implementation): + int[] a = new int[8]; + for (int i = 0; i < a.length; ++i) { + for (int j = 0; j < i; ++j) { + a[j] = 0; + } + for (;;) { + Cache c = new Cache(4); + for (int k = 0; k < i; ++k) { + c.add(new boolean[1], a[k]); + } + assertEquals( + 6, + (c.add(new boolean[1], -1) + c.add(new boolean[1], -2) + + c.add(new boolean[1], -3) + c.add(new boolean[1], -4))); + int j = i - 1; + while (j >= 0 && a[j] == 3) { + --j; + } + if (j < 0) { + break; + } + ++a[j]; + for (int k = j + 1; k < i; ++k) { + a[k] = 0; + } + } + } + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/Marshaling_Test.java b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Marshaling_Test.java new file mode 100644 index 0000000000..4a6c93652e --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Marshaling_Test.java @@ -0,0 +1,353 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.uno.protocols.urp; + +import com.sun.star.lib.uno.typedesc.TypeDescription; +import com.sun.star.uno.Any; +import com.sun.star.uno.Type; +import com.sun.star.uno.TypeClass; +import com.sun.star.uno.XInterface; +import java.lang.reflect.Array; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class Marshaling_Test { + @Test public void test() throws Exception { + short cacheSize = (short)256; + TestBridge testBridge = new TestBridge(); + Marshal marshal = new Marshal(testBridge, cacheSize); + + TestObject testObject = new TestObject(); + + TestPrimitiveSeqStruct x = new TestPrimitiveSeqStruct(); + x.zAny = new Object[]{Integer.valueOf(1), Double.valueOf(2) }; + + + Object data[] = new Object[] { + new com.sun.star.uno.RuntimeException("testRuntimeException"), + new com.sun.star.uno.Exception("testException"), + Boolean.TRUE, + Byte.valueOf((byte)47), + Character.valueOf('k'), + Double.valueOf(0.12345), + TestEnum.B, + Float.valueOf(0.5678f), + Integer.valueOf(0), + Integer.valueOf(128), + Integer.valueOf(0x0f00), + Integer.valueOf(0x0f0000), + Integer.valueOf(0x0f000000), + Integer.valueOf(-128), + Integer.valueOf(0xff00), + Integer.valueOf(0xff0000), + Integer.valueOf(0xff000000), + Long.valueOf(666L), + Short.valueOf((short)444), + "blabla", + Integer.valueOf(10), // Any as object + Integer.valueOf(10), // Any as object + new Any(new Type(Integer.class), Integer.valueOf(10)), // Any as Any + new Any(new Type(Integer.class), Integer.valueOf(10)), // Any as Any + null, + new TestPrimitiveStruct(), + x, + new byte[]{1,2,3,4,5,6,7}, // primitive sequence + new int[]{7,6,5,4,3,2,1}, // primitive sequence + new Object[]{Integer.valueOf(123), "hallo"}, // any sequence + new TestPrimitiveStruct[]{new TestPrimitiveStruct(), new TestPrimitiveStruct()}, // sequence of primitive structs + new TestPrimitiveSeqStruct[]{new TestPrimitiveSeqStruct(), new TestPrimitiveSeqStruct()}, // sequence of primitive structs + new TestNestedStruct(), + new TestNestedSeqStruct(), + new Type(Void.class), + new Type(String.class), + new Type(Object.class), + new Type(Byte.class), + new Type(Integer.class), + new Type(Double.class), + new Type(Float.class), + new Type(Character.class), + new Type(Short.class), + new Type(Boolean.class), + new Type(void.class), + new Type(byte.class), + new Type(int.class), + new Type(double.class), + new Type(float.class), + new Type(char.class), + new Type(short.class), + new Type(boolean.class), + new Type(Class.forName("[Ljava.lang.String;")), + new Type(Class.forName("[Ljava.lang.Object;")), + new Type(Class.forName("[B")), + new Type(Class.forName("[Z")), + new Type("boolean"), + new Type("byte"), + new Type("char"), + new Type("short"), + new Type("long"), + new Type("hyper"), + new Type("float"), + new Type("double"), + new Type("string"), + new Type("void"), + new Type("any"), + new Type( + "com.sun.star.lib.uno.protocols.urp.TestEnum", TypeClass.ENUM), + new Type("[]boolean", TypeClass.SEQUENCE), + new Type("[][]byte", TypeClass.SEQUENCE), + new Type("[][][]char", TypeClass.SEQUENCE), + new Type("[][][][]short", TypeClass.SEQUENCE), + new Type("[][][][][]any", TypeClass.SEQUENCE), + new Type("com.sun.star.uno.XInterface", TypeClass.INTERFACE), + new Type("[]com.sun.star.uno.XInterface", TypeClass.SEQUENCE), + testObject, + testObject, + new TestInterfaceStruct(testObject, null) + }; + + TypeDescription dataTypes[] = new TypeDescription[] { + TypeDescription.getTypeDescription(com.sun.star.uno.RuntimeException.class), + TypeDescription.getTypeDescription(com.sun.star.uno.Exception.class), + TypeDescription.getTypeDescription(Boolean.class), + TypeDescription.getTypeDescription(Byte.class), + TypeDescription.getTypeDescription(Character.class), + TypeDescription.getTypeDescription(Double.class), + TypeDescription.getTypeDescription(TestEnum.class), + TypeDescription.getTypeDescription(Float.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Integer.class), + TypeDescription.getTypeDescription(Long.class), + TypeDescription.getTypeDescription(Short.class), + TypeDescription.getTypeDescription(String.class), + TypeDescription.getTypeDescription(Object.class), + TypeDescription.getTypeDescription(Any.class), + TypeDescription.getTypeDescription(Any.class), + TypeDescription.getTypeDescription(Object.class), + TypeDescription.getTypeDescription(XInterface.class), + TypeDescription.getTypeDescription(TestPrimitiveStruct.class), + TypeDescription.getTypeDescription(TestPrimitiveSeqStruct.class), + TypeDescription.getTypeDescription(Class.forName("[B")), + TypeDescription.getTypeDescription(Class.forName("[I")), + TypeDescription.getTypeDescription(Class.forName("[Lcom.sun.star.uno.Any;")), + TypeDescription.getTypeDescription( + Class.forName( + "[Lcom.sun.star.lib.uno.protocols.urp." + + "TestPrimitiveStruct;")), + TypeDescription.getTypeDescription( + Class.forName( + "[Lcom.sun.star.lib.uno.protocols.urp." + + "TestPrimitiveSeqStruct;")), + TypeDescription.getTypeDescription(TestNestedStruct.class), + TypeDescription.getTypeDescription(TestNestedSeqStruct.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(Type.class), + TypeDescription.getTypeDescription(XInterface.class), + TypeDescription.getTypeDescription(XInterface.class), + TypeDescription.getTypeDescription(TestInterfaceStruct.class), + }; + + + Unmarshal unmarshal = new Unmarshal(testBridge, cacheSize); + for(int i = 0; i < dataTypes.length; ++ i) { + Object op1 = data[i]; + marshal.writeValue(dataTypes[i], data[i]); + + unmarshal.reset(marshal.reset()); + + Object op2 = unmarshal.readValue(dataTypes[i]); + + if(op1 instanceof Any) + op1 = ((Any)op1).getObject(); + + assertTrue(compareObjects(op1, op2)); + } + } + + private static boolean compareArrays(Object op1, Object op2) throws Exception { + boolean result = true; + if((op1.getClass().getComponentType() == op2.getClass().getComponentType()) + && (Array.getLength(op1) == Array.getLength(op2))) + { + for(int i = 0; i < Array.getLength(op1); ++ i) + result = result & compareObjects(Array.get(op1, i), Array.get(op2, i)); + } + + return result; + } + + private static boolean compareInterfaces(XInterface op1, XInterface op2) { + return op1 == op2; + } + + private static boolean compareStructs(Class zClass, Object op1, Object op2) throws Exception { + boolean result = true; + + Field fields[] = zClass.getFields(); + + for(int i = 0; i < fields.length && result; ++ i) { + if((fields[i].getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) == 0) { // neither static nor transient ? + result = result & compareObjects(fields[i].get(op1), fields[i].get(op2)); + + if(!result) + System.err.println("blabal :" + fields[i]); + } + } + + return result; + } + + private static boolean compareStructs(Object op1, Object op2) throws Exception { + boolean result = true; + + if(op1.getClass() != op2.getClass()) + result = false; + else { + result = compareStructs(op1.getClass(), op1, op2); + } + + return result; + } + + private static boolean compareThrowable(Throwable op1, Throwable op2) throws Exception { + boolean result = true; + + if(op1.getClass() != op2.getClass()) + result = false; + else { + result = compareStructs(op1.getClass(), op1, op2); + + result = result & op1.getMessage().equals(op2.getMessage()); + } + + return result; + } + + private static boolean compareObjects(Object op1, Object op2) throws Exception { + boolean result = false; + + if(op1 == op2) + result = true; + + else if(op1.getClass().isPrimitive() && op2.getClass().isPrimitive()) + result = op1.equals(op2); + + else if(op1.getClass() == Byte.class && op2.getClass() == Byte.class) + result = op1.equals(op2); + + else if(op1.getClass() == Type.class && op2.getClass() == Type.class) + result = op1.equals(op2); + + else if(op1.getClass() == Boolean.class && op2.getClass() == Boolean.class) + result = op1.equals(op2); + + else if(op1.getClass() == Short.class && op2.getClass() == Short.class) + result = op1.equals(op2); + + else if(Throwable.class.isAssignableFrom(op1.getClass()) && Throwable.class.isAssignableFrom(op2.getClass())) + result = compareThrowable((Throwable)op1, (Throwable)op2); + + else if(op1.getClass() == Integer.class && op2.getClass() == Integer.class) + result = op1.equals(op2); + + else if(op1.getClass() == Character.class && op2.getClass() == Character.class) + result = op1.equals(op2); + + else if(op1.getClass() == Long.class && op2.getClass() == Long.class) + result = op1.equals(op2); + + else if(op1.getClass() == Void.class && op2.getClass() == Void.class) + result = op1.equals(op2); + + else if(op1.getClass() == Float.class && op2.getClass() == Float.class) + result = op1.equals(op2); + + else if(op1.getClass() == Double.class && op2.getClass() == Double.class) + result = op1.equals(op2); + + else if(op1.getClass().isArray() && op2.getClass().isArray()) + result = compareArrays(op1, op2); + + else if(op1.getClass() == Void.class || op2.getClass() == void.class) // write nothing ? + result = true; + + else if(XInterface.class.isAssignableFrom(op1.getClass()) && XInterface.class.isAssignableFrom(op2.getClass())) + result = compareInterfaces((XInterface)op1, (XInterface)op2); + + else if(op1.getClass() == String.class && op2.getClass() == String.class) // is it a String ? + result = ((String)op1).equals(op2); + + else if(op1.getClass() == Type.class && op2.getClass() == Type.class) // types? + result = op1.equals(op2); + + else // otherwise it must be a struct + result = compareStructs(op1, op2); + + return result; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/Protocol_Test.java b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Protocol_Test.java new file mode 100644 index 0000000000..cda58aad8f --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/Protocol_Test.java @@ -0,0 +1,311 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.uno.protocols.urp; + +import com.sun.star.lib.uno.environments.remote.Message; +import com.sun.star.lib.uno.environments.remote.IProtocol; +import com.sun.star.lib.uno.environments.remote.ThreadId; +import com.sun.star.lib.uno.typedesc.TypeDescription; +import com.sun.star.uno.Any; +import com.sun.star.uno.IBridge; +import com.sun.star.uno.Type; +import com.sun.star.uno.XInterface; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; +import java.util.LinkedList; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class Protocol_Test { + @Test public void test() throws Exception { + IBridge iBridge = new TestBridge(); + PipedInputStream inA = new PipedInputStream(); + PipedOutputStream outA = new PipedOutputStream(inA); + PipedInputStream inB = new PipedInputStream(); + PipedOutputStream outB = new PipedOutputStream(inB); + Endpoint iSender = new Endpoint(iBridge, inA, outB); + Endpoint iReceiver = new Endpoint(iBridge, inB, outA); + + TestObject testObject = new TestObject(); + String oId = (String)iBridge.mapInterfaceTo(testObject, new Type(XInterface.class)); + + testCall(iSender, iReceiver, oId); + testCallWithInParameter(iSender, iReceiver, oId); + testCallWithOutParameter(iSender, iReceiver, oId); + testCallWithInOutParameter(iSender, iReceiver, oId); + testCallWithResult(iSender, iReceiver, oId); + testCallWhichRaisesException(iSender, iReceiver, oId); + testCallWithIn_Out_InOut_Paramters_and_result(iSender, iReceiver, oId); + testCallWhichReturnsAny(iSender, iReceiver, oId); + } + + public void testCall( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "method", new ThreadId(new byte[] { 0, 1 }), new Object[0]); + iReceiver.readMessage(); + + // send a reply + iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); + iSender.readMessage(); + } + + public void testCallWithInParameter( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "methodWithInParameter", new ThreadId(new byte[] { 0, 1 }), + new Object[] { "hallo" }); + Message iMessage = iReceiver.readMessage(); + Object[] t_params = iMessage.getArguments(); + assertEquals("hallo", t_params[0]); + + // send a reply + iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); + iMessage = iSender.readMessage(); + } + + public void testCallWithOutParameter( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + Object params[] = new Object[]{new String[1]}; + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "methodWithOutParameter", new ThreadId(new byte[] { 0, 1 }), + params); + Message iMessage = iReceiver.readMessage(); + + + Object[] t_params = iMessage.getArguments(); + ((String [])t_params[0])[0] = "testString"; + + // send an exception as reply + iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); + iSender.readMessage(); + + assertEquals("testString", ((String [])params[0])[0]); + } + + public void testCallWithInOutParameter( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + Object params[] = new Object[]{new String[]{"inString"}}; + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "methodWithInOutParameter", new ThreadId(new byte[] { 0, 1 }), + params); + Message iMessage = iReceiver.readMessage(); + + + Object[] t_params = iMessage.getArguments(); + assertEquals("inString", ((String [])t_params[0])[0]); + + // provide reply + ((String [])t_params[0])[0] = "outString"; + + // send an exception as reply + iReceiver.writeReply(false, new ThreadId(new byte[] { 0, 1 }), null); + iSender.readMessage(); + + assertEquals("outString", ((String [])params[0])[0]); + } + + public void testCallWithResult( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "methodWithResult", new ThreadId(new byte[] { 0, 1 }), + new Object[0]); + iReceiver.readMessage(); + + // send a reply + iReceiver.writeReply( + false, new ThreadId(new byte[] { 0, 1 }), "resultString"); + Message iMessage = iSender.readMessage(); + Object result = iMessage.getResult(); + + assertEquals("resultString", result); + } + + public void testCallWhichRaisesException( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + // send a second request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "method", new ThreadId(new byte[] { 0, 1 }), new Object[0]); + iReceiver.readMessage(); + + // send an exception as reply + iReceiver.writeReply( + true, new ThreadId(new byte[] { 0, 1 }), + new com.sun.star.uno.RuntimeException("test the exception")); + Message iMessage = iSender.readMessage(); + + Object result = iMessage.getResult(); + + assertTrue(result instanceof com.sun.star.uno.RuntimeException); + } + + public void testCallWithIn_Out_InOut_Paramters_and_result( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + Object params[] = new Object[]{"hallo", new String[1], new String[]{"inOutString"}}; + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "MethodWithIn_Out_InOut_Paramters_and_result", + new ThreadId(new byte[] { 0, 1 }), params); + Message iMessage = iReceiver.readMessage(); + + Object[] t_params = iMessage.getArguments(); + + assertEquals("hallo", t_params[0]); + + assertEquals("inOutString", ((String [])t_params[2])[0]); + + ((String [])t_params[1])[0] = "outString"; + ((String [])t_params[2])[0] = "inOutString_res"; + + // send an exception as reply + iReceiver.writeReply( + false, new ThreadId(new byte[] { 0, 1 }), "resultString"); + iMessage = iSender.readMessage(); + + Object result = iMessage.getResult(); + assertEquals("outString", ((String [])params[1])[0]); + + assertEquals("inOutString_res", ((String [])params[2])[0]); + + assertEquals("resultString", result); + } + + public void testCallWhichReturnsAny( + Endpoint iSender, Endpoint iReceiver, String oId) throws Exception + { + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "returnAny", new ThreadId(new byte[] { 0, 1 }), null); + iReceiver.readMessage(); + // send a reply + iReceiver.writeReply( + false, new ThreadId(new byte[] { 0, 1 }), Any.VOID); + Message iMessage = iSender.readMessage(); + Object result = iMessage.getResult(); + assertTrue( + result instanceof Any && + ((TypeDescription.getTypeDescription(((Any) result).getType()). + getZClass()) == + void.class)); + + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "returnAny", new ThreadId(new byte[] { 0, 1 }), null); + iReceiver.readMessage(); + // send a reply + iReceiver.writeReply( + false, new ThreadId(new byte[] { 0, 1 }), + new Any(XInterface.class, null)); + iMessage = iSender.readMessage(); + result = iMessage.getResult(); + assertNull(result); + + // send an ordinary request + iSender.writeRequest( + oId, TypeDescription.getTypeDescription(TestXInterface.class), + "returnAny", new ThreadId(new byte[] { 0, 1 }), null); + iReceiver.readMessage(); + // send a reply + iReceiver.writeReply( + false, new ThreadId(new byte[] { 0, 1 }), Integer.valueOf(501)); + iMessage = iSender.readMessage(); + result = iMessage.getResult(); + assertEquals(501, result); + } + + private static final class Endpoint { + public Endpoint(IBridge bridge, InputStream input, OutputStream output) + throws IOException + { + protocol = new urp(bridge, null, input, output); + new Thread() { + @Override + public void run() { + for (;;) { + Object o; + try { + o = protocol.readMessage(); + } catch (IOException e) { + o = e; + } + synchronized (queue) { + queue.addLast(o); + } + } + } + }.start(); + protocol.init(); + } + + public Message readMessage() throws IOException { + for (;;) { + synchronized (queue) { + if (!queue.isEmpty()) { + Object o = queue.removeFirst(); + if (o instanceof Message) { + return (Message) o; + } else { + throw (IOException) o; + } + } + } + } + } + + public boolean writeRequest( + String oid, TypeDescription type, String function, ThreadId tid, + Object[] arguments) + throws IOException + { + return protocol.writeRequest(oid, type, function, tid, arguments); + } + + public void writeReply(boolean exception, ThreadId tid, Object result) + throws IOException + { + protocol.writeReply(exception, tid, result); + } + + private final IProtocol protocol; + private final LinkedList<Object> queue = new LinkedList<Object>(); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestBridge.java b/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestBridge.java new file mode 100644 index 0000000000..acd1b3948d --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestBridge.java @@ -0,0 +1,106 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.uno.protocols.urp; + +import java.io.IOException; + +import java.util.HashMap; + + +import com.sun.star.uno.IBridge; +import com.sun.star.uno.IEnvironment; +import com.sun.star.uno.Type; + + +class TestBridge implements IBridge { + private static final boolean DEBUG = false; + + private final HashMap<String,Object> _hashtable = new HashMap<String,Object>(); + + private IEnvironment _source ;//= new com.sun.star.lib.uno.environments.java.java_environment(null); + + + private class MyEnv implements IEnvironment { + public Object getContext() { + return null; + } + + public String getName() { + return null; + } + + public Object registerInterface(Object object, String oId[], Type type) { + return null; + } + + public void revokeInterface(String oId, Type type) { + } + + public Object getRegisteredInterface(String oid, Type type) { + Object object = _hashtable.get(oid); + + if(DEBUG) System.err.println("##### " + getClass().getName() + ".getRegisteredInterface:" + oid + " " + object); + + return object; + } + + public String getRegisteredObjectIdentifier(Object object) { + return null; + } + + public void list() { + } + } + + TestBridge() { + _source = new MyEnv(); + } + + public Object mapInterfaceTo(Object object, Type type) { + if (object == null) { + return null; + } else { + String oid = ">" + object.toString() + type.toString() + "<"; + _hashtable.put(oid, object); + return oid; + } + } + + public Object mapInterfaceFrom(Object object, Type type) { + String oid = (String)object; + + return _hashtable.get(oid); + } + + public IEnvironment getSourceEnvironment() { + return _source; + } + + public IEnvironment getTargetEnvironment() { + return null; + } + + public void acquire() {} + + public void release() {} + + public void dispose() throws InterruptedException, IOException {} +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestObject.java b/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestObject.java new file mode 100644 index 0000000000..f36710e1d2 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/TestObject.java @@ -0,0 +1,63 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ +package com.sun.star.lib.uno.protocols.urp; + + + +class TestObject implements TestXInterface { + public void method1( /*IN*/java.lang.Object itf ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public void method2( /*OUT*/java.lang.Object[] itf ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public void method3( /*INOUT*/java.lang.Object[] itf ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public Object method4( ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + return null; + } + + public Object returnAny( ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + return null; + } + + + public void method() throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public void methodWithInParameter( /*IN*/String text ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public void methodWithOutParameter( /*OUT*/String[] text ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public void methodWithInOutParameter( /*INOUT*/String[] text ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + } + + public String methodWithResult( ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + return "TestObject_resultString"; + } + + public String MethodWithIn_Out_InOut_Paramters_and_result( /*IN*/String text, /*OUT*/String[] outtext, /*INOUT*/String[] inouttext ) throws com.sun.star.uno.Exception, com.sun.star.uno.RuntimeException { + return "TestObject_resultString"; + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/protocols/urp/interfaces.idl b/ridljar/test/com/sun/star/lib/uno/protocols/urp/interfaces.idl new file mode 100644 index 0000000000..eb2cdec3f5 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/protocols/urp/interfaces.idl @@ -0,0 +1,96 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +module com { module sun { module star { module lib { module uno { +module protocols { module urp { + + struct TestPrimitiveStruct { + boolean zBool; + short zShort; + unsigned short zUShort; + long zLong; + unsigned long zULong; + hyper zHyper; + unsigned hyper zUHyper; + float zFloat; + double zDouble; + char zChar; + byte zByte; + string zString; + any zAny; + }; + + struct TestPrimitiveSeqStruct { + sequence<boolean> zBool; + sequence<short> zShort; + sequence<unsigned short> zUShort; + sequence<long> zLong; + sequence<unsigned long> zULong; + sequence<hyper> zHyper; + sequence<unsigned hyper> zUHyper; + sequence<float> zFloat; + sequence<double> zDouble; + sequence<char> zChar; + sequence<byte> zByte; + sequence<string> zString; + sequence<any> zAny; + }; + + struct TestNestedStruct { + TestPrimitiveStruct primitiveStruct; + TestPrimitiveSeqStruct primitiveSeqStruct; + }; + + struct TestNestedSeqStruct { + sequence< sequence< long > > val; + }; + + interface TestXInterface : com::sun::star::uno::XInterface { + void method1([in] com::sun::star::uno::XInterface itf) raises( com::sun::star::uno::Exception ); + void method2([out] com::sun::star::uno::XInterface itf) raises( com::sun::star::uno::Exception ); + void method3([inout] com::sun::star::uno::XInterface itf) raises( com::sun::star::uno::Exception ); + com::sun::star::uno::XInterface method4() raises( com::sun::star::uno::Exception ); + + any returnAny() raises( com::sun::star::uno::Exception ); + + void method() raises( com::sun::star::uno::Exception ); + void methodWithInParameter([in] string text) raises( com::sun::star::uno::Exception ); + void methodWithOutParameter([out] string text) raises( com::sun::star::uno::Exception ); + void methodWithInOutParameter([inout] string text) raises( com::sun::star::uno::Exception ); + string methodWithResult() raises( com::sun::star::uno::Exception ); + + string MethodWithIn_Out_InOut_Paramters_and_result([in] string text, [out] string outtext, [inout] string inouttext) raises( com::sun::star::uno::Exception ); + }; + + struct TestInterfaceStruct + { + com::sun::star::uno::XInterface hallo; + + com::sun::star::beans::XPropertySet hallo2; + }; + + enum TestEnum { + A = 7, + B = 8, + C = 11 + }; + +}; }; }; }; }; }; }; + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/uno/typedesc/TypeDescription_Test.java b/ridljar/test/com/sun/star/lib/uno/typedesc/TypeDescription_Test.java new file mode 100644 index 0000000000..13271dc0e2 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/uno/typedesc/TypeDescription_Test.java @@ -0,0 +1,298 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.uno.typedesc; + +import com.sun.star.lib.uno.typeinfo.MethodTypeInfo; +import com.sun.star.lib.uno.typeinfo.TypeInfo; +import com.sun.star.uno.Any; +import com.sun.star.uno.Type; +import com.sun.star.uno.TypeClass; +import com.sun.star.uno.XInterface; +import com.sun.star.uno.XNamingService; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class TypeDescription_Test { + @Test public void test() throws Exception { + TypeDescription voidTD = TypeDescription.getTypeDescription( + void.class); + TypeDescription stringTD = TypeDescription.getTypeDescription( + String.class); + TypeDescription typeTD = TypeDescription.getTypeDescription( + Type.class); + TypeDescription anyTD = TypeDescription.getTypeDescription(Any.class); + TypeDescription interfaceTD = TypeDescription.getTypeDescription( + XInterface.class); + + MethodSignature sigBuildinSyncTypeToAny = new MethodSignature( + true, false, new TypeDescription[] { typeTD }, + new TypeDescription[1], anyTD); + MethodSignature sigBuildinAsyncToVoid = new MethodSignature( + true, true, new TypeDescription[0], new TypeDescription[0], + voidTD); + MethodSignature sigAddonSyncStringToVoid = new MethodSignature( + false, false, new TypeDescription[] { stringTD }, + new TypeDescription[1], voidTD); + MethodSignature sigAddonSyncStringInterfaceToVoid = new MethodSignature( + false, false, new TypeDescription[] { stringTD, interfaceTD }, + new TypeDescription[2], voidTD); + MethodSignature sigAddonSyncStringToInterface = new MethodSignature( + false, false, new TypeDescription[] { stringTD }, + new TypeDescription[1], interfaceTD); + + TypeSignature emptyTypeSig = new TypeSignature( + null, new String[0], null, new String[0], null); + TypeSignature interfaceTypeSig = new TypeSignature( + null, new String[] { "queryInterface", "acquire", "release" }, + new MethodSignature[] { sigBuildinSyncTypeToAny, + sigBuildinAsyncToVoid, + sigBuildinAsyncToVoid }, + new String[0], null); + TypeSignature exceptionTypeSig = new TypeSignature( + null, new String[0], null, + new String[]{"Context"}, new TypeSignature[] { interfaceTypeSig }); + // com.sun.star.uno.Exception.idl says that Exception (a) has no + // base exception, and (b) has two fields, Message and Context; the + // generated com.sun.star.uno.Exception.java, however, (a) is + // inherited from java.lang.Exception, and (b) has only one field, + // Context, as Message is inherited from java.lang.Exception + TypeSignature namingServiceTypeSig = new TypeSignature( + interfaceTypeSig, + new String[] { "getRegisteredObject", "registerObject", + "revokeObject" }, + new MethodSignature[] { sigAddonSyncStringToInterface, + sigAddonSyncStringInterfaceToVoid, + sigAddonSyncStringToVoid }, + new String[0], null); + + Object[] byteData = new Object[] { + "byte", "[B", byte.class, TypeClass.BYTE }; + Object[] stringData = new Object[] { + "string", "[Ljava.lang.String;", java.lang.String.class, + TypeClass.STRING }; + Object[] typeClassData = new Object[] { + "com.sun.star.uno.TypeClass", "[Lcom.sun.star.uno.TypeClass;", + TypeClass.class, TypeClass.ENUM }; + Object[] interfaceData = new Object[] { + "com.sun.star.uno.XInterface", "[Lcom.sun.star.uno.XInterface;", + XInterface.class, TypeClass.INTERFACE }; + Object[] exceptionData = new Object [] { + "com.sun.star.uno.Exception", "[Lcom.sun.star.uno.Exception;", + com.sun.star.uno.Exception.class, TypeClass.EXCEPTION, + new Object[] { interfaceData } }; + Object[] namingServiceData = new Object[] { + "com.sun.star.uno.XNamingService", + "[Lcom.sun.star.uno.XNamingService;", XNamingService.class, + TypeClass.INTERFACE, null, interfaceData }; + + emptyTypeSig.test("TypeSignature.test(byte)", byteData, + TypeDescription.getTypeDescription("byte")); + emptyTypeSig.test("TypeSignature.test(string)", stringData, + TypeDescription.getTypeDescription("string")); + emptyTypeSig.test("TypeSignature.test(TypeClass)", typeClassData, + TypeDescription.getTypeDescription( + "com.sun.star.uno.TypeClass")); + exceptionTypeSig.test("TypeSignature.test(com.sun.star.uno.Exception)", + exceptionData, + TypeDescription.getTypeDescription( + "com.sun.star.uno.Exception")); + interfaceTypeSig.test("TypeSignature.test(XInterface)", interfaceData, + TypeDescription.getTypeDescription( + "com.sun.star.uno.XInterface")); + namingServiceTypeSig.test("TypeSignature.test(XNamingService)", + namingServiceData, + TypeDescription.getTypeDescription( + "com.sun.star.uno.XNamingService")); + } + + @Test public void testUnsigned() throws ClassNotFoundException { + assertEquals( + "TypeDescription for UNSIGNED LONG", "unsigned long", + TypeDescription.getTypeDescription(Type.UNSIGNED_LONG).getTypeName()); + } + + @Test public void testGetMethodDescription() { + TypeDescription td = TypeDescription.getTypeDescription(XDerived.class); + td.getMethodDescription("fn"); + } + + @Test public void testSequence() throws ClassNotFoundException { + assertEquals( + "unsigned short", + TypeDescription.getTypeDescription("[]unsigned short").getComponentType().getTypeName()); + } + + public interface XBase extends XInterface { + void fn(); + + TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("fn", 0, 0) }; + } + + public interface XDerived extends XBase { + TypeInfo[] UNOTYPEINFO = null; + } + + private final class MethodSignature { + public MethodSignature( + boolean buildIn, boolean oneWay, TypeDescription[] inParameters, + TypeDescription[] outParameters, TypeDescription returnValue) + { + this.buildIn = buildIn; + this.oneWay = oneWay; + this.inParameters = inParameters; + this.outParameters = outParameters; + this.returnValue = returnValue; + } + + public void test(String prefix, int index, + MethodDescription description) { + assertEquals(prefix + "; getIndex", index, description.getIndex()); + assertEquals( + prefix + "; getMethod", buildIn, + description.getMethod() == null); + assertEquals(prefix + "; isOneway", oneWay, description.isOneway()); + TypeDescription[] in = description.getInSignature(); + assertEquals( + prefix + "; getInSignature", inParameters.length, in.length); + for (int i = 0; i < in.length; ++i) { + assertEquals( + prefix + "; getInSignature " + i, inParameters[i], in[i]); + } + TypeDescription[] out = description.getOutSignature(); + assertEquals( + prefix + "; getOutSignature", outParameters.length, out.length); + for (int i = 0; i < out.length; ++i) { + assertTrue( + prefix + "; getOutSignature " + i, + (out[i] == null + ? outParameters[i] == null + : out[i].equals(outParameters[i]))); + } + assertEquals( + prefix + "; getReturnSignature", returnValue, + description.getReturnSignature()); + } + + private final boolean buildIn; + private final boolean oneWay; + private final TypeDescription[] inParameters; + private final TypeDescription[] outParameters; + private final TypeDescription returnValue; + } + + private final class TypeSignature { + public TypeSignature(TypeSignature superType, String[] methodNames, + MethodSignature[] methodSignatures, + String[] fieldNames, + TypeSignature[] fieldSignatures) { + this._superType = superType; + this.methodNames = methodNames; + this.methodSignatures = methodSignatures; + methodOffset = superType == null ? 0 + : superType.methodOffset + superType.methodNames.length; + this.fieldSignatures = fieldSignatures; + this.fieldNames = fieldNames; + fieldOffset = superType == null ? 0 + : superType.fieldOffset + superType.fieldNames.length; + } + + public void test(String prefix, Object[] data, + TypeDescription description) throws Exception { + assertEquals( + prefix + "; getTypeName", data[0], description.getTypeName()); + assertEquals( + prefix + "; equals", + TypeDescription.getTypeDescription((String)data[0]), + description); + assertEquals( + prefix + "; getArrayTypeName", data[1], + description.getArrayTypeName()); + assertSame( + prefix + "; getZClass", data[2], description.getZClass()); + assertSame( + prefix + "; getTypeClass", data[3], description.getTypeClass()); + assertNull( + prefix + "; getComponentType", description.getComponentType()); + + MethodDescription[] mds = description.getMethodDescriptions(); + assertTrue( + prefix + "; getMethodDescriptions", + mds == null + ? methodSignatures == null + : mds.length == methodSignatures.length); + if (methodSignatures != null) { + for (int i = 0; i < methodSignatures.length; ++i) { + methodSignatures[i].test( + prefix + "; getMethodDescriptions " + i, + i + methodOffset, mds[i]); + } + } + for (int i = 0; i < methodNames.length; ++i) { + MethodDescription md = description.getMethodDescription( + i + methodOffset); + assertNotNull( + prefix + "; getMethodDescription " + (i + methodOffset), + md); + methodSignatures[i].test( + prefix + "; getMethodDescription " + (i + methodOffset), + i + methodOffset, md); + } + for (int i = 0; i < methodNames.length; ++i) { + MethodDescription md = description.getMethodDescription( + methodNames[i]); + assertNotNull( + prefix + "; getMethodDescription " + methodNames[i], md); + methodSignatures[i].test( + prefix + "; getMethodDescription " + methodNames[i], + i + methodOffset, md); + } + + FieldDescription[] fds = description.getFieldDescriptions(); + assertTrue( + prefix + "; getFieldDescriptions", + fds == null + ? fieldSignatures == null + : fds.length == fieldSignatures.length); + if (fieldSignatures != null) { + for (int i = 0; i < fieldSignatures.length; ++i) { + fieldSignatures[i].test( + prefix + "; getFieldDescriptions " + i, + (Object[]) ((Object[]) data[4])[i], + fds[i].getTypeDescription()); + } + } + + TypeDescription supert = description.getSuperType(); + assertEquals( + prefix + "; getSuperType", data.length < 6, supert == null); + if (supert != null && data[5] != null) { + _superType.test(prefix + "; getSuperType", (Object[]) data[5], + supert); + } + } + + private final TypeSignature _superType; + private final MethodSignature[] methodSignatures; + private final String[] methodNames; + private final int methodOffset; + private final TypeSignature[] fieldSignatures; + private final String[] fieldNames; + private final int fieldOffset; + } +} diff --git a/ridljar/test/com/sun/star/lib/util/NativeLibraryLoader_Test.java b/ridljar/test/com/sun/star/lib/util/NativeLibraryLoader_Test.java new file mode 100644 index 0000000000..e4101f2dd7 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/util/NativeLibraryLoader_Test.java @@ -0,0 +1,76 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class NativeLibraryLoader_Test { + @Test public void testEncoded() throws MalformedURLException { + File dir = new File(System.getProperty("user.dir")); + File subdir = new File(dir, "with space"); + File file1 = new File(subdir, "file"); + + String fileUrl = dir.toURI().toURL().toString(); + if (!fileUrl.endsWith("/")) { + fileUrl += "/"; + } + fileUrl += "with%20space/file"; + final URL url = new URL(fileUrl); + + File file2 = NativeLibraryLoader.getResource( + new ClassLoader() { + @Override + public URL getResource(String name) { + return url; + } + }, + "dummy"); + assertEquals("Files are equal", file1, file2); + } + + @Test public void testUnencoded() throws MalformedURLException { + File dir = new File(System.getProperty("user.dir")); + File subdir = new File(dir, "with space"); + File file1 = new File(subdir, "file"); + + String fileUrl = dir.toURI().toURL().toString(); + if (!fileUrl.endsWith("/")) { + fileUrl += "/"; + } + fileUrl += "with space/file"; + final URL url = new URL(fileUrl); + + File file2 = NativeLibraryLoader.getResource( + new ClassLoader() { + @Override + public URL getResource(String name) { + return url; + } + }, + "dummy"); + assertEquals("Files are equal", file1, file2); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java b/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java new file mode 100644 index 0000000000..378a1ac787 --- /dev/null +++ b/ridljar/test/com/sun/star/lib/util/WeakMap_Test.java @@ -0,0 +1,74 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.lib.util; + +import org.junit.Test; +import util.WaitUnreachable; +import static org.junit.Assert.*; + +public final class WeakMap_Test { + @Test public void test() { + WeakMap<String,Object> m = new WeakMap<String,Object>(); + assertEquals(0, m.size()); + assertTrue(m.isEmpty()); + assertFalse(m.containsKey("key1")); + assertFalse(m.containsValue(null)); + WaitUnreachable u1 = new WaitUnreachable(new Object()); + m.put("key1", u1.get()); + WaitUnreachable u2 = new WaitUnreachable(new Disposable()); + m.put("key2", u2.get()); + assertEquals(2, m.size()); + assertFalse(m.isEmpty()); + assertTrue(m.containsKey("key1")); + assertTrue(m.containsKey("key2")); + assertFalse(m.containsKey("key3")); + assertTrue(m.containsValue(m.get("key1"))); + assertTrue(m.containsValue(m.get("key2"))); + assertEquals(u1.get(), WeakMap.getValue(m.get("key1"))); + assertEquals(u2.get(), WeakMap.getValue(m.get("key2"))); + assertEquals(2, m.values().size()); + assertTrue(m.values().contains(m.get("key1"))); + assertTrue(m.values().contains(m.get("key2"))); + u1.waitUnreachable(); + assertNull(WeakMap.getValue(m.get("key1"))); + ((Disposable) u2.get()).dispose(); + assertNull(WeakMap.getValue(m.get("key2"))); + m.clear(); + u2.waitUnreachable(); + assertEquals(0, m.size()); + m.put("key2", new Object()); + assertEquals(1, m.size()); + } + + // This simple class (single listener, no synchronization) exploits + // knowledge about the implementation of WeakMap: + private static final class Disposable implements DisposeNotifier { + public void addDisposeListener(DisposeListener listener) { + this.listener = listener; + } + + public void dispose() { + if (listener != null) { + listener.notifyDispose(this); + } + } + + private DisposeListener listener = null; + } +} diff --git a/ridljar/test/com/sun/star/uno/AnyConverter_Test.java b/ridljar/test/com/sun/star/uno/AnyConverter_Test.java new file mode 100644 index 0000000000..193d86baf2 --- /dev/null +++ b/ridljar/test/com/sun/star/uno/AnyConverter_Test.java @@ -0,0 +1,904 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import com.sun.star.lang.XTypeProvider; +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + +public final class AnyConverter_Test { + Any anyBool; + Any anyChar; + Any anyByte; + Any anyShort; + Any anyInt; + Any anyLong; + Any anyFloat; + Any anyDouble; + Any anyStr; + Any anyType; + Any anyArByte; + Any anyVoid; + Any anyXTypeProvider; + + Boolean aBool; + Character aChar; + Byte aByte; + Short aShort; + Integer aInt; + Long aLong; + Float aFloat; + Double aDouble; + Object aObj; + String aStr; + Type aType; + byte[] arByte; + + /** Class variables are initialized before each Test method */ + @Before public void setUp() + { + aBool= Boolean.TRUE; + aChar= Character.valueOf('A'); + aByte= Byte.valueOf((byte) 111); + aShort= Short.valueOf((short) 11111); + aInt= Integer.valueOf( 1111111); + aLong= Long.valueOf( 0xffffffff); + aFloat= Float.valueOf( 3.14f); + aDouble= Double.valueOf( 3.145); + aObj= new ATypeProvider(); + aStr= "I am a string"; + aType= new Type(String.class); + arByte= new byte[] {1,2,3}; + + anyVoid= new Any(new Type(void.class), null); + anyBool= new Any(new Type(Boolean.TYPE), aBool); + anyChar= new Any(new Type(Character.TYPE), aChar); + anyByte= new Any(new Type(Byte.TYPE), aByte); + anyShort= new Any(new Type(Short.TYPE), aShort); + anyInt= new Any(new Type(Integer.TYPE), aInt); + anyLong= new Any(new Type(Long.TYPE), aLong); + anyFloat= new Any(new Type(Float.TYPE), aFloat); + anyDouble= new Any(new Type(Double.TYPE), aDouble); + anyStr= new Any(new Type(String.class), aStr); + anyType= new Any(new Type(Type.class), aType); + anyArByte= new Any(new Type(byte[].class), arByte); + anyXTypeProvider= new Any(new Type(XTypeProvider.class), aObj); + } + + @Test public void test_toBoolean() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + boolean b= AnyConverter.toBoolean(aBool); + assertEquals(b, aBool.booleanValue()); + b= AnyConverter.toBoolean(anyBool); + assertEquals(b, ((Boolean)anyBool.getObject()).booleanValue()); + + // must fail + try { AnyConverter.toBoolean(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toBoolean(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toChar() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + char b= AnyConverter.toChar(aChar); + assertEquals(b, aChar.charValue()); + b= AnyConverter.toChar(anyChar); + assertEquals(b, ((Character)anyChar.getObject()).charValue()); + + // must fail + try { AnyConverter.toChar(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toByte() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + byte val= AnyConverter.toByte(aByte); + assertEquals(val, aByte.byteValue()); + val= AnyConverter.toByte(anyByte); + assertEquals(val, ((Byte)anyByte.getObject()).byteValue()); + + // must fail + try { AnyConverter.toByte(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toByte(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toShort() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + short sh= AnyConverter.toShort(aByte); + assertEquals(sh, aByte.byteValue()); + sh= AnyConverter.toShort(aShort); + assertEquals(sh, aShort.shortValue()); + sh= AnyConverter.toShort(anyByte); + assertEquals(sh, ((Byte)anyByte.getObject()).byteValue()); + sh= AnyConverter.toShort(anyShort); + assertEquals(sh, ((Short) anyShort.getObject()).shortValue()); + Any a = new Any( Type.UNSIGNED_SHORT, Short.valueOf((short)5) ); + assertEquals(5, AnyConverter.toUnsignedShort( a )); + + // must fail + try { AnyConverter.toShort(a); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toUnsignedShort(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toChar(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toShort(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toInt() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + int val= AnyConverter.toInt(aByte); + assertEquals(val, aByte.byteValue()); + val= AnyConverter.toInt(aShort); + assertEquals(val, aShort.shortValue()); + val= AnyConverter.toInt(aInt); + assertEquals(val, aInt.intValue()); + val= AnyConverter.toInt(anyByte); + assertEquals(val, ((Byte)anyByte.getObject()).byteValue()); + val= AnyConverter.toInt(anyShort); + assertEquals(val, ((Short) anyShort.getObject()).shortValue()); + val= AnyConverter.toInt(anyInt); + assertEquals(val, ((Integer) anyInt.getObject()).intValue()); + Any a = new Any( Type.UNSIGNED_SHORT, Short.valueOf((short)5) ); + assertEquals(5, AnyConverter.toInt(a)); + assertEquals(5, AnyConverter.toUnsignedInt(a)); + a = new Any( Type.UNSIGNED_LONG, Integer.valueOf(5) ); + assertEquals(5, AnyConverter.toUnsignedInt(a)); + + // must fail + try { AnyConverter.toUnsignedInt(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(a); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toUnsignedInt(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toInt(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toLong() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + long val= AnyConverter.toLong(aByte); + assertEquals(val, aByte.byteValue()); + val= AnyConverter.toLong(aShort); + assertEquals(val, aShort.shortValue()); + val= AnyConverter.toLong(aInt); + assertEquals(val, aInt.intValue()); + val= AnyConverter.toLong(aLong); + assertEquals(val, aLong.longValue()); + val= AnyConverter.toLong(anyByte); + assertEquals(val, ((Byte)anyByte.getObject()).byteValue()); + val= AnyConverter.toLong(anyShort); + assertEquals(val, ((Short) anyShort.getObject()).shortValue()); + val= AnyConverter.toLong(anyInt); + assertEquals(val, ((Integer) anyInt.getObject()).intValue()); + val= AnyConverter.toLong(anyLong); + assertEquals(val, ((Long) anyLong.getObject()).longValue()); + Any a = new Any( Type.UNSIGNED_SHORT, Short.valueOf((short)5) ); + assertEquals(5, AnyConverter.toLong(a)); + assertEquals(5, AnyConverter.toUnsignedLong(a)); + a = new Any( Type.UNSIGNED_LONG, Integer.valueOf(5) ); + assertEquals(5, AnyConverter.toUnsignedLong(a)); + assertEquals(5, AnyConverter.toLong(a)); + a = new Any( Type.UNSIGNED_HYPER, Long.valueOf(5) ); + assertEquals(5, AnyConverter.toUnsignedLong(a)); + + // must fail + try { AnyConverter.toUnsignedLong(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toUnsignedLong(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(a); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toUnsignedLong(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toLong(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toFloat() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + float val= AnyConverter.toFloat(aByte); + assertEquals(val, aByte.byteValue(), 0); // 111 = 111.0 + val= AnyConverter.toFloat(anyByte); + assertEquals(val, ((Byte)anyByte.getObject()).byteValue(), 0); + val= AnyConverter.toFloat(aShort); + assertEquals(val, aShort.shortValue(), 0); //11111 = 11111.0 + val= AnyConverter.toFloat(anyShort); + assertEquals(val, ((Short) anyShort.getObject()).shortValue(), 0); + val= AnyConverter.toFloat(aFloat); + assertEquals(val, aFloat.floatValue(), 0); + val= AnyConverter.toFloat(anyFloat); + assertEquals(val, ((Float) anyFloat.getObject()).floatValue(), 0); + + // must fail + try { AnyConverter.toFloat(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toFloat(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toDouble() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + double val= AnyConverter.toDouble(aByte); + assertEquals(val, aByte.byteValue(), 0); // 111 = 111.0 + val= AnyConverter.toDouble(anyByte); + assertEquals(val, ((Byte)anyByte.getObject()).byteValue(), 0); + val= AnyConverter.toDouble(aShort); + assertEquals(val, aShort.shortValue(), 0); //11111 = 11111.0 + val= AnyConverter.toDouble(anyShort); + assertEquals(val, ((Short) anyShort.getObject()).shortValue(), 0); + val= AnyConverter.toDouble(aInt); + assertEquals(val, aInt.intValue(), 0); + val= AnyConverter.toDouble(anyInt); + assertEquals(val, ((Integer) anyInt.getObject()).intValue(), 0); + val= AnyConverter.toDouble(aFloat); + assertEquals(val, aFloat.floatValue(), 0); + val= AnyConverter.toDouble(anyFloat); + float float1= ((Float) anyFloat.getObject()).floatValue(); + assertTrue(val <= (float1 + 0.1) || val >= (float1 - 0.1)); + val= AnyConverter.toDouble(aDouble); + assertEquals(val, aDouble.doubleValue(), 0); + val= AnyConverter.toDouble(anyDouble); + assertEquals(val, ((Double) anyDouble.getObject()).doubleValue(), 0); + + // must fail + try { AnyConverter.toDouble(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toDouble(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toObject() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + Type _type= new Type(XTypeProvider.class); + Object val= AnyConverter.toObject(_type, aObj); + assertTrue(UnoRuntime.areSame(val, aObj)); + val= AnyConverter.toObject( + _type, new Any( new Type(XTypeProvider.class), null)); + assertNull(val); + + // structs, exceptions + com.sun.star.lang.IllegalArgumentException exc = + new com.sun.star.lang.IllegalArgumentException(); + Any any_exc = new Any( + new Type("com.sun.star.lang.IllegalArgumentException", + TypeClass.EXCEPTION), exc); + assertEquals( + exc, + AnyConverter.toObject( + new Type(com.sun.star.lang.IllegalArgumentException.class), + any_exc)); + assertEquals( + exc, + AnyConverter.toObject( + new Type(com.sun.star.uno.RuntimeException.class), any_exc)); + try { + AnyConverter.toObject( + new Type(com.sun.star.uno.Exception.class), any_exc); + fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + any_exc = new Any(com.sun.star.lang.IllegalArgumentException.class, + exc); + assertEquals( + exc, + AnyConverter.toObject( + new Type(com.sun.star.lang.IllegalArgumentException.class), + any_exc)); + assertEquals( + exc, + AnyConverter.toObject( + new Type(com.sun.star.uno.RuntimeException.class), any_exc)); + try { + AnyConverter.toObject( + new Type(com.sun.star.uno.Exception.class), any_exc); + fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + + // must fail + try { AnyConverter.toObject(_type, aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toObject(_type, anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toObject(_type, anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toObject(_type, new Object()); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toString() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + String val= AnyConverter.toString(aStr); + assertEquals(val, aStr); + val= AnyConverter.toString(anyStr); + assertEquals(val, anyStr.getObject()); + + // must fail + try { AnyConverter.toString(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(aType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyType); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toString(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toType() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + Type val= AnyConverter.toType(aType); + assertSame(val, aType); + val= AnyConverter.toType(anyType); + assertSame(val, anyType.getObject()); + + // must fail + try { AnyConverter.toType(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_toArray() + throws com.sun.star.lang.IllegalArgumentException + { + // must work + Object val= AnyConverter.toArray(arByte); + assertSame(val, arByte); + val= AnyConverter.toArray(anyArByte); + assertSame(val, anyArByte.getObject()); + + // must fail + try { AnyConverter.toType(aBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyBool); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyChar); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyShort); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyInt); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyLong); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyFloat); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyDouble); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aObj); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(aStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyStr); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyVoid); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(arByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + try { AnyConverter.toType(anyArByte); fail(); + } catch (com.sun.star.lang.IllegalArgumentException ie) {} + } + + @Test public void test_isBoolean() { + assertTrue(AnyConverter.isBoolean(aBool)); + assertTrue(AnyConverter.isBoolean(anyBool)); + assertFalse(AnyConverter.isBoolean(aChar)); + } + + @Test public void test_isChar() { + assertTrue(AnyConverter.isChar(aChar)); + assertTrue(AnyConverter.isChar(anyChar)); + assertFalse(AnyConverter.isChar(aBool)); + } + + @Test public void test_isByte() { + assertTrue(AnyConverter.isByte(aByte)); + assertTrue(AnyConverter.isByte(anyByte)); + assertFalse(AnyConverter.isByte(aBool)); + } + + @Test public void test_isShort() { + assertTrue(AnyConverter.isShort(aShort)); + assertTrue(AnyConverter.isShort(anyShort)); + assertEquals(Type.SHORT, AnyConverter.getType(anyShort)); + Any a = new Any( Type.UNSIGNED_SHORT, Short.valueOf((short)5) ); + assertEquals(Type.UNSIGNED_SHORT, AnyConverter.getType(a)); + assertFalse(AnyConverter.isShort(a)); + assertFalse(Type.SHORT.equals(AnyConverter.getType(a))); + assertFalse(AnyConverter.isShort(aBool)); + } + + @Test public void test_isInt() { + assertTrue(AnyConverter.isInt(aInt)); + assertTrue(AnyConverter.isInt(anyInt)); + assertEquals(Type.LONG, AnyConverter.getType(anyInt)); + Any a = new Any(Type.UNSIGNED_LONG, Integer.valueOf(5)); + assertEquals(Type.UNSIGNED_LONG, AnyConverter.getType(a)); + assertFalse(AnyConverter.isInt(a)); + assertFalse(Type.LONG.equals(AnyConverter.getType(a))); + assertFalse(AnyConverter.isInt(aBool)); + } + + @Test public void test_isLong() { + assertTrue(AnyConverter.isLong(aLong)); + assertTrue(AnyConverter.isLong(anyLong)); + assertEquals(Type.HYPER, AnyConverter.getType(anyLong)); + Any a = new Any( Type.UNSIGNED_HYPER, Long.valueOf(5) ); + assertEquals(Type.UNSIGNED_HYPER, AnyConverter.getType(a)); + assertFalse(AnyConverter.isLong(a)); + assertFalse(Type.HYPER.equals( AnyConverter.getType(a) )); + assertFalse(AnyConverter.isLong(aBool)); + } + + @Test public void test_isFloat() { + assertTrue(AnyConverter.isFloat(aFloat)); + assertTrue(AnyConverter.isFloat(anyFloat)); + assertFalse(AnyConverter.isFloat(aDouble)); + } + + @Test public void test_isDouble() { + assertTrue(AnyConverter.isDouble(aDouble)); + assertTrue(AnyConverter.isDouble(anyDouble)); + assertFalse(AnyConverter.isDouble(aFloat)); + } + + @Test public void test_isObject() { + assertTrue(AnyConverter.isObject(aObj)); + assertTrue(AnyConverter.isObject( new Any( XInterface.class, null))); + assertFalse(AnyConverter.isObject(new Object())); + } + + @Test public void test_isString() { + assertTrue(AnyConverter.isString(aStr)); + assertTrue(AnyConverter.isString(anyStr)); + assertFalse(AnyConverter.isString(new Object())); + } + + @Test public void test_isType() { + assertTrue(AnyConverter.isType(aType)); + assertTrue(AnyConverter.isType(anyType)); + assertFalse(AnyConverter.isType(new Object())); + } + + @Test public void test_isArray() { + assertTrue(AnyConverter.isArray(arByte)); + assertTrue(AnyConverter.isArray(anyArByte)); + assertFalse(AnyConverter.isArray(new Object())); + } + + @Test public void test_isVoid() { + assertTrue(AnyConverter.isVoid(anyVoid)); + assertFalse(AnyConverter.isVoid(new Object())); + } +} + + +class ATypeProvider implements com.sun.star.lang.XTypeProvider +{ + + public byte[] getImplementationId() + { + return new byte[0]; + } + + public com.sun.star.uno.Type[] getTypes() + { + return new Type[]{new Type(XTypeProvider.class)}; + } + +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/uno/Any_Test.java b/ridljar/test/com/sun/star/uno/Any_Test.java new file mode 100644 index 0000000000..6dc3a15b1b --- /dev/null +++ b/ridljar/test/com/sun/star/uno/Any_Test.java @@ -0,0 +1,48 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import java.util.logging.Level; +import java.util.logging.Logger; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class Any_Test { + + private static final Logger logger = Logger.getLogger(Any_Test.class.getName()); + + @Test public void testAnyAny() { + try { + new Any(Type.ANY, null); + fail("IllegalArgumentException expected"); + } catch (IllegalArgumentException e) { + logger.log(Level.FINE, "IllegalArgumentException caught"); + } + } + + @Test public void testComplete() { + assertSame(Any.VOID, Any.complete(Any.VOID)); + assertEquals( + new Any(Type.LONG, Integer.valueOf(10)), Any.complete(Integer.valueOf(10))); + assertEquals( + new Any(new Type(XInterface.class), null), Any.complete(null)); + XInterface x = new XInterface() {}; + assertEquals(new Any(new Type(XInterface.class), x), Any.complete(x)); + } +} diff --git a/ridljar/test/com/sun/star/uno/Type_Test.java b/ridljar/test/com/sun/star/uno/Type_Test.java new file mode 100644 index 0000000000..1499b8230e --- /dev/null +++ b/ridljar/test/com/sun/star/uno/Type_Test.java @@ -0,0 +1,104 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import org.junit.Test; +import static org.junit.Assert.*; + +public final class Type_Test { + @Test public void testZClass() { + assertSame("VOID", void.class, new Type("void").getZClass()); + assertSame("BOOLEAN", boolean.class, new Type("boolean").getZClass()); + assertSame("BYTE", byte.class, new Type("byte").getZClass()); + assertSame("SHORT", short.class, new Type("short").getZClass()); + assertSame( + "UNSIGNED SHORT", short.class, + new Type("unsigned short").getZClass()); + assertSame("LONG", int.class, new Type("long").getZClass()); + assertSame( + "UNSIGNED LONG", int.class, new Type("unsigned long").getZClass()); + assertSame("HYPER", long.class, new Type("hyper").getZClass()); + assertSame( + "UNSIGNED HYPER", long.class, + new Type("unsigned hyper").getZClass()); + assertSame("FLOAT", float.class, new Type("float").getZClass()); + assertSame("DOUBLE", double.class, new Type("double").getZClass()); + assertSame("CHAR", char.class, new Type("char").getZClass()); + assertSame("STRING", String.class, new Type("string").getZClass()); + assertSame("TYPE", Type.class, new Type("type").getZClass()); + assertSame("ANY", Object.class, new Type("any").getZClass()); + assertSame( + "sequence of BOOLEAN", boolean[].class, + new Type("[]boolean", TypeClass.SEQUENCE).getZClass()); + assertSame( + "sequence of sequence of XComponentContext", + XComponentContext[][].class, + new Type("[][]com.sun.star.uno.XComponentContext", TypeClass.SEQUENCE).getZClass()); + assertSame( + "enum TypeClass", TypeClass.class, + new Type("com.sun.star.uno.TypeClass", TypeClass.ENUM).getZClass()); + assertSame( + "struct Uik", Uik.class, + new Type("com.sun.star.uno.Uik", TypeClass.STRUCT).getZClass()); + assertSame( + "exception Exception", com.sun.star.uno.Exception.class, + new Type("com.sun.star.uno.Exception", TypeClass.EXCEPTION).getZClass()); + assertSame( + "exception RuntimeException", + com.sun.star.uno.RuntimeException.class, + new Type("com.sun.star.uno.RuntimeException", TypeClass.EXCEPTION).getZClass()); + assertSame( + "exception DeploymentException", DeploymentException.class, + new Type("com.sun.star.uno.DeploymentException", TypeClass.EXCEPTION).getZClass()); + assertSame( + "interface XInterface", XInterface.class, + new Type("com.sun.star.uno.XInterface", TypeClass.INTERFACE).getZClass()); + assertSame( + "interface XComponentContext", XComponentContext.class, + new Type("com.sun.star.uno.XComponentContext", TypeClass.INTERFACE). getZClass()); + + assertSame(boolean.class, new Type(boolean.class).getZClass()); + assertSame(boolean.class, new Type(Boolean.class).getZClass()); + assertSame(boolean[].class, new Type(boolean[].class).getZClass()); + + try { + new Type(Boolean[].class); + fail(); + } catch (java.lang.RuntimeException e) {} + } + + @Test public void testIsSupertypeOf() { + Type ifc = new Type(com.sun.star.uno.XInterface.class); + Type ctx = new Type(com.sun.star.uno.XComponentContext.class); + Type exc = new Type(com.sun.star.uno.RuntimeException.class); + assertTrue("LONG :> LONG", Type.LONG.isSupertypeOf(Type.LONG)); + assertFalse("not ANY :> XInterface", Type.ANY.isSupertypeOf(ifc)); + assertTrue("ANY :> ANY", Type.ANY.isSupertypeOf(Type.ANY)); + assertFalse("not ANY :> LONG", Type.ANY.isSupertypeOf(Type.LONG)); + assertFalse("not XInterface :> ANY", ifc.isSupertypeOf(Type.ANY)); + assertTrue("XInterface :> XInterface", ifc.isSupertypeOf(ifc)); + assertTrue("XInterface :> XComponentContext", ifc.isSupertypeOf(ctx)); + assertFalse( + "not XComponentContext :> XInterface", ctx.isSupertypeOf(ifc)); + assertTrue( + "XComponentContext :> XComponentContext", ctx.isSupertypeOf(ctx)); + assertFalse( + "not XInterface :> RuntimeException", ifc.isSupertypeOf(exc)); + } +} diff --git a/ridljar/test/com/sun/star/uno/UnoRuntime_EnvironmentTest.java b/ridljar/test/com/sun/star/uno/UnoRuntime_EnvironmentTest.java new file mode 100644 index 0000000000..b10adf12e6 --- /dev/null +++ b/ridljar/test/com/sun/star/uno/UnoRuntime_EnvironmentTest.java @@ -0,0 +1,85 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import com.sun.star.comp.connections.PipedConnection; +import org.junit.Test; +import util.WaitUnreachable; +import static org.junit.Assert.*; + +public final class UnoRuntime_EnvironmentTest { + @Test public void test_getEnvironment() throws java.lang.Exception { + Object o1 = new Object(); + Object o2 = new Object(); + + // get two environments with different contexts + WaitUnreachable java_environment1 = new WaitUnreachable( + UnoRuntime.getEnvironment("java", o1)); + WaitUnreachable java_environment2 = new WaitUnreachable( + UnoRuntime.getEnvironment("java", o2)); + + // ensure that the environments are different + assertNotSame(java_environment1.get(), java_environment2.get()); + + // test if we get the same environment when we reget it + assertTrue( + UnoRuntime.areSame( + java_environment1.get(), + UnoRuntime.getEnvironment("java", o1))); + assertTrue( + UnoRuntime.areSame( + java_environment2.get(), + UnoRuntime.getEnvironment("java", o2))); + + // drop the environments and wait until they are gc + java_environment1.waitUnreachable(); + java_environment2.waitUnreachable(); + } + + @Test public void test_getBridge() throws java.lang.Exception { + PipedConnection conn = new PipedConnection(new Object[0]); + new PipedConnection(new Object[] { conn }); + + // get a bridge + IBridge iBridge = UnoRuntime.getBridgeByName( + "java", null, "remote", "testname", + new Object[] { "urp", conn, null }); + + // reget the bridge, it must be the same as above + IBridge iBridge_tmp = UnoRuntime.getBridgeByName( + "java", null, "remote", "testname", + new Object[] { "urp", conn, null }); + assertTrue(UnoRuntime.areSame(iBridge_tmp, iBridge)); + + // dispose the bridge, this removes the entry from the runtime + iBridge.dispose(); + + conn = new PipedConnection(new Object[0]); + new PipedConnection(new Object[] { conn }); + + // reget the bridge, it must be a different one + iBridge_tmp = UnoRuntime.getBridgeByName( + "java", null, "remote", "testname", + new Object[]{ "urp", conn, null }); + assertFalse(UnoRuntime.areSame(iBridge_tmp, iBridge)); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java b/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java new file mode 100644 index 0000000000..fb2afe8ae1 --- /dev/null +++ b/ridljar/test/com/sun/star/uno/UnoRuntime_Test.java @@ -0,0 +1,196 @@ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import com.sun.star.beans.Optional; +import org.junit.Test; +import static org.junit.Assert.*; + +public final class UnoRuntime_Test { + @Test public void test_generateOid() { + // Test if UnoRuntime generates an OID for a simple class: + assertNotNull("Test1", UnoRuntime.generateOid(new Test1())); + + // Test if UnoRuntime generates an OID for a class implementing + // IQueryInterface and returning null from getOid: + assertNotNull("Test2", UnoRuntime.generateOid(new Test2())); + + // Test if a delegator object has the same OID as its creator: + Test4 test4 = new Test4(); + Ifc ifc = UnoRuntime.queryInterface(Ifc.class, test4); + assertEquals( + "Test4", UnoRuntime.generateOid(ifc), + UnoRuntime.generateOid(test4)); + } + + @Test public void test_queryInterface() { + // Test if a query for an interface which is not supported returns null: + assertNull("Test1", UnoRuntime.queryInterface(Ifc.class, new Test1())); + + // Test if a query for an interface which is supported through + // IQueryInterface succeeds: + assertNotNull( + "Test2", UnoRuntime.queryInterface(Ifc.class, new Test2())); + + // Test if a query for an interface which is directly supported (through + // inheritance) succeeds: + assertNotNull( + "Test3", UnoRuntime.queryInterface(Ifc.class, new Test3())); + } + + @Test public void test_areSame() { + assertTrue( + UnoRuntime.areSame( + new Any(Type.UNSIGNED_LONG, Integer.valueOf(3)), + new Any(Type.UNSIGNED_LONG, Integer.valueOf(3)))); + assertFalse( + UnoRuntime.areSame( + new Any(Type.UNSIGNED_LONG, Integer.valueOf(3)), Integer.valueOf(3))); + assertFalse(UnoRuntime.areSame(new int[] { 1 }, new int[] { 1, 2 })); + assertTrue( + UnoRuntime.areSame( + TypeClass.UNSIGNED_LONG, + new Any(new Type(TypeClass.class), TypeClass.UNSIGNED_LONG))); + assertTrue( + UnoRuntime.areSame( + new Any( + new Type("com.sun.star.beans.Optional<unsigned long>"), + new Optional<Integer>()), + new Any( + new Type("com.sun.star.beans.Optional<unsigned long>"), + new Optional<Integer>(false, Integer.valueOf(0))))); + assertFalse(UnoRuntime.areSame(new Test1(), new Test2())); + Test2 test2 = new Test2(); + assertTrue( + "Test2", + UnoRuntime.areSame( + UnoRuntime.queryInterface(Ifc.class, test2), test2)); + } + + @SuppressWarnings("rawtypes") + @Test public void test_completeValue() { + assertEquals( + Integer.valueOf(0), UnoRuntime.completeValue(Type.UNSIGNED_LONG, null)); + Object v = UnoRuntime.completeValue( + new Type("[][]unsigned long"), null); + assertTrue(v instanceof int[][]); + assertEquals(0, ((int[][]) v).length); + assertSame( + TypeClass.VOID, + UnoRuntime.completeValue(new Type(TypeClass.class), null)); + v = UnoRuntime.completeValue( + new Type("com.sun.star.beans.Optional<unsigned long>"), null); + assertTrue(v instanceof Optional); + assertFalse(((Optional) v).IsPresent); + assertNull(((Optional) v).Value); + } + + @Test public void test_currentContext() throws InterruptedException { + TestThread t1 = new TestThread(); + TestThread t2 = new TestThread(); + t1.start(); + t2.start(); + t1.join(); + t2.join(); + Object v1 = t1.context.getValueByName(""); + Object v2 = t2.context.getValueByName(""); + assertFalse(t1.context == t2.context); + assertSame(v1, t1); + assertSame(v2, t2); + assertNotSame(v1, v2); + } + + private interface Ifc extends XInterface {} + + private static class Test1 {} + + private static class Test2 implements XInterface, IQueryInterface { + public String getOid() { + return null; + } + + public Object queryInterface(Type type) { + return type.equals(new Type(Ifc.class)) ? t2 : null; + } + + public boolean isSame(Object object) { + return object == t2; + } + + private static final class T2 implements Ifc {} + + private final T2 t2 = new T2(); + } + + private static class Test3 implements Ifc {} + + private static class Test4 implements XInterface, IQueryInterface { + public String getOid() { + return null; + } + + public Object queryInterface(Type type) { + return type.equals(new Type(Ifc.class)) ? t4 : null; + } + + public boolean isSame(Object object) { + return object == t4; + } + + private final class T4 implements Ifc, IQueryInterface { + public String getOid() { + return UnoRuntime.generateOid(Test4.this); + } + + public Object queryInterface(Type type) { + return Test4.this.queryInterface(type); + } + + public boolean isSame(Object object) { + return UnoRuntime.areSame(Test4.this, object); + } + } + + private final T4 t4 = new T4(); + } + + private final class TestThread extends Thread { + @Override + public void run() { + //TODO: JUnit does not notice if these asserts fail: + assertNull(UnoRuntime.getCurrentContext()); + context = new TestCurrentContext(); + UnoRuntime.setCurrentContext(context); + assertSame(context, UnoRuntime.getCurrentContext()); + assertSame(this, context.getValueByName("")); + UnoRuntime.setCurrentContext(null); + assertNull(UnoRuntime.getCurrentContext()); + } + + public XCurrentContext context = null; + } + + private static final class TestCurrentContext implements XCurrentContext { + public Object getValueByName(String name) { + return value; + } + + private final Object value = Thread.currentThread(); + } +} diff --git a/ridljar/test/com/sun/star/uno/WeakReference_Test.java b/ridljar/test/com/sun/star/uno/WeakReference_Test.java new file mode 100644 index 0000000000..960c05dad4 --- /dev/null +++ b/ridljar/test/com/sun/star/uno/WeakReference_Test.java @@ -0,0 +1,107 @@ +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +package com.sun.star.uno; + +import java.util.ArrayList; +import java.util.Iterator; +import org.junit.Test; +import util.WaitUnreachable; +import static org.junit.Assert.*; + +public final class WeakReference_Test { + @Test public void test() { + Object o = new MockWeak(); + WeakReference r1 = new WeakReference(o); + WeakReference r2 = new WeakReference(r1); + assertSame(o, r1.get()); + assertSame(o, r2.get()); + WaitUnreachable u = new WaitUnreachable(o); + o = null; + u.waitUnreachable(); + assertNull("a3", r1.get()); + assertNull("a4", r2.get()); + } + + private static final class MockWeak implements XWeak { + public XAdapter queryAdapter() { + return adapter; + } + + @Override + protected void finalize() throws Throwable { + adapter.dispose(); + super.finalize(); + } + + private static final class Adapter implements XAdapter { + public Adapter(Object obj) { + ref = new java.lang.ref.WeakReference<Object>(obj); + } + + public Object queryAdapted() { + return ref.get(); + } + + public void addReference(XReference ref) { + synchronized (this) { + if (listeners != null) { + listeners.add(ref); + return; + } + } + ref.dispose(); + } + + public synchronized void removeReference(XReference ref) { + if (listeners != null) { + listeners.remove(ref); + } + } + + public void dispose() { + ArrayList<XReference> l; + synchronized (this){ + l = listeners; + listeners = null; + } + if (l != null) { + java.lang.RuntimeException ex = null; + for (Iterator<XReference> i = l.iterator(); i.hasNext();) { + try { + i.next().dispose(); + } catch (java.lang.RuntimeException e) { + ex = e; + } + } + if (ex != null) { + throw ex; + } + } + } + + private final java.lang.ref.WeakReference<Object> ref; + private ArrayList<XReference> listeners = new ArrayList<XReference>(); + } + + private final Adapter adapter = new Adapter(this); + } +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |