1 /*
  2  * Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 import java.util.HashMap;
 25 import java.util.LinkedHashMap;
 26 import java.util.WeakHashMap;
 27 
 28 import org.junit.jupiter.api.Test;
 29 
 30 import static org.junit.jupiter.api.Assertions.assertFalse;
 31 import static org.junit.jupiter.api.Assertions.assertThrows;
 32 import static org.junit.jupiter.api.Assertions.assertTrue;
 33 import static org.junit.jupiter.api.Assertions.assertEquals;
 34 
 35 /*
 36  * @test
 37  * @summary Check WeakHashMap throws IdentityException when Value Objects are put
 38  * @enablePreview
 39  * @run junit WeakHashMapValues
 40  */
 41 public class WeakHashMapValues {
 42 
 43     /*
 44      * Check that any kind of put with a value class as a key throws IdentityException
 45      */
 46     @Test
 47     void checkThrowsIdentityException() {
 48         WeakHashMap<Object, Object> whm = new WeakHashMap<>();
 49         Object key = new Foo(1);
 50         assertThrows(IdentityException.class, () -> whm.put(key, "1"));
 51         assertThrows(IdentityException.class, () -> whm.putIfAbsent(key, "2"));
 52         assertThrows(IdentityException.class, () -> whm.compute(key, (_, _) -> "3"));
 53         assertThrows(IdentityException.class, () -> whm.computeIfAbsent(key, (_) -> "4"));
 54 
 55         HashMap<Object, String> hmap = new HashMap<>();
 56         hmap.put(key, "6");
 57         assertThrows(IdentityException.class, () -> whm.putAll(hmap));
 58     }
 59 
 60     /*
 61      * Check that any kind of put with Integer as a value class as a key throws IdentityException
 62      */
 63     @Test
 64     void checkIntegerThrowsIdentityException() {
 65         WeakHashMap<Object, Object> whm = new WeakHashMap<>();
 66         Object key = 1;
 67         assertThrows(IdentityException.class, () -> whm.put(key, "1"));
 68         assertThrows(IdentityException.class, () -> whm.putIfAbsent(key, "2"));
 69         assertThrows(IdentityException.class, () -> whm.compute(key, (_, _) -> "3"));
 70         assertThrows(IdentityException.class, () -> whm.computeIfAbsent(key, (_) -> "4"));
 71 
 72         HashMap<Object, String> hmap = new HashMap<>();
 73         hmap.put(key, "6");
 74         assertThrows(IdentityException.class, () -> whm.putAll(hmap));
 75 
 76     }
 77 
 78     /**
 79      * Check that queries with a value object return false or null.
 80      */
 81     @Test
 82     void checkValueObjectGet() {
 83         WeakHashMap<Object, Object> whm = new WeakHashMap<>();
 84         Object key = "X";
 85         Object v = new Foo(1);
 86         assertEquals(whm.get(v), null, "Get of value object should return null");
 87         assertEquals(whm.containsKey(v), false, "containsKey should return false");
 88     }
 89 
 90     /**
 91      * Check WeakHashMap.putAll from a source map containing a value object as a key throws.
 92      */
 93     @Test
 94     void checkValueObjectPutAll() {
 95         // src is mix of identity and value objects (Integer is value class with --enable-preview)
 96         HashMap<Object, Object> srcMap = new LinkedHashMap<>();
 97         srcMap.put("abc", "Vabc");
 98         srcMap.put(1, "V1");
 99         srcMap.put("xyz", "Vxyz");
100         WeakHashMap<Object, Object> whm = new WeakHashMap<>();
101         assertThrows(IdentityException.class, () -> whm.putAll(srcMap));
102         assertTrue(whm.containsKey("abc"), "Identity key should have been copied");
103         assertFalse(whm.containsKey(1), "Value object key should not have been copied");
104         assertEquals(1, whm.size(), "Result map size");
105     }
106 }
107 
108 value class Foo {
109     int x;
110     Foo(int x) {
111         this.x = x;
112     }
113 }