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 java.util.ArrayList;
27 import java.util.Iterator;
28 import util.WaitUnreachable;
29 
30 import org.junit.Test;
31 import static org.junit.Assert.*;
32 
33 public final class WeakReference_Test {
34     @Test
test()35     public void test() {
36         Object o = new MockWeak();
37         WeakReference r1 = new WeakReference(o);
38         WeakReference r2 = new WeakReference(r1);
39         assertTrue("", r1.get() == o);
40         assertTrue("", r2.get() == o);
41         WaitUnreachable u = new WaitUnreachable(o);
42         o = null;
43         u.waitUnreachable();
44         assertTrue("a3", r1.get() == null);
45         assertTrue("a4", r2.get() == null);
46     }
47 
48     private static final class MockWeak implements XWeak {
queryAdapter()49         public XAdapter queryAdapter() {
50             return adapter;
51         }
52 
finalize()53         protected void finalize() {
54             adapter.dispose();
55         }
56 
57         private static final class Adapter implements XAdapter {
Adapter(Object obj)58             public Adapter(Object obj) {
59                 ref = new java.lang.ref.WeakReference(obj);
60             }
61 
queryAdapted()62             public Object queryAdapted() {
63                 return ref.get();
64             }
65 
addReference(XReference ref)66             public void addReference(XReference ref) {
67                 synchronized (this) {
68                     if (listeners != null) {
69                         listeners.add(ref);
70                         return;
71                     }
72                 }
73                 ref.dispose();
74             }
75 
removeReference(XReference ref)76             public synchronized void removeReference(XReference ref) {
77                 if (listeners != null) {
78                     listeners.remove(ref);
79                 }
80             }
81 
dispose()82             public void dispose() {
83                 ArrayList l;
84                 synchronized (this){
85                     l = listeners;
86                     listeners = null;
87                 }
88                 if (l != null) {
89                     java.lang.RuntimeException ex = null;
90                     for (Iterator i = l.iterator(); i.hasNext();) {
91                         try {
92                             ((XReference) i.next()).dispose();
93                         } catch (java.lang.RuntimeException e) {
94                             ex = e;
95                         }
96                     }
97                     if (ex != null) {
98                         throw ex;
99                     }
100                 }
101             }
102 
103             private final java.lang.ref.WeakReference ref;
104             private ArrayList listeners = new ArrayList();
105         }
106 
107         private final Adapter adapter = new Adapter(this);
108     }
109 }
110