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