1.1 --- a/test/testlibrary/AssertsTest.java Wed Sep 25 13:58:13 2013 +0200 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,237 +0,0 @@ 1.4 -/* 1.5 - * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 - * 1.8 - * This code is free software; you can redistribute it and/or modify it 1.9 - * under the terms of the GNU General Public License version 2 only, as 1.10 - * published by the Free Software Foundation. 1.11 - * 1.12 - * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 - * version 2 for more details (a copy is included in the LICENSE file that 1.16 - * accompanied this code). 1.17 - * 1.18 - * You should have received a copy of the GNU General Public License version 1.19 - * 2 along with this work; if not, write to the Free Software Foundation, 1.20 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 - * 1.22 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 - * or visit www.oracle.com if you need additional information or have any 1.24 - * questions. 1.25 - */ 1.26 - 1.27 -import static com.oracle.java.testlibrary.Asserts.*; 1.28 - 1.29 -/* @test 1.30 - * @summary Tests the different assertions in the Assert class 1.31 - * @library /testlibrary 1.32 - */ 1.33 -public class AssertsTest { 1.34 - private static class Foo implements Comparable<Foo> { 1.35 - final int id; 1.36 - public Foo(int id) { 1.37 - this.id = id; 1.38 - } 1.39 - 1.40 - public int compareTo(Foo f) { 1.41 - return new Integer(id).compareTo(new Integer(f.id)); 1.42 - } 1.43 - } 1.44 - 1.45 - public static void main(String[] args) throws Exception { 1.46 - testLessThan(); 1.47 - testLessThanOrEqual(); 1.48 - testEquals(); 1.49 - testGreaterThanOrEqual(); 1.50 - testGreaterThan(); 1.51 - testNotEquals(); 1.52 - testNull(); 1.53 - testNotNull(); 1.54 - testTrue(); 1.55 - testFalse(); 1.56 - } 1.57 - 1.58 - private static void testLessThan() throws Exception { 1.59 - expectPass(Assertion.LT, 1, 2); 1.60 - 1.61 - expectFail(Assertion.LT, 2, 2); 1.62 - expectFail(Assertion.LT, 2, 1); 1.63 - expectFail(Assertion.LT, null, 2); 1.64 - expectFail(Assertion.LT, 2, null); 1.65 - } 1.66 - 1.67 - private static void testLessThanOrEqual() throws Exception { 1.68 - expectPass(Assertion.LTE, 1, 2); 1.69 - expectPass(Assertion.LTE, 2, 2); 1.70 - 1.71 - expectFail(Assertion.LTE, 3, 2); 1.72 - expectFail(Assertion.LTE, null, 2); 1.73 - expectFail(Assertion.LTE, 2, null); 1.74 - } 1.75 - 1.76 - private static void testEquals() throws Exception { 1.77 - expectPass(Assertion.EQ, 1, 1); 1.78 - expectPass(Assertion.EQ, null, null); 1.79 - 1.80 - Foo f1 = new Foo(1); 1.81 - expectPass(Assertion.EQ, f1, f1); 1.82 - 1.83 - Foo f2 = new Foo(1); 1.84 - expectFail(Assertion.EQ, f1, f2); 1.85 - expectFail(Assertion.LTE, null, 2); 1.86 - expectFail(Assertion.LTE, 2, null); 1.87 - } 1.88 - 1.89 - private static void testGreaterThanOrEqual() throws Exception { 1.90 - expectPass(Assertion.GTE, 1, 1); 1.91 - expectPass(Assertion.GTE, 2, 1); 1.92 - 1.93 - expectFail(Assertion.GTE, 1, 2); 1.94 - expectFail(Assertion.GTE, null, 2); 1.95 - expectFail(Assertion.GTE, 2, null); 1.96 - } 1.97 - 1.98 - private static void testGreaterThan() throws Exception { 1.99 - expectPass(Assertion.GT, 2, 1); 1.100 - 1.101 - expectFail(Assertion.GT, 1, 1); 1.102 - expectFail(Assertion.GT, 1, 2); 1.103 - expectFail(Assertion.GT, null, 2); 1.104 - expectFail(Assertion.GT, 2, null); 1.105 - } 1.106 - 1.107 - private static void testNotEquals() throws Exception { 1.108 - expectPass(Assertion.NE, null, 1); 1.109 - expectPass(Assertion.NE, 1, null); 1.110 - 1.111 - Foo f1 = new Foo(1); 1.112 - Foo f2 = new Foo(1); 1.113 - expectPass(Assertion.NE, f1, f2); 1.114 - 1.115 - expectFail(Assertion.NE, null, null); 1.116 - expectFail(Assertion.NE, f1, f1); 1.117 - expectFail(Assertion.NE, 1, 1); 1.118 - } 1.119 - 1.120 - private static void testNull() throws Exception { 1.121 - expectPass(Assertion.NULL, null); 1.122 - 1.123 - expectFail(Assertion.NULL, 1); 1.124 - } 1.125 - 1.126 - private static void testNotNull() throws Exception { 1.127 - expectPass(Assertion.NOTNULL, 1); 1.128 - 1.129 - expectFail(Assertion.NOTNULL, null); 1.130 - } 1.131 - 1.132 - private static void testTrue() throws Exception { 1.133 - expectPass(Assertion.TRUE, true); 1.134 - 1.135 - expectFail(Assertion.TRUE, false); 1.136 - } 1.137 - 1.138 - private static void testFalse() throws Exception { 1.139 - expectPass(Assertion.FALSE, false); 1.140 - 1.141 - expectFail(Assertion.FALSE, true); 1.142 - } 1.143 - 1.144 - private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args) 1.145 - throws Exception { 1.146 - Assertion.run(assertion, args); 1.147 - } 1.148 - 1.149 - private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args) 1.150 - throws Exception { 1.151 - try { 1.152 - Assertion.run(assertion, args); 1.153 - } catch (RuntimeException e) { 1.154 - return; 1.155 - } 1.156 - throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) + 1.157 - " to throw a RuntimeException"); 1.158 - } 1.159 - 1.160 -} 1.161 - 1.162 -enum Assertion { 1.163 - LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE; 1.164 - 1.165 - public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) { 1.166 - String msg = "Expected " + format(assertion, args) + " to pass"; 1.167 - switch (assertion) { 1.168 - case LT: 1.169 - assertLessThan(args[0], args[1], msg); 1.170 - break; 1.171 - case LTE: 1.172 - assertLessThanOrEqual(args[0], args[1], msg); 1.173 - break; 1.174 - case EQ: 1.175 - assertEquals(args[0], args[1], msg); 1.176 - break; 1.177 - case GTE: 1.178 - assertGreaterThanOrEqual(args[0], args[1], msg); 1.179 - break; 1.180 - case GT: 1.181 - assertGreaterThan(args[0], args[1], msg); 1.182 - break; 1.183 - case NE: 1.184 - assertNotEquals(args[0], args[1], msg); 1.185 - break; 1.186 - case NULL: 1.187 - assertNull(args == null ? args : args[0], msg); 1.188 - break; 1.189 - case NOTNULL: 1.190 - assertNotNull(args == null ? args : args[0], msg); 1.191 - break; 1.192 - case FALSE: 1.193 - assertFalse((Boolean) args[0], msg); 1.194 - break; 1.195 - case TRUE: 1.196 - assertTrue((Boolean) args[0], msg); 1.197 - break; 1.198 - default: 1.199 - // do nothing 1.200 - } 1.201 - } 1.202 - 1.203 - public static String format(Assertion assertion, Object ... args) { 1.204 - switch (assertion) { 1.205 - case LT: 1.206 - return asString("assertLessThan", args); 1.207 - case LTE: 1.208 - return asString("assertLessThanOrEqual", args); 1.209 - case EQ: 1.210 - return asString("assertEquals", args); 1.211 - case GTE: 1.212 - return asString("assertGreaterThanOrEquals", args); 1.213 - case GT: 1.214 - return asString("assertGreaterThan", args); 1.215 - case NE: 1.216 - return asString("assertNotEquals", args); 1.217 - case NULL: 1.218 - return asString("assertNull", args); 1.219 - case NOTNULL: 1.220 - return asString("assertNotNull", args); 1.221 - case FALSE: 1.222 - return asString("assertFalse", args); 1.223 - case TRUE: 1.224 - return asString("assertTrue", args); 1.225 - default: 1.226 - return ""; 1.227 - } 1.228 - } 1.229 - 1.230 - private static String asString(String assertion, Object ... args) { 1.231 - if (args == null) { 1.232 - return String.format("%s(null)", assertion); 1.233 - } 1.234 - if (args.length == 1) { 1.235 - return String.format("%s(%s)", assertion, args[0]); 1.236 - } else { 1.237 - return String.format("%s(%s, %s)", assertion, args[0], args[1]); 1.238 - } 1.239 - } 1.240 -}