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