ClassUtils { @Deprecated static public URL getResourceURL(Object obj, String resource) { return getResourceURL(obj.getClass(), resource); }  static Constructor getConstructorOnType(Class clas, Class[] parameterTypes); @Deprecated static URL getResourceURL(Object obj, String resource); @Deprecated static URL getResourceURL(Class clazz, String resource); static URL getResourceURL(ClassLoader classLoader, String resource); static URL getResourceURL(ClassLoader classLoader, String resource, String allowedProtocols); static InputStream urlToStream(URL url, int timeoutMs); static Reader urlToReader(URL url); static Reader urlToReader(URL url, int timeoutMs); static boolean implementsInterface(Class class1, Class iface); static boolean implementsInterface(String className, String iface); static long lastModified(Class aClass); static Class loadClass(String className); static Object newInstance(String className); static Object newInstance(String className, Class[] parameterClasses, Object[] parameterObjects); static Object newInstance(String className, Object[] parameterObjects); static String which(Class aClass); static String nameOf(ClassLoader classLoader); static String nameOf(Object o); static void invokeSetter(Object o, String name, Object value); static void invokeSetter(Object o, String name, Object value, Class clazz); static Object invokeGetterSafe(Object o, String name, boolean forceAccess); static Object invokeGetter(Object o, String name, boolean forceAccess); static Object invokeGetter(Object o, String name); static String invokeStringGetterSafe(Object o, String name); static String invokeStringGetter(Object o, String name); static Object getFieldValueSafe(Object o, String name); static Object getFieldValue(Object o, Class c, String name); static Object getFieldValue(Object o, String name); static Object getDeclaredFieldValue(Object o, Class c, String name); static Object getDeclaredFieldValue(Object o, String name); static String debugObject(Object o); static String reflectionToString(final Object o, final String fieldnameEnd); static String getCleanedFilePath(String path);  }
@Test public void getResourceURL() throws URISyntaxException, IOException { URL baseUrl = classLoader.getResource(fileName); assertEquals(baseUrl.getFile(), ClassUtils.getResourceURL(this, fileName).getFile()); }
@Test public void getResourceURLAndValidateContentsO() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(this, fileName); assertEquals(fileContent, Misc.streamToString(url.openStream()).trim()); }
@Test public void getResourceURLAndValidateContentsC1() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, fileName); assertEquals(fileContent, Misc.streamToString(url.openStream()).trim()); }
@Test public void getResourceURLfromExternalFile() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(this, fileName); String fullPath = url.toURI().toString(); URL url2 = ClassUtils.getResourceURL(classLoader, fullPath, "file"); assertEquals(url.getFile(), url2.getFile()); }
@Test public void getResourceURLfromExternalFileError() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(this, fileName); String fullPath = url.toURI().toString(); assertNull(ClassUtils.getResourceURL(classLoader, fullPath, "")); }
@Test public void getResourceURLnoFileError() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "apple.pie.with.raisins"); assertNull(url); }
@Test public void getResourceURLnoExternalFileErrorC1() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "apple.pie.with.raisins", "file"); assertNull(url); }
@Test public void getResourceURLnoExternalFileErrorC2() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "file: assertEquals("", url.getFile()); }
@Test public void getResourceURLfromHttpProtocolNotAllowed1() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNull(url); }
@Test public void getResourceURLfromHttpProtocolNotAllowed2() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNull(url); }
@Test public void getResourceURLfromHttpProtocolNotAllowed3() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNull(url); }
@Test public void getResourceURLfromHttpProtocolNotAllowed4() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNull(url); }
@Test public void getResourceURLfromHttpProtocolNotAllowed5() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNull(url); }
@Test public void getResourceURLfromHttpProtocolAllowed1() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(classLoader, "http: assertNotNull(url); }
@Test public void getResourceURLnullClassLoader1() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(nullClassLoader, fileName); assertEquals(fileContent, Misc.streamToString(url.openStream()).trim()); }
@Test public void getResourceURLnullClassLoaderNonExistingFile() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(nullClassLoader, "apple.pie.with.raisins"); assertNull(url); }
@Test public void getResourceURLnullClassLoaderWithExternalFile() throws URISyntaxException, IOException { URL url = ClassUtils.getResourceURL(nullClassLoader, "file: assertEquals("", url.getFile()); }
@Test public void localClassLoader5UrlWithFileScheme() throws IOException, ConfigurationException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); String resource="/ClassLoader/ClassLoaderTestFile"; URL url = ClassUtils.getResourceURL(cl, resource); String resourceAsFileUrl=url.toExternalForm(); assertThat(resourceAsFileUrl, startsWith("file:")); testUri(cl,resourceAsFileUrl,"-- /ClassLoader/ClassLoaderTestFile --","file"); }
@Test public void localClassLoader6UrlWithFileSchemeButNotAllowed() throws IOException, ConfigurationException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); String resource="/ClassLoader/ClassLoaderTestFile"; URL url = ClassUtils.getResourceURL(cl, resource); String resourceAsFileUrl=url.toExternalForm(); assertThat(resourceAsFileUrl, startsWith("file:")); URL actual = ClassUtils.getResourceURL(cl, resourceAsFileUrl); assertNull("file protocol was allowed but should not", actual); }
@Test public void bytesClassLoader09UrlWithFileScheme() throws IOException, ConfigurationException { ClassLoader clres = Thread.currentThread().getContextClassLoader(); String resource="/ClassLoader/fileOnlyOnLocalClassPath.txt"; URL url = ClassUtils.getResourceURL(clres, resource); String resourceAsFileUrl=url.toExternalForm(); assertThat(resourceAsFileUrl, startsWith("file:")); ClassLoader cl = getBytesClassLoader(); testUri(cl,resourceAsFileUrl,"-- /ClassLoader/fileOnlyOnLocalClassPath.txt --","file"); }
@Test public void bytesClassLoader10UrlWithFileSchemeButNotAllowed() throws IOException, ConfigurationException { ClassLoader clres = Thread.currentThread().getContextClassLoader(); String resource="/ClassLoader/fileOnlyOnLocalClassPath.xml"; URL url = ClassUtils.getResourceURL(clres, resource); String resourceAsFileUrl=url.toExternalForm(); assertThat(resourceAsFileUrl, startsWith("file:")); ClassLoader cl = getBytesClassLoader(); URL actual = ClassUtils.getResourceURL(cl, resourceAsFileUrl); assertNull("file protocol was allowed but should not", actual); }