Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/testlibrary_tests/AssertsTest.java
32278 views
/*1* Copyright (c) 2013, 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*/2223import static com.oracle.java.testlibrary.Asserts.*;2425/* @test26* @summary Tests the different assertions in the Assert class27* @library /testlibrary28*/29public class AssertsTest {30private static class Foo implements Comparable<Foo> {31final int id;32public Foo(int id) {33this.id = id;34}3536public int compareTo(Foo f) {37return new Integer(id).compareTo(new Integer(f.id));38}39}4041public static void main(String[] args) throws Exception {42testLessThan();43testLessThanOrEqual();44testEquals();45testGreaterThanOrEqual();46testGreaterThan();47testNotEquals();48testNull();49testNotNull();50testTrue();51testFalse();52}5354private static void testLessThan() throws Exception {55expectPass(Assertion.LT, 1, 2);5657expectFail(Assertion.LT, 2, 2);58expectFail(Assertion.LT, 2, 1);59expectFail(Assertion.LT, null, 2);60expectFail(Assertion.LT, 2, null);61}6263private static void testLessThanOrEqual() throws Exception {64expectPass(Assertion.LTE, 1, 2);65expectPass(Assertion.LTE, 2, 2);6667expectFail(Assertion.LTE, 3, 2);68expectFail(Assertion.LTE, null, 2);69expectFail(Assertion.LTE, 2, null);70}7172private static void testEquals() throws Exception {73expectPass(Assertion.EQ, 1, 1);74expectPass(Assertion.EQ, null, null);7576Foo f1 = new Foo(1);77expectPass(Assertion.EQ, f1, f1);7879Foo f2 = new Foo(1);80expectFail(Assertion.EQ, f1, f2);81expectFail(Assertion.LTE, null, 2);82expectFail(Assertion.LTE, 2, null);83}8485private static void testGreaterThanOrEqual() throws Exception {86expectPass(Assertion.GTE, 1, 1);87expectPass(Assertion.GTE, 2, 1);8889expectFail(Assertion.GTE, 1, 2);90expectFail(Assertion.GTE, null, 2);91expectFail(Assertion.GTE, 2, null);92}9394private static void testGreaterThan() throws Exception {95expectPass(Assertion.GT, 2, 1);9697expectFail(Assertion.GT, 1, 1);98expectFail(Assertion.GT, 1, 2);99expectFail(Assertion.GT, null, 2);100expectFail(Assertion.GT, 2, null);101}102103private static void testNotEquals() throws Exception {104expectPass(Assertion.NE, null, 1);105expectPass(Assertion.NE, 1, null);106107Foo f1 = new Foo(1);108Foo f2 = new Foo(1);109expectPass(Assertion.NE, f1, f2);110111expectFail(Assertion.NE, null, null);112expectFail(Assertion.NE, f1, f1);113expectFail(Assertion.NE, 1, 1);114}115116private static void testNull() throws Exception {117expectPass(Assertion.NULL, null);118119expectFail(Assertion.NULL, 1);120}121122private static void testNotNull() throws Exception {123expectPass(Assertion.NOTNULL, 1);124125expectFail(Assertion.NOTNULL, null);126}127128private static void testTrue() throws Exception {129expectPass(Assertion.TRUE, true);130131expectFail(Assertion.TRUE, false);132}133134private static void testFalse() throws Exception {135expectPass(Assertion.FALSE, false);136137expectFail(Assertion.FALSE, true);138}139140private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args)141throws Exception {142Assertion.run(assertion, args);143}144145private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args)146throws Exception {147try {148Assertion.run(assertion, args);149} catch (RuntimeException e) {150return;151}152throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) +153" to throw a RuntimeException");154}155156}157158enum Assertion {159LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE;160161public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) {162String msg = "Expected " + format(assertion, args) + " to pass";163switch (assertion) {164case LT:165assertLessThan(args[0], args[1], msg);166break;167case LTE:168assertLessThanOrEqual(args[0], args[1], msg);169break;170case EQ:171assertEquals(args[0], args[1], msg);172break;173case GTE:174assertGreaterThanOrEqual(args[0], args[1], msg);175break;176case GT:177assertGreaterThan(args[0], args[1], msg);178break;179case NE:180assertNotEquals(args[0], args[1], msg);181break;182case NULL:183assertNull(args == null ? args : args[0], msg);184break;185case NOTNULL:186assertNotNull(args == null ? args : args[0], msg);187break;188case FALSE:189assertFalse((Boolean) args[0], msg);190break;191case TRUE:192assertTrue((Boolean) args[0], msg);193break;194default:195// do nothing196}197}198199public static String format(Assertion assertion, Object ... args) {200switch (assertion) {201case LT:202return asString("assertLessThan", args);203case LTE:204return asString("assertLessThanOrEqual", args);205case EQ:206return asString("assertEquals", args);207case GTE:208return asString("assertGreaterThanOrEquals", args);209case GT:210return asString("assertGreaterThan", args);211case NE:212return asString("assertNotEquals", args);213case NULL:214return asString("assertNull", args);215case NOTNULL:216return asString("assertNotNull", args);217case FALSE:218return asString("assertFalse", args);219case TRUE:220return asString("assertTrue", args);221default:222return "";223}224}225226private static String asString(String assertion, Object ... args) {227if (args == null) {228return String.format("%s(null)", assertion);229}230if (args.length == 1) {231return String.format("%s(%s)", assertion, args[0]);232} else {233return String.format("%s(%s, %s)", assertion, args[0], args[1]);234}235}236}237238239