Skip to content

Commit 0595452

Browse files
murdosgastaldi
andauthored
(fix) Element was inserted at the wrong location (#114)
* (fix) Element was inserted at the wrong location * Add minor enhancements --------- Co-authored-by: George Gastaldi <[email protected]>
1 parent a992d18 commit 0595452

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

src/main/java/io/fabric8/maven/MavenJDOMWriter.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ class MavenJDOMWriter {
8080
/**
8181
* Field factory.
8282
*/
83-
private DefaultJDOMFactory factory;
83+
private final DefaultJDOMFactory factory;
8484

8585
/**
8686
* Field lineSeparator.
8787
*/
88-
private String lineSeparator;
88+
private final String lineSeparator;
8989

9090
// ----------------/
9191
// - Constructors -/
@@ -175,7 +175,7 @@ protected Element findAndReplaceSimpleElement(Counter counter, Element parent, S
175175
}
176176
}
177177

178-
boolean shouldExist = (text != null) && (text.trim().length() > 0);
178+
boolean shouldExist = (text != null) && (!text.trim().isEmpty());
179179
Element element = updateElement(counter, parent, name, shouldExist);
180180
if (shouldExist) {
181181
element.setText(text);
@@ -262,7 +262,7 @@ protected void insertAtPreferredLocation(Element parent, Element child, Counter
262262
Iterator it = parent.getContent().iterator();
263263
Text lastText = null;
264264
int offset = 0;
265-
while (it.hasNext() && (elementCounter <= counter.getCurrentIndex())) {
265+
while (it.hasNext() && (elementCounter < counter.getCurrentIndex())) {
266266
Object next = it.next();
267267
offset = offset + 1;
268268
if (next instanceof Element) {
@@ -274,17 +274,13 @@ protected void insertAtPreferredLocation(Element parent, Element child, Counter
274274
lastText = (Text) next;
275275
}
276276
}
277-
if ((lastText != null) && (lastText.getTextTrim().length() == 0)) {
278-
lastText = (Text) lastText.clone();
277+
if ((lastText != null) && (lastText.getTextTrim().isEmpty())) {
278+
lastText = lastText.clone();
279279
} else {
280-
StringBuilder starter = new StringBuilder(lineSeparator);
281-
for (int i = 0; i < counter.getDepth(); i++) {
282-
starter.append(" ");
283-
}
284-
lastText = factory.text(starter.toString());
280+
lastText = factory.text(lineSeparator + " ".repeat(Math.max(0, counter.getDepth())));
285281
}
286282
if (parent.getContentSize() == 0) {
287-
Text finalText = (Text) lastText.clone();
283+
Text finalText = lastText.clone();
288284
finalText.setText(finalText.getText().substring(0, finalText.getText().length() - " ".length()));
289285
parent.addContent(contentIndex, finalText);
290286
}

src/test/java/io/fabric8/maven/MavenTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.util.Collections;
1414
import java.util.Properties;
1515

16+
import org.apache.maven.model.Dependency;
1617
import org.apache.maven.model.Model;
1718
import org.apache.maven.model.Scm;
1819
import org.assertj.core.api.Assertions;
@@ -147,4 +148,27 @@ void should_write_scm_tag(@TempDir Path tempDir) throws Exception {
147148

148149
}
149150

151+
@Test
152+
void should_respect_insertion_order(@TempDir Path tempDir) throws Exception {
153+
URL resource = getClass().getResource("parent/parent-pom.xml");
154+
Path basePom = Paths.get(resource.toURI());
155+
Model model = Maven.readModel(basePom);
156+
157+
Path updatedPom = tempDir.resolve("updated-pom.xml");
158+
Files.copy(basePom, updatedPom);
159+
160+
Dependency dependency = model.getDependencies().stream().filter(d -> d.getArtifactId().equals("quarkus-junit5-internal")).findFirst().orElseThrow();
161+
dependency.setVersion("1.0.0");
162+
dependency.setOptional("true");
163+
Maven.writeModel(model, updatedPom);
164+
165+
assertThat(Files.readString(updatedPom))
166+
.contains("<dependency>\n" +
167+
" <groupId>io.quarkus</groupId>\n" +
168+
" <artifactId>quarkus-junit5-internal</artifactId>\n" +
169+
" <version>1.0.0</version>\n" +
170+
" <scope>test</scope>\n" +
171+
" <optional>true</optional>\n" +
172+
" </dependency>");
173+
}
150174
}

0 commit comments

Comments
 (0)