Update ooo320-m1
[ooovba.git] / jurt / test / com / sun / star / uno / WeakReference_Test.java
blobb004d15c079b237b0cf2dd484899089c4b1cb440
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakReference_Test.java,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.uno;
33 import complexlib.ComplexTestCase;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import util.WaitUnreachable;
38 public final class WeakReference_Test extends ComplexTestCase {
39 public String getTestObjectName() {
40 return getClass().getName();
43 public String[] getTestMethodNames() {
44 return new String[] { "test" };
47 public void test() {
48 Object o = new MockWeak();
49 WeakReference r1 = new WeakReference(o);
50 WeakReference r2 = new WeakReference(r1);
51 assure("", r1.get() == o);
52 assure("", r2.get() == o);
53 WaitUnreachable u = new WaitUnreachable(o);
54 o = null;
55 u.waitUnreachable();
56 assure("a3", r1.get() == null);
57 assure("a4", r2.get() == null);
60 private static final class MockWeak implements XWeak {
61 public XAdapter queryAdapter() {
62 return adapter;
65 protected void finalize() {
66 adapter.dispose();
69 private static final class Adapter implements XAdapter {
70 public Adapter(Object obj) {
71 ref = new java.lang.ref.WeakReference(obj);
74 public Object queryAdapted() {
75 return ref.get();
78 public void addReference(XReference ref) {
79 synchronized (this) {
80 if (listeners != null) {
81 listeners.add(ref);
82 return;
85 ref.dispose();
88 public synchronized void removeReference(XReference ref) {
89 if (listeners != null) {
90 listeners.remove(ref);
94 public void dispose() {
95 ArrayList l;
96 synchronized (this){
97 l = listeners;
98 listeners = null;
100 if (l != null) {
101 java.lang.RuntimeException ex = null;
102 for (Iterator i = l.iterator(); i.hasNext();) {
103 try {
104 ((XReference) i.next()).dispose();
105 } catch (java.lang.RuntimeException e) {
106 ex = e;
109 if (ex != null) {
110 throw ex;
115 private final java.lang.ref.WeakReference ref;
116 private ArrayList listeners = new ArrayList();
119 private final Adapter adapter = new Adapter(this);