Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing new endWithNewline parameter + test. #403

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
<<: *shared
jdk21:
docker:
- image: cimg/openjdk:21.0
- image: cimg/openjdk:21.0.2
<<: *shared
jdk17:
docker:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ assert log.text.contains('lineSeparator = ')
assert log.text.contains('expandEmptyElements = true')
assert log.text.contains('spaceBeforeCloseEmptyElement = false')
assert log.text.contains('keepBlankLines = true')
assert log.text.contains('endWithNewline = true')
assert log.text.contains('nrOfIndentSpace = 2')
assert log.text.contains('ignoreLineSeparators = true')
assert log.text.contains('indentBlankLines = false')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ sortpom:sort
User property: sort.encoding
Encoding for the files.

endWithNewline (Default: true)
User property: sort.endWithNewline
Whether to ensure that sorted pom ends with a newline.

expandEmptyElements (Default: true)
User property: sort.expandEmptyElements
Should an empty xml element be expanded with start and end tag, or be
Expand Down Expand Up @@ -178,6 +182,10 @@ sortpom:verify
User property: sort.encoding
Encoding for the files.

endWithNewline (Default: true)
User property: sort.endWithNewline
Whether to ensure that sorted pom ends with a newline.

expandEmptyElements (Default: true)
User property: sort.expandEmptyElements
Should an empty xml element be expanded with start and end tag, or be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<configuration>
<predefinedSortOrder>default_1_0_0</predefinedSortOrder>
<keepBlankLines>false</keepBlankLines>
<endWithNewline>false</endWithNewline>
<spaceBeforeCloseEmptyElement>true</spaceBeforeCloseEmptyElement>
<expandEmptyElements>false</expandEmptyElements>
</configuration>
Expand All @@ -39,4 +40,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
5 changes: 3 additions & 2 deletions maven-plugin/src/it/predefined-sort-order/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<simple-prop/>
</properties>

<build>
<plugins>
<plugin>
Expand All @@ -32,8 +32,9 @@
<configuration>
<predefinedSortOrder>default_1_0_0</predefinedSortOrder>
<keepBlankLines>false</keepBlankLines>
<endWithNewline>false</endWithNewline>
<spaceBeforeCloseEmptyElement>true</spaceBeforeCloseEmptyElement>

<expandEmptyElements>false</expandEmptyElements>
</configuration>
</execution>
Expand Down
4 changes: 4 additions & 0 deletions maven-plugin/src/main/java/sortpom/AbstractParentMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ abstract class AbstractParentMojo extends AbstractMojo {
@Parameter(property = "sort.keepBlankLines", defaultValue = "true")
boolean keepBlankLines;

/** Whether to ensure that sorted pom ends with a newline. */
@Parameter(property = "sort.endWithNewline", defaultValue = "true")
boolean endWithNewline;

/**
* Number of space characters to use as indentation. A value of -1 indicates that tab character
* should be used instead.
Expand Down
3 changes: 2 additions & 1 deletion maven-plugin/src/main/java/sortpom/SortMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
lineSeparator,
expandEmptyElements,
spaceBeforeCloseEmptyElement,
keepBlankLines)
keepBlankLines,
endWithNewline)
.setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation)
.setSortOrder(sortOrderFile, predefinedSortOrder)
.setSortEntities(
Expand Down
3 changes: 2 additions & 1 deletion maven-plugin/src/main/java/sortpom/VerifyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public void setup(SortPomLogger mavenLogger) throws MojoFailureException {
lineSeparator,
expandEmptyElements,
spaceBeforeCloseEmptyElement,
keepBlankLines)
keepBlankLines,
endWithNewline)
.setIndent(nrOfIndentSpace, indentBlankLines, indentSchemaLocation)
.setSortOrder(sortOrderFile, predefinedSortOrder)
.setSortEntities(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,15 @@ void parameterSortPropertiesShouldEndUpInWrapperFactoryImpl() {
}

@Test
void parameterKeepBlankLineShouldEndUpInXmlProcessor() {
void parameterKeepBlankLineShouldEndUpInTextWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean("keepBlankLines", textWrapperCreator);
}

@Test
void parameterEndWithNewlineShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean("endWithNewline", xmlOutputGenerator);
}

@Test
void parameterIndentBlankLineShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ void pomFileParameter() {

@Test
void createBackupFileParameter() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("createBackupFile", sortPomService);
assertParameterMoveFromMojoToRestOfApplicationForBoolean("createBackupFile", sortPomService);
}

@Test
void keepTimestampParameter() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("keepTimestamp", fileUtil);
assertParameterMoveFromMojoToRestOfApplicationForBoolean("keepTimestamp", fileUtil);
}

@Test
Expand Down Expand Up @@ -106,13 +106,13 @@ void parameterNrOfIndentSpaceShouldEndUpInXmlProcessor() {

@Test
void expandEmptyElementsParameter() {
testParameterMoveFromMojoToRestOfApplicationForBoolean(
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"expandEmptyElements", xmlOutputGenerator);
}

@Test
void spaceBeforeCloseEmptyElementParameter() {
testParameterMoveFromMojoToRestOfApplicationForBoolean(
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"spaceBeforeCloseEmptyElement", xmlOutputGenerator);
}

Expand Down Expand Up @@ -160,32 +160,40 @@ void parameterSortPluginsShouldEndUpInWrapperFactoryImpl() {

@Test
void parameterSortModulesShouldEndUpInWrapperFactoryImpl() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("sortModules", elementWrapperCreator);
assertParameterMoveFromMojoToRestOfApplicationForBoolean("sortModules", elementWrapperCreator);
}

@Test
void parameterSortExecutionsShouldEndUpInWrapperFactoryImpl() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("sortExecutions", elementWrapperCreator);
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"sortExecutions", elementWrapperCreator);
}

@Test
void parameterSortPropertiesShouldEndUpInWrapperFactoryImpl() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("sortProperties", elementWrapperCreator);
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"sortProperties", elementWrapperCreator);
}

@Test
void parameterKeepBlankLineShouldEndUpInXmlProcessor() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("keepBlankLines", textWrapperCreator);
void parameterKeepBlankLineShouldEndUpInTextWrapperCreator() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean("keepBlankLines", textWrapperCreator);
}

@Test
void parameterEndWithNewlineShouldEndUpInXmlProcessor() {
assertParameterMoveFromMojoToRestOfApplicationForBoolean("endWithNewline", xmlOutputGenerator);
}

@Test
void parameterIndentBlankLineShouldEndUpInXmlProcessor() {
testParameterMoveFromMojoToRestOfApplicationForBoolean("indentBlankLines", xmlOutputGenerator);
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"indentBlankLines", xmlOutputGenerator);
}

@Test
void parameterIndentSchemaLocationShouldEndUpInXmlProcessor() {
testParameterMoveFromMojoToRestOfApplicationForBoolean(
assertParameterMoveFromMojoToRestOfApplicationForBoolean(
"indentSchemaLocation", xmlOutputGenerator);
}

Expand Down Expand Up @@ -216,7 +224,7 @@ private void assertParameterMoveFromMojoToRestOfApplication(
}
}

private void testParameterMoveFromMojoToRestOfApplicationForBoolean(
private void assertParameterMoveFromMojoToRestOfApplicationForBoolean(
String parameterName, Object... whereParameterCanBeFound) {
new ReflectionHelper(verifyMojo).setField(parameterName, true);

Expand Down
25 changes: 24 additions & 1 deletion sorter/src/main/java/sortpom/output/PatchedXMLWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.IOException;
import java.io.Writer;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.OutputFormat;
Expand All @@ -19,18 +20,21 @@ class PatchedXMLWriter extends XMLWriter {
private final boolean indentBlankLines;
private final boolean indentSchemaLocation;
private final boolean spaceBeforeCloseEmptyElement;
private final boolean endWithNewline;

public PatchedXMLWriter(
Writer writer,
OutputFormat format,
boolean spaceBeforeCloseEmptyElement,
boolean indentBlankLines,
boolean indentSchemaLocation) {
boolean indentSchemaLocation,
boolean endWithNewline) {
super(writer, format);
this.format = format;
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
this.endWithNewline = endWithNewline;
}

/** Handle spaceBeforeCloseEmptyElement option */
Expand Down Expand Up @@ -58,6 +62,25 @@ protected void writeProcessingInstruction(ProcessingInstruction pi) throws IOExc
lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
}

@Override
public void write(Document doc) throws IOException {
writeDeclaration();

if (doc.getDocType() != null) {
indent();
writeDocType(doc.getDocType());
}

for (int i = 0, size = doc.nodeCount(); i < size; i++) {
Node node = doc.node(i);
writeNode(node);
}

if (endWithNewline) {
writePrintln();
}
}

/** Handle Custom NewLineTest node and potential indent of empty line */
@Override
protected void writeNodeText(Node node) throws IOException {
Expand Down
7 changes: 5 additions & 2 deletions sorter/src/main/java/sortpom/output/XmlOutputGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class XmlOutputGenerator {
private boolean indentSchemaLocation;
private boolean spaceBeforeCloseEmptyElement;
private String lineSeparator;
private boolean endWithNewline;

/** Setup default configuration */
public void setup(PluginParameters pluginParameters) {
Expand All @@ -28,7 +29,8 @@ public void setup(PluginParameters pluginParameters) {
this.indentBlankLines = pluginParameters.indentBlankLines;
this.indentSchemaLocation = pluginParameters.indentSchemaLocation;
this.spaceBeforeCloseEmptyElement = pluginParameters.spaceBeforeCloseEmptyElement;
lineSeparator = pluginParameters.lineSeparatorUtil.toString();
this.lineSeparator = pluginParameters.lineSeparatorUtil.toString();
this.endWithNewline = pluginParameters.endWithNewline;
}

/**
Expand All @@ -45,7 +47,8 @@ public String getSortedXml(Document newDocument) {
createPrettyFormat(),
spaceBeforeCloseEmptyElement,
indentBlankLines,
indentSchemaLocation);
indentSchemaLocation,
endWithNewline);
xmlWriter.write(newDocument);
writer.writeDelayedNewline();
return writer.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class PluginParameters {
public final boolean sortModules;
public final boolean sortExecutions;
public final boolean keepBlankLines;
public final boolean endWithNewline;
public final boolean indentBlankLines;
public final boolean indentSchemaLocation;
public final VerifyFailType verifyFailType;
Expand All @@ -40,6 +41,7 @@ private PluginParameters(
boolean expandEmptyElements,
boolean spaceBeforeCloseEmptyElement,
boolean keepBlankLines,
boolean endWithNewline,
String indentCharacters,
boolean indentBlankLines,
boolean indentSchemaLocation,
Expand Down Expand Up @@ -75,6 +77,7 @@ private PluginParameters(
this.sortModules = sortModules;
this.sortExecutions = sortExecutions;
this.keepBlankLines = keepBlankLines;
this.endWithNewline = endWithNewline;
this.indentBlankLines = indentBlankLines;
this.indentSchemaLocation = indentSchemaLocation;
this.verifyFailType = verifyFailType;
Expand Down Expand Up @@ -111,6 +114,7 @@ public static class Builder {
private boolean sortModules;
private boolean sortExecutions;
private boolean keepBlankLines;
private boolean endWithNewline;
private VerifyFailType verifyFailType;
private VerifyFailOnType verifyFailOn;
private boolean ignoreLineSeparators;
Expand Down Expand Up @@ -148,11 +152,13 @@ public Builder setFormatting(
final String lineSeparator,
final boolean expandEmptyElements,
final boolean spaceBeforeCloseEmptyElement,
final boolean keepBlankLines) {
final boolean keepBlankLines,
final boolean endWithNewline) {
this.lineSeparatorUtil = new LineSeparatorUtil(lineSeparator);
this.expandEmptyElements = expandEmptyElements;
this.spaceBeforeCloseEmptyElement = spaceBeforeCloseEmptyElement;
this.keepBlankLines = keepBlankLines;
this.endWithNewline = endWithNewline;
return this;
}

Expand Down Expand Up @@ -217,6 +223,7 @@ public PluginParameters build() {
expandEmptyElements,
spaceBeforeCloseEmptyElement,
keepBlankLines,
endWithNewline,
indentCharacters,
indentBlankLines,
indentSchemaLocation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void simulateIOExceptionToTriggerExceptionMessage() {

XmlOutputGenerator xmlOutputGenerator = new XmlOutputGenerator();
xmlOutputGenerator.setup(
PluginParameters.builder().setFormatting("\n", true, true, false).build());
PluginParameters.builder().setFormatting("\n", true, true, false, true).build());

final Executable testMethod = () -> xmlOutputGenerator.getSortedXml(document);

Expand All @@ -64,7 +64,7 @@ void definedParentNamespaceShouldBeReused() {

XmlOutputGenerator xmlOutputGenerator = new XmlOutputGenerator();
xmlOutputGenerator.setup(
PluginParameters.builder().setFormatting("\n", true, true, false).build());
PluginParameters.builder().setFormatting("\n", true, true, false, true).build());

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(
Expand All @@ -89,7 +89,7 @@ void redefinedParentNamespaceShouldBeWrittenAgain() {

XmlOutputGenerator xmlOutputGenerator = new XmlOutputGenerator();
xmlOutputGenerator.setup(
PluginParameters.builder().setFormatting("\n", true, true, false).build());
PluginParameters.builder().setFormatting("\n", true, true, false, true).build());

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(
Expand All @@ -107,7 +107,7 @@ void attributeCalledXmlnsShouldNotBePrinted() {

XmlOutputGenerator xmlOutputGenerator = new XmlOutputGenerator();
xmlOutputGenerator.setup(
PluginParameters.builder().setFormatting("\n", true, true, false).build());
PluginParameters.builder().setFormatting("\n", true, true, false, true).build());

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void illegalEncodingWhenGeneratingPomFileShouldWork() {
xmlOutputGenerator.setup(
PluginParameters.builder()
.setEncoding("gurka-2000")
.setFormatting("\n", true, true, false)
.setFormatting("\n", true, true, false, true)
.setIndent(2, false, false)
.build());

Expand Down
Loading