bump product version to 4.1.6.2
[LibreOffice.git] / bridges / test / java_remote / Bug108825_Test.java
blob000270a80c85472d6b85ece51896eb341ee7a0a8
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package test.java_remote;
21 import com.sun.star.bridge.XInstanceProvider;
22 import com.sun.star.lib.uno.typeinfo.MethodTypeInfo;
23 import com.sun.star.lib.uno.typeinfo.TypeInfo;
24 import com.sun.star.uno.UnoRuntime;
25 import com.sun.star.uno.XComponentContext;
26 import com.sun.star.uno.XInterface;
27 import complexlib.ComplexTestCase;
28 import test.lib.TestBed;
30 /**
31 * Test case for bug #108825#.
33 * <p>Bug #108825# "Java UNO Remote Bridge: Mapped-out Objects Not Held" shows
34 * that local objects that are mapped out via a remote bridge, but not held
35 * locally, might be garbage collected while there are still remote references
36 * to them. This test is not guaranteed to always work reliably, see comment in
37 * the code.</p>
39 public final class Bug108825_Test extends ComplexTestCase {
40 public String getTestObjectName() {
41 return getClass().getName();
44 public String[] getTestMethodNames() {
45 return new String[] { "test" };
48 public void test() throws Exception {
49 TestBed t = new TestBed();
50 assure("test", t.execute(new Provider(t), true, Client.class, 0));
53 public static final class Client extends TestBed.Client {
54 public static void main(String[] args) {
55 new Client().execute();
58 protected boolean run(XComponentContext context) throws Throwable {
59 XTest test = UnoRuntime.queryInterface(
60 XTest.class, getBridge(context).getInstance("Test"));
61 // Send the XObject that is held on the server side amidst two
62 // dummies that are not held on the server side; then wait for the
63 // dummies to be garbage collected, hoping that the XObject, if it
64 // is erroneously not held on the client side, will be garbage
65 // collected, too. Obviously, this is not guaranteed to always work
66 // (the VM might chose not to garbage collect the dummies, hanging
67 // the test forever; or the VM might chose to garbage collect the
68 // dummies but not the XObject, making the test pass erroneously).
69 test.offer(new Dummy(), new XObject() { public void call() {} },
70 new Dummy());
71 System.out.println("Client waiting for garbage collection...");
72 for (;;) {
73 synchronized (lock) {
74 if (finalizedCount == 2) {
75 break;
78 test.remoteGc();
79 gc();
81 System.out.println("Client garbage collection done.");
82 test.notification();
83 return true;
86 private final class Dummy implements XDummy {
87 protected void finalize() {
88 synchronized (lock) {
89 ++finalizedCount;
94 private final Object lock = new Object();
95 private int finalizedCount = 0;
98 // Make it as likely as possible that the VM reclaims all garbage:
99 private static void gc() {
100 System.gc();
101 System.runFinalization();
102 byte[] garbage = new byte[1024 * 1024];
105 private static final class Provider implements XInstanceProvider {
106 public Provider(TestBed testBed) {
107 this.testBed = testBed;
110 public Object getInstance(String instanceName) {
111 return new XTest() {
112 public void offer(XDummy dummy1, XObject obj, XDummy dummy2)
114 this.obj = obj;
117 public void remoteGc() {
118 gc();
121 public void notification() {
122 obj.call();
123 testBed.serverDone(true);
126 private XObject obj;
130 private final TestBed testBed;
133 public interface XDummy extends XInterface {
134 TypeInfo[] UNOTYPEINFO = null;
137 public interface XObject extends XInterface {
138 void call();
140 TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("call", 0, 0) };
143 public interface XTest extends XInterface {
144 void offer(XDummy dummy1, XObject obj, XDummy dummy2);
146 void remoteGc();
148 void notification();
150 TypeInfo[] UNOTYPEINFO = { new MethodTypeInfo("offer", 0, 0),
151 new MethodTypeInfo("remoteGc", 1, 0),
152 new MethodTypeInfo("notification", 2, 0) };