diff --git a/integration-tests/src/test/java/org/wildfly/channelplugin/it/UpgradeComponentsMojoIT.java b/integration-tests/src/test/java/org/wildfly/channelplugin/it/UpgradeComponentsMojoIT.java index 1a04854..68ff9c9 100644 --- a/integration-tests/src/test/java/org/wildfly/channelplugin/it/UpgradeComponentsMojoIT.java +++ b/integration-tests/src/test/java/org/wildfly/channelplugin/it/UpgradeComponentsMojoIT.java @@ -44,7 +44,7 @@ void basic_project_test_case(MavenExecutionResult result) { Model model = result.getMavenProjectResult().getModel(); DependencyModel dependencyModel = new DependencyModel(model); - // verify version property has been overriden + // verify version property has been overridden assertThat(model.getProperties().getProperty("undertow.version")) .usingComparator(VersionMatcher.COMPARATOR).isEqualTo("2.2.17.SP1-redhat-00001"); @@ -82,6 +82,9 @@ void basic_project_test_case(MavenExecutionResult result) { assertThat(o).isPresent(); assertThat(o.get().getVersion()).isEqualTo("1.0.0.Final"); }); + + assertThat(model.getRepositories()).isNotEmpty(); + assertThat(model.getRepositories().get(0).getUrl()).startsWith("file://"); // TODO: check complete URL } /** diff --git a/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java b/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java index 3f7ac40..bacabc5 100644 --- a/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java +++ b/plugin/src/main/java/org/wildfly/channelplugin/manipulation/PomManipulator.java @@ -75,7 +75,79 @@ public void injectManagedDependency(ArtifactRef dependency, Collection stack = new Stack(); + String path = ""; + + while (eventReader.hasNext()) { + XMLEvent event = eventReader.nextEvent(); + if (event.isStartElement()) { + if (path.equals("/project") && event.asStartElement().getName().getLocalPart().equals(REPOSITORIES)) { + // section is already present + return; + } + + stack.push(path); + path = path + "/" + event.asStartElement().getName().getLocalPart(); + } else if (event.isEndElement()) { + if (event.asEndElement().getName().getLocalPart().equals(PROJECT)) { + eventReader.mark(0); + eventReader.replaceMark(0, composeRepositoriesElementString() + + "" + ); + eventReader.clearMark(0); + return; + } + + path = stack.pop(); + } + } + } + + static void injectPluginRepositoriesSection(ModifiedPomXMLEventReader eventReader) throws XMLStreamException { + eventReader.rewind(); + + Stack stack = new Stack(); + String path = ""; + + while (eventReader.hasNext()) { + XMLEvent event = eventReader.nextEvent(); + if (event.isStartElement()) { + if (path.equals("/project") && event.asStartElement().getName().getLocalPart().equals(PLUGIN_REPOSITORIES)) { + // section is already present + return; + } + + stack.push(path); + path = path + "/" + event.asStartElement().getName().getLocalPart(); + } else if (event.isEndElement()) { + if (event.asEndElement().getName().getLocalPart().equals(PROJECT)) { + eventReader.mark(0); + eventReader.replaceMark(0, composePluginRepositoriesElementString() + + "" + ); + eventReader.clearMark(0); + return; + } + + path = stack.pop(); + } + } + } + public void injectRepository(String id, String url) throws XMLStreamException { + injectRepository(eventReader, id, url); + } + + public void injectPluginRepository(String id, String url) throws XMLStreamException { + injectPluginRepository(eventReader, id, url); + } + + static void injectRepository(ModifiedPomXMLEventReader eventReader, String id, String url) throws XMLStreamException { + injectRepositoriesSection(eventReader); + eventReader.rewind(); Stack stack = new Stack(); @@ -103,10 +175,12 @@ public void injectRepository(String id, String url) throws XMLStreamException { } } - public void injectPluginRepository(String id, String url) throws XMLStreamException { + static void injectPluginRepository(ModifiedPomXMLEventReader eventReader, String id, String url) throws XMLStreamException { + injectPluginRepositoriesSection(eventReader); + eventReader.rewind(); - Stack stack = new Stack(); + Stack stack = new Stack<>(); String path = ""; while (eventReader.hasNext()) { @@ -289,6 +363,16 @@ private static String composeDependenciesElementString() { + " \n"; } + private static String composeRepositoriesElementString() { + return " \n" + + " \n"; + } + + private static String composePluginRepositoriesElementString() { + return " \n" + + " \n"; + } + private static String composeDependencyElementString(ArtifactRef artifact, Collection exclusions, String oldVersion) { StringBuilder sb = new StringBuilder(); sb.append(" \n"); diff --git a/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java b/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java index 60307da..e0ed601 100644 --- a/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java +++ b/plugin/src/test/java/org/wildfly/channelplugin/manipulation/PomManipulatorTestCase.java @@ -71,6 +71,34 @@ public void testInsertProperty() throws IOException, XMLStreamException, Manipul Assertions.assertThat(model.getProperties().getProperty("prop")).isEqualTo("value"); } + @Test + public void testInsertRepository() throws IOException, XMLStreamException, ManipulationException { + Model model = readModel(); + Assertions.assertThat(model.getRepositories()).isEmpty(); + Assertions.assertThat(model.getPluginRepositories()).isEmpty(); + + PomManipulator.injectRepository(eventReader, "repo", "https://maven/repo"); + + model = readModel(); + Assertions.assertThat(model.getRepositories().size()).isEqualTo(1); + Assertions.assertThat(model.getRepositories().get(0)) + .matches(r -> r.getId().equals("repo") && r.getUrl().equals("https://maven/repo")); + } + + @Test + public void testInsertPluginRepository() throws IOException, XMLStreamException, ManipulationException { + Model model = readModel(); + Assertions.assertThat(model.getRepositories()).isEmpty(); + Assertions.assertThat(model.getPluginRepositories()).isEmpty(); + + PomManipulator.injectRepository(eventReader, "repo", "https://maven/repo"); + + model = readModel(); + Assertions.assertThat(model.getRepositories().size()).isEqualTo(1); + Assertions.assertThat(model.getRepositories().get(0)) + .matches(r -> r.getId().equals("repo") && r.getUrl().equals("https://maven/repo")); + } + private Model readModel() throws IOException, ManipulationException { Path pomFile = Files.createTempFile("pom", "xml"); Files.write(pomFile, content.toString().getBytes());