1 /* 2 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import static jdk.test.lib.Asserts.*; 25 26 /* @test 27 * @summary Tests the different assertions in the Assert class 28 * @modules java.base/jdk.internal.misc 29 * @library /test/lib 30 */ 31 public class AssertsTest { 32 private static class Foo implements Comparable<Foo> { 33 final int id; 34 public Foo(int id) { 35 this.id = id; 36 } 37 38 public int compareTo(Foo f) { 39 return new Integer(id).compareTo(new Integer(f.id)); 40 } 41 } 42 43 public static void main(String[] args) throws Exception { 44 testLessThan(); 45 testLessThanOrEqual(); 46 testEquals(); 47 testGreaterThanOrEqual(); 48 testGreaterThan(); 49 testNotEquals(); 50 testNull(); 51 testNotNull(); 52 testTrue(); 53 testFalse(); 54 } 55 56 private static void testLessThan() throws Exception { 57 expectPass(Assertion.LT, 1, 2); 58 59 expectFail(Assertion.LT, 2, 2); 60 expectFail(Assertion.LT, 2, 1); 61 expectFail(Assertion.LT, null, 2); 62 expectFail(Assertion.LT, 2, null); 63 } 64 65 private static void testLessThanOrEqual() throws Exception { 66 expectPass(Assertion.LTE, 1, 2); 67 expectPass(Assertion.LTE, 2, 2); 68 69 expectFail(Assertion.LTE, 3, 2); 70 expectFail(Assertion.LTE, null, 2); 71 expectFail(Assertion.LTE, 2, null); 72 } 73 74 private static void testEquals() throws Exception { 75 expectPass(Assertion.EQ, 1, 1); 76 expectPass(Assertion.EQ, null, null); 77 78 Foo f1 = new Foo(1); 79 expectPass(Assertion.EQ, f1, f1); 80 81 Foo f2 = new Foo(1); 82 expectFail(Assertion.EQ, f1, f2); 83 expectFail(Assertion.LTE, null, 2); 84 expectFail(Assertion.LTE, 2, null); 85 } 86 87 private static void testGreaterThanOrEqual() throws Exception { 88 expectPass(Assertion.GTE, 1, 1); 89 expectPass(Assertion.GTE, 2, 1); 90 91 expectFail(Assertion.GTE, 1, 2); 92 expectFail(Assertion.GTE, null, 2); 93 expectFail(Assertion.GTE, 2, null); 94 } 95 96 private static void testGreaterThan() throws Exception { 97 expectPass(Assertion.GT, 2, 1); 98 99 expectFail(Assertion.GT, 1, 1); 100 expectFail(Assertion.GT, 1, 2); 101 expectFail(Assertion.GT, null, 2); 102 expectFail(Assertion.GT, 2, null); 103 } 104 105 private static void testNotEquals() throws Exception { 106 expectPass(Assertion.NE, null, 1); 107 expectPass(Assertion.NE, 1, null); 108 109 Foo f1 = new Foo(1); 110 Foo f2 = new Foo(1); 111 expectPass(Assertion.NE, f1, f2); 112 113 expectFail(Assertion.NE, null, null); 114 expectFail(Assertion.NE, f1, f1); 115 expectFail(Assertion.NE, 1, 1); 116 } 117 118 private static void testNull() throws Exception { 119 expectPass(Assertion.NULL, null); 120 121 expectFail(Assertion.NULL, 1); 122 } 123 124 private static void testNotNull() throws Exception { 125 expectPass(Assertion.NOTNULL, 1); 126 127 expectFail(Assertion.NOTNULL, null); 128 } 129 130 private static void testTrue() throws Exception { 131 expectPass(Assertion.TRUE, true); 132 133 expectFail(Assertion.TRUE, false); 134 } 135 136 private static void testFalse() throws Exception { 137 expectPass(Assertion.FALSE, false); 138 139 expectFail(Assertion.FALSE, true); 140 } 141 142 private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args) 143 throws Exception { 144 Assertion.run(assertion, args); 145 } 146 147 private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args) 148 throws Exception { 149 try { 150 Assertion.run(assertion, args); 151 } catch (RuntimeException e) { 152 return; 153 } 154 throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) + 155 " to throw a RuntimeException"); 156 } 157 158 } 159 160 enum Assertion { 161 LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE; 162 163 public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) { 164 String msg = "Expected " + format(assertion, args) + " to pass"; 165 switch (assertion) { 166 case LT: 167 assertLessThan(args[0], args[1], msg); 168 break; 169 case LTE: 170 assertLessThanOrEqual(args[0], args[1], msg); 171 break; 172 case EQ: 173 assertEquals(args[0], args[1], msg); 174 break; 175 case GTE: 176 assertGreaterThanOrEqual(args[0], args[1], msg); 177 break; 178 case GT: 179 assertGreaterThan(args[0], args[1], msg); 180 break; 181 case NE: 182 assertNotEquals(args[0], args[1], msg); 183 break; 184 case NULL: 185 assertNull(args == null ? args : args[0], msg); 186 break; 187 case NOTNULL: 188 assertNotNull(args == null ? args : args[0], msg); 189 break; 190 case FALSE: 191 assertFalse((Boolean) args[0], msg); 192 break; 193 case TRUE: 194 assertTrue((Boolean) args[0], msg); 195 break; 196 default: 197 // do nothing 198 } 199 } 200 201 public static String format(Assertion assertion, Object ... args) { 202 switch (assertion) { 203 case LT: 204 return asString("assertLessThan", args); 205 case LTE: 206 return asString("assertLessThanOrEqual", args); 207 case EQ: 208 return asString("assertEquals", args); 209 case GTE: 210 return asString("assertGreaterThanOrEquals", args); 211 case GT: 212 return asString("assertGreaterThan", args); 213 case NE: 214 return asString("assertNotEquals", args); 215 case NULL: 216 return asString("assertNull", args); 217 case NOTNULL: 218 return asString("assertNotNull", args); 219 case FALSE: 220 return asString("assertFalse", args); 221 case TRUE: 222 return asString("assertTrue", args); 223 default: 224 return ""; 225 } 226 } 227 228 private static String asString(String assertion, Object ... args) { 229 if (args == null) { 230 return String.format("%s(null)", assertion); 231 } 232 if (args.length == 1) { 233 return String.format("%s(%s)", assertion, args[0]); 234 } else { 235 return String.format("%s(%s, %s)", assertion, args[0], args[1]); 236 } 237 } 238 }