Skip to content

Commit

Permalink
Add TestPlatformConfigProvider and use it in maven cli tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jon Harper <jon.harper87@gmail.com>
  • Loading branch information
jonenst committed Aug 30, 2019
1 parent ca9462f commit 35ac3d7
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 2 deletions.
7 changes: 6 additions & 1 deletion commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private static ModuleConfigRepository loadModuleRepository(Path[] configDirs, St
* Loads a {@link ModuleConfigRepository} from a single directory.
* Reads from yaml file if it exists, else from xml file, else from properties file.
*/
private static ModuleConfigRepository loadModuleRepository(Path configDir, String configName) {
static ModuleConfigRepository loadModuleRepository(Path configDir, String configName) {
Path yamlConfigFile = configDir.resolve(configName + ".yml");
if (Files.exists(yamlConfigFile)) {
LOGGER.info("Platform configuration defined by YAML file {}", yamlConfigFile);
Expand Down
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;
}
}

}
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());
}
}
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<jena.version>3.0.0</jena.version>
<jgrapht.version>1.0.1</jgrapht.version>
<jimfs.version>1.1</jimfs.version>
<reflections.version>0.9.11</reflections.version>
<jjwt.version>0.9.0</jjwt.version>
<jodatime.version>2.9.7</jodatime.version>
<junit.version>4.12</junit.version>
Expand Down Expand Up @@ -221,6 +222,15 @@
<generateBackupPoms>false</generateBackupPoms>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<powsybl.config.provider>test</powsybl.config.provider>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -579,6 +589,11 @@
<artifactId>jimfs</artifactId>
<version>${jimfs.version}</version>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>${reflections.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
Expand Down Expand Up @@ -851,5 +866,18 @@
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.jimfs</groupId>
<artifactId>jimfs</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>

0 comments on commit 35ac3d7

Please sign in to comment.