assertLoaded(boolean.class, "boolean"); assertLoaded(byte.class, "byte"); assertLoaded(char.class, "char"); assertLoaded(double.class, "double"); assertLoaded(float.class, "float"); assertLoaded(int.class, "int"); assertLoaded(long.class, "long"); assertLoaded(short.class, "short"); assertLoaded(void.class, "void"); assertLoaded(String.class, "string"); assertLoaded(Number.class, "java.lang.Number"); assertLoaded(boolean[].class, "boolean[]"); assertLoaded(byte[].class, "byte[]"); assertLoaded(char[].class, "char[]"); assertLoaded(double[].class, "double[]"); assertLoaded(float[].class, "float[]"); assertLoaded(int[].class, "int[]"); assertLoaded(long[].class, "long[]"); assertLoaded(short[].class, "short[]"); assertLoaded(null, "void[]"); assertLoaded(String[].class, "string[]"); assertLoaded(Number[].class, "java.lang.Number[]"); assertLoaded(boolean[][].class, "boolean[][]"); assertLoaded(byte[][].class, "byte[][]"); assertLoaded(char[][].class, "char[][]"); assertLoaded(double[][].class, "double[][]"); assertLoaded(float[][].class, "float[][]"); assertLoaded(int[][].class, "int[][]"); assertLoaded(long[][].class, "long[][]");
/** Tests {@link Types#component(Type)}. */ @Test public void testComponent() { @SuppressWarnings("unused") class Struct { private int[] intArray; private double d; private String[][] strings; private Void v; private List<String>[] list; private HashMap<Integer, Float> map; } assertSame(int.class, componentType(Struct.class, "intArray")); assertNull(componentType(Struct.class, "d")); assertSame(String[].class, componentType(Struct.class, "strings")); assertSame(null, componentType(Struct.class, "v")); assertSame(List.class, componentType(Struct.class, "list")); assertSame(null, componentType(Struct.class, "map")); }
/** Tests {@link Types#location} with a class in a JAR file. */ @Test public void testLocationClassInJar() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final File jar = new File(tmpDir, "test.jar"); final JarOutputStream out = new JarOutputStream(new FileOutputStream(jar)); final String path = getClass().getName().replace('.', '/') + ".class"; out.putNextEntry(new ZipEntry(path)); copy(getClass().getResource("/" + path).openStream(), out, true); final ClassLoader classLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(jar, FileUtils.urlToFile(location)); jar.deleteOnExit(); }
/** Convenience method to call {@link Types#raw} on a field. */ private Class<?> raw(final Class<?> c, final String fieldName) { return Types.raw(type(c, fieldName)); }
/** Tests exceptions from {@link Types#location(Class, boolean)}. */ @Test(expected = IllegalArgumentException.class) public void testLocationFailureLoud() { final Class<?> weirdClass = loadCustomClass(); assertEquals("Hello", weirdClass.getName()); Types.location(weirdClass, false); }
/** Tests {@link Types#raw(Type)}. */ @Test public void testRaw() { @SuppressWarnings("unused") class Struct { private int[] intArray; private double d; private String[][] strings; private Void v; private List<String> list; private HashMap<Integer, Float> map; } assertSame(int[].class, raw(Struct.class, "intArray")); assertSame(double.class, raw(Struct.class, "d")); assertSame(String[][].class, raw(Struct.class, "strings")); assertSame(Void.class, raw(Struct.class, "v")); assertSame(List.class, raw(Struct.class, "list")); assertSame(HashMap.class, raw(Struct.class, "map")); }
/** Tests {@link Types#raws}. */ @Test public void testRaws() { final Field field = Types.field(Thing.class, "thing"); // Object assertAllTheSame(Types.raws(Types.fieldType(field, Thing.class)), Object.class); // N extends Number assertAllTheSame(Types.raws(Types.fieldType(field, NumberThing.class)), Number.class); // Integer assertAllTheSame(Types.raws(Types.fieldType(field, IntegerThing.class)), Integer.class); // Serializable & Cloneable assertAllTheSame(Types.raws(Types.fieldType(field, ComplexThing.class)), Serializable.class, Cloneable.class); }
/** Convenience method to call {@link Types#component} on a field. */ private Class<?> componentType(final Class<?> c, final String fieldName) { return Types.raw(Types.component(type(c, fieldName))); }
/** Tests quiet behavior of {@link Types#location(Class, boolean)}. */ @Test public void testLocationFailureQuiet() { final Class<?> weirdClass = loadCustomClass(); assertEquals("Hello", weirdClass.getName()); assertNull(Types.location(weirdClass)); }
/** Tests {@link Types#param}. */ @Test public void testParam() { class Struct { @SuppressWarnings("unused") private List<int[]> list; } final Type listType = type(Struct.class, "list"); final Type paramType = Types.param(listType, List.class, 0); final Class<?> paramClass = Types.raw(paramType); assertSame(int[].class, paramClass); }
/** Tests {@link Types#location} with a class on the file system. */ @Test public void testLocationUnpackedClass() throws IOException { final File tmpDir = createTemporaryDirectory("class-utils-test-"); final String path = getClass().getName().replace('.', '/') + ".class"; final File classFile = new File(tmpDir, path); assertTrue(classFile.getParentFile().exists() || classFile.getParentFile().mkdirs()); copy(getClass().getResource("/" + path).openStream(), new FileOutputStream(classFile), true); final ClassLoader classLoader = new URLClassLoader(new URL[] { tmpDir.toURI().toURL() }, null); final Class<?> c = Types.load(getClass().getName(), classLoader); final URL location = Types.location(c); assertEquals(tmpDir, FileUtils.urlToFile(location)); FileUtils.deleteRecursively(tmpDir); }
/** Tests {@link Types#name}. */ public void testName() { @SuppressWarnings("unused") class Struct { private List<String> list; } assertEquals("boolean", Types.name(boolean.class)); assertEquals("java.lang.String", Types.name(String.class)); assertEquals("List<String>[]", Types.name(type(Struct.class, "list"))); }