1 /* 2 * Copyright (c) 2013, 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 com.oracle.java.testlibrary.Asserts.*; 25 26 /* @test 27 * @summary Tests the different assertions in the Assert class 28 * @library /testlibrary 29 */ 30 public class AssertsTest { 31 private static class Foo implements Comparable<Foo> { 32 final int id; 33 public Foo(int id) { 34 this.id = id; 35 } 36 37 public int compareTo(Foo f) { 38 return new Integer(id).compareTo(new Integer(f.id)); 39 } 40 } 41 42 public static void main(String[] args) throws Exception { 43 testLessThan(); 44 testLessThanOrEqual(); 45 testEquals(); 46 testGreaterThanOrEqual(); 47 testGreaterThan(); 48 testNotEquals(); 49 testNull(); 50 testNotNull(); 51 testTrue(); 52 testFalse(); 53 } 54 55 private static void testLessThan() throws Exception { 56 expectPass(Assertion.LT, 1, 2); 57 58 expectFail(Assertion.LT, 2, 2); 59 expectFail(Assertion.LT, 2, 1); 60 expectFail(Assertion.LT, null, 2); 61 expectFail(Assertion.LT, 2, null); 62 } 63 64 private static void testLessThanOrEqual() throws Exception { 65 expectPass(Assertion.LTE, 1, 2); 66 expectPass(Assertion.LTE, 2, 2); 67 68 expectFail(Assertion.LTE, 3, 2); 69 expectFail(Assertion.LTE, null, 2); 70 expectFail(Assertion.LTE, 2, null); 71 } 72 73 private static void testEquals() throws Exception { 74 expectPass(Assertion.EQ, 1, 1); 75 expectPass(Assertion.EQ, null, null); 76 77 Foo f1 = new Foo(1); 78 expectPass(Assertion.EQ, f1, f1); 79 80 Foo f2 = new Foo(1); 81 expectFail(Assertion.EQ, f1, f2); 82 expectFail(Assertion.LTE, null, 2); 83 expectFail(Assertion.LTE, 2, null); 84 } 85 86 private static void testGreaterThanOrEqual() throws Exception { 87 expectPass(Assertion.GTE, 1, 1); 88 expectPass(Assertion.GTE, 2, 1); 89 90 expectFail(Assertion.GTE, 1, 2); 91 expectFail(Assertion.GTE, null, 2); 92 expectFail(Assertion.GTE, 2, null); 93 } 94 95 private static void testGreaterThan() throws Exception { 96 expectPass(Assertion.GT, 2, 1); 97 98 expectFail(Assertion.GT, 1, 1); 99 expectFail(Assertion.GT, 1, 2); 100 expectFail(Assertion.GT, null, 2); 101 expectFail(Assertion.GT, 2, null); 102 } 103 104 private static void testNotEquals() throws Exception { 105 expectPass(Assertion.NE, null, 1); 106 expectPass(Assertion.NE, 1, null); 107 108 Foo f1 = new Foo(1); 109 Foo f2 = new Foo(1); 110 expectPass(Assertion.NE, f1, f2); 111 112 expectFail(Assertion.NE, null, null); 113 expectFail(Assertion.NE, f1, f1); 114 expectFail(Assertion.NE, 1, 1); 115 } 116 117 private static void testNull() throws Exception { 118 expectPass(Assertion.NULL, null); 119 120 expectFail(Assertion.NULL, 1); 121 } 122 123 private static void testNotNull() throws Exception { 124 expectPass(Assertion.NOTNULL, 1); 125 126 expectFail(Assertion.NOTNULL, null); 127 } 128 129 private static void testTrue() throws Exception { 130 expectPass(Assertion.TRUE, true); 131 132 expectFail(Assertion.TRUE, false); 133 } 134 135 private static void testFalse() throws Exception { 136 expectPass(Assertion.FALSE, false); 137 138 expectFail(Assertion.FALSE, true); 139 } 140 141 private static <T extends Comparable<T>> void expectPass(Assertion assertion, T ... args) 142 throws Exception { 143 Assertion.run(assertion, args); 144 } 145 146 private static <T extends Comparable<T>> void expectFail(Assertion assertion, T ... args) 147 throws Exception { 148 try { 149 Assertion.run(assertion, args); 150 } catch (RuntimeException e) { 151 return; 152 } 153 throw new Exception("Expected " + Assertion.format(assertion, (Object[]) args) + 154 " to throw a RuntimeException"); 155 } 156 157 } 158 159 enum Assertion { 160 LT, LTE, EQ, GTE, GT, NE, NULL, NOTNULL, FALSE, TRUE; 161 162 public static <T extends Comparable<T>> void run(Assertion assertion, T ... args) { 163 String msg = "Expected " + format(assertion, args) + " to pass"; 164 switch (assertion) { 165 case LT: 166 assertLessThan(args[0], args[1], msg); 167 break; 168 case LTE: 169 assertLessThanOrEqual(args[0], args[1], msg); 170 break; 171 case EQ: 172 assertEquals(args[0], args[1], msg); 173 break; 174 case GTE: 175 assertGreaterThanOrEqual(args[0], args[1], msg); 176 break; 177 case GT: 178 assertGreaterThan(args[0], args[1], msg); 179 break; 180 case NE: 181 assertNotEquals(args[0], args[1], msg); 182 break; 183 case NULL: 184 assertNull(args == null ? args : args[0], msg); 185 break; 186 case NOTNULL: 187 assertNotNull(args == null ? args : args[0], msg); 188 break; 189 case FALSE: 190 assertFalse((Boolean) args[0], msg); 191 break; 192 case TRUE: 193 assertTrue((Boolean) args[0], msg); 194 break; 195 default: 196 // do nothing 197 } 198 } 199 200 public static String format(Assertion assertion, Object ... args) { 201 switch (assertion) { 202 case LT: 203 return asString("assertLessThan", args); 204 case LTE: 205 return asString("assertLessThanOrEqual", args); 206 case EQ: 207 return asString("assertEquals", args); 208 case GTE: 209 return asString("assertGreaterThanOrEquals", args); 210 case GT: 211 return asString("assertGreaterThan", args); 212 case NE: 213 return asString("assertNotEquals", args); 214 case NULL: 215 return asString("assertNull", args); 216 case NOTNULL: 217 return asString("assertNotNull", args); 218 case FALSE: 219 return asString("assertFalse", args); 220 case TRUE: 221 return asString("assertTrue", args); 222 default: 223 return ""; 224 } 225 } 226 227 private static String asString(String assertion, Object ... args) { 228 if (args == null) { 229 return String.format("%s(null)", assertion); 230 } 231 if (args.length == 1) { 232 return String.format("%s(%s)", assertion, args[0]); 233 } else { 234 return String.format("%s(%s, %s)", assertion, args[0], args[1]); 235 } 236 } 237 }