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.lib.util;
25 
26 import util.WaitUnreachable;
27 
28 import org.junit.Test;
29 import static org.junit.Assert.*;
30 
31 public final class WeakMap_Test {
32     @Test
test()33     public void test() {
34         WeakMap m = new WeakMap();
35         assertTrue("", m.size() == 0);
36         assertTrue("", m.isEmpty());
37         assertTrue("", !m.containsKey("key1"));
38         assertTrue("", !m.containsValue(null));
39         WaitUnreachable u1 = new WaitUnreachable(new Object());
40         m.put("key1", u1.get());
41         WaitUnreachable u2 = new WaitUnreachable(new Disposable());
42         m.put("key2", u2.get());
43         assertTrue("", m.size() == 2);
44         assertTrue("", !m.isEmpty());
45         assertTrue("", m.containsKey("key1"));
46         assertTrue("", m.containsKey("key2"));
47         assertTrue("", !m.containsKey("key3"));
48         assertTrue("", m.containsValue(m.get("key1")));
49         assertTrue("", m.containsValue(m.get("key2")));
50         assertTrue("", WeakMap.getValue(m.get("key1")).equals(u1.get()));
51         assertTrue("", WeakMap.getValue(m.get("key2")).equals(u2.get()));
52         assertTrue("", m.values().size() == 2);
53         assertTrue("", m.values().contains(m.get("key1")));
54         assertTrue("", m.values().contains(m.get("key2")));
55         u1.waitUnreachable();
56         assertTrue("", WeakMap.getValue(m.get("key1")) == null);
57         ((Disposable) u2.get()).dispose();
58         assertTrue("", WeakMap.getValue(m.get("key2")) == null);
59         m.clear();
60         u2.waitUnreachable();
61         assertTrue("", m.size() == 0);
62         m.put("key2", new Object());
63         assertTrue("", m.size() == 1);
64     }
65 
66     // This simple class (single listener, no synchronization) exploits
67     // knowledge about the implementation of WeakMap:
68     private static final class Disposable implements DisposeNotifier {
addDisposeListener(DisposeListener listener)69         public void addDisposeListener(DisposeListener listener) {
70             this.listener = listener;
71         }
72 
dispose()73         public void dispose() {
74             if (listener != null) {
75                 listener.notifyDispose(this);
76             }
77         }
78 
79         private DisposeListener listener = null;
80     }
81 }
82