Example usage for org.eclipse.jgit.api Git cloneRepository

List of usage examples for org.eclipse.jgit.api Git cloneRepository

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git cloneRepository.

Prototype

public static CloneCommand cloneRepository() 

Source Link

Document

Return a command object to execute a clone command

Usage

From source file:org.kie.workbench.common.services.backend.compiler.impl.utils.JGitUtils.java

License:Apache License

public static Git tempClone(final JGitFileSystem fs, final String uuid) {
    try {//  w  w w  .  ja  v a 2s  .c o  m
        File tempRepoPlace;
        int i = 0;
        while (true) {
            tempRepoPlace = new File(TEMP + uuid + "_" + i,
                    fs.getGit().getRepository().getDirectory().getName().replaceFirst("\\.git", ""));
            if (tempRepoPlace.exists()) {
                i++;
            } else {
                break;
            }
        }
        return Git.cloneRepository().setURI(fs.getGit().getRepository().getDirectory().toURI().toString())
                .setDirectory(tempRepoPlace).setBare(false).setCloneAllBranches(true).call();
    } catch (GitAPIException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.kie.workbench.common.services.backend.compiler.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithCloneTest() throws Exception {

    final String repoName = "myrepoxxxx";
    final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName),
            new HashMap<String, Object>() {
                {//from   www  .  j av  a2s  .co  m
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                }
            });

    ioService.startBatch(fs);

    ioService.write(fs.getPath("/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("target/test-classes/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "target/test-classes/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "target/test-classes/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummyA/pom.xml"), new String(java.nio.file.Files.readAllBytes(
            new File("target/test-classes/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummyB/pom.xml"), new String(java.nio.file.Files.readAllBytes(
            new File("target/test-classes/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    Path tmpCloned = Files.createTempDirectory("cloned");

    final File gitClonedFolder = new File(tmpCloned.toFile(), ".clone.git");

    final Git cloned = Git.cloneRepository()
            .setURI(fs.getGit().getRepository().getDirectory().toURI().toString()).setBare(false)
            .setDirectory(gitClonedFolder).call();

    assertThat(cloned).isNotNull();

    //Compile the repo

    Path prjFolder = Paths.get(gitClonedFolder + "/");
    byte[] encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertThat(pomAsAstring).doesNotContain(TestConstants.TAKARI_LIFECYCLE_ARTIFACT);

    final AFCompiler compiler = KieMavenCompilerFactory
            .getCompiler(EnumSet.of(KieDecorator.ENABLE_LOGGING, KieDecorator.ENABLE_INCREMENTAL_BUILD));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepoPath, info,
            new String[] { MavenCLIArgs.COMPILE }, Boolean.FALSE);
    CompilationResponse res = compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmpCloned, res, this.getClass(), testName);
    assertThat(res.isSuccessful()).isTrue();
    Path incrementalConfiguration = Paths.get(prjFolder + TestConstants.TARGET_TAKARI_PLUGIN);
    assertThat(incrementalConfiguration.toFile()).exists();

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertThat(pomAsAstring).contains(TestConstants.KIE_TAKARI_LIFECYCLE_ARTIFACT);

    TestUtil.rm(tmpCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithPullRebaseUberfireTest() throws Exception {

    //Setup origin in memory
    final URI originRepo = URI.create("git://repo");
    final JGitFileSystem origin = (JGitFileSystem) ioService.newFileSystem(originRepo,
            new HashMap<String, Object>() {
                {//from  w  ww.j a  v  a 2  s. c o  m
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                    put("listMode", "ALL");
                }
            });
    ioService.startBatch(origin);

    ioService.write(origin.getPath("/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummyA/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummyB/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    // clone into a regularfs
    Path tmpRootCloned = Files.createTempDirectory("cloned");
    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), ".clone"));

    final Git cloned = Git.cloneRepository()
            .setURI(origin.getGit().getRepository().getDirectory().toURI().toString()).setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertThat(cloned).isNotNull();

    PullCommand pc = cloned.pull().setRemote("origin").setRebase(Boolean.TRUE);
    PullResult pullRes = pc.call();
    assertThat(pullRes.getRebaseResult().getStatus()).isEqualTo(RebaseResult.Status.UP_TO_DATE);// nothing changed yet

    RebaseCommand rb = cloned.rebase().setUpstream("origin/master");
    RebaseResult rbResult = rb.setPreserveMerges(true).call();
    assertThat(rbResult.getStatus().isSuccessful()).isTrue();

    //Compile the repo
    byte[] encoded = Files.readAllBytes(Paths.get(tmpCloned + "/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertThat(pomAsAstring).doesNotContain(TestConstants.TAKARI_LIFECYCLE_ARTIFACT);

    Path prjFolder = Paths.get(tmpCloned + "/");

    final AFCompiler compiler = KieMavenCompilerFactory
            .getCompiler(EnumSet.of(KieDecorator.ENABLE_LOGGING, KieDecorator.ENABLE_INCREMENTAL_BUILD));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepoPath, info,
            new String[] { MavenCLIArgs.COMPILE }, Boolean.TRUE);
    CompilationResponse res = compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmpCloned, res, this.getClass(), testName);

    assertThat(res.isSuccessful()).isTrue();

    Path incrementalConfiguration = Paths.get(prjFolder + TestConstants.TARGET_TAKARI_PLUGIN);
    assertThat(incrementalConfiguration.toFile()).exists();

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertThat(pomAsAstring).contains(TestConstants.KIE_TAKARI_LIFECYCLE_ARTIFACT);

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithAllDecoratorsTest() throws Exception {
    String alternateSettingsAbsPath = TestUtilMaven.getSettingsFile();
    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>() {
                {/*from w ww. j  av a  2s  .c  o m*/
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                    put("listMode", "ALL");
                }
            });
    assertThat(origin).isNotNull();

    ioService.startBatch(origin);

    ioService.write(origin.getPath("/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("/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("/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("/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());
    assertThat(lastCommit).isNotNull();

    // 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(origin.getGit().getRepository().getDirectory().toURI().toString()).setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertThat(cloned).isNotNull();

    final AFCompiler compiler = KieMavenCompilerFactory
            .getCompiler(EnumSet.of(KieDecorator.UPDATE_JGIT_BEFORE_BUILD, KieDecorator.ENABLE_LOGGING));
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmpCloned + "/"));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepoPath, info, new String[] {
            MavenCLIArgs.COMPILE, MavenCLIArgs.ALTERNATE_USER_SETTINGS + alternateSettingsAbsPath },
            Boolean.TRUE);
    CompilationResponse res = compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmpCloned, res, this.getClass(), testName);
    assertThat(res.isSuccessful()).isTrue();

    lastCommit = origin.getGit().resolveRevCommit(origin.getGit().getRef(MASTER_BRANCH).getObjectId());
    assertThat(lastCommit).isNotNull();

    //change one file and commit on the origin repo
    ioService.write(origin.getPath("/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());
    assertThat(commitBefore).isNotNull();
    assertThat(lastCommit.getId().toString()).isNotEqualTo(commitBefore.getId().toString());

    //recompile
    res = compiler.compile(req);
    TestUtil.saveMavenLogIfCompilationResponseNotSuccessfull(tmpCloned, res, this.getClass(), testName);
    assertThat(res.isSuccessful()).isTrue();

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.DefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithCloneTest() throws Exception {

    final String repoName = "myrepo";
    final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName),
            new HashMap<String, Object>() {
                {//from ww w .  j  a v  a  2  s. com
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                }
            });

    ioService.startBatch(fs);

    ioService.write(fs.getPath("/dummy/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummy/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummy/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummy/dummyA/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummy/dummyB/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    Path tmpRootCloned = Files.createTempDirectory("cloned");

    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), "dummy"));

    final File gitClonedFolder = new File(tmpCloned.toFile(), ".clone.git");

    final Git cloned = Git.cloneRepository()
            .setURI(fs.getGit().getRepository().getDirectory().toURI().toString()).setBare(false)
            .setDirectory(gitClonedFolder).call();

    assertNotNull(cloned);

    //Compile the repo
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.LOG_OUTPUT_AFTER);
    Path prjFolder = Paths.get(gitClonedFolder + "/dummy/");
    byte[] encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    Assert.assertFalse(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.TRUE);

    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(),
                "KieDefaultMavenCompilerOnInMemoryFSTest.buildWithCloneTest");
    }
    assertTrue(res.getMavenOutput().isPresent());
    assertTrue(res.isSuccessful());

    Path incrementalConfiguration = Paths.get(
            prjFolder + "/target/incremental/io.takari.maven.plugins_takari-lifecycle-plugin_compile_compile");
    assertTrue(incrementalConfiguration.toFile().exists());

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertTrue(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.DefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithPullRebaseUberfireTest() throws Exception {

    //Setup origin in memory
    final URI originRepo = URI.create("git://repo");
    final JGitFileSystem origin = (JGitFileSystem) ioService.newFileSystem(originRepo,
            new HashMap<String, Object>() {
                {//from   ww w .  j  a  va 2  s .c om
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                    put("listMode", "ALL");
                }
            });
    ioService.startBatch(origin);

    ioService.write(origin.getPath("/dummy/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummy/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummy/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummy/dummyA/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummy/dummyB/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    // clone into a regularfs
    Path tmpRootCloned = Files.createTempDirectory("cloned");
    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), ".clone"));

    final Git cloned = Git.cloneRepository().setURI("git://localhost:9418/repo").setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertNotNull(cloned);

    PullCommand pc = cloned.pull().setRemote("origin").setRebase(Boolean.TRUE);
    PullResult pullRes = pc.call();
    assertTrue(pullRes.getRebaseResult().getStatus().equals(RebaseResult.Status.UP_TO_DATE));// nothing changed yet

    RebaseCommand rb = cloned.rebase().setUpstream("origin/master");
    RebaseResult rbResult = rb.setPreserveMerges(true).call();
    assertTrue(rbResult.getStatus().isSuccessful());

    //Compile the repo
    AFCompiler compiler = MavenCompilerFactory.getCompiler(Decorator.LOG_OUTPUT_AFTER);

    byte[] encoded = Files.readAllBytes(Paths.get(tmpCloned + "/dummy/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    Assert.assertFalse(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    Path prjFolder = Paths.get(tmpCloned + "/dummy/");

    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.TRUE);

    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(),
                "KieDefaultMavenCompilerOnInMemoryFSTest.buildWithPullRebaseUberfireTest");
    }

    assertTrue(res.isSuccessful());

    Path incrementalConfiguration = Paths.get(
            prjFolder + "/target/incremental/io.takari.maven.plugins_takari-lifecycle-plugin_compile_compile");
    assertTrue(incrementalConfiguration.toFile().exists());

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertTrue(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.DefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithAllDecoratorsTest() throws Exception {
    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>() {
                {//  w  ww  .  ja  va2s  .c  o m
                    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"));
    //@TODO find a way to retrieve the address git://... of the repo
    final Git cloned = Git.cloneRepository().setURI("git://localhost:9418/repo").setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertNotNull(cloned);

    //@TODO refactor and use only one between the URI or Git
    //@TODO find a way to resolve the problem of the prjname inside .git folder
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmpCloned + "/dummy"));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.COMPILE }, 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());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithCloneTest() throws Exception {

    final String repoName = "myrepo";
    final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName),
            new HashMap<String, Object>() {
                {//from  w  w w . ja va 2s.c om
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                }
            });

    ioService.startBatch(fs);

    ioService.write(fs.getPath("/dummy/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("target/test-classes/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummy/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "target/test-classes/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummy/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "target/test-classes/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(fs.getPath("/dummy/dummyA/pom.xml"), new String(java.nio.file.Files.readAllBytes(
            new File("target/test-classes/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(fs.getPath("/dummy/dummyB/pom.xml"), new String(java.nio.file.Files.readAllBytes(
            new File("target/test-classes/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    Path tmpRootCloned = Files.createTempDirectory("cloned");

    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), "dummy"));

    final File gitClonedFolder = new File(tmpCloned.toFile(), ".clone.git");

    final Git cloned = Git.cloneRepository()
            .setURI(fs.getGit().getRepository().getDirectory().toURI().toString()).setBare(false)
            .setDirectory(gitClonedFolder).call();

    assertNotNull(cloned);

    //Compile the repo
    AFCompiler compiler = KieMavenCompilerFactory.getCompiler(KieDecorator.LOG_OUTPUT_AFTER);
    Path prjFolder = Paths.get(gitClonedFolder + "/dummy/");
    byte[] encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    Assert.assertFalse(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.TRUE);

    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(),
                "KieDefaultMavenCompilerTest.buildWithCloneTest");
    }
    assertTrue(res.getMavenOutput().isPresent());
    assertTrue(res.isSuccessful());

    Path incrementalConfiguration = Paths.get(
            prjFolder + "/target/incremental/io.takari.maven.plugins_takari-lifecycle-plugin_compile_compile");
    assertTrue(incrementalConfiguration.toFile().exists());

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertTrue(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithPullRebaseUberfireTest() throws Exception {

    //Setup origin in memory
    final URI originRepo = URI.create("git://repo");
    final JGitFileSystem origin = (JGitFileSystem) ioService.newFileSystem(originRepo,
            new HashMap<String, Object>() {
                {//  w  w w  .j  a  v  a2 s .co  m
                    put("init", Boolean.TRUE);
                    put("internal", Boolean.TRUE);
                    put("listMode", "ALL");
                }
            });
    ioService.startBatch(origin);

    ioService.write(origin.getPath("/dummy/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummy/dummyA/src/main/java/dummy/DummyA.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyA/src/main/java/dummy/DummyA.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummy/dummyB/src/main/java/dummy/DummyB.java"),
            new String(java.nio.file.Files.readAllBytes(new File(
                    "src/test/projects/dummy_multimodule_untouched/dummyB/src/main/java/dummy/DummyB.java")
                            .toPath())));
    ioService.write(origin.getPath("/dummy/dummyA/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyA/pom.xml").toPath())));
    ioService.write(origin.getPath("/dummy/dummyB/pom.xml"), new String(java.nio.file.Files
            .readAllBytes(new File("src/test/projects/dummy_multimodule_untouched/dummyB/pom.xml").toPath())));
    ioService.endBatch();

    // clone into a regularfs
    Path tmpRootCloned = Files.createTempDirectory("cloned");
    Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), ".clone"));

    final Git cloned = Git.cloneRepository().setURI("git://localhost:9418/repo").setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertNotNull(cloned);

    PullCommand pc = cloned.pull().setRemote("origin").setRebase(Boolean.TRUE);
    PullResult pullRes = pc.call();
    assertTrue(pullRes.getRebaseResult().getStatus().equals(RebaseResult.Status.UP_TO_DATE));// nothing changed yet

    RebaseCommand rb = cloned.rebase().setUpstream("origin/master");
    RebaseResult rbResult = rb.setPreserveMerges(true).call();
    assertTrue(rbResult.getStatus().isSuccessful());

    //Compile the repo
    AFCompiler compiler = KieMavenCompilerFactory.getCompiler(KieDecorator.LOG_OUTPUT_AFTER);

    byte[] encoded = Files.readAllBytes(Paths.get(tmpCloned + "/dummy/pom.xml"));
    String pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    Assert.assertFalse(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    Path prjFolder = Paths.get(tmpCloned + "/dummy/");

    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(prjFolder);
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.CLEAN, MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.TRUE);

    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(),
                "KieDefaultMavenCompilerTest.buildWithPullRebaseUberfireTest");
    }

    assertTrue(res.isSuccessful());

    Path incrementalConfiguration = Paths.get(
            prjFolder + "/target/incremental/io.takari.maven.plugins_takari-lifecycle-plugin_compile_compile");
    assertTrue(incrementalConfiguration.toFile().exists());

    encoded = Files.readAllBytes(Paths.get(prjFolder + "/pom.xml"));
    pomAsAstring = new String(encoded, StandardCharsets.UTF_8);
    assertTrue(pomAsAstring.contains("<artifactId>takari-lifecycle-plugin</artifactId>"));

    TestUtil.rm(tmpRootCloned.toFile());
}

From source file:org.kie.workbench.common.services.backend.compiler.nio.kie.KieDefaultMavenCompilerTest.java

License:Apache License

@Test
public void buildWithAllDecoratorsTest() throws Exception {
    AFCompiler compiler = KieMavenCompilerFactory.getCompiler(KieDecorator.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>() {
                {//from   w w w.j ava2 s  .  c o  m
                    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"));
    //@TODO find a way to retrieve the address git://... of the repo
    final Git cloned = Git.cloneRepository().setURI("git://localhost:9418/repo").setBare(false)
            .setDirectory(tmpCloned.toFile()).call();

    assertNotNull(cloned);

    //@TODO refactor and use only one between the URI or Git
    //@TODO find a way to resolve the problem of the prjname inside .git folder
    WorkspaceCompilationInfo info = new WorkspaceCompilationInfo(Paths.get(tmpCloned + "/dummy"));
    CompilationRequest req = new DefaultCompilationRequest(mavenRepo.toAbsolutePath().toString(), info,
            new String[] { MavenCLIArgs.COMPILE }, new HashMap<>(), Boolean.TRUE);
    CompilationResponse res = compiler.compileSync(req);
    if (res.getMavenOutput().isPresent() && !res.isSuccessful()) {
        TestUtil.writeMavenOutputIntoTargetFolder(res.getMavenOutput().get(),
                "KieDefaultMavenCompilerTest.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(),
                "KieDefaultMavenCompilerTest.buildWithAllDecoratorsTest");
    }
    assertTrue(res.isSuccessful());
    assertTrue(res.getMavenOutput().isPresent());

    TestUtil.rm(tmpRootCloned.toFile());
}