/
Assert.java
171 lines (144 loc) · 6.02 KB
/
Assert.java
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.baidu.hugegraph.testutil;
import java.util.concurrent.CompletableFuture;
import java.util.function.Consumer;
import java.util.function.Function;
import org.hamcrest.BaseMatcher;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Description;
public class Assert extends org.junit.Assert {
@FunctionalInterface
public interface ThrowableRunnable {
void run() throws Throwable;
}
@FunctionalInterface
public interface ThrowableConsumer<T> {
void accept(T t) throws Throwable;
}
public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable) {
CompletableFuture<?> future = assertThrowsFuture(throwable, runnable);
future.thenAccept(e -> {
System.err.println(e);
});
}
public static void assertThrows(Class<? extends Throwable> throwable,
ThrowableRunnable runnable,
Consumer<Throwable> exceptionConsumer) {
CompletableFuture<Throwable> future = assertThrowsFuture(throwable,
runnable);
future.thenAccept(exceptionConsumer);
}
public static CompletableFuture<Throwable> assertThrowsFuture(
Class<? extends Throwable> clazz,
ThrowableRunnable runnable) {
CompletableFuture<Throwable> future = new CompletableFuture<>();
boolean fail = false;
try {
runnable.run();
fail = true;
} catch (Throwable e) {
if (!clazz.isInstance(e)) {
Assert.fail(String.format(
"Bad exception type %s(expected %s)",
e.getClass().getName(), clazz.getName()));
}
future.complete(e);
}
if (fail) {
String msg = String.format("No exception was thrown(expected %s)",
clazz.getName());
future.completeExceptionally(new AssertionError(msg));
Assert.fail(msg);
}
return future;
}
public static void assertEquals(byte expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(short expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(char expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(int expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(long expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(float expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertEquals(double expected, Object actual) {
org.junit.Assert.assertEquals(expected, actual);
}
public static void assertGt(Number expected, Object actual) {
org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> {
return cmp > 0;
}, ">"));
}
public static void assertGte(Number expected, Object actual) {
org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> {
return cmp >= 0;
}, ">="));
}
public static void assertLt(Number expected, Object actual) {
org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> {
return cmp < 0;
}, "<"));
}
public static void assertLte(Number expected, Object actual) {
org.junit.Assert.assertThat(actual, new NumberMatcher(expected, cmp -> {
return cmp <= 0;
}, "<="));
}
public static void assertContains(String sub, String actual) {
org.junit.Assert.assertThat(actual, CoreMatchers.containsString(sub));
}
public static void assertInstanceOf(Class<?> clazz, Object object) {
org.junit.Assert.assertThat(object, CoreMatchers.instanceOf(clazz));
}
private static class NumberMatcher extends BaseMatcher<Object> {
private final String symbol;
private final Number expected;
private final Function<Integer, Boolean> cmp;
public NumberMatcher(Number expected, Function<Integer, Boolean> cmp,
String symbol) {
this.expected = expected;
this.cmp = cmp;
this.symbol = symbol;
}
@SuppressWarnings("unchecked")
@Override
public boolean matches(Object actual) {
Assert.assertInstanceOf(this.expected.getClass(), actual);
Assert.assertInstanceOf(Comparable.class, actual);
int cmp = ((Comparable<Number>) actual).compareTo(this.expected);
return this.cmp.apply(cmp);
}
@Override
public void describeTo(Description desc) {
desc.appendText("a number ").appendText(this.symbol)
.appendText(" ").appendText(this.expected.toString());
}
}
}