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