1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package io.earcam.instrumental.reflect;
20
21 import static io.earcam.instrumental.reflect.Types.isClass;
22 import static io.earcam.instrumental.reflect.Types.isInterface;
23 import static io.earcam.instrumental.reflect.Types.requireClass;
24 import static io.earcam.instrumental.reflect.Types.requireInterface;
25 import static org.hamcrest.Matchers.*;
26 import static org.junit.jupiter.api.Assertions.*;
27 import static org.hamcrest.MatcherAssert.assertThat;
28
29 import java.io.InputStream;
30 import java.io.Serializable;
31 import java.lang.reflect.Type;
32 import java.math.BigDecimal;
33 import java.util.Formattable;
34 import java.util.jar.JarInputStream;
35 import java.util.zip.ZipInputStream;
36
37 import org.junit.jupiter.api.Test;
38
39 import io.earcam.instrumental.reflect.Types;
40
41 public class TypesTest {
42
43 @Test
44 public void serializableIsAnInterface()
45 {
46 assertThat(isInterface(Serializable.class), is(true));
47 }
48
49
50 @Test
51 public void overrideAnnotationIsNotAnInterface()
52 {
53 assertThat(isInterface(Override.class), is(false));
54 }
55
56
57 @Test
58 public void stringIsNotAnInterface()
59 {
60 assertThat(isInterface(String.class), is(false));
61 assertThat(isInterface(String.class.getCanonicalName()), is(false));
62 }
63
64
65 @Test
66 public void thisTestIsAClass()
67 {
68 assertThat(isClass(TypesTest.class), is(true));
69 assertThat(isClass(TypesTest.class.getCanonicalName()), is(true));
70 }
71
72
73 @Test
74 public void thisObjectIsAClass()
75 {
76 assertThat(isClass(Object.class), is(true));
77 }
78
79
80 @Test
81 public void serializableIsNotAClass()
82 {
83 assertThat(isClass(Serializable.class), is(false));
84 assertThat(isClass(Serializable.class.getCanonicalName()), is(false));
85 }
86
87
88 @Test
89 public void overrideAnnotationIsNotAClass()
90 {
91 assertThat(isClass(Override.class), is(false));
92 assertThat(isClass(Override.class.getCanonicalName()), is(false));
93 }
94
95
96 @Test
97 public void throwsWhenRequireClassReceivesAnInterface()
98 {
99
100 try {
101 requireClass(Comparable.class);
102 fail();
103 } catch(IllegalArgumentException e) {}
104
105 }
106
107
108 @Test
109 public void throwsWhenRequireInterfaceReceivesAClass()
110 {
111 try {
112 requireInterface(BigDecimal.class);
113 fail();
114 } catch(IllegalArgumentException e) {}
115 }
116
117
118 @Test
119 public void doesNotThrowsWhenRequireClassReceivesAClass()
120 {
121 requireClass(System.class);
122 }
123
124
125 @Test
126 public void doesNotThrowsWhenRequireInterfaceReceivesAnInterface()
127 {
128 requireInterface(Formattable.class);
129 }
130
131
132 @Test
133 public void loadsClassFromSimpleType()
134 {
135 Type type = new Type() {
136 @Override
137 public String getTypeName()
138 {
139 return BigDecimal.class.getCanonicalName();
140 }
141 };
142 assertThat(Types.getClass(type), is(equalTo(BigDecimal.class)));
143 }
144
145
146 @Test
147 public void isPrimitiveFromString()
148 {
149 assertThat(Types.isPrimitive(long.class.getCanonicalName()), is(true));
150 }
151
152
153 @Test
154 public void isPrimitiveFromType()
155 {
156 assertThat(Types.isPrimitive(long.class), is(true));
157 }
158
159
160 @Test
161 public void isPrimitiveArrayFromType()
162 {
163
164 assertThat(Types.isPrimitiveArray(long[].class), is(true));
165 }
166
167
168 @Test
169 public void isNotPrimitiveArrayFromType()
170 {
171 assertThat(Types.isPrimitiveArray(Long[].class), is(false));
172 }
173
174
175 @Test
176 public void isPrimitiveWrapper()
177 {
178 assertThat(Types.isPrimitiveWrapper(Double.class), is(true));
179 }
180
181
182 @Test
183 public void isNotPrimitiveWrapper()
184 {
185 assertThat(Types.isPrimitiveWrapper(Number.class), is(false));
186 }
187
188
189 @Test
190 public void wrapperToPrimitive()
191 {
192 assertThat(Types.wrapperToPrimitive(Float.class), is(float.class));
193 }
194
195
196 @Test
197 public void primitiveToWrapper()
198 {
199 assertThat(Types.primitiveToWrapper(short.class), is(Short.class));
200 }
201
202
203 @Test
204 void implementsAll()
205 {
206
207 assertThat(Types.implementsAll(String.class,
208 Serializable.class,
209 Comparable.class,
210 CharSequence.class), is(true));
211
212 }
213
214
215 @Test
216 void implementsSome()
217 {
218 assertThat(Types.implementsAll(String.class, Serializable.class, Comparable.class), is(true));
219 }
220
221
222 @Test
223 void doesNotImplementsAll()
224 {
225 assertThat(Types.implementsAll(String.class, Serializable.class, Comparable.class, CharSequence.class, InputStream.class), is(false));
226 }
227
228
229 @Test
230 void extendsFromImmediately()
231 {
232 assertThat(Types.extendsFrom(JarInputStream.class, ZipInputStream.class), is(true));
233 }
234
235
236 @Test
237 void extendsFromEventually()
238 {
239 assertThat(Types.extendsFrom(JarInputStream.class, InputStream.class), is(true));
240 }
241
242
243 @Test
244 void extendsFromUltimately()
245 {
246 assertThat(Types.extendsFrom(JarInputStream.class, Object.class), is(true));
247 }
248
249
250 @Test
251 void doesNotExtendFrom()
252 {
253 assertThat(Types.extendsFrom(JarInputStream.class, String.class), is(false));
254 }
255
256
257 @Test
258 void doesNotImplementsAny()
259 {
260 assertThat(Types.implementsAll(TypesTest.class, Serializable.class, Comparable.class), is(false));
261 }
262 }