Skip to content

Commit

Permalink
Merge pull request #15 from TomasHofman/injecting-repos-fix
Browse files Browse the repository at this point in the history
Do not skip injecting repositories if the repositories element doesn'…
  • Loading branch information
TomasHofman authored Jun 9, 2024
2 parents 14a845c + d276f3d commit bc84e8e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,79 @@ public void injectManagedDependency(ArtifactRef dependency, Collection<ProjectRe
injectManagedDependency(eventReader, dependency, exclusions, oldVersion);
}

static void injectRepositoriesSection(ModifiedPomXMLEventReader eventReader) throws XMLStreamException {
eventReader.rewind();

Stack<String> stack = new Stack<String>();
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()
+ "</project>"
);
eventReader.clearMark(0);
return;
}

path = stack.pop();
}
}
}

static void injectPluginRepositoriesSection(ModifiedPomXMLEventReader eventReader) throws XMLStreamException {
eventReader.rewind();

Stack<String> stack = new Stack<String>();
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()
+ "</project>"
);
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<String> stack = new Stack<String>();
Expand Down Expand Up @@ -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<String> stack = new Stack<String>();
Stack<String> stack = new Stack<>();
String path = "";

while (eventReader.hasNext()) {
Expand Down Expand Up @@ -289,6 +363,16 @@ private static String composeDependenciesElementString() {
+ " </dependencies>\n";
}

private static String composeRepositoriesElementString() {
return " <repositories>\n"
+ " </repositories>\n";
}

private static String composePluginRepositoriesElementString() {
return " <pluginRepositories>\n"
+ " </pluginRepositories>\n";
}

private static String composeDependencyElementString(ArtifactRef artifact, Collection<ProjectRef> exclusions, String oldVersion) {
StringBuilder sb = new StringBuilder();
sb.append(" <dependency>\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit bc84e8e

Please sign in to comment.