aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrnveach <rveach02@gmail.com>2017-10-04 15:01:19 -0400
committerRoman Ivanov <romani@users.noreply.github.com>2017-12-25 14:04:06 -0800
commitf3a14ddefe32e4fd766007943237ba2a0a0933a4 (patch)
tree52d9592c5d482fa44bf5baa8b3af2fd15247f186
parent6e74c7d42d90264899ef5ab78712755a3ef1811f (diff)
downloadcheckstyle-f3a14ddefe32e4fd766007943237ba2a0a0933a4.tar.gz
Issue #5141: added xml verification for tests
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java18
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/AbstractTreeTestSupport.java18
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/AbstractXmlTestSupport.java169
-rw-r--r--src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java189
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLogger.xml5
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerEmpty.xml3
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerError.xml6
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorModuleId.xml6
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorNullFileName.xml4
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorZeroColumn.xml6
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerException.xml6
-rw-r--r--src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerExceptionNullFileName.xml4
12 files changed, 265 insertions, 169 deletions
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java
index 9b79d8f59..849cf8981 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractPathTestSupport.java
@@ -21,8 +21,15 @@ package com.puppycrawl.tools.checkstyle;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Paths;
public abstract class AbstractPathTestSupport {
+ protected static final String LF_REGEX = "\\\\n";
+
+ protected static final String CRLF_REGEX = "\\\\r\\\\n";
+
/**
* Returns the exact location for the package where the file is present.
* @return path for the package name for the file.
@@ -46,4 +53,15 @@ public abstract class AbstractPathTestSupport {
protected final String getResourcePath(String filename) {
return "/" + getPackageLocation() + "/" + filename;
}
+
+ /** Reads the contents of a file.
+ * @param filename the name of the file whose contents are to be read
+ * @return contents of the file with all {@code \r\n} replaced by {@code \n}
+ * @throws IOException if I/O exception occurs while reading
+ */
+ protected static String readFile(String filename) throws IOException {
+ return new String(Files.readAllBytes(
+ Paths.get(filename)), StandardCharsets.UTF_8)
+ .replaceAll(CRLF_REGEX, LF_REGEX);
+ }
}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractTreeTestSupport.java b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractTreeTestSupport.java
index dd676666b..01113fa95 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractTreeTestSupport.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractTreeTestSupport.java
@@ -23,16 +23,9 @@ import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
-import java.nio.file.Files;
-import java.nio.file.Paths;
public abstract class AbstractTreeTestSupport extends AbstractPathTestSupport {
- protected static final String LF_REGEX = "\\\\n";
-
- protected static final String CRLF_REGEX = "\\\\r\\\\n";
-
/**
* Returns canonical path for the file with the given file name.
* The path is formed base on the non-compilable resources location.
@@ -118,15 +111,4 @@ public abstract class AbstractTreeTestSupport extends AbstractPathTestSupport {
assertEquals("Generated tree from the javadoc file should match the pre-defined tree",
expectedContents, actualContents);
}
-
- /** Reads the contents of a file.
- * @param filename the name of the file whose contents are to be read
- * @return contents of the file with all {@code \r\n} replaced by {@code \n}
- * @throws IOException if I/O exception occurs while reading
- */
- protected static String readFile(String filename) throws IOException {
- return new String(Files.readAllBytes(
- Paths.get(filename)), StandardCharsets.UTF_8)
- .replaceAll(CRLF_REGEX, LF_REGEX);
- }
}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/AbstractXmlTestSupport.java b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractXmlTestSupport.java
new file mode 100644
index 000000000..75973d21c
--- /dev/null
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/AbstractXmlTestSupport.java
@@ -0,0 +1,169 @@
+////////////////////////////////////////////////////////////////////////////////
+// checkstyle: Checks Java source code for adherence to a set of rules.
+// Copyright (C) 2001-2017 the original author or authors.
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+////////////////////////////////////////////////////////////////////////////////
+
+package com.puppycrawl.tools.checkstyle;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Set;
+import java.util.function.BiPredicate;
+
+import javax.xml.parsers.ParserConfigurationException;
+
+import org.junit.Assert;
+import org.w3c.dom.Document;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+
+import com.puppycrawl.tools.checkstyle.internal.utils.XmlUtil;
+
+public abstract class AbstractXmlTestSupport extends AbstractModuleTestSupport {
+
+ protected static Document getOutputStreamXml(ByteArrayOutputStream outputStream)
+ throws ParserConfigurationException {
+ final String xml = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
+
+ return XmlUtil.getRawXml("audit output", xml, xml);
+ }
+
+ protected static void verifyXml(String expectedOutputFile,
+ ByteArrayOutputStream actualOutputStream, String... messages) throws Exception {
+ verifyXml(expectedOutputFile, actualOutputStream, null, messages);
+ }
+
+ protected static void verifyXml(String expectedOutputFile,
+ ByteArrayOutputStream actualOutputStream,
+ BiPredicate<Node, Node> ordered, String... messages) throws Exception {
+ String expectedContents = readFile(expectedOutputFile);
+
+ for (int i = 0; i < messages.length; i++) {
+ expectedContents = expectedContents.replace("$" + i, messages[i]);
+ }
+
+ final Document expectedDocument = XmlUtil.getRawXml("audit output", expectedContents,
+ expectedContents);
+ final Document actualDocument = getOutputStreamXml(actualOutputStream);
+
+ verifyXmlNode(expectedDocument, actualDocument, "/", ordered);
+ }
+
+ private static void verifyXmlNodes(Node expected, Node actual, String path,
+ BiPredicate<Node, Node> ordered) {
+ final Node expectedFirstChild = expected.getFirstChild();
+ final Node actualFirstChild = actual.getFirstChild();
+
+ if (expectedFirstChild == null) {
+ Assert.assertNull("no children nodes should exist: " + path, actualFirstChild);
+ Assert.assertEquals("text should be the same: " + path, expected.getNodeValue(),
+ actual.getNodeValue());
+ }
+ else {
+ Assert.assertNotNull("children nodes should exist: " + path, actualFirstChild);
+
+ if (ordered == null) {
+ Node actualChild = actualFirstChild;
+
+ for (Node expectedChild = expectedFirstChild; expectedChild != null;
+ expectedChild = expectedChild.getNextSibling()) {
+ verifyXmlNode(expectedChild, actualChild, path, ordered);
+
+ actualChild = actualChild.getNextSibling();
+ }
+
+ Assert.assertNull("node have same number of children: " + path, actualChild);
+ }
+ else {
+ final Set<Node> expectedChildren = XmlUtil.getChildrenElements(expected);
+ final Set<Node> actualChildren = XmlUtil.getChildrenElements(actual);
+
+ Assert.assertEquals("node have same number of children: " + path,
+ expectedChildren.size(), actualChildren.size());
+
+ for (Node expectedChild : expectedChildren) {
+ Node foundChild = null;
+
+ for (Node actualChild : actualChildren) {
+ if (ordered.test(expectedChild, actualChild)) {
+ foundChild = actualChild;
+ break;
+ }
+ }
+
+ Assert.assertNotNull("node should exist: " + path + expectedChild.getNodeName()
+ + "/", foundChild);
+
+ verifyXmlNode(expectedChild, foundChild, path, ordered);
+ }
+ }
+ }
+ }
+
+ private static void verifyXmlNode(Node expected, Node actual, String path,
+ BiPredicate<Node, Node> ordered) {
+ if (expected == null) {
+ Assert.assertNull("no node should exist: " + path, actual);
+ }
+ else {
+ Assert.assertNotNull("node should exist: " + path, actual);
+ Assert.assertEquals("node should have same name: " + path, expected.getNodeName(),
+ actual.getNodeName());
+ Assert.assertEquals("node should have same type: " + path, expected.getNodeType(),
+ actual.getNodeType());
+
+ final String newPath = path + expected.getNodeName() + "/";
+
+ verifyXmlAttributes(expected.getAttributes(), actual.getAttributes(), newPath);
+
+ verifyXmlNodes(expected, actual, newPath, ordered);
+ }
+ }
+
+ private static void verifyXmlAttributes(NamedNodeMap expected, NamedNodeMap actual,
+ String path) {
+ if (expected == null) {
+ Assert.assertNull("no attributes should exist: " + path, actual);
+ }
+ else {
+ Assert.assertNotNull("attributes should exist: " + path, actual);
+
+ for (int i = 0; i < expected.getLength(); i++) {
+ verifyXmlAttribute(expected.item(i), actual.item(i), path);
+ }
+
+ Assert.assertEquals("node have same number of attributes: " + path,
+ expected.getLength(), actual.getLength());
+ }
+ }
+
+ private static void verifyXmlAttribute(Node expected, Node actual, String path) {
+ final String expectedName = expected.getNodeName();
+
+ Assert.assertNotNull("attribute value for '" + expectedName + "' should not be null: "
+ + path, actual);
+
+ Assert.assertEquals("attribute name should match: " + path, expectedName,
+ actual.getNodeName());
+
+ // ignore checkstyle version in xml as it changes each release
+ if (!"/#document/checkstyle".equals(path) && !"version".equals(expectedName)) {
+ Assert.assertEquals("attribute value for '" + expectedName + "' should match: " + path,
+ expected.getNodeValue(), actual.getNodeValue());
+ }
+ }
+}
diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
index fc95ac7e9..5d3a7ffd2 100644
--- a/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
+++ b/src/test/java/com/puppycrawl/tools/checkstyle/XMLLoggerTest.java
@@ -24,15 +24,8 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import java.io.BufferedReader;
-import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.InputStreamReader;
import java.io.PrintWriter;
-import java.nio.charset.StandardCharsets;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.regex.Pattern;
import org.junit.Test;
@@ -42,17 +35,21 @@ import com.puppycrawl.tools.checkstyle.api.CheckstyleException;
import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
import com.puppycrawl.tools.checkstyle.internal.utils.CloseAndFlushTestByteArrayOutputStream;
-import com.puppycrawl.tools.checkstyle.utils.CommonUtils;
/**
* Enter a description of class XMLLoggerTest.java.
* @author Rick Giles
*/
// -@cs[AbbreviationAsWordInName] Test should be named as its main class.
-public class XMLLoggerTest {
+public class XMLLoggerTest extends AbstractXmlTestSupport {
private final CloseAndFlushTestByteArrayOutputStream outStream =
new CloseAndFlushTestByteArrayOutputStream();
+ @Override
+ protected String getPackageLocation() {
+ return "com/puppycrawl/tools/checkstyle/xmllogger";
+ }
+
@Test
public void testEncode()
throws IOException {
@@ -117,7 +114,7 @@ public class XMLLoggerTest {
@Test
public void testCloseStream()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream,
AutomaticBean.OutputStreamOptions.CLOSE);
logger.auditStarted(null);
@@ -125,13 +122,12 @@ public class XMLLoggerTest {
assertEquals("Invalid close count", 1, outStream.getCloseCount());
- final String[] expectedLines = CommonUtils.EMPTY_STRING_ARRAY;
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
}
@Test
public void testNoCloseStream()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream,
AutomaticBean.OutputStreamOptions.NONE);
logger.auditStarted(null);
@@ -140,42 +136,33 @@ public class XMLLoggerTest {
assertEquals("Invalid close count", 0, outStream.getCloseCount());
outStream.close();
- final String[] expectedLines = CommonUtils.EMPTY_STRING_ARRAY;
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
}
@Test
public void testFileStarted()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final AuditEvent ev = new AuditEvent(this, "Test.java");
logger.fileStarted(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLogger.xml"), outStream);
}
@Test
public void testFileFinished()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final AuditEvent ev = new AuditEvent(this, "Test.java");
logger.fileFinished(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLogger.xml"), outStream);
}
@Test
- public void testAddError() throws IOException {
+ public void testAddError() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -185,17 +172,11 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, "Test.java", message);
logger.addError(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
- + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerError.xml"), outStream, message.getMessage());
}
@Test
- public void testAddErrorWithNullFileName() throws IOException {
+ public void testAddErrorWithNullFileName() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -205,15 +186,12 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, null, message);
logger.addError(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
- + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerErrorNullFileName.xml"), outStream,
+ message.getMessage());
}
@Test
- public void testAddErrorModuleId() throws IOException {
+ public void testAddErrorModuleId() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -223,17 +201,11 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, "Test.java", message);
logger.addError(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
- + " source=\"module\"/>",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerErrorModuleId.xml"), outStream, message.getMessage());
}
@Test
- public void testAddErrorOnZeroColumns() throws IOException {
+ public void testAddErrorOnZeroColumns() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -243,17 +215,12 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, "Test.java", message);
logger.addError(ev);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "<error line=\"1\" severity=\"error\" message=\"key\""
- + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerErrorZeroColumn.xml"), outStream,
+ message.getMessage());
}
@Test
- public void testAddIgnored() throws IOException {
+ public void testAddIgnored() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -263,13 +230,12 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, "Test.java", message);
logger.addError(ev);
logger.auditFinished(null);
- final String[] expectedLines = CommonUtils.EMPTY_STRING_ARRAY;
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerEmpty.xml"), outStream);
}
@Test
public void testAddException()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -278,20 +244,13 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, "Test.java", message);
logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;"
- + "&#10;&lt;/exception&gt;&#10;",
- "</file>",
- };
-
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
assertEquals("Invalid close count", 1, outStream.getCloseCount());
}
@Test
public void testAddExceptionWithNullFileName()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -300,18 +259,13 @@ public class XMLLoggerTest {
final AuditEvent ev = new AuditEvent(this, null, message);
logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
logger.auditFinished(null);
- final String[] expectedLines = {
- "&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;"
- + "&#10;&lt;/exception&gt;&#10;",
- };
-
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerExceptionNullFileName.xml"), outStream);
assertEquals("Invalid close count", 1, outStream.getCloseCount());
}
@Test
public void testAddExceptionAfterFileStarted()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
@@ -325,20 +279,13 @@ public class XMLLoggerTest {
logger.addException(ev, new TestException("msg", new RuntimeException("msg")));
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;"
- + "&#10;&lt;/exception&gt;&#10;",
- "</file>",
- };
-
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
assertEquals("Invalid close count", 1, outStream.getCloseCount());
}
@Test
public void testAddExceptionBeforeFileFinished()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -349,20 +296,13 @@ public class XMLLoggerTest {
final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
logger.fileFinished(fileFinishedEvent);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;"
- + "&#10;&lt;/exception&gt;&#10;",
- "</file>",
- };
-
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
assertEquals("Invalid close count", 1, outStream.getCloseCount());
}
@Test
public void testAddExceptionBetweenFileStartedAndFinished()
- throws IOException {
+ throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final LocalizedMessage message =
@@ -375,19 +315,12 @@ public class XMLLoggerTest {
final AuditEvent fileFinishedEvent = new AuditEvent(this, "Test.java");
logger.fileFinished(fileFinishedEvent);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;"
- + "&#10;&lt;/exception&gt;&#10;",
- "</file>",
- };
-
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerException.xml"), outStream);
assertEquals("Invalid close count", 1, outStream.getCloseCount());
}
@Test
- public void testAuditFinishedWithoutFileFinished() throws IOException {
+ public void testAuditFinishedWithoutFileFinished() throws Exception {
final XMLLogger logger = new XMLLogger(outStream, true);
logger.auditStarted(null);
final AuditEvent fileStartedEvent = new AuditEvent(this, "Test.java");
@@ -401,13 +334,7 @@ public class XMLLoggerTest {
logger.addError(errorEvent);
logger.auditFinished(null);
- final String[] expectedLines = {
- "<file name=\"Test.java\">",
- "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\""
- + " source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>",
- "</file>",
- };
- verifyLines(expectedLines);
+ verifyXml(getPath("ExpectedXMLLoggerError.xml"), outStream, message.getMessage());
}
@Test
@@ -419,45 +346,6 @@ public class XMLLoggerTest {
assertNotNull("instance should not be null", logger);
}
- private String[] getOutStreamLines()
- throws IOException {
- final byte[] bytes = outStream.toByteArray();
- final ByteArrayInputStream inStream =
- new ByteArrayInputStream(bytes);
- final List<String> lineList = new ArrayList<>();
- try (BufferedReader reader = new BufferedReader(
- new InputStreamReader(inStream, StandardCharsets.UTF_8))) {
- while (true) {
- final String line = reader.readLine();
- if (line == null) {
- break;
- }
- lineList.add(line);
- }
- }
- return lineList.toArray(new String[lineList.size()]);
- }
-
- /**
- * Verify output lines from auditStart to auditEnd.
- * Take into consideration checkstyle element (first and last lines).
- * @param expectedLines expected error report lines
- */
- private void verifyLines(String... expectedLines)
- throws IOException {
- final String[] lines = getOutStreamLines();
- assertEquals("length.", expectedLines.length + 3, lines.length);
- assertEquals("first line.",
- "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
- lines[0]);
- final Pattern checkstyleOpenTag = Pattern.compile("^<checkstyle version=\".*\">$");
- assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches());
- for (int i = 0; i < expectedLines.length; i++) {
- assertEquals("line " + i + ".", expectedLines[i], lines[i + 2]);
- }
- assertEquals("last line.", "</checkstyle>", lines[lines.length - 1]);
- }
-
private static class TestException extends RuntimeException {
private static final long serialVersionUID = 1L;
@@ -471,5 +359,4 @@ public class XMLLoggerTest {
printWriter.print("stackTrace\r\nexample");
}
}
-
}
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLogger.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLogger.xml
new file mode 100644
index 000000000..9d02b7689
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLogger.xml
@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<file name="Test.java">
+</file>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerEmpty.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerEmpty.xml
new file mode 100644
index 000000000..91fd28495
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerEmpty.xml
@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerError.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerError.xml
new file mode 100644
index 000000000..b3998bf18
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerError.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<file name="Test.java">
+<error line="1" column="1" severity="error" message="$0" source="com.puppycrawl.tools.checkstyle.XMLLoggerTest"/>
+</file>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorModuleId.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorModuleId.xml
new file mode 100644
index 000000000..dc78a6df5
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorModuleId.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<file name="Test.java">
+<error line="1" column="1" severity="error" message="$0" source="module"/>
+</file>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorNullFileName.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorNullFileName.xml
new file mode 100644
index 000000000..dae3ac4ec
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorNullFileName.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<error line="1" column="1" severity="error" message="$0" source="com.puppycrawl.tools.checkstyle.XMLLoggerTest"/>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorZeroColumn.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorZeroColumn.xml
new file mode 100644
index 000000000..b0959a9c0
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerErrorZeroColumn.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<file name="Test.java">
+<error line="1" severity="error" message="$0" source="com.puppycrawl.tools.checkstyle.XMLLoggerTest"/>
+</file>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerException.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerException.xml
new file mode 100644
index 000000000..9f125a0da
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerException.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+<file name="Test.java">
+&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;&#10;&lt;/exception&gt;&#10;
+</file>
+</checkstyle>
diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerExceptionNullFileName.xml b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerExceptionNullFileName.xml
new file mode 100644
index 000000000..0a5b431d7
--- /dev/null
+++ b/src/test/resources/com/puppycrawl/tools/checkstyle/xmllogger/ExpectedXMLLoggerExceptionNullFileName.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<checkstyle version="">
+&lt;exception&gt;&#10;&lt;![CDATA[&#10;stackTrace&#10;example]]&gt;&#10;&lt;/exception&gt;&#10;
+</checkstyle>