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