From d6bd15c9de6180a07e5f74f611a8203f980a516e Mon Sep 17 00:00:00 2001 From: Jon Harper Date: Fri, 6 Sep 2019 16:16:20 +0200 Subject: [PATCH] TestPlatformConfigProvider, replace google reflections with a file listing config files Google Reflections doesn't work on java9+ : https://github.com/ronmamo/reflections/issues/202 Signed-off-by: Jon Harper --- config-test/pom.xml | 4 -- .../TestPlatformConfigProvider.java | 54 +++++++------------ .../com/powsybl/configtest/filelist.txt | 2 + pom.xml | 6 --- 4 files changed, 22 insertions(+), 44 deletions(-) create mode 100644 config-test/src/test/resources/com/powsybl/configtest/filelist.txt diff --git a/config-test/pom.xml b/config-test/pom.xml index 51b2f9c75d0..48cd19edcdf 100644 --- a/config-test/pom.xml +++ b/config-test/pom.xml @@ -57,10 +57,6 @@ com.google.jimfs jimfs - - org.reflections - reflections - junit junit diff --git a/config-test/src/main/java/com/powsybl/configtest/TestPlatformConfigProvider.java b/config-test/src/main/java/com/powsybl/configtest/TestPlatformConfigProvider.java index 3143c7a6336..8afe90b17f5 100644 --- a/config-test/src/main/java/com/powsybl/configtest/TestPlatformConfigProvider.java +++ b/config-test/src/main/java/com/powsybl/configtest/TestPlatformConfigProvider.java @@ -9,24 +9,24 @@ import com.google.auto.service.AutoService; import com.google.common.jimfs.Configuration; import com.google.common.jimfs.Jimfs; - import com.powsybl.commons.config.ModuleConfigRepository; import com.powsybl.commons.config.ClassicPlatformConfigProvider; import com.powsybl.commons.config.PlatformConfig; import com.powsybl.commons.config.PlatformConfigProvider; -import org.reflections.Reflections; -import org.reflections.util.ClasspathHelper; -import org.reflections.vfs.Vfs; +import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.io.InputStream; import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; import java.nio.file.FileSystem; import java.nio.file.Files; import java.nio.file.Path; -import java.util.Set; +import java.util.Collections; +import java.util.List; /** * Provides a PlatformConfig reading config from the classpath only @@ -40,6 +40,7 @@ public class TestPlatformConfigProvider implements PlatformConfigProvider { private static final String NAME = "test"; + private static final String FILELIST_PATH = "filelist.txt"; //Using a static for the FileSystem to show that it is a singleton //and won't be closed until the jvm is shut down. private static final FileSystem JIMFS = Jimfs.newFileSystem(Configuration.unix()); @@ -55,22 +56,26 @@ public String getName() { @Override public PlatformConfig getPlatformConfig() { - // ForManifest enriches the default static and context classloaders with - // additional jars from the Class-Path attribute. - // Needed for maven surefire in manifest-only jar mode (the default) but doesn't - // hurt for other cases. Not sure why it is not the default. - Reflections reflections = new Reflections(ClasspathHelper.forManifest(), new ResourcesScanner()); - String thisPackagePath = TestPlatformConfigProvider.class.getPackage().getName().replace('.', '/'); - Set resources = reflections.getResources(s -> s.startsWith(thisPackagePath)); + InputStream resourceList = TestPlatformConfigProvider.class.getResourceAsStream(FILELIST_PATH); + List resources; + if (resourceList != null) { + try { + resources = IOUtils.readLines(resourceList, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } else { + resources = Collections.emptyList(); + } + Path cfgDir; try { cfgDir = Files.createDirectories(JIMFS.getPath(CONFIG_DIR).toAbsolutePath()); - ClassLoader classLoader = TestPlatformConfigProvider.class.getClassLoader(); for (String resource : resources) { // The resources have relative paths (no leading slash) with full package path. - Path dest = cfgDir.resolve(resource.substring(resource.lastIndexOf('/') + 1)); + Path dest = cfgDir.resolve(resource); LOGGER.info("Copying classpath resource: {} -> {}", resource, dest); - Files.copy(classLoader.getResourceAsStream(resource), dest); + Files.copy(TestPlatformConfigProvider.class.getResourceAsStream(resource), dest); } } catch (IOException e) { throw new UncheckedIOException("Failed to initialize test config", e); @@ -79,23 +84,4 @@ public PlatformConfig getPlatformConfig() { return new PlatformConfig(repository, cfgDir); } - // This class must be named ResourceScanner because the getSimpleName is used by - // reflections.getResources - @SuppressWarnings("squid:S2176") - private static class ResourcesScanner extends org.reflections.scanners.ResourcesScanner { - - // To have the full path in the key to filter by directory. - @Override - public Object scan(Vfs.File file, Object classObject) { - getStore().put(file.getRelativePath(), file.getRelativePath()); - return classObject; - } - - // To fix https://github.com/ronmamo/reflections/issues/102 - @Override - public boolean acceptResult(final String fqn) { - return false; - } - } - } diff --git a/config-test/src/test/resources/com/powsybl/configtest/filelist.txt b/config-test/src/test/resources/com/powsybl/configtest/filelist.txt new file mode 100644 index 00000000000..ce6ba0ebbe4 --- /dev/null +++ b/config-test/src/test/resources/com/powsybl/configtest/filelist.txt @@ -0,0 +1,2 @@ +config.yml +other.txt diff --git a/pom.xml b/pom.xml index 0d4e7e7d102..e1be463ac75 100644 --- a/pom.xml +++ b/pom.xml @@ -141,7 +141,6 @@ 3.0.0 1.0.1 1.1 - 0.9.11 0.9.0 2.9.7 4.12 @@ -589,11 +588,6 @@ jimfs ${jimfs.version} - - org.reflections - reflections - ${reflections.version} - com.google.protobuf protobuf-java