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