Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/lib/testlibrary/AssertsTest.java
38833 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 jdk.testlibrary.Asserts.*;2425/* @test26* @summary Tests the different assertions in the Assert class27*/28public class AssertsTest {29private static class Foo implements Comparable<Foo> {30final int id;31public Foo(int id) {32this.id = id;33}3435public int compareTo(Foo f) {36return new Integer(id).compareTo(new Integer(f.id));37}38}3940public static void main(String[] args) throws Exception {41testLessThan();42testLessThanOrEqual();43testEquals();44testGreaterThanOrEqual();45testGreaterThan();46testNotEquals();47testNull();48testNotNull();49testTrue();50testFalse();51}5253private static void testLessThan() throws Exception {54expectPass(Assertion.LT, 1, 2);5556expectFail(Assertion.LT, 2, 2);57expectFail(Assertion.LT, 2, 1);58expectFail(Assertion.LT, null, 2);59expectFail(Assertion.LT, 2, null);60}6162private static void testLessThanOrEqual() throws Exception {63expectPass(Assertion.LTE, 1, 2);64expectPass(Assertion.LTE, 2, 2);6566expectFail(Assertion.LTE, 3, 2);67expectFail(Assertion.LTE, null, 2);68expectFail(Assertion.LTE, 2, null);69}7071private static void testEquals() throws Exception {72expectPass(Assertion.EQ, 1, 1);73expectPass(Assertion.EQ, null, null);7475Foo f1 = new Foo(1);76expectPass(Assertion.EQ, f1, f1);7778Foo f2 = new Foo(1);79expectFail(Assertion.EQ, f1, f2);80expectFail(Assertion.LTE, null, 2);81expectFail(Assertion.LTE, 2, null);82}8384private static void testGreaterThanOrEqual() throws Exception {85expectPass(Assertion.GTE, 1, 1);86expectPass(Assertion.GTE, 2, 1);8788expectFail(Assertion.GTE, 1, 2);89expectFail(Assertion.GTE, null, 2);90expectFail(Assertion.GTE, 2, null);91}9293private static void testGreaterThan() throws Exception {94expectPass(Assertion.GT, 2, 1);9596expectFail(Assertion.GT, 1, 1);97expectFail(Assertion.GT, 1, 2);98expectFail(Assertion.GT, null, 2);99expectFail(Assertion.GT, 2, null);100}101102private static void testNotEquals() throws Exception {103expectPass(Assertion.NE, null, 1);104expectPass(Assertion.NE, 1, null);105106Foo f1 = new Foo(1);107Foo f2 = new Foo(1);108expectPass(Assertion.NE, f1, f2);109110expectFail(Assertion.NE, null, null);111expectFail(Assertion.NE, f1, f1);112expectFail(Assertion.NE, 1, 1);113}114115private static void testNull() throws Exception {116expectPass(Assertion.NULL, null);117118expectFail(Assertion.NULL, 1);119}120121private static void testNotNull() throws Exception {122expectPass(Assertion.NOTNULL, 1);123124expectFail(Assertion.NOTNULL, null);125}126127private static void testTrue() throws Exception {128expectPass(Assertion.TRUE, true);129130expectFail(Assertion.TRUE, false);131}132133private static void testFalse() throws Exception {134expectPass(Assertion.FALSE, false);135136expectFail(Assertion.FALSE, true);137}138139private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args)140throws Exception {141Assertion.run(assertion, args);142}143144private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args)145throws Exception {146try {147Assertion.run(assertion, args);148} catch (RuntimeException e) {149return;150}151throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) +152" to throw a RuntimeException");153}154155}156157enum Assertion {158LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE;159160public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) {161String msg = "Expected " + format(assertion, args) + " to pass";162switch (assertion) {163case LT:164assertLessThan(args[0], args[1], msg);165break;166case LTE:167assertLessThanOrEqual(args[0], args[1], msg);168break;169case EQ:170assertEquals(args[0], args[1], msg);171break;172case GTE:173assertGreaterThanOrEqual(args[0], args[1], msg);174break;175case GT:176assertGreaterThan(args[0], args[1], msg);177break;178case NE:179assertNotEquals(args[0], args[1], msg);180break;181case NULL:182assertNull(args == null ? args : args[0], msg);183break;184case NOTNULL:185assertNotNull(args == null ? args : args[0], msg);186break;187case FALSE:188assertFalse((Boolean) args[0], msg);189break;190case TRUE:191assertTrue((Boolean) args[0], msg);192break;193default:194// do nothing195}196}197198public static String format(Assertion assertion, Object ... args) {199switch (assertion) {200case LT:201return asString("assertLessThan", args);202case LTE:203return asString("assertLessThanOrEqual", args);204case EQ:205return asString("assertEquals", args);206case GTE:207return asString("assertGreaterThanOrEquals", args);208case GT:209return asString("assertGreaterThan", args);210case NE:211return asString("assertNotEquals", args);212case NULL:213return asString("assertNull", args);214case NOTNULL:215return asString("assertNotNull", args);216case FALSE:217return asString("assertFalse", args);218case TRUE:219return asString("assertTrue", args);220default:221return "";222}223}224225private static String asString(String assertion, Object ... args) {226if (args == null) {227return String.format("%s(null)", assertion);228}229if (args.length == 1) {230return String.format("%s(%s)", assertion, args[0]);231} else {232return String.format("%s(%s, %s)", assertion, args[0], args[1]);233}234}235}236237238