Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use environment variable VERTX_MODS if set. #21

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 44 additions & 4 deletions src/main/java/org/vertx/testtools/JavaClassRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,30 @@ public class JavaClassRunner extends BlockJUnit4ClassRunner {
public JavaClassRunner(Class<?> klass) throws InitializationError {
super(klass);
setTestProperties();
mgr = PlatformLocator.factory.createPlatformManager();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(createClassLoader());
try {
mgr = PlatformLocator.factory.createPlatformManager();
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}

private void setTestProperties() {
// We set the properties here, rather than letting the build script do it
// This means tests can run directly in an IDE with the correct properties set
// without having to create custom test configurations
String modsDir = null;
String modsDir = System.getenv("VERTX_MODS");
File propsFile = new File("vertx.properties");
if (propsFile.exists()) {
loadProps(propsFile);
} else {
propsFile = new File("gradle.properties");
if (propsFile.exists()) {
loadProps(propsFile);
modsDir = "build/mods";
if (modsDir == null) {
modsDir = "build/mods";
}
} else {
File pom = new File("pom.xml");
if (pom.exists()) {
Expand All @@ -105,7 +113,9 @@ private void setTestProperties() {
} catch (FileNotFoundException e) {
//Ignore
}
modsDir = "target/mods";
if (modsDir == null) {
modsDir = "target/mods";
}
}
}
}
Expand Down Expand Up @@ -335,6 +345,36 @@ private void waitForLatch(CountDownLatch latch) {
}
}

protected ClassLoader createClassLoader() {
// We need to add some extra entries to the classpath so that any overrridden Vert.x platform config,
// e.g. cluster.xml, langs.properties etc can picked up when running the module
// Users can put such config either in a src/main/platform_lib directory (if they don't want it in the module)
// or in a src/main/resources/platform_lib directory (if they want it in the module, e.g. for fatjars)
List<URL> urls = new ArrayList<>();
try {
addURLs(urls, "src/main/platform_lib");
addURLs(urls, "src/main/resources/platform_lib");
} catch (IOException e) {
throw new RuntimeException(e);
}

return new URLClassLoader(urls.toArray(new URL[urls.size()]), getClass().getClassLoader());
}

private void addURLs(List<URL> urls, String dirName) throws IOException {
File dir = new File(dirName);
if (dir.exists()) {
urls.add(dir.getCanonicalFile().toURI().toURL());
File[] files = dir.listFiles();
if (files != null) {
for (File file: files) {
String path = file.getCanonicalPath();
if (path.endsWith(".jar") || path.endsWith(".zip")) {
urls.add(file.getCanonicalFile().toURI().toURL());
}
}
}
}
}

}