Skip to content

Commit

Permalink
Remove leftover indentation text nodes in DOM when elements are remov…
Browse files Browse the repository at this point in the history
…ed (#130)

* Make sure no extra line breaks are introduced

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

Co-authored-by: Quentin <[email protected]>

* Remove extra indentation text nodes

When removing an element, extra text nodes may be still left over, this ensures these extra text nodes are removed

* Also test removing dependency

* more tests for investigation

* Only clone when text is not empty

* Revert "Only clone when text is not empty"

This reverts commit bfcb8e2.

* Last text is constant

* Remove extra indentation text nodes when an element is removed

* Refactor Counter creation

* Make MavenJDOMWriter final

---------

Co-authored-by: Quentin <[email protected]>
Co-authored-by: Aurélien Mino <[email protected]>
  • Loading branch information
3 people authored Mar 23, 2024
1 parent c2b30a4 commit af89d8d
Show file tree
Hide file tree
Showing 7 changed files with 599 additions and 144 deletions.
325 changes: 181 additions & 144 deletions src/main/java/io/fabric8/maven/MavenJDOMWriter.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>jhlite-test-maven-project</artifactId>
<version>0.0.1</version>
<name>Test project</name>
<packaging>pom</packaging>

<properties>
<json-web-token.version>0.11.5</json-web-token.version>
<logstash-logback-encoder.version>7.2</logstash-logback-encoder.version>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${json-web-token.version}</version>
<classifier>classif</classifier>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>jhlite-test-maven-project</artifactId>
<version>0.0.1</version>
<name>Test project</name>
<packaging>pom</packaging>

<properties>
<json-web-token.version>0.11.5</json-web-token.version>
<logstash-logback-encoder.version>7.2</logstash-logback-encoder.version>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${json-web-token.version}</version>
<classifier>classif</classifier>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>jhlite-test-maven-project</artifactId>
<version>0.0.1</version>
<name>Test project</name>
<packaging>pom</packaging>

<properties>
<json-web-token.version>0.11.5</json-web-token.version>
<logstash-logback-encoder.version>7.2</logstash-logback-encoder.version>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${json-web-token.version}</version>
<classifier>classif</classifier>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
31 changes: 31 additions & 0 deletions src/test/java/io/fabric8/maven/MavenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.xmlunit.assertj.XmlAssert;

/**
Expand Down Expand Up @@ -349,4 +351,33 @@ void should_support_no_line_separator() {
Approvals.verify(sw.toString());
}

@Test
void should_not_add_extra_line_break() throws Exception {
Path pom = Paths.get(getClass().getResource("extra-line-pom.xml").toURI());
Model model = Maven.readModel(pom);
model.getDependencyManagement().getDependencies().remove(0);
Dependency dep = new Dependency();
dep.setGroupId("org.springframework.boot");
dep.setArtifactId("spring-boot-dependencies");
dep.setVersion("${spring-boot.version}");
dep.setScope("import");
model.getDependencyManagement().addDependency(dep);
StringWriter sw = new StringWriter();
Maven.writeModel(model, sw);
Approvals.verify(sw.toString());
}

@ValueSource(strings = { "extra-line-pom.xml", "extra-line-pom_alt.xml" })
@ParameterizedTest
void should_not_add_extra_line_break_alt(String inputFile) throws Exception {
Path pom = Paths.get(getClass().getResource(inputFile).toURI());
Model model = Maven.readModel(pom);
Dependency dependency = new Dependency();
dependency.setGroupId("org.springframework.boot");
dependency.setArtifactId("spring-boot-starter-parent");
model.getDependencies().add(1, dependency);
StringWriter sw = new StringWriter();
Maven.writeModel(model, sw);
Approvals.verify(sw.toString(), Approvals.NAMES.withParameters(inputFile));
}
}
76 changes: 76 additions & 0 deletions src/test/resources/io/fabric8/maven/extra-line-pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8" ?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<artifactId>jhlite-test-maven-project</artifactId>
<version>0.0.1</version>
<name>Test project</name>
<packaging>pom</packaging>

<properties>
<json-web-token.version>0.11.5</json-web-token.version>
<logstash-logback-encoder.version>7.2</logstash-logback-encoder.version>
<spring-boot.version>2.7.1</spring-boot.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>${springdoc-openapi.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${json-web-token.version}</version>
<classifier>classif</classifier>
<scope>test</scope>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Loading

0 comments on commit af89d8d

Please sign in to comment.