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
;
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
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() {} },
71 System
.out
.println("Client waiting for garbage collection...");
74 if (finalizedCount
== 2) {
81 System
.out
.println("Client garbage collection done.");
86 private final class Dummy
implements XDummy
{
87 protected void finalize() {
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() {
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
) {
112 public void offer(XDummy dummy1
, XObject obj
, XDummy dummy2
)
117 public void remoteGc() {
121 public void notification() {
123 testBed
.serverDone(true);
130 private final TestBed testBed
;
133 public interface XDummy
extends XInterface
{
134 TypeInfo
[] UNOTYPEINFO
= null;
137 public interface XObject
extends XInterface
{
140 TypeInfo
[] UNOTYPEINFO
= { new MethodTypeInfo("call", 0, 0) };
143 public interface XTest
extends XInterface
{
144 void offer(XDummy dummy1
, XObject obj
, XDummy dummy2
);
150 TypeInfo
[] UNOTYPEINFO
= { new MethodTypeInfo("offer", 0, 0),
151 new MethodTypeInfo("remoteGc", 1, 0),
152 new MethodTypeInfo("notification", 2, 0) };