1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakReference_Test.java,v $
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" };
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
);
56 assure("a3", r1
.get() == null);
57 assure("a4", r2
.get() == null);
60 private static final class MockWeak
implements XWeak
{
61 public XAdapter
queryAdapter() {
65 protected void finalize() {
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() {
78 public void addReference(XReference ref
) {
80 if (listeners
!= null) {
88 public synchronized void removeReference(XReference ref
) {
89 if (listeners
!= null) {
90 listeners
.remove(ref
);
94 public void dispose() {
101 java
.lang
.RuntimeException ex
= null;
102 for (Iterator i
= l
.iterator(); i
.hasNext();) {
104 ((XReference
) i
.next()).dispose();
105 } catch (java
.lang
.RuntimeException e
) {
115 private final java
.lang
.ref
.WeakReference ref
;
116 private ArrayList listeners
= new ArrayList();
119 private final Adapter adapter
= new Adapter(this);