Skip to content

Commit 149eae8

Browse files
committed
Update unit tests for GenericUtils
This converts the tests of now-deprecated methods from ClassUtils and ConversionUtils to test the analogous GenericUtils methods instead.
1 parent db432d8 commit 149eae8

File tree

3 files changed

+171
-110
lines changed

3 files changed

+171
-110
lines changed

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

-49
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,8 @@
4343
import java.io.InputStream;
4444
import java.io.OutputStream;
4545
import java.io.Serializable;
46-
import java.lang.reflect.Field;
47-
import java.lang.reflect.Type;
4846
import java.net.URL;
4947
import java.net.URLClassLoader;
50-
import java.util.List;
5148
import java.util.jar.JarOutputStream;
5249
import java.util.zip.ZipEntry;
5350

@@ -131,45 +128,6 @@ public void testGetArrayClass() {
131128
assertNull(ClassUtils.getArrayClass(void.class));
132129
}
133130

134-
/** Tests {@link ClassUtils#getTypes(java.lang.reflect.Field, Class)}. */
135-
@Test
136-
public void testGetTypes() {
137-
final Field field = ClassUtils.getField(Thing.class, "thing");
138-
139-
// T
140-
final Type tType = ClassUtils.getGenericType(field, Thing.class);
141-
assertEquals("capture of ?", tType.toString());
142-
143-
// N extends Number
144-
final Type nType = ClassUtils.getGenericType(field, NumberThing.class);
145-
assertEquals("capture of ?", nType.toString());
146-
147-
// Integer
148-
final Type iType = ClassUtils.getGenericType(field, IntegerThing.class);
149-
assertSame(Integer.class, iType);
150-
}
151-
152-
/** Tests {@link ClassUtils#getGenericType}. */
153-
@Test
154-
public void testGetGenericType() {
155-
final Field field = ClassUtils.getField(Thing.class, "thing");
156-
157-
// Object
158-
assertAllTheSame(ClassUtils.getTypes(field, Thing.class), Object.class);
159-
160-
// N extends Number
161-
assertAllTheSame(ClassUtils.getTypes(field, NumberThing.class),
162-
Number.class);
163-
164-
// Integer
165-
assertAllTheSame(ClassUtils.getTypes(field, IntegerThing.class),
166-
Integer.class);
167-
168-
// Serializable & Cloneable
169-
assertAllTheSame(ClassUtils.getTypes(field, ComplexThing.class),
170-
Serializable.class, Cloneable.class);
171-
}
172-
173131
@Test
174132
public void testUnpackedClass() throws IOException {
175133
final File tmpDir = createTemporaryDirectory("class-utils-test-");
@@ -227,13 +185,6 @@ private void copy(final InputStream in, final OutputStream out,
227185
if (closeOut) out.close();
228186
}
229187

230-
private <T> void assertAllTheSame(final List<T> list, T... values) {
231-
assertEquals(list.size(), values.length);
232-
for (int i = 0; i < values.length; i++) {
233-
assertSame(list.get(i), values[i]);
234-
}
235-
}
236-
237188
private void assertLoaded(final Class<?> c, final String name) {
238189
assertSame(c, ClassUtils.loadClass(name));
239190
}

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

-61
Original file line numberDiff line numberDiff line change
@@ -316,46 +316,6 @@ public void testConvertToEnum() {
316316
assertNull(notAnEnum);
317317
}
318318

319-
/** Tests {@link ConversionUtils#getClass(Type)}. */
320-
@Test
321-
public void testGetClass() {
322-
@SuppressWarnings("unused")
323-
class Struct {
324-
private int[] intArray;
325-
private double d;
326-
private String[][] strings;
327-
private Void v;
328-
private List<String> list;
329-
private HashMap<Integer, Float> map;
330-
}
331-
assertSame(int[].class, getClass(Struct.class, "intArray"));
332-
assertSame(double.class, getClass(Struct.class, "d"));
333-
assertSame(String[][].class, getClass(Struct.class, "strings"));
334-
assertSame(Void.class, getClass(Struct.class, "v"));
335-
assertSame(List.class, getClass(Struct.class, "list"));
336-
assertSame(HashMap.class, getClass(Struct.class, "map"));
337-
}
338-
339-
/** Tests {@link ConversionUtils#getComponentClass(Type)}. */
340-
@Test
341-
public void testGetComponentClass() {
342-
@SuppressWarnings("unused")
343-
class Struct {
344-
private int[] intArray;
345-
private double d;
346-
private String[][] strings;
347-
private Void v;
348-
private List<String>[] list;
349-
private HashMap<Integer, Float> map;
350-
}
351-
assertSame(int.class, getComponentClass(Struct.class, "intArray"));
352-
assertNull(getComponentClass(Struct.class, "d"));
353-
assertSame(String[].class, getComponentClass(Struct.class, "strings"));
354-
assertSame(null, getComponentClass(Struct.class, "v"));
355-
assertSame(List.class, getComponentClass(Struct.class, "list"));
356-
assertSame(null, getComponentClass(Struct.class, "map"));
357-
}
358-
359319
/** Tests {@link ConversionUtils#getNonprimitiveType(Class)}. */
360320
@Test
361321
public void testGetNonprimitiveType() {
@@ -763,27 +723,6 @@ private <T> List<T> getValueList(final T... values) {
763723
return list;
764724
}
765725

766-
/** Convenience method to get the {@link Type} of a field. */
767-
private Type type(final Class<?> c, final String fieldName) {
768-
return ClassUtils.getField(c, fieldName).getGenericType();
769-
}
770-
771-
/**
772-
* Convenience method to call {@link ConversionUtils#getClass(Type)} on a
773-
* field.
774-
*/
775-
private Class<?> getClass(final Class<?> c, final String fieldName) {
776-
return ConversionUtils.getClass(type(c, fieldName));
777-
}
778-
779-
/**
780-
* Convenience method to call {@link ConversionUtils#getComponentClass(Type)}
781-
* on a field.
782-
*/
783-
private Class<?> getComponentClass(final Class<?> c, final String fieldName) {
784-
return ConversionUtils.getComponentClass(type(c, fieldName));
785-
}
786-
787726
// -- Helper Classes --
788727

789728
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
* #L%
30+
*/
31+
32+
package org.scijava.util;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNull;
36+
import static org.junit.Assert.assertSame;
37+
38+
import java.io.Serializable;
39+
import java.lang.reflect.Field;
40+
import java.lang.reflect.Type;
41+
import java.util.HashMap;
42+
import java.util.List;
43+
44+
import org.junit.Test;
45+
import org.scijava.util.ClassUtilsTest.ComplexThing;
46+
import org.scijava.util.ClassUtilsTest.IntegerThing;
47+
import org.scijava.util.ClassUtilsTest.NumberThing;
48+
import org.scijava.util.ClassUtilsTest.Thing;
49+
50+
/**
51+
* Tests {@link GenericUtils}.
52+
*
53+
* @author Mark Hiner
54+
* @author Curtis Rueden
55+
*/
56+
public class GenericUtilsTest {
57+
58+
/** Tests {@link GenericUtils#getClass(Type)}. */
59+
@Test
60+
public void testGetClass() {
61+
@SuppressWarnings("unused")
62+
class Struct {
63+
64+
private int[] intArray;
65+
private double d;
66+
private String[][] strings;
67+
private Void v;
68+
private List<String> list;
69+
private HashMap<Integer, Float> map;
70+
}
71+
assertSame(int[].class, getClass(Struct.class, "intArray"));
72+
assertSame(double.class, getClass(Struct.class, "d"));
73+
assertSame(String[][].class, getClass(Struct.class, "strings"));
74+
assertSame(Void.class, getClass(Struct.class, "v"));
75+
assertSame(List.class, getClass(Struct.class, "list"));
76+
assertSame(HashMap.class, getClass(Struct.class, "map"));
77+
}
78+
79+
/** Tests {@link GenericUtils#getComponentClass(Type)}. */
80+
@Test
81+
public void testGetComponentClass() {
82+
@SuppressWarnings("unused")
83+
class Struct {
84+
85+
private int[] intArray;
86+
private double d;
87+
private String[][] strings;
88+
private Void v;
89+
private List<String>[] list;
90+
private HashMap<Integer, Float> map;
91+
}
92+
assertSame(int.class, getComponentClass(Struct.class, "intArray"));
93+
assertNull(getComponentClass(Struct.class, "d"));
94+
assertSame(String[].class, getComponentClass(Struct.class, "strings"));
95+
assertSame(null, getComponentClass(Struct.class, "v"));
96+
assertSame(List.class, getComponentClass(Struct.class, "list"));
97+
assertSame(null, getComponentClass(Struct.class, "map"));
98+
}
99+
100+
/**
101+
* Tests {@link GenericUtils#getFieldClasses(java.lang.reflect.Field, Class)}.
102+
*/
103+
@Test
104+
public void testGetFieldClasses() {
105+
final Field field = ClassUtils.getField(Thing.class, "thing");
106+
107+
// T
108+
final Type tType = GenericUtils.getFieldType(field, Thing.class);
109+
assertEquals("capture of ?", tType.toString());
110+
111+
// N extends Number
112+
final Type nType = GenericUtils.getFieldType(field, NumberThing.class);
113+
assertEquals("capture of ?", nType.toString());
114+
115+
// Integer
116+
final Type iType = GenericUtils.getFieldType(field, IntegerThing.class);
117+
assertSame(Integer.class, iType);
118+
}
119+
120+
/** Tests {@link GenericUtils#getFieldClasses(Field, Class)}. */
121+
@Test
122+
public void testGetGenericType() {
123+
final Field field = ClassUtils.getField(Thing.class, "thing");
124+
125+
// Object
126+
assertAllTheSame(GenericUtils.getFieldClasses(field, Thing.class),
127+
Object.class);
128+
129+
// N extends Number
130+
assertAllTheSame(GenericUtils.getFieldClasses(field, NumberThing.class),
131+
Number.class);
132+
133+
// Integer
134+
assertAllTheSame(GenericUtils.getFieldClasses(field, IntegerThing.class),
135+
Integer.class);
136+
137+
// Serializable & Cloneable
138+
assertAllTheSame(GenericUtils.getFieldClasses(field, ComplexThing.class),
139+
Serializable.class, Cloneable.class);
140+
}
141+
142+
// -- Helper methods --
143+
144+
/** Convenience method to get the {@link Type} of a field. */
145+
private Type type(final Class<?> c, final String fieldName) {
146+
return ClassUtils.getField(c, fieldName).getGenericType();
147+
}
148+
149+
/**
150+
* Convenience method to call {@link GenericUtils#getClass(Type)} on a field.
151+
*/
152+
private Class<?> getClass(final Class<?> c, final String fieldName) {
153+
return GenericUtils.getClass(type(c, fieldName));
154+
}
155+
156+
/**
157+
* Convenience method to call {@link GenericUtils#getComponentClass(Type)} on
158+
* a field.
159+
*/
160+
private Class<?> getComponentClass(final Class<?> c, final String fieldName) {
161+
return GenericUtils.getComponentClass(type(c, fieldName));
162+
}
163+
164+
private <T> void assertAllTheSame(final List<T> list, final T... values) {
165+
assertEquals(list.size(), values.length);
166+
for (int i = 0; i < values.length; i++) {
167+
assertSame(list.get(i), values[i]);
168+
}
169+
}
170+
171+
}

0 commit comments

Comments
 (0)