Skip to content

Commit 6a82017

Browse files
committed
Add unit tests for ClassUtils field type methods
1 parent 6dd249a commit 6a82017

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

src/test/java/org/scijava/util/ClassUtilsTest.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@
4141
import java.io.IOException;
4242
import java.io.InputStream;
4343
import java.io.OutputStream;
44+
import java.io.Serializable;
45+
import java.lang.reflect.Field;
46+
import java.lang.reflect.Type;
4447
import java.net.URL;
4548
import java.net.URLClassLoader;
49+
import java.util.List;
4650
import java.util.jar.JarOutputStream;
4751
import java.util.zip.ZipEntry;
4852

@@ -126,6 +130,51 @@ public void testGetArrayClass() {
126130
assertNull(ClassUtils.getArrayClass(void.class));
127131
}
128132

133+
/** Tests {@link ClassUtils#getTypes(java.lang.reflect.Field, Class)}. */
134+
@Test
135+
public void testGetTypes() {
136+
final Field field = ClassUtils.getField(Thing.class, "thing");
137+
138+
// T
139+
final Type tType = ClassUtils.getGenericType(field, Thing.class);
140+
assertEquals("capture of ?", tType.toString());
141+
142+
// N extends Number
143+
final Type nType = ClassUtils.getGenericType(field, NumberThing.class);
144+
assertEquals("capture of ?", nType.toString());
145+
146+
// Integer
147+
final Type iType = ClassUtils.getGenericType(field, IntegerThing.class);
148+
assertSame(Integer.class, iType);
149+
}
150+
151+
/** Tests {@link ClassUtils#getGenericType}. */
152+
@Test
153+
public void testGetGenericType() {
154+
final Field field = ClassUtils.getField(Thing.class, "thing");
155+
156+
// Object
157+
final List<Class<?>> tTypes = ClassUtils.getTypes(field, Thing.class);
158+
assertEquals(1, tTypes.size());
159+
assertSame(Object.class, tTypes.get(0));
160+
161+
// N extends Number
162+
final List<Class<?>> nTypes = ClassUtils.getTypes(field, NumberThing.class);
163+
assertEquals(1, nTypes.size());
164+
assertEquals(Number.class, nTypes.get(0));
165+
166+
// Integer
167+
final List<Class<?>> iTypes = ClassUtils.getTypes(field, IntegerThing.class);
168+
assertEquals(1, iTypes.size());
169+
assertSame(Integer.class, iTypes.get(0));
170+
171+
// Serializable & Cloneable
172+
final List<Class<?>> cTypes = ClassUtils.getTypes(field, ComplexThing.class);
173+
assertEquals(2, cTypes.size());
174+
assertSame(Serializable.class, cTypes.get(0));
175+
assertSame(Cloneable.class, cTypes.get(1));
176+
}
177+
129178
@Test
130179
public void testUnpackedClass() throws IOException {
131180
final File tmpDir = FileUtils.createTemporaryDirectory("class-utils-test", "");
@@ -184,4 +233,24 @@ private void assertLoaded(final Class<?> c, final String name) {
184233
assertSame(c, ClassUtils.loadClass(name));
185234
}
186235

236+
// -- Helper classes --
237+
238+
public static class Thing<T> {
239+
public T thing;
240+
}
241+
242+
public static class NumberThing<N extends Number> extends Thing<N> {
243+
// NB: No implementation needed.
244+
}
245+
246+
public static class IntegerThing extends NumberThing<Integer> {
247+
// NB: No implementation needed.
248+
}
249+
250+
public static class ComplexThing<T extends Serializable & Cloneable> extends
251+
Thing<T>
252+
{
253+
// NB: No implementation needed.
254+
}
255+
187256
}

0 commit comments

Comments
 (0)