Skip to content

Commit

Permalink
Fixing test for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ekryd committed May 21, 2022
1 parent 8098a52 commit a580cf6
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 16 deletions.
20 changes: 10 additions & 10 deletions sorter/src/main/java/sortpom/XmlOutputGenerator.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package sortpom;

import org.dom4j.*;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.Namespace;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultText;
import org.dom4j.tree.NamespaceStack;
import sortpom.exception.FailureException;
import sortpom.content.NewlineText;
import sortpom.exception.FailureException;
import sortpom.parameter.PluginParameters;
import sortpom.util.StringLineSeparatorWriter;

Expand Down Expand Up @@ -148,7 +154,7 @@ protected void writeNodeText(Node node) throws IOException {
if (isElementSpacePreserved(node.getParent())) {
super.writeNodeText(node);
} else {
// Otherwise trim the text content
// Otherwise, trim the text content
String text = node.getText();
super.write(new DefaultText(text == null ? null : text.trim()));
}
Expand Down Expand Up @@ -182,13 +188,7 @@ protected void writeAttributes(Element element) throws IOException {
parentNamespaceStack.push(prefix, uri);
writeNamespace(prefix, uri);
}
} else if (attName.equals("xmlns")) {
if (parentNamespaceStack.getDefaultNamespace() == null) {
String uri = attribute.getValue();
parentNamespaceStack.push(null, uri);
writeNamespace(null, uri);
}
} else {
} else if (!attName.equals("xmlns")) {
writeAttribute(attribute);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public String toString() {
}

/**
* This method will not perform anything. Flushing is only done when toString method is called
* Nope, no manual flushing
*/
@Override
public void flush() {
// Nope, no manual flushing
throw new UnsupportedOperationException();
}
}
57 changes: 56 additions & 1 deletion sorter/src/test/java/sortpom/XmlOutputGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package sortpom;

import org.dom4j.Document;
import org.dom4j.tree.DefaultAttribute;
import org.dom4j.tree.DefaultElement;
import org.dom4j.tree.DefaultNamespace;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import sortpom.exception.FailureException;
import sortpom.parameter.PluginParameters;

import java.io.IOException;

import static java.util.Collections.singletonList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
Expand All @@ -24,7 +28,6 @@ class XmlOutputGeneratorTest {

@Test
void simulateIOExceptionToTriggerExceptionMessage() {

Document document = spy(createXmlFragment());
// Simulate an IOException (a checked one, no less)
when(document.node(0)).thenAnswer(invocation -> {
Expand All @@ -42,5 +45,57 @@ void simulateIOExceptionToTriggerExceptionMessage() {

assertThat("Unexpected message", thrown.getMessage(), is(equalTo("Could not format pom files content")));
}

@Test
void definedParentNamespaceShouldBeReused() {
Document document = createXmlFragment();
String namespaceUri = "this is a uri";
document.getRootElement().setAttributes(singletonList(new DefaultAttribute("xmlns:namespace", namespaceUri)));

DefaultElement child = new DefaultElement("child");
child.add(new DefaultAttribute("attr1", "value", new DefaultNamespace("namespace", namespaceUri)));
document.getRootElement().add(child);

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

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(sortedXml, is(equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Gurka xmlns:namespace=\"this is a uri\">\n" +
"<child namespace:attr1=\"value\"></child>\n</Gurka>\n")));
}

@Test
void redefinedParentNamespaceShouldBeWrittenAgain() {
Document document = createXmlFragment();
document.getRootElement().setAttributes(singletonList(new DefaultAttribute("xmlns:namespace", "this is a uri")));

DefaultElement child = new DefaultElement("child");
child.add(new DefaultAttribute("attr1", "value", new DefaultNamespace("namespace", "another uri")));
document.getRootElement().add(child);

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

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(sortedXml, is(equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Gurka xmlns:namespace=\"this is a uri\">\n" +
"<child xmlns:namespace=\"another uri\" namespace:attr1=\"value\"></child>\n</Gurka>\n")));
}

@Test
void attributeCalledXmlnsShouldNotBePrinted() {
Document document = createXmlFragment();
document.getRootElement().setAttributes(singletonList(new DefaultAttribute("xmlns", "value")));

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

String sortedXml = xmlOutputGenerator.getSortedXml(document);
assertThat(sortedXml, is(equalTo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Gurka></Gurka>\n")));
}
}
14 changes: 14 additions & 0 deletions sorter/src/test/java/sortpom/content/IgnoreSectionTokenTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sortpom.content;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

class IgnoreSectionTokenTest {

@Test
void appendTextShouldNotBeSupported() {
IgnoreSectionToken ignoreSectionToken = new IgnoreSectionToken(null, null, "value");
assertThrows(UnsupportedOperationException.class, () -> ignoreSectionToken.appendText("whatever"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ void toStringShouldFlushBuffer() {

@Test
void testWriteDeprecated1() {

final Executable testMethod = () -> writer.write(new char[0]);

final UnsupportedOperationException thrown = assertThrows(UnsupportedOperationException.class, testMethod);
Expand All @@ -77,7 +76,6 @@ void testWriteDeprecated1() {

@Test
void testWriteDeprecated2() {

final Executable testMethod = () -> writer.write("", 0, 0);

final UnsupportedOperationException thrown = assertThrows(UnsupportedOperationException.class, testMethod);
Expand All @@ -87,12 +85,15 @@ void testWriteDeprecated2() {

@Test
void testWriteDeprecated3() {

final Executable testMethod = () -> writer.write(new char[0], 0, 0);

final UnsupportedOperationException thrown = assertThrows(UnsupportedOperationException.class, testMethod);

assertThat(thrown.getMessage(), is(nullValue()));
}

@Test
void flushShouldNotBeSupported() {
assertThrows(UnsupportedOperationException.class, writer::flush);
}
}

0 comments on commit a580cf6

Please sign in to comment.