1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.uno;
25 
26 import complexlib.ComplexTestCase;
27 import java.util.ArrayList;
28 import java.util.Iterator;
29 import util.WaitUnreachable;
30 
31 public final class WeakReference_Test extends ComplexTestCase {
getTestObjectName()32     public String getTestObjectName() {
33         return getClass().getName();
34     }
35 
getTestMethodNames()36     public String[] getTestMethodNames() {
37         return new String[] { "test" };
38     }
39 
test()40     public void test() {
41         Object o = new MockWeak();
42         WeakReference r1 = new WeakReference(o);
43         WeakReference r2 = new WeakReference(r1);
44         assure("", r1.get() == o);
45         assure("", r2.get() == o);
46         WaitUnreachable u = new WaitUnreachable(o);
47         o = null;
48         u.waitUnreachable();
49         assure("a3", r1.get() == null);
50         assure("a4", r2.get() == null);
51     }
52 
53     private static final class MockWeak implements XWeak {
queryAdapter()54         public XAdapter queryAdapter() {
55             return adapter;
56         }
57 
finalize()58         protected void finalize() {
59             adapter.dispose();
60         }
61 
62         private static final class Adapter implements XAdapter {
Adapter(Object obj)63             public Adapter(Object obj) {
64                 ref = new java.lang.ref.WeakReference(obj);
65             }
66 
queryAdapted()67             public Object queryAdapted() {
68                 return ref.get();
69             }
70 
addReference(XReference ref)71             public void addReference(XReference ref) {
72                 synchronized (this) {
73                     if (listeners != null) {
74                         listeners.add(ref);
75                         return;
76                     }
77                 }
78                 ref.dispose();
79             }
80 
removeReference(XReference ref)81             public synchronized void removeReference(XReference ref) {
82                 if (listeners != null) {
83                     listeners.remove(ref);
84                 }
85             }
86 
dispose()87             public void dispose() {
88                 ArrayList l;
89                 synchronized (this){
90                     l = listeners;
91                     listeners = null;
92                 }
93                 if (l != null) {
94                     java.lang.RuntimeException ex = null;
95                     for (Iterator i = l.iterator(); i.hasNext();) {
96                         try {
97                             ((XReference) i.next()).dispose();
98                         } catch (java.lang.RuntimeException e) {
99                             ex = e;
100                         }
101                     }
102                     if (ex != null) {
103                         throw ex;
104                     }
105                 }
106             }
107 
108             private final java.lang.ref.WeakReference ref;
109             private ArrayList listeners = new ArrayList();
110         }
111 
112         private final Adapter adapter = new Adapter(this);
113     }
114 }
115