branch feature/jdk11 created (now 5494472)
This is an automated email from the git hooks/post-receive script. New change to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git at 5494472 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader This branch includes the following new commits: new 5494472 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "adds" were already present in the repository and have only been added to this reference. Detailed log of new commits: commit 549447247668419dfaf3f3f1a78a8d2f4cb1935f Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 14:33:04 2020 +0100 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
This is an automated email from the git hooks/post-receive script. New commit to branch feature/jdk11 in repository nuiton-utils. See https://gitlab.nuiton.org/nuiton/nuiton-utils.git commit 549447247668419dfaf3f3f1a78a8d2f4cb1935f Author: Arnaud Thimel <thimel@codelutin.com> Date: Mon Jan 6 14:33:04 2020 +0100 Remove obsolete methods : Resource#addDefaultClassLoader and Resource#addClassLoader --- src/main/java/org/nuiton/util/Resource.java | 34 -------- src/test/java/org/nuiton/util/ResourceTest.java | 102 +----------------------- 2 files changed, 2 insertions(+), 134 deletions(-) diff --git a/src/main/java/org/nuiton/util/Resource.java b/src/main/java/org/nuiton/util/Resource.java index ca7d6ab..5c9c078 100644 --- a/src/main/java/org/nuiton/util/Resource.java +++ b/src/main/java/org/nuiton/util/Resource.java @@ -30,7 +30,6 @@ import javax.swing.ImageIcon; import java.io.File; import java.io.FileInputStream; import java.io.IOException; -import java.lang.reflect.Method; import java.net.JarURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; @@ -74,39 +73,6 @@ public class Resource { // Resource } - /** - * Permet d'ajouter dans le classloader par defaut une nouvelle URL dans - * lequel il faut rechercher les fichiers. - * - * @param url l'url a ajouter - */ - public static void addDefaultClassLoader(URL url) { - ClassLoader classLoader = ClassLoader.getSystemClassLoader(); - addClassLoader(classLoader, url); - } - - /** - * Permet d'ajouter dans un classloader une nouvelle URL dans - * lequel il faut rechercher les fichiers. - * - * @param classLoader le classloader a modifier - * @param url l'url a ajouter - */ - public static void addClassLoader(ClassLoader classLoader, URL url) { - if (classLoader instanceof URLClassLoader) { - try { - Method method = URLClassLoader.class.getDeclaredMethod("addURL", - new Class[]{URL.class}); - method.setAccessible(true); - method.invoke(classLoader, url); - } catch (Exception eee) { - throw new RuntimeException(t("nuitonutil.error.add.url.in.classloader", classLoader, eee)); - } - } else { - throw new UnsupportedOperationException(Resource.class.getName() + " is not compatible with Java 9+ so far. See https://gitlab.nuiton.org/nuiton/nuiton-utils/issues/319"); - } - } - /** * Recherche la ressource nom. * diff --git a/src/test/java/org/nuiton/util/ResourceTest.java b/src/test/java/org/nuiton/util/ResourceTest.java index e047949..76d6353 100644 --- a/src/test/java/org/nuiton/util/ResourceTest.java +++ b/src/test/java/org/nuiton/util/ResourceTest.java @@ -25,22 +25,16 @@ package org.nuiton.util; import org.apache.commons.lang3.SystemUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.junit.After; import org.junit.Assert; import org.junit.Assume; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TestName; import java.io.File; import java.io.IOException; -import java.lang.reflect.Field; import java.net.URL; -import java.net.URLClassLoader; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; /** @@ -54,77 +48,6 @@ public class ResourceTest { // ResourceTest /** Logger. */ private static final Log log = LogFactory.getLog(ResourceTest.class); - // unaltered classloader - static ClassLoader systemClassLoader; - - @BeforeClass - public static void beforeClass() { - systemClassLoader = ClassLoader.getSystemClassLoader(); - } - - @Before - public void beforeTest() throws NoSuchFieldException, IllegalAccessException { - String errorMessage = "Test is disabled because " + Resource.class.getName() + " is not compatible with Java 9+ so far. See https://gitlab.nuiton.org/nuiton/nuiton-utils/issues/319"; - boolean testEnabled = systemClassLoader instanceof URLClassLoader; - if (!testEnabled && log.isErrorEnabled()) { - log.error(errorMessage); - } - Assume.assumeTrue(errorMessage, testEnabled); - URLClassLoader classLoader = (URLClassLoader) systemClassLoader; - ClassLoader parent = classLoader.getParent(); - ClassLoader currentClassLoader = new URLClassLoader(classLoader.getURLs(), parent); - Field field = ClassLoader.class.getDeclaredField("scl"); - field.setAccessible(true); - field.set(null, currentClassLoader); - } - - @After - public void afterTest() throws NoSuchFieldException, IllegalAccessException { - Field field = ClassLoader.class.getDeclaredField("scl"); - field.setAccessible(true); - field.set(null, systemClassLoader); - } - - @Test - public void testAddDefaultClassLoader() throws Exception { - String javaExecFilename = getJavaExecName(); - - Assert.assertNull(ResourceTest.class.getResource("/bin/" + javaExecFilename)); - - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - - //FIXME-tchemit-2012-07-23 On windows os java does not eixsts but java.exe does - File result = new File(repository, "bin" + File.separator + javaExecFilename); - - Assert.assertTrue(result.exists()); - - URL resultURL = Resource.getURL("bin/" + javaExecFilename); - Assert.assertEquals(result.toURI().toURL(), resultURL); - } - - @Test - public void testGetURL() throws Exception { - URL url; - - String javaExecFilename = getJavaExecName(); - - url = Resource.getURL("README.md"); - Assert.assertNotNull(url); - - try { - Resource.getURL("bin/" + javaExecFilename); - Assert.fail(); - } catch (ResourceNotFoundException e) { - Assert.assertTrue(true); - } - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - url = Resource.getURL("bin/" + javaExecFilename); - Assert.assertNotNull(url); - - } - @Test public void testGetURLsFromDirectory() throws Exception { @@ -147,37 +70,16 @@ public class ResourceTest { // ResourceTest } @Test - public void testGetURLsFromJar() throws Exception { + public void testGetURLsFromJarBeforeJava9() throws Exception { File repository = new File(System.getProperty("java.home")); File file = new File(repository, "lib" + File.separator + "rt.jar"); + Assume.assumeTrue(file.exists()); List<URL> result = Resource.getURLsFromJar(file, ".*OutOfMemoryError.*"); Assert.assertNotNull(result); Assert.assertEquals(1, result.size()); } - @Test - public void testGetURLs() throws Exception { - if (log.isInfoEnabled()) { - log.info(Arrays.asList(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs())); - } - - String javaExecFilename = getJavaExecName(); - String fileSeparatorRegex = StringUtil.getFileSeparatorRegex(); - File repository = new File(System.getProperty("java.home")); - Resource.addDefaultClassLoader(repository.toURI().toURL()); - List<URL> result; - - // comes - result = Resource.getURLs(".*bin" + fileSeparatorRegex + javaExecFilename); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - - result = Resource.getURLs("META-INF/MANIFEST.MF"); - Assert.assertNotNull(result); - Assert.assertEquals(1, result.size()); - } - @Test public void testIsJar() throws Exception { Assert.assertTrue(Resource.isJar("toto.jar")); -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.
participants (1)
-
nuiton.org scm