use of org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder in project mule by mulesoft.
the class ArtifactClassLoaderRunner method createClassLoaderTestRunner.
/**
* Creates the {@link ArtifactClassLoaderHolder} with the isolated class loaders.
*
* @param klass the test class being executed
* @param runnerConfiguration {@link RunnerConfiguration} based on annotated test class.
* @return creates a {@link ArtifactClassLoaderHolder} that would be used to run the test. This way the test will be isolated
* and it will behave similar as an application running in a Mule standalone container.
*/
private static synchronized ArtifactClassLoaderHolder createClassLoaderTestRunner(Class<?> klass, RunnerConfiguration runnerConfiguration) {
final File targetTestClassesFolder = new File(klass.getProtectionDomain().getCodeSource().getLocation().getPath());
ArtifactIsolatedClassLoaderBuilder builder = new ArtifactIsolatedClassLoaderBuilder();
final File rootArtifactClassesFolder = new File(targetTestClassesFolder.getParentFile(), "classes");
builder.setRootArtifactClassesFolder(rootArtifactClassesFolder);
builder.setPluginResourcesFolder(targetTestClassesFolder.getParentFile());
builder.setProvidedExclusions(runnerConfiguration.getProvidedExclusions());
builder.setTestExclusions(runnerConfiguration.getTestExclusions());
builder.setTestInclusions(runnerConfiguration.getTestInclusions());
builder.setExportPluginClasses(runnerConfiguration.getExportPluginClasses());
builder.setApplicationSharedLibCoordinates(runnerConfiguration.getSharedApplicationRuntimeLibs());
builder.setApplicationLibCoordinates(runnerConfiguration.getApplicationRuntimeLibs());
builder.setTestRunnerExportedLibCoordinates(runnerConfiguration.getTestRunnerExportedRuntimeLibs());
builder.setExtensionMetadataGeneration(true);
Properties excludedProperties;
try {
excludedProperties = getExcludedProperties();
} catch (IOException e) {
throw new RuntimeException("Error while reading excluded properties", e);
}
Set<String> excludedArtifactsList = getExcludedArtifacts(excludedProperties);
builder.setExcludedArtifacts(excludedArtifactsList);
builder.setExtraBootPackages(getExtraBootPackages(excludedProperties));
builder.setExtraPrivilegedArtifacts(runnerConfiguration.getExtraPrivilegedArtifacts());
final ClassPathUrlProvider classPathUrlProvider = new ClassPathUrlProvider();
List<URL> classPath = classPathUrlProvider.getURLs();
builder.setClassPathUrlProvider(classPathUrlProvider);
WorkspaceLocationResolver workspaceLocationResolver = new AutoDiscoverWorkspaceLocationResolver(classPath, rootArtifactClassesFolder);
final MavenClientProvider mavenClientProvider = discoverProvider(ArtifactClassLoaderRunner.class.getClassLoader());
final Supplier<File> localMavenRepository = mavenClientProvider.getLocalRepositorySuppliers().environmentMavenRepositorySupplier();
final SettingsSupplierFactory settingsSupplierFactory = mavenClientProvider.getSettingsSupplierFactory();
final Optional<File> globalSettings = settingsSupplierFactory.environmentGlobalSettingsSupplier();
final Optional<File> userSettings = settingsSupplierFactory.environmentUserSettingsSupplier();
final MavenConfiguration.MavenConfigurationBuilder mavenConfigurationBuilder = newMavenConfigurationBuilder().forcePolicyUpdateNever(true).localMavenRepositoryLocation(localMavenRepository.get());
if (globalSettings.isPresent()) {
mavenConfigurationBuilder.globalSettingsLocation(globalSettings.get());
} else {
LOGGER.info("Maven global settings couldn't be found, M2_HOME environment variable has to be set in order to use global settings (if needed)");
}
if (userSettings.isPresent()) {
mavenConfigurationBuilder.userSettingsLocation(userSettings.get());
} else {
LOGGER.info("Maven user settings couldn't be found, this could cause a wrong resolution for dependencies");
}
final MavenConfiguration mavenConfiguration = mavenConfigurationBuilder.build();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Using MavenConfiguration: {}", mavenConfiguration);
}
final DependencyResolver dependencyResolver = new DependencyResolver(mavenConfiguration, of(new DefaultWorkspaceReader(classPath, workspaceLocationResolver)));
builder.setClassPathClassifier(new AetherClassPathClassifier(dependencyResolver, new ArtifactClassificationTypeResolver(dependencyResolver)));
return builder.build();
}
Aggregations