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