AssertsTest.java revision 8359:ed6389f70257
1238582Smm/* 2238582Smm * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved. 3238582Smm * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4238582Smm * 5238582Smm * This code is free software; you can redistribute it and/or modify it 6238582Smm * under the terms of the GNU General Public License version 2 only, as 7238582Smm * published by the Free Software Foundation. 8238582Smm * 9238582Smm * This code is distributed in the hope that it will be useful, but WITHOUT 10238582Smm * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11238582Smm * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12238582Smm * version 2 for more details (a copy is included in the LICENSE file that 13238582Smm * accompanied this code). 14238582Smm * 15238582Smm * You should have received a copy of the GNU General Public License version 16238582Smm * 2 along with this work; if not, write to the Free Software Foundation, 17238582Smm * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18238582Smm * 19238582Smm * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20238582Smm * or visit www.oracle.com if you need additional information or have any 21238582Smm * questions. 22238582Smm */ 23238582Smm 24238582Smmimport static jdk.test.lib.Asserts.*; 25238582Smm 26238582Smm/* @test 27238582Smm * @summary Tests the different assertions in the Assert class 28238582Smm * @library /testlibrary 29238582Smm */ 30238582Smmpublic class AssertsTest { 31238582Smm private static class Foo implements Comparable<Foo> { 32238582Smm final int id; 33238582Smm public Foo(int id) { 34238582Smm this.id = id; 35238582Smm } 36238582Smm 37238582Smm public int compareTo(Foo f) { 38238582Smm return new Integer(id).compareTo(new Integer(f.id)); 39238582Smm } 40238582Smm } 41238582Smm 42238582Smm public static void main(String[] args) throws Exception { 43238582Smm testLessThan(); 44238582Smm testLessThanOrEqual(); 45238582Smm testEquals(); 46238582Smm testGreaterThanOrEqual(); 47238582Smm testGreaterThan(); 48238582Smm testNotEquals(); 49238582Smm testNull(); 50238582Smm testNotNull(); 51238582Smm testTrue(); 52238582Smm testFalse(); 53238582Smm } 54238582Smm 55238582Smm private static void testLessThan() throws Exception { 56238582Smm expectPass(Assertion.LT, 1, 2); 57238582Smm 58238582Smm expectFail(Assertion.LT, 2, 2); 59238582Smm expectFail(Assertion.LT, 2, 1); 60238582Smm expectFail(Assertion.LT, null, 2); 61238582Smm expectFail(Assertion.LT, 2, null); 62238582Smm } 63238582Smm 64238582Smm private static void testLessThanOrEqual() throws Exception { 65238582Smm expectPass(Assertion.LTE, 1, 2); 66238582Smm expectPass(Assertion.LTE, 2, 2); 67238582Smm 68238582Smm expectFail(Assertion.LTE, 3, 2); 69238582Smm expectFail(Assertion.LTE, null, 2); 70238582Smm expectFail(Assertion.LTE, 2, null); 71238582Smm } 72238582Smm 73238582Smm private static void testEquals() throws Exception { 74238582Smm expectPass(Assertion.EQ, 1, 1); 75238582Smm expectPass(Assertion.EQ, null, null); 76238582Smm 77238582Smm Foo f1 = new Foo(1); 78238582Smm expectPass(Assertion.EQ, f1, f1); 79238582Smm 80238582Smm Foo f2 = new Foo(1); 81238582Smm expectFail(Assertion.EQ, f1, f2); 82238582Smm expectFail(Assertion.LTE, null, 2); 83238582Smm expectFail(Assertion.LTE, 2, null); 84238582Smm } 85238582Smm 86238582Smm private static void testGreaterThanOrEqual() throws Exception { 87238582Smm expectPass(Assertion.GTE, 1, 1); 88238582Smm expectPass(Assertion.GTE, 2, 1); 89238582Smm 90238582Smm expectFail(Assertion.GTE, 1, 2); 91238582Smm expectFail(Assertion.GTE, null, 2); 92238582Smm expectFail(Assertion.GTE, 2, null); 93238582Smm } 94238582Smm 95238582Smm private static void testGreaterThan() throws Exception { 96238582Smm expectPass(Assertion.GT, 2, 1); 97238582Smm 98238582Smm expectFail(Assertion.GT, 1, 1); 99238582Smm expectFail(Assertion.GT, 1, 2); 100238582Smm expectFail(Assertion.GT, null, 2); 101238582Smm expectFail(Assertion.GT, 2, null); 102238582Smm } 103238582Smm 104238582Smm private static void testNotEquals() throws Exception { 105238582Smm expectPass(Assertion.NE, null, 1); 106238582Smm expectPass(Assertion.NE, 1, null); 107238582Smm 108238582Smm Foo f1 = new Foo(1); 109238582Smm Foo f2 = new Foo(1); 110238582Smm expectPass(Assertion.NE, f1, f2); 111238582Smm 112238582Smm expectFail(Assertion.NE, null, null); 113238582Smm expectFail(Assertion.NE, f1, f1); 114238582Smm expectFail(Assertion.NE, 1, 1); 115238582Smm } 116238582Smm 117238582Smm private static void testNull() throws Exception { 118238582Smm expectPass(Assertion.NULL, null); 119238582Smm 120238582Smm expectFail(Assertion.NULL, 1); 121238582Smm } 122238582Smm 123238582Smm private static void testNotNull() throws Exception { 124238582Smm 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 159enum 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} 238