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