|
| 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