Search in sources :

Example 1 with DefaultCompilationRequest

use of org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadProjectClassloaderFromStringTest.

@Test
public void loadProjectClassloaderFromStringTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE, MavenCLIArgs.INSTALL }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadProjectClassloaderFromStringTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.loadDependenciesClassloaderFromProject(uberfireTmp.toAbsolutePath().toString(), mavenRepo.toAbsolutePath().toString());
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("org.slf4j.LoggerFactory");
        assertFalse(clazz.isInterface());
        Method m = clazz.getMethod("getLogger", String.class);
        Logger logger = (Logger) m.invoke(clazz, "Dummy");
        assertTrue(logger.getName().equals("Dummy"));
        logger.info("dependency loaded from the prj classpath");
    } catch (ClassNotFoundException e) {
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) Logger(org.slf4j.Logger) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 2 with DefaultCompilationRequest

use of org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadProjectClassloaderTest.

@Test
public void loadProjectClassloaderTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE, MavenCLIArgs.INSTALL }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadProjectClassloaderTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    List<String> pomList = new ArrayList<>();
    MavenUtils.searchPoms(Paths.get("src/test/projects/dummy_kie_multimodule_classloader/"), pomList);
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.loadDependenciesClassloaderFromProject(pomList, mavenRepo.toAbsolutePath().toString());
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("org.slf4j.LoggerFactory");
        assertFalse(clazz.isInterface());
        Method m = clazz.getMethod("getLogger", String.class);
        Logger logger = (Logger) m.invoke(clazz, "Dummy");
        assertTrue(logger.getName().equals("Dummy"));
        logger.info("dependency loaded from the prj classpath");
    } catch (ClassNotFoundException e) {
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) Logger(org.slf4j.Logger) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 3 with DefaultCompilationRequest

use of org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest in project kie-wb-common by kiegroup.

the class ClassLoaderProviderTest method loadTargetFolderClassloaderTest.

@Test
public void loadTargetFolderClassloaderTest() throws Exception {
    // we use NIO for this part of the test because Uberfire lack the implementation to copy a tree
    Path tmpRoot = Files.createTempDirectory("repo");
    Path tmp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_classloader"), tmp);
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.NONE);
    Path uberfireTmp = Paths.get(tmp.toAbsolutePath().toString());
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(uberfireTmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.FALSE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "ClassLoaderProviderTest.loadTargetFolderClassloaderTest");
    }
    assertTrue(res.isSuccessful());
    AFClassLoaderProvider kieClazzLoaderProvider = new ClassLoaderProviderImpl();
    List<String> pomList = new ArrayList<>();
    MavenUtils.searchPoms(uberfireTmp, pomList);
    Optional<ClassLoader> clazzLoader = kieClazzLoaderProvider.getClassloaderFromProjectTargets(pomList, Boolean.FALSE);
    assertNotNull(clazzLoader);
    assertTrue(clazzLoader.isPresent());
    ClassLoader prjClassloader = clazzLoader.get();
    // we try to load the only dep in the prj with a simple call method to see if is loaded or not
    Class clazz;
    try {
        clazz = prjClassloader.loadClass("dummy.DummyB");
        assertFalse(clazz.isInterface());
        Object obj = clazz.newInstance();
        assertTrue(obj.toString().startsWith("dummy.DummyB"));
        Method m = clazz.getMethod("greetings", new Class[] {});
        Object greeting = m.invoke(obj, new Object[] {});
        assertTrue(greeting.toString().equals("Hello World !"));
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        fail();
    }
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) ArrayList(java.util.ArrayList) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) Method(java.lang.reflect.Method) ClassLoaderProviderImpl(org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl) URLClassLoader(java.net.URLClassLoader) AFClassLoaderProvider(org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 4 with DefaultCompilationRequest

use of org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest in project kie-wb-common by kiegroup.

the class DefaultIncrementalCompilerEnablerTest method testReadKiePluginTest.

@Test
public void testReadKiePluginTest() throws Exception {
    Path tmpRoot = Files.createTempDirectory("repo");
    // NIO creation and copy content
    Path temp = Files.createDirectories(Paths.get(tmpRoot.toString(), "dummy"));
    TestUtil.copyTree(Paths.get("src/test/projects/dummy_kie_multimodule_untouched"), temp);
    // end NIO
    Path tmp = Paths.get(tmpRoot.toAbsolutePath().toString(), "dummy");
    Path mainPom = Paths.get(tmp.toAbsolutePath().toString(), "pom.xml");
    byte[] encoded = Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString(), "pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertFalse(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));
    assertFalse(pomAsAstring.contains("<packaging>kjar</packaging>"));
    byte[] encodedDummyB = Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString(), "/dummyB/pom.xml"));
    String pomAsAstringDummyB = new String(encodedDummyB, StandardCharsets.UTF_8);
    assertTrue(pomAsAstringDummyB.contains("<packaging>kjar</packaging>"));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(tmp);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE, "-X" }, new HashMap<>(), Boolean.FALSE);
    DefaultIncrementalCompilerEnabler enabler = new DefaultIncrementalCompilerEnabler(Compilers.JAVAC);
    assertTrue(enabler.process(req).getResult());
    assertTrue(info.isKiePluginPresent());
    encoded = Files.readAllBytes(Paths.get(mainPom.toString()));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertTrue(pomAsAstring.contains("kie-takari-plugin"));
    TestUtil.rm(tmpRoot.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) DefaultIncrementalCompilerEnabler(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultIncrementalCompilerEnabler) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) Test(org.junit.Test)

Example 5 with DefaultCompilationRequest

use of org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest in project kie-wb-common by kiegroup.

the class DefaultMavenCompilerTest method buildWithAllDecoratorsTest.

// 
@Test
public void buildWithAllDecoratorsTest() throws Exception {
    String alternateSettingsAbsPath = new File("src/test/settings.xml").getAbsolutePath();
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.JGIT_BEFORE_AND_LOG_AFTER);
    String MASTER_BRANCH = "master";
    // Setup origin in memory
    final URI originRepo = URI.create("git://repo");
    final JGitFileSystem origin = (JGitFileSystem) ioService.newFileSystem(originRepo, new HashMap<String, Object>() {

        {
            put("init", Boolean.TRUE);
            put("internal", Boolean.TRUE);
            put("listMode", "ALL");
        }
    });
    assertNotNull(origin);
    ioService.startBatch(origin);
    ioService.write(origin.getPath("/dummy/pom.xml"), new String(java.nio.file.Files.readAllBytes(new File("target/test-classes/kjar-2-single-resources/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummy/src/main/java/org/kie/maven/plugin/test/Person.java"), new String(java.nio.file.Files.readAllBytes(new File("target/test-classes/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java").toPath())));
    ioService.write(origin.getPath("/dummy/src/main/resources/AllResourceTypes/simple-rules.drl"), new String(java.nio.file.Files.readAllBytes(new File("target/test-classes/kjar-2-single-resources/src/main/resources/AllResourceTypes/simple-rules.drl").toPath())));
    ioService.write(origin.getPath("/dummy/src/main/resources/META-INF/kmodule.xml"), new String(java.nio.file.Files.readAllBytes(new File("target/test-classes/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml").toPath())));
    ioService.endBatch();
    RevCommit lastCommit = origin.getGit().resolveRevCommit(origin.getGit().getRef(MASTER_BRANCH).getObjectId());
    assertNotNull(lastCommit);
    // clone into a regularfs
    Path tmpRootCloned = Files.createTempDirectory("cloned");
    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), ".clone.git"));
    final Git cloned = Git.cloneRepository().setURI("git://localhost:9418/repo").setBare(false).setDirectory(tmpCloned.toFile()).call();
    assertNotNull(cloned);
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmpCloned + "/dummy"));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info, new String[] { MavenCLIArgs.COMPILE, MavenCLIArgs.ALTERNATE_USER_SETTINGS + alternateSettingsAbsPath }, new HashMap<>(), Boolean.TRUE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "KieDefaultMavenCompilerOnInMemoryFSTest.buildWithAllDecoratorsTest");
    }
    assertTrue(res.getMavenOutput().isPresent());
    assertTrue(res.isSuccessful());
    lastCommit = origin.getGit().resolveRevCommit(origin.getGit().getRef(MASTER_BRANCH).getObjectId());
    assertNotNull(lastCommit);
    // change one file and commit on the origin repo
    ioService.write(origin.getPath("/dummy/src/main/java/org/kie/maven/plugin/test/Person.java"), new String(java.nio.file.Files.readAllBytes(new File("src/test/projects/Person.java").toPath())));
    RevCommit commitBefore = origin.getGit().resolveRevCommit(origin.getGit().getRef(MASTER_BRANCH).getObjectId());
    assertNotNull(commitBefore);
    assertFalse(lastCommit.getId().toString().equals(commitBefore.getId().toString()));
    // recompile
    res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(), "KieDefaultMavenCompilerOnInMemoryFSTest.buildWithAllDecoratorsTest");
    }
    assertTrue(res.isSuccessful());
    assertTrue(res.getMavenOutput().isPresent());
    TestUtil.rm(tmpRootCloned.toFile());
}
Also used : Path(org.uberfire.java.nio.file.Path) HashMap(java.util.HashMap) CompilationResponse(org.kie.workbench.common.services.backend.compiler.CompilationResponse) URI(java.net.URI) Git(org.eclipse.jgit.api.Git) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) File(java.io.File) JGitFileSystem(org.uberfire.java.nio.fs.jgit.JGitFileSystem) DefaultCompilationRequest(org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Aggregations

DefaultCompilationRequest (org.kie.workbench.common.services.backend.compiler.nio.impl.DefaultCompilationRequest)27 Test (org.junit.Test)26 Path (org.uberfire.java.nio.file.Path)25 CompilationResponse (org.kie.workbench.common.services.backend.compiler.CompilationResponse)24 CompilationRequest (org.kie.workbench.common.services.backend.compiler.nio.CompilationRequest)13 WorkspaceCompilationInfo (org.kie.workbench.common.services.backend.compiler.nio.WorkspaceCompilationInfo)13 AFCompiler (org.kie.workbench.common.services.backend.compiler.nio.AFCompiler)12 File (java.io.File)10 JGitFileSystem (org.uberfire.java.nio.fs.jgit.JGitFileSystem)9 HashMap (java.util.HashMap)8 Git (org.eclipse.jgit.api.Git)7 Method (java.lang.reflect.Method)6 URI (java.net.URI)6 URLClassLoader (java.net.URLClassLoader)6 ArrayList (java.util.ArrayList)6 AFClassLoaderProvider (org.kie.workbench.common.services.backend.compiler.AFClassLoaderProvider)6 ClassLoaderProviderImpl (org.kie.workbench.common.services.backend.compiler.nio.impl.ClassLoaderProviderImpl)6 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 Logger (org.slf4j.Logger)4 PullCommand (org.eclipse.jgit.api.PullCommand)2