• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2005, 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 /*
25  * @test
26  * @bug 4904074 6328220 6330389 8308167
27  * @modules java.base/jdk.internal.util
28  * @summary Basic tests for several Map.Entry implementations
29  * @author Martin Buchholz
30  */
31 
32 package test.java.util.AbstractMap;
33 
34 import java.util.Map;
35 import jdk.internal.util.NullableKeyValueHolder;
36 
37 import static java.util.AbstractMap.SimpleEntry;
38 import static java.util.AbstractMap.SimpleImmutableEntry;
39 
40 public class SimpleEntries {
41     private static String k = "foo";
42     private static Long v = 1L;
43     private static Long v2 = 2L;
realMain(String[] args)44     private static void realMain(String[] args) throws Throwable {
45         testEntry(new SimpleEntry<String,Long>(k,v));
46         testEntry(new SimpleImmutableEntry<String,Long>(k,v));
47         testEntry(Map.entry(k,v));
48         testEntry(Map.Entry.copyOf(Map.entry(k,v)));
49         testEntry(new NullableKeyValueHolder(k,v));
50         testNullEntry(new SimpleEntry<String,Long>(null,null));
51         testNullEntry(new SimpleImmutableEntry<String,Long>(null,null));
52         testNullEntry(new NullableKeyValueHolder(null,null));
53     }
54 
testEntry(Map.Entry<String,Long> e)55     private static void testEntry(Map.Entry<String,Long> e) {
56         equal(e.getKey(), k);
57         equal(e.getValue(), v);
58         equal(e, new SimpleEntry<String,Long>(k,v));
59         check(! e.equals(new SimpleEntry<String,Long>(k,v2)));
60         check(! e.equals(null));
61         equal(e, new SimpleImmutableEntry<String,Long>(k,v));
62         equal(e.toString(), k+"="+v);
63         check(e.hashCode() == 101575); // hash("foo") ^ hash(1L)
64         if (e instanceof SimpleEntry) {
65             equal(e.setValue(v2), v);
66             equal(e.getValue(), v2);
67             equal(e.setValue(null), v2);
68             equal(e.getValue(), null);
69         } else {
70             try { e.setValue(v2); fail(); }
71             catch (UnsupportedOperationException t) {}
72             catch (Throwable t) { unexpected(t); }
73         }
74     }
75 
testNullEntry(Map.Entry<String,Long> e)76     private static void testNullEntry(Map.Entry<String,Long> e) {
77         equal(e.getKey(), null);
78         equal(e.getValue(), null);
79         equal(e, new SimpleEntry<String,Long>(null, null));
80         equal(e, new SimpleImmutableEntry<String,Long>(null, null));
81         equal(e.toString(), "null=null");
82         check(e.hashCode() == 0);
83         if (e instanceof SimpleEntry) {
84             equal(e.setValue(v), null);
85             equal(e.getValue(), v);
86         } else {
87             try { e.setValue(null); fail(); }
88             catch (UnsupportedOperationException t) {}
89             catch (Throwable t) { unexpected(t); }
90         }
91     }
92 
93     //--------------------- Infrastructure ---------------------------
94     static volatile int passed = 0, failed = 0;
pass()95     static void pass() {passed++;}
fail()96     static void fail() {failed++; Thread.dumpStack();}
fail(String msg)97     static void fail(String msg) {System.out.println(msg); fail();}
unexpected(Throwable t)98     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)99     static void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)100     static void equal(Object x, Object y) {
101         if (x == null ? y == null : x.equals(y)) pass();
102         else fail(x + " not equal to " + y);}
main(String[] args)103     public static void main(String[] args) throws Throwable {
104         try {realMain(args);} catch (Throwable t) {unexpected(t);}
105         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
106         if (failed > 0) throw new AssertionError("Some tests failed");}
107 }
108