-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TestPlatformConfigProvider and use it in maven cli tests
Signed-off-by: Jon Harper <jon.harper87@gmail.com>
- Loading branch information
Showing
5 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
commons/src/main/java/com/powsybl/commons/config/TestPlatformConfigProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright (c) 2019, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.commons.config; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
|
||
import org.reflections.Reflections; | ||
import org.reflections.util.ClasspathHelper; | ||
import org.reflections.vfs.Vfs; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Set; | ||
|
||
/** | ||
* Provides a PlatformConfig reading config from the classpath only | ||
* | ||
* @author Jon Harper <jon.harper at rte-france.com> | ||
*/ | ||
@AutoService(PlatformConfigProvider.class) | ||
public class TestPlatformConfigProvider implements PlatformConfigProvider { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(TestPlatformConfigProvider.class); | ||
|
||
private static final String NAME = "test"; | ||
|
||
private static final String CONFIG_NAME = "config"; | ||
static final String CONFIG_DIR = "/unittests"; | ||
|
||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
|
||
@Override | ||
public PlatformConfig getPlatformConfig() { | ||
FileSystem jimfs = Jimfs.newFileSystem(Configuration.unix()); | ||
|
||
// 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<String> resources = reflections.getResources(s -> s.startsWith(thisPackagePath)); | ||
Path cfgDir; | ||
try { | ||
cfgDir = Files.createDirectories(jimfs.getPath(CONFIG_DIR)); | ||
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)); | ||
LOGGER.info("Copying classpath resource: {} -> {}", resource, dest); | ||
Files.copy(classLoader.getResourceAsStream(resource), dest); | ||
} | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to initialize test config", e); | ||
} | ||
ModuleConfigRepository repository = StandardPlatformConfigProvider.loadModuleRepository(cfgDir, CONFIG_NAME); | ||
return new PlatformConfig(repository, cfgDir); | ||
} | ||
|
||
// This class must be named ResourceScanner because the getSimpleName is used by | ||
// reflections.getResources | ||
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; | ||
} | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
commons/src/test/java/com/powsybl/commons/config/DefaultConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.powsybl.commons.config; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class DefaultConfigTest { | ||
|
||
@Test | ||
public void test() { | ||
assertEquals(TestPlatformConfigProvider.CONFIG_DIR, | ||
PlatformConfig.defaultConfig().getConfigDir().toString()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters