Search in sources :

Example 1 with ClassPathUrlProvider

use of org.mule.test.runner.api.ClassPathUrlProvider 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();
}
Also used : ArtifactIsolatedClassLoaderBuilder(org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder) MavenConfiguration(org.mule.maven.client.api.model.MavenConfiguration) SettingsSupplierFactory(org.mule.maven.client.api.SettingsSupplierFactory) IOException(java.io.IOException) Properties(java.util.Properties) RunnerModuleUtils.getExcludedProperties(org.mule.test.runner.utils.RunnerModuleUtils.getExcludedProperties) URL(java.net.URL) AutoDiscoverWorkspaceLocationResolver(org.mule.test.runner.maven.AutoDiscoverWorkspaceLocationResolver) WorkspaceLocationResolver(org.mule.test.runner.api.WorkspaceLocationResolver) DependencyResolver(org.mule.test.runner.api.DependencyResolver) AetherClassPathClassifier(org.mule.test.runner.api.AetherClassPathClassifier) AutoDiscoverWorkspaceLocationResolver(org.mule.test.runner.maven.AutoDiscoverWorkspaceLocationResolver) MavenClientProvider(org.mule.maven.client.api.MavenClientProvider) DefaultWorkspaceReader(org.mule.test.runner.classification.DefaultWorkspaceReader) File(java.io.File) ClassPathUrlProvider(org.mule.test.runner.api.ClassPathUrlProvider) ArtifactClassificationTypeResolver(org.mule.test.runner.api.ArtifactClassificationTypeResolver)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Properties (java.util.Properties)1 MavenClientProvider (org.mule.maven.client.api.MavenClientProvider)1 SettingsSupplierFactory (org.mule.maven.client.api.SettingsSupplierFactory)1 MavenConfiguration (org.mule.maven.client.api.model.MavenConfiguration)1 AetherClassPathClassifier (org.mule.test.runner.api.AetherClassPathClassifier)1 ArtifactClassificationTypeResolver (org.mule.test.runner.api.ArtifactClassificationTypeResolver)1 ArtifactIsolatedClassLoaderBuilder (org.mule.test.runner.api.ArtifactIsolatedClassLoaderBuilder)1 ClassPathUrlProvider (org.mule.test.runner.api.ClassPathUrlProvider)1 DependencyResolver (org.mule.test.runner.api.DependencyResolver)1 WorkspaceLocationResolver (org.mule.test.runner.api.WorkspaceLocationResolver)1 DefaultWorkspaceReader (org.mule.test.runner.classification.DefaultWorkspaceReader)1 AutoDiscoverWorkspaceLocationResolver (org.mule.test.runner.maven.AutoDiscoverWorkspaceLocationResolver)1 RunnerModuleUtils.getExcludedProperties (org.mule.test.runner.utils.RunnerModuleUtils.getExcludedProperties)1