PomEditorDefault implements PomEditor { public boolean addDependencies(List<DynamicPomDependency> deps, Path pomPath) { if (deps.isEmpty()) { return false; } boolean result = false; try { org.uberfire.java.nio.file.Path filePath = Paths.get(pomPath.toURI()); Model model = getPOMModel(filePath); Map<String, String> keys = getKeysFromDeps(model.getDependencies()); for (DynamicPomDependency dep : deps) { String keyDep = getKeyFromDep(dep); if (!keys.containsKey(keyDep)) { Dependency pomDep = getMavenDependency(dep); model.getDependencies().add(pomDep); result = true; } else { List<Dependency> modelDeps = model.getDependencies(); for (Dependency modelDep : modelDeps) { if (modelDep.getGroupId().equals(dep.getGroupID()) && modelDep.getArtifactId().equals(dep.getArtifactID()) && !modelDep.getVersion().equals(dep.getVersion())) { modelDep.setVersion(dep.getVersion()); result = true; } } } } if (result) { writePOMModelOnFS(filePath, model); } } catch (Exception ex) { logger.error(ex.getMessage(), ex); result = false; } return result; } PomEditorDefault(); boolean addDependency(DynamicPomDependency dep,
                                 Path pomPath); boolean addDependencies(List<DynamicPomDependency> deps,
                                   Path pomPath);  }
@Test public void addDepsTest() { DynamicPomDependency dep = new DynamicPomDependency("junit", "junit", "4.12", ""); List<DynamicPomDependency> deps = Arrays.asList(dep); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isTrue(); }
@Test public void addAndOverrideVersionDepTest() throws Exception { tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummyOverride", "target/test-classes/dummy"); DynamicPomDependency dep = new DynamicPomDependency("javax.persistence", "javax.persistence-api", "1.0.3.Final", ""); List<DynamicPomDependency> deps = Arrays.asList(dep); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isTrue(); MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + fileSeparator + POM)))); Dependency changedDep = getDependency(model.getDependencies(), "javax.persistence", "javax.persistence-api"); assertThat(changedDep.getVersion()).isEqualTo("1.0.3.Final"); }
@Test public void addDuplicatedDepsTest() { DynamicPomDependency dep = new DynamicPomDependency(TestUtil.GROUP_ID_TEST, TestUtil.ARTIFACT_ID_TEST, TestUtil.VERSION_ID_TEST, ""); List<DynamicPomDependency> deps = Arrays.asList(dep); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }
@Test public void addAndOverrideVersionDepsTest() throws Exception { tmp = TestUtil.createAndCopyToDirectory(tmpRoot, "dummyOverride", "target/test-classes/dummyOverride"); DynamicPomDependency dep = new DynamicPomDependency("junit", "junit", "4.13", ""); DynamicPomDependency depTwo = new DynamicPomDependency("javax.persistence", "javax.persistence-api", "1.0.3.Final", ""); List<DynamicPomDependency> deps = Arrays.asList(dep, depTwo); boolean result = editor.addDependencies(deps, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isTrue(); MavenXpp3Reader reader = new MavenXpp3Reader(); Model model = reader.read(new ByteArrayInputStream(Files.readAllBytes(Paths.get(tmp.toAbsolutePath().toString() + fileSeparator + POM)))); Dependency changedDep = getDependency(model.getDependencies(), "javax.persistence", "javax.persistence-api"); assertThat(changedDep.getVersion()).isEqualTo("1.0.3.Final"); changedDep = getDependency(model.getDependencies(), "junit", "junit"); assertThat(changedDep.getVersion()).isEqualTo("4.13"); }