ehelin@5531: /* ehelin@5531: * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. ehelin@5531: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ehelin@5531: * ehelin@5531: * This code is free software; you can redistribute it and/or modify it ehelin@5531: * under the terms of the GNU General Public License version 2 only, as ehelin@5531: * published by the Free Software Foundation. ehelin@5531: * ehelin@5531: * This code is distributed in the hope that it will be useful, but WITHOUT ehelin@5531: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ehelin@5531: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ehelin@5531: * version 2 for more details (a copy is included in the LICENSE file that ehelin@5531: * accompanied this code). ehelin@5531: * ehelin@5531: * You should have received a copy of the GNU General Public License version ehelin@5531: * 2 along with this work; if not, write to the Free Software Foundation, ehelin@5531: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ehelin@5531: * ehelin@5531: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ehelin@5531: * or visit www.oracle.com if you need additional information or have any ehelin@5531: * questions. ehelin@5531: */ ehelin@5531: ehelin@5531: import static com.oracle.java.testlibrary.Asserts.*; ehelin@5531: ehelin@5531: /* @test ehelin@5531: * @summary Tests the different assertions in the Assert class ehelin@5531: * @library /testlibrary ehelin@5531: */ ehelin@5531: public class AssertsTest { ehelin@5531: private static class Foo implements Comparable { ehelin@5531: final int id; ehelin@5531: public Foo(int id) { ehelin@5531: this.id = id; ehelin@5531: } ehelin@5531: ehelin@5531: public int compareTo(Foo f) { ehelin@5531: return new Integer(id).compareTo(new Integer(f.id)); ehelin@5531: } ehelin@5531: } ehelin@5531: ehelin@5531: public static void main(String[] args) throws Exception { ehelin@5531: testLessThan(); ehelin@5531: testLessThanOrEqual(); ehelin@5531: testEquals(); ehelin@5531: testGreaterThanOrEqual(); ehelin@5531: testGreaterThan(); ehelin@5531: testNotEquals(); ehelin@5531: testNull(); ehelin@5531: testNotNull(); ehelin@5531: testTrue(); ehelin@5531: testFalse(); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testLessThan() throws Exception { ehelin@5531: expectPass(Assertion.LT, 1, 2); ehelin@5531: ehelin@5531: expectFail(Assertion.LT, 2, 2); ehelin@5531: expectFail(Assertion.LT, 2, 1); ehelin@5531: expectFail(Assertion.LT, null, 2); ehelin@5531: expectFail(Assertion.LT, 2, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testLessThanOrEqual() throws Exception { ehelin@5531: expectPass(Assertion.LTE, 1, 2); ehelin@5531: expectPass(Assertion.LTE, 2, 2); ehelin@5531: ehelin@5531: expectFail(Assertion.LTE, 3, 2); ehelin@5531: expectFail(Assertion.LTE, null, 2); ehelin@5531: expectFail(Assertion.LTE, 2, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testEquals() throws Exception { ehelin@5531: expectPass(Assertion.EQ, 1, 1); ehelin@5531: expectPass(Assertion.EQ, null, null); ehelin@5531: ehelin@5531: Foo f1 = new Foo(1); ehelin@5531: expectPass(Assertion.EQ, f1, f1); ehelin@5531: ehelin@5531: Foo f2 = new Foo(1); ehelin@5531: expectFail(Assertion.EQ, f1, f2); ehelin@5531: expectFail(Assertion.LTE, null, 2); ehelin@5531: expectFail(Assertion.LTE, 2, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testGreaterThanOrEqual() throws Exception { ehelin@5531: expectPass(Assertion.GTE, 1, 1); ehelin@5531: expectPass(Assertion.GTE, 2, 1); ehelin@5531: ehelin@5531: expectFail(Assertion.GTE, 1, 2); ehelin@5531: expectFail(Assertion.GTE, null, 2); ehelin@5531: expectFail(Assertion.GTE, 2, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testGreaterThan() throws Exception { ehelin@5531: expectPass(Assertion.GT, 2, 1); ehelin@5531: ehelin@5531: expectFail(Assertion.GT, 1, 1); ehelin@5531: expectFail(Assertion.GT, 1, 2); ehelin@5531: expectFail(Assertion.GT, null, 2); ehelin@5531: expectFail(Assertion.GT, 2, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testNotEquals() throws Exception { ehelin@5531: expectPass(Assertion.NE, null, 1); ehelin@5531: expectPass(Assertion.NE, 1, null); ehelin@5531: ehelin@5531: Foo f1 = new Foo(1); ehelin@5531: Foo f2 = new Foo(1); ehelin@5531: expectPass(Assertion.NE, f1, f2); ehelin@5531: ehelin@5531: expectFail(Assertion.NE, null, null); ehelin@5531: expectFail(Assertion.NE, f1, f1); ehelin@5531: expectFail(Assertion.NE, 1, 1); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testNull() throws Exception { ehelin@5531: expectPass(Assertion.NULL, null); ehelin@5531: ehelin@5531: expectFail(Assertion.NULL, 1); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testNotNull() throws Exception { ehelin@5531: expectPass(Assertion.NOTNULL, 1); ehelin@5531: ehelin@5531: expectFail(Assertion.NOTNULL, null); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testTrue() throws Exception { ehelin@5531: expectPass(Assertion.TRUE, true); ehelin@5531: ehelin@5531: expectFail(Assertion.TRUE, false); ehelin@5531: } ehelin@5531: ehelin@5531: private static void testFalse() throws Exception { ehelin@5531: expectPass(Assertion.FALSE, false); ehelin@5531: ehelin@5531: expectFail(Assertion.FALSE, true); ehelin@5531: } ehelin@5531: ehelin@5531: private static > void expectPass(Assertion assertion, T ... args) ehelin@5531: throws Exception { ehelin@5531: Assertion.run(assertion, args); ehelin@5531: } ehelin@5531: ehelin@5531: private static > void expectFail(Assertion assertion, T ... args) ehelin@5531: throws Exception { ehelin@5531: try { ehelin@5531: Assertion.run(assertion, args); ehelin@5531: } catch (RuntimeException e) { ehelin@5531: return; ehelin@5531: } ehelin@5531: throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) + ehelin@5531: " to throw a RuntimeException"); ehelin@5531: } ehelin@5531: ehelin@5531: } ehelin@5531: ehelin@5531: enum Assertion { ehelin@5531: LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE; ehelin@5531: ehelin@5531: public static > void run(Assertion assertion, T ... args) { ehelin@5531: String msg = "Expected " + format(assertion, args) + " to pass"; ehelin@5531: switch (assertion) { ehelin@5531: case LT: ehelin@5531: assertLessThan(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case LTE: ehelin@5531: assertLessThanOrEqual(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case EQ: ehelin@5531: assertEquals(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case GTE: ehelin@5531: assertGreaterThanOrEqual(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case GT: ehelin@5531: assertGreaterThan(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case NE: ehelin@5531: assertNotEquals(args[0], args[1], msg); ehelin@5531: break; ehelin@5531: case NULL: ehelin@5531: assertNull(args == null ? args : args[0], msg); ehelin@5531: break; ehelin@5531: case NOTNULL: ehelin@5531: assertNotNull(args == null ? args : args[0], msg); ehelin@5531: break; ehelin@5531: case FALSE: ehelin@5531: assertFalse((Boolean) args[0], msg); ehelin@5531: break; ehelin@5531: case TRUE: ehelin@5531: assertTrue((Boolean) args[0], msg); ehelin@5531: break; ehelin@5531: default: ehelin@5531: // do nothing ehelin@5531: } ehelin@5531: } ehelin@5531: ehelin@5531: public static String format(Assertion assertion, Object ... args) { ehelin@5531: switch (assertion) { ehelin@5531: case LT: ehelin@5531: return asString("assertLessThan", args); ehelin@5531: case LTE: ehelin@5531: return asString("assertLessThanOrEqual", args); ehelin@5531: case EQ: ehelin@5531: return asString("assertEquals", args); ehelin@5531: case GTE: ehelin@5531: return asString("assertGreaterThanOrEquals", args); ehelin@5531: case GT: ehelin@5531: return asString("assertGreaterThan", args); ehelin@5531: case NE: ehelin@5531: return asString("assertNotEquals", args); ehelin@5531: case NULL: ehelin@5531: return asString("assertNull", args); ehelin@5531: case NOTNULL: ehelin@5531: return asString("assertNotNull", args); ehelin@5531: case FALSE: ehelin@5531: return asString("assertFalse", args); ehelin@5531: case TRUE: ehelin@5531: return asString("assertTrue", args); ehelin@5531: default: ehelin@5531: return ""; ehelin@5531: } ehelin@5531: } ehelin@5531: ehelin@5531: private static String asString(String assertion, Object ... args) { ehelin@5531: if (args == null) { ehelin@5531: return String.format("%s(null)", assertion); ehelin@5531: } ehelin@5531: if (args.length == 1) { ehelin@5531: return String.format("%s(%s)", assertion, args[0]); ehelin@5531: } else { ehelin@5531: return String.format("%s(%s, %s)", assertion, args[0], args[1]); ehelin@5531: } ehelin@5531: } ehelin@5531: }