1*a046d00fSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*a046d00fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*a046d00fSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*a046d00fSAndrew Rist * distributed with this work for additional information 6*a046d00fSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*a046d00fSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*a046d00fSAndrew Rist * "License"); you may not use this file except in compliance 9*a046d00fSAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*a046d00fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*a046d00fSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*a046d00fSAndrew Rist * software distributed under the License is distributed on an 15*a046d00fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*a046d00fSAndrew Rist * KIND, either express or implied. See the License for the 17*a046d00fSAndrew Rist * specific language governing permissions and limitations 18*a046d00fSAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*a046d00fSAndrew Rist *************************************************************/ 21*a046d00fSAndrew Rist 22*a046d00fSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir package com.sun.star.lib.util; 25cdf0e10cSrcweir 26cdf0e10cSrcweir import complexlib.ComplexTestCase; 27cdf0e10cSrcweir import util.WaitUnreachable; 28cdf0e10cSrcweir 29cdf0e10cSrcweir public final class WeakMap_Test extends ComplexTestCase { getTestMethodNames()30cdf0e10cSrcweir public String[] getTestMethodNames() { 31cdf0e10cSrcweir return new String[] { "test" }; 32cdf0e10cSrcweir } 33cdf0e10cSrcweir test()34cdf0e10cSrcweir public void test() { 35cdf0e10cSrcweir WeakMap m = new WeakMap(); 36cdf0e10cSrcweir assure("", m.size() == 0); 37cdf0e10cSrcweir assure("", m.isEmpty()); 38cdf0e10cSrcweir assure("", !m.containsKey("key1")); 39cdf0e10cSrcweir assure("", !m.containsValue(null)); 40cdf0e10cSrcweir WaitUnreachable u1 = new WaitUnreachable(new Object()); 41cdf0e10cSrcweir m.put("key1", u1.get()); 42cdf0e10cSrcweir WaitUnreachable u2 = new WaitUnreachable(new Disposable()); 43cdf0e10cSrcweir m.put("key2", u2.get()); 44cdf0e10cSrcweir assure("", m.size() == 2); 45cdf0e10cSrcweir assure("", !m.isEmpty()); 46cdf0e10cSrcweir assure("", m.containsKey("key1")); 47cdf0e10cSrcweir assure("", m.containsKey("key2")); 48cdf0e10cSrcweir assure("", !m.containsKey("key3")); 49cdf0e10cSrcweir assure("", m.containsValue(m.get("key1"))); 50cdf0e10cSrcweir assure("", m.containsValue(m.get("key2"))); 51cdf0e10cSrcweir assure("", WeakMap.getValue(m.get("key1")).equals(u1.get())); 52cdf0e10cSrcweir assure("", WeakMap.getValue(m.get("key2")).equals(u2.get())); 53cdf0e10cSrcweir assure("", m.values().size() == 2); 54cdf0e10cSrcweir assure("", m.values().contains(m.get("key1"))); 55cdf0e10cSrcweir assure("", m.values().contains(m.get("key2"))); 56cdf0e10cSrcweir u1.waitUnreachable(); 57cdf0e10cSrcweir assure("", WeakMap.getValue(m.get("key1")) == null); 58cdf0e10cSrcweir ((Disposable) u2.get()).dispose(); 59cdf0e10cSrcweir assure("", WeakMap.getValue(m.get("key2")) == null); 60cdf0e10cSrcweir m.clear(); 61cdf0e10cSrcweir u2.waitUnreachable(); 62cdf0e10cSrcweir assure("", m.size() == 0); 63cdf0e10cSrcweir m.put("key2", new Object()); 64cdf0e10cSrcweir assure("", m.size() == 1); 65cdf0e10cSrcweir } 66cdf0e10cSrcweir 67cdf0e10cSrcweir // This simple class (single listener, no synchronization) exploits 68cdf0e10cSrcweir // knowledge about the implementation of WeakMap: 69cdf0e10cSrcweir private static final class Disposable implements DisposeNotifier { addDisposeListener(DisposeListener listener)70cdf0e10cSrcweir public void addDisposeListener(DisposeListener listener) { 71cdf0e10cSrcweir this.listener = listener; 72cdf0e10cSrcweir } 73cdf0e10cSrcweir dispose()74cdf0e10cSrcweir public void dispose() { 75cdf0e10cSrcweir if (listener != null) { 76cdf0e10cSrcweir listener.notifyDispose(this); 77cdf0e10cSrcweir } 78cdf0e10cSrcweir } 79cdf0e10cSrcweir 80cdf0e10cSrcweir private DisposeListener listener = null; 81cdf0e10cSrcweir } 82cdf0e10cSrcweir } 83