Search in sources :

Example 41 with URLClassLoader

use of java.net.URLClassLoader in project groovy by apache.

the class ScriptCompilationExecuter method execute.

public long execute() throws Exception {
    ClassLoader cl = new URLClassLoader(classpath, ClassLoader.getSystemClassLoader().getParent());
    GroovyClassLoader gcl = new GroovyClassLoader(cl);
    CompilationUnit cu = new CompilationUnit(new CompilerConfiguration(), null, gcl, new GroovyClassLoader(this.getClass().getClassLoader()));
    for (File source : sources) {
        cu.addSource(source);
    }
    long sd = System.nanoTime();
    cu.compile(CompilePhase.CLASS_GENERATION.getPhaseNumber());
    long dur = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - sd);
    return dur;
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) URLClassLoader(java.net.URLClassLoader) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) URLClassLoader(java.net.URLClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) File(java.io.File)

Example 42 with URLClassLoader

use of java.net.URLClassLoader in project sonarqube by SonarSource.

the class ManifestUtilsTest method emptyManifest.

@Test
public void emptyManifest() throws Exception {
    Manifest mf = new Manifest();
    File jar = createJar(mf, "emptyManifest.jar");
    URLClassLoader classloader = new URLClassLoader(FileUtils.toURLs(new File[] { jar }));
    assertThat(ManifestUtils.getPropertyValues(classloader, "foo")).isEmpty();
}
Also used : URLClassLoader(java.net.URLClassLoader) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.Test)

Example 43 with URLClassLoader

use of java.net.URLClassLoader in project sonarqube by SonarSource.

the class ManifestUtilsTest method manyManifests.

@Test
public void manyManifests() throws Exception {
    Manifest mf1 = new Manifest();
    mf1.getMainAttributes().putValue("foo", "bar");
    File jar1 = createJar(mf1, "manyManifests-one.jar");
    Manifest mf2 = new Manifest();
    mf2.getMainAttributes().putValue("foo", "otherbar");
    File jar2 = createJar(mf2, "manyManifests-two.jar");
    URLClassLoader classloader = new URLClassLoader(FileUtils.toURLs(new File[] { jar1, jar2 }));
    List<String> values = ManifestUtils.getPropertyValues(classloader, "foo");
    assertThat(values).containsOnly("bar", "otherbar");
}
Also used : URLClassLoader(java.net.URLClassLoader) Manifest(java.util.jar.Manifest) File(java.io.File) Test(org.junit.Test)

Example 44 with URLClassLoader

use of java.net.URLClassLoader in project che by eclipse.

the class JUnitTestRunner method execute.

/**
     * {@inheritDoc}
     */
@Override
public TestResult execute(Map<String, String> testParameters) throws Exception {
    String projectAbsolutePath = testParameters.get("absoluteProjectPath");
    boolean updateClasspath = Boolean.valueOf(testParameters.get("updateClasspath"));
    boolean runClass = Boolean.valueOf(testParameters.get("runClass"));
    String projectPath = testParameters.get("projectPath");
    String projectType = "";
    if (projectManager != null) {
        projectType = projectManager.getProject(projectPath).getType();
    }
    ClassLoader currentClassLoader = this.getClass().getClassLoader();
    TestClasspathProvider classpathProvider = classpathRegistry.getTestClasspathProvider(projectType);
    URLClassLoader providedClassLoader = (URLClassLoader) classpathProvider.getClassLoader(projectAbsolutePath, projectPath, updateClasspath);
    projectClassLoader = new URLClassLoader(providedClassLoader.getURLs(), null) {

        @Override
        protected Class<?> findClass(String name) throws ClassNotFoundException {
            if (name.startsWith("javassist.")) {
                return currentClassLoader.loadClass(name);
            }
            return super.findClass(name);
        }
    };
    boolean isJUnit4Compatible = false;
    boolean isJUnit3Compatible = false;
    try {
        Class.forName(JUNIT4X_RUNNER_CLASS, true, projectClassLoader);
        isJUnit4Compatible = true;
    } catch (Exception ignored) {
    }
    try {
        Class.forName(JUNIT3X_RUNNER_CLASS, true, projectClassLoader);
        isJUnit3Compatible = true;
    } catch (Exception ignored) {
    }
    boolean useJUnitV3API = false;
    if (!isJUnit4Compatible) {
        if (!isJUnit3Compatible) {
            throw new ClassNotFoundException("JUnit classes not found in the following project classpath: " + Arrays.asList(providedClassLoader.getURLs()));
        } else {
            useJUnitV3API = true;
        }
    }
    String currentWorkingDir = System.getProperty("user.dir");
    try {
        System.setProperty("user.dir", projectAbsolutePath);
        TestResult testResult;
        if (runClass) {
            String fqn = testParameters.get("fqn");
            testResult = useJUnitV3API ? run3x(fqn) : run4x(fqn);
        } else {
            testResult = useJUnitV3API ? runAll3x(projectAbsolutePath) : runAll4x(projectAbsolutePath);
        }
        return testResult;
    } finally {
        System.setProperty("user.dir", currentWorkingDir);
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) TestResult(org.eclipse.che.api.testing.shared.TestResult) TestClasspathProvider(org.eclipse.che.plugin.testing.classpath.server.TestClasspathProvider)

Example 45 with URLClassLoader

use of java.net.URLClassLoader in project pinpoint by naver.

the class PinpointClassFilterTest method testDoFilter_Package.

@Test
public void testDoFilter_Package() throws Exception {
    final URLClassLoader agentClassLoader = new URLClassLoader(new URL[0]);
    ClassFileFilter filter = new PinpointClassFilter(agentClassLoader);
    final ClassLoader defaultClassLoader = ClassLoaderUtils.getDefaultClassLoader();
    Assert.assertSame("pinpoint", filter.accept(defaultClassLoader, "com/navercorp/pinpoint/", null, null, null), ClassFileFilter.SKIP);
    Assert.assertSame(filter.accept(defaultClassLoader, "java/test", null, null, null), ClassFileFilter.CONTINUE);
    Assert.assertSame(filter.accept(defaultClassLoader, "javax/test", null, null, null), ClassFileFilter.CONTINUE);
    Assert.assertSame(filter.accept(defaultClassLoader, "test", null, null, null), ClassFileFilter.CONTINUE);
}
Also used : URLClassLoader(java.net.URLClassLoader) URLClassLoader(java.net.URLClassLoader) Test(org.junit.Test)

Aggregations

URLClassLoader (java.net.URLClassLoader)1351 URL (java.net.URL)872 File (java.io.File)514 Test (org.junit.Test)317 IOException (java.io.IOException)256 ArrayList (java.util.ArrayList)202 MalformedURLException (java.net.MalformedURLException)186 Method (java.lang.reflect.Method)177 InvocationTargetException (java.lang.reflect.InvocationTargetException)68 JarFile (java.util.jar.JarFile)54 InputStream (java.io.InputStream)50 HashSet (java.util.HashSet)49 HashMap (java.util.HashMap)44 URISyntaxException (java.net.URISyntaxException)41 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)35 Path (java.nio.file.Path)33 QuickTest (com.hazelcast.test.annotation.QuickTest)32 Test (org.junit.jupiter.api.Test)28 URI (java.net.URI)27 List (java.util.List)27