Path: blob/master/test/jdk/java/util/AbstractMap/SimpleEntries.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4904074 6328220 633038926* @summary Basic tests for SimpleEntry, SimpleImmutableEntry27* @author Martin Buchholz28*/2930import java.util.Map;3132import static java.util.AbstractMap.SimpleEntry;33import static java.util.AbstractMap.SimpleImmutableEntry;3435public class SimpleEntries {36private static String k = "foo";37private static Long v = 1L;38private static Long v2 = 2L;39private static void realMain(String[] args) throws Throwable {40testEntry(new SimpleEntry<String,Long>(k,v));41testEntry(new SimpleImmutableEntry<String,Long>(k,v));42testNullEntry(new SimpleEntry<String,Long>(null,null));43testNullEntry(new SimpleImmutableEntry<String,Long>(null,null));44}4546private static void testEntry(Map.Entry<String,Long> e) {47equal(e.getKey(), k);48equal(e.getValue(), v);49equal(e, new SimpleEntry<String,Long>(k,v));50check(! e.equals(new SimpleEntry<String,Long>(k,v2)));51check(! e.equals(null));52equal(e, new SimpleImmutableEntry<String,Long>(k,v));53equal(e.toString(), k+"="+v);54if (e instanceof SimpleEntry) {55equal(e.setValue(v2), v);56equal(e.getValue(), v2);57equal(e.setValue(null), v2);58equal(e.getValue(), null);59} else {60try { e.setValue(v2); fail(); }61catch (UnsupportedOperationException t) {}62catch (Throwable t) { unexpected(t); }63}64}6566private static void testNullEntry(Map.Entry<String,Long> e) {67equal(e.getKey(), null);68equal(e.getValue(), null);69equal(e, new SimpleEntry<String,Long>(null, null));70equal(e, new SimpleImmutableEntry<String,Long>(null, null));71equal(e.toString(), "null=null");72if (e instanceof SimpleEntry) {73equal(e.setValue(v), null);74equal(e.getValue(), v);75} else {76try { e.setValue(null); fail(); }77catch (UnsupportedOperationException t) {}78catch (Throwable t) { unexpected(t); }79}80}8182//--------------------- Infrastructure ---------------------------83static volatile int passed = 0, failed = 0;84static void pass() {passed++;}85static void fail() {failed++; Thread.dumpStack();}86static void fail(String msg) {System.out.println(msg); fail();}87static void unexpected(Throwable t) {failed++; t.printStackTrace();}88static void check(boolean cond) {if (cond) pass(); else fail();}89static void equal(Object x, Object y) {90if (x == null ? y == null : x.equals(y)) pass();91else fail(x + " not equal to " + y);}92public static void main(String[] args) throws Throwable {93try {realMain(args);} catch (Throwable t) {unexpected(t);}94System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);95if (failed > 0) throw new AssertionError("Some tests failed");}96}979899