-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAssert.java
More file actions
122 lines (109 loc) · 4.56 KB
/
Assert.java
File metadata and controls
122 lines (109 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package org.variantsync.diffdetective.util;
import java.util.function.Supplier;
/**
* Assertions which cannot be disabled.
*
* <p>Assertions should document preconditions and postconditions which aren't enforced by the type
* system. All assertions in this class throw {@code AssertionError} to abort the program
* execution. At no point an {@code AssertionError} should be catched and the normal control flow
* resumed. The only valid reasons to catch such an exception is to add additional information to
* the exception or to ensure a fast and correct program shutdown.
*
* <p>The default Java assertions are disabled by default which makes them unsuitable for this
* research project because correctness is a higher goal then a little performance. This class
* provides assertions in a similar manner as JUnit and should be used for all assertions outside
* of unit tests (for unit tests use JUnit's assertions). Disabling or Enabling Java's
* {@code assert} should make no difference whatsoever.
*/
public class Assert {
/**
* Abort program execution if {@code cond} is false.
*
* <p>If the checked condition is not obvious in the source code
* {@link assertTrue(boolean, String)} should be used to help identifying issues quickly.
*
* @param cond the condition which has to be true
* @throws AssertionError if {@code cond} is false
*/
public static void assertTrue(boolean cond) {
assertTrue(cond, "assertion failed");
}
/**
* Abort program execution if {@code cond} is false.
*
* <p>Overload of {@link assertTrue(boolean, String)} for computationally expensive messages. It's
* used to save the little execution time to construct a helpful error message in the common
* case of a correct assumption.
*
* @param cond the condition which has to be true
* @param errorMessage a supplier of a single message identifying what condition is checked
* @throws AssertionError if {@code cond} is false
*/
public static void assertTrue(boolean cond, final Supplier<String> errorMessage) {
if (!cond) {
fail(errorMessage.get());
}
}
/**
* Abort program execution if {@code cond} is false.
*
* <p>If {@code errorMessage} is computationally expensive, consider using
* {@link assertTrue(boolean, Supplier<String>)}.
*
* @param cond the condition which has to be true
* @param errorMessage a message identifying what condition is checked
* @throws AssertionError if {@code cond} is false
*/
public static void assertTrue(boolean cond, String errorMessage) {
if (!cond) {
fail(errorMessage);
}
}
/**
* Abort program execution if {@code condition} is true.
*
* <p>If the checked condition is not obvious in the source code
* {@link assertTrue(boolean, String)} should be used to help identifying issues quickly.
*
* @param condition the condition which has to be false
* @throws AssertionError if {@code condition} is true
*/
public static void assertFalse(boolean condition) {
assertTrue(!condition);
}
public static void assertFalse(boolean condition, final Supplier<String> errorMessage) {
assertTrue(!condition, errorMessage);
}
public static void assertFalse(boolean condition, String errorMessage) {
assertTrue(!condition, errorMessage);
}
/** Throws {@link AssertionError} with {@code errorMessage} as error message. */
public static <T> T fail(String errorMessage) {
throw new AssertionError(errorMessage);
}
/** Abort program execution if {@code o} is {@code null}. */
public static void assertNotNull(Object o) {
if (o == null) {
fail("Given object is null but assumed to be not null.");
}
}
/** Abort program execution if {@code o} is not {@code null}. */
public static void assertNull(Object o) {
if (o != null) {
fail("Given object is not null but assumed to be null.");
}
}
public static <T> void assertEquals(T expected, T actual) {
assertEquals(expected, actual, null);
}
public static <T> void assertEquals(T expected, T actual, String message) {
String prefix = message == null ? "" : message + ": ";
if (expected == null) {
if (actual != null) {
fail(prefix + "expected is null but actually it is " + actual);
}
} else {
assertTrue(expected.equals(actual), prefix + expected + " != " + actual);
}
}
}