PomEditorDefault implements PomEditor { public boolean addDependency(DynamicPomDependency dep, Path pomPath) { if (dep == null || !isGroupIDValid(dep) || !isArtifactIDValid(dep)) { return false; } try { Dependency pomDep = getMavenDependency(dep); org.uberfire.java.nio.file.Path filePath = Paths.get(pomPath.toURI()); Model model = getPOMModel(filePath); Map<String, String> keys = getKeysFromDeps(model.getDependencies()); String keyDep = getKeyFromDep(dep); if (!keys.containsKey(keyDep)) { model.getDependencies().add(pomDep); } else { String versionKey = keys.get(keyDep); List<Dependency> modelDeps = model.getDependencies(); for (Dependency modelDep : modelDeps) { if (modelDep.getGroupId().equals(dep.getGroupID()) && modelDep.getArtifactId().equals(dep.getArtifactID())) { modelDep.setVersion(versionKey); } } return false; } writePOMModelOnFS(filePath, model); } catch (Exception ex) { logger.error(ex.getMessage(), ex); return false; } return true; } PomEditorDefault(); boolean addDependency(DynamicPomDependency dep,
                                 Path pomPath); boolean addDependencies(List<DynamicPomDependency> deps,
                                   Path pomPath);  }
@Test public void addEmptyDepTest() { DynamicPomDependency dep = new DynamicPomDependency("", "", "", ""); boolean result = editor.addDependency(dep, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }
@Test public void addNullDepTest() { boolean result = editor.addDependency(null, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }
@Test public void addNullGroupIDTest() { DynamicPomDependency dep = new DynamicPomDependency(null, "junit", "4.12", ""); boolean result = editor.addDependency(dep, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }
@Test public void addNullArtifactIDTest() { DynamicPomDependency dep = new DynamicPomDependency("junit", null, "4.12", ""); boolean result = editor.addDependency(dep, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }
@Test public void addDepTest() { DynamicPomDependency dep = new DynamicPomDependency("junit", "junit", "4.12", ""); boolean result = editor.addDependency(dep, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isTrue(); }
@Test public void addDuplicatedDepTest() { DynamicPomDependency dep = new DynamicPomDependency(TestUtil.GROUP_ID_TEST, TestUtil.ARTIFACT_ID_TEST, TestUtil.VERSION_ID_TEST, ""); boolean result = editor.addDependency(dep, PathFactory.newPath(tmp.toAbsolutePath().toString() + fileSeparator + POM, tmp.toUri().toString() + fileSeparator + POM)); assertThat(result).isFalse(); }