J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2005 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any 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.*; |
| 32 | import java.util.concurrent.*; |
| 33 | import static java.util.AbstractMap.*; |
| 34 | |
| 35 | public class SimpleEntries { |
| 36 | private static String k = "foo"; |
| 37 | private static Long v = 1L; |
| 38 | private static Long v2 = 2L; |
| 39 | private static void realMain(String[] args) throws Throwable { |
| 40 | testEntry(new SimpleEntry<String,Long>(k,v)); |
| 41 | testEntry(new SimpleImmutableEntry<String,Long>(k,v)); |
| 42 | testNullEntry(new SimpleEntry<String,Long>(null,null)); |
| 43 | testNullEntry(new SimpleImmutableEntry<String,Long>(null,null)); |
| 44 | } |
| 45 | |
| 46 | private static void testEntry(Map.Entry<String,Long> e) { |
| 47 | equal(e.getKey(), k); |
| 48 | equal(e.getValue(), v); |
| 49 | equal(e, new SimpleEntry<String,Long>(k,v)); |
| 50 | check(! e.equals(new SimpleEntry<String,Long>(k,v2))); |
| 51 | check(! e.equals(null)); |
| 52 | equal(e, new SimpleImmutableEntry<String,Long>(k,v)); |
| 53 | equal(e.toString(), k+"="+v); |
| 54 | if (e instanceof SimpleEntry) { |
| 55 | equal(e.setValue(v2), v); |
| 56 | equal(e.getValue(), v2); |
| 57 | equal(e.setValue(null), v2); |
| 58 | equal(e.getValue(), null); |
| 59 | } else { |
| 60 | try { e.setValue(v2); fail(); } |
| 61 | catch (UnsupportedOperationException t) {} |
| 62 | catch (Throwable t) { unexpected(t); } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | private static void testNullEntry(Map.Entry<String,Long> e) { |
| 67 | equal(e.getKey(), null); |
| 68 | equal(e.getValue(), null); |
| 69 | equal(e, new SimpleEntry<String,Long>(null, null)); |
| 70 | equal(e, new SimpleImmutableEntry<String,Long>(null, null)); |
| 71 | equal(e.toString(), "null=null"); |
| 72 | if (e instanceof SimpleEntry) { |
| 73 | equal(e.setValue(v), null); |
| 74 | equal(e.getValue(), v); |
| 75 | } else { |
| 76 | try { e.setValue(null); fail(); } |
| 77 | catch (UnsupportedOperationException t) {} |
| 78 | catch (Throwable t) { unexpected(t); } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | //--------------------- Infrastructure --------------------------- |
| 83 | static volatile int passed = 0, failed = 0; |
| 84 | static void pass() {passed++;} |
| 85 | static void fail() {failed++; Thread.dumpStack();} |
| 86 | static void fail(String msg) {System.out.println(msg); fail();} |
| 87 | static void unexpected(Throwable t) {failed++; t.printStackTrace();} |
| 88 | static void check(boolean cond) {if (cond) pass(); else fail();} |
| 89 | static void equal(Object x, Object y) { |
| 90 | if (x == null ? y == null : x.equals(y)) pass(); |
| 91 | else fail(x + " not equal to " + y);} |
| 92 | public static void main(String[] args) throws Throwable { |
| 93 | try {realMain(args);} catch (Throwable t) {unexpected(t);} |
| 94 | System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
| 95 | if (failed > 0) throw new AssertionError("Some tests failed");} |
| 96 | } |