001: package com.puppycrawl.tools.checkstyle;
002:
003: import java.io.BufferedReader;
004: import java.io.ByteArrayInputStream;
005: import java.io.ByteArrayOutputStream;
006: import java.io.IOException;
007: import java.io.InputStreamReader;
008: import java.io.PrintWriter;
009: import java.util.ArrayList;
010: import java.util.regex.Pattern;
011:
012: import com.puppycrawl.tools.checkstyle.api.AuditEvent;
013: import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
014: import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
015: import com.puppycrawl.tools.checkstyle.api.Utils;
016:
017: import junit.framework.TestCase;
018:
019: /**
020: * Enter a description of class XMLLoggerTest.java.
021: * @author Rick Giles
022: * @version 11-Dec-2002
023: */
024: public class XMLLoggerTest extends TestCase {
025: private ByteArrayOutputStream outStream;
026:
027: public void setUp() throws Exception {
028: outStream = new ByteArrayOutputStream();
029: }
030:
031: public void testEncode() throws IOException {
032: final XMLLogger logger = new XMLLogger(outStream, false);
033: final String[][] encodings = { { "<", "<" },
034: { ">", ">" }, { "'", "'" }, { "\"", """ },
035: { "&", "&" }, { "<", "<" },
036: { "abc;", "abc;" }, };
037: for (int i = 0; i < encodings.length; i++) {
038: final String encoded = logger.encode(encodings[i][0]);
039: assertEquals("\"" + encodings[i][0] + "\"",
040: encodings[i][1], encoded);
041: }
042: outStream.close();
043: }
044:
045: public void testIsReference() throws IOException {
046: final XMLLogger logger = new XMLLogger(outStream, false);
047: final String[] reference = { "�", "�", };
048: for (int i = 0; i < reference.length; i++) {
049: assertTrue("reference: " + reference[i], logger
050: .isReference(reference[i]));
051: }
052: final String[] noReference = { "&", "&;", "&#;", "&#a;",
053: "�", "&#x;", "&#xg;", };
054: for (int i = 0; i < noReference.length; i++) {
055: assertFalse("no reference: " + noReference[i], logger
056: .isReference(noReference[i]));
057: }
058:
059: outStream.close();
060: }
061:
062: public void testCloseStream() throws IOException {
063: final XMLLogger logger = new XMLLogger(outStream, true);
064: logger.auditStarted(null);
065: logger.auditFinished(null);
066: final String[] expectedLines = {};
067: verifyLines(expectedLines);
068: }
069:
070: public void testNoCloseStream() throws IOException {
071: final XMLLogger logger = new XMLLogger(outStream, false);
072: logger.auditStarted(null);
073: logger.auditFinished(null);
074: outStream.close();
075: final String[] expectedLines = {};
076: verifyLines(expectedLines);
077: }
078:
079: public void testFileStarted() throws IOException {
080: final XMLLogger logger = new XMLLogger(outStream, true);
081: logger.auditStarted(null);
082: final AuditEvent ev = new AuditEvent(this , "Test.java");
083: logger.fileStarted(ev);
084: logger.auditFinished(null);
085: final String[] expectedLines = { "<file name=\"Test.java\">" };
086: verifyLines(expectedLines);
087: }
088:
089: public void testFileFinished() throws IOException {
090: final XMLLogger logger = new XMLLogger(outStream, true);
091: logger.auditStarted(null);
092: final AuditEvent ev = new AuditEvent(this , "Test.java");
093: logger.fileFinished(ev);
094: logger.auditFinished(null);
095: final String[] expectedLines = { "</file>" };
096: verifyLines(expectedLines);
097: }
098:
099: public void testAddError() throws IOException {
100: final XMLLogger logger = new XMLLogger(outStream, true);
101: logger.auditStarted(null);
102: final LocalizedMessage message = new LocalizedMessage(1, 1,
103: "messages.properties", "key", null,
104: SeverityLevel.ERROR, null, this .getClass());
105: final AuditEvent ev = new AuditEvent(this , "Test.java", message);
106: logger.addError(ev);
107: logger.auditFinished(null);
108: final String[] expectedLines = { "<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\" source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>" };
109: verifyLines(expectedLines);
110: }
111:
112: public void testAddException() throws IOException {
113: final XMLLogger logger = new XMLLogger(outStream, true);
114: logger.auditStarted(null);
115: final LocalizedMessage message = new LocalizedMessage(1, 1,
116: "messages.properties", null, null, null, this
117: .getClass());
118: final AuditEvent ev = new AuditEvent(this , "Test.java", message);
119: logger.addException(ev, new TestThrowable());
120: logger.auditFinished(null);
121: final String[] expectedLines = { "<exception>",
122: "<![CDATA[", "stackTrace]]>",
123: "</exception>", "", };
124: verifyLines(expectedLines);
125: }
126:
127: private String[] getOutStreamLines() throws IOException {
128: final byte[] bytes = outStream.toByteArray();
129: final ByteArrayInputStream inStream = new ByteArrayInputStream(
130: bytes);
131: final BufferedReader reader = new BufferedReader(
132: new InputStreamReader(inStream));
133: final ArrayList lineList = new ArrayList();
134: while (true) {
135: final String line = reader.readLine();
136: if (line == null) {
137: break;
138: }
139: lineList.add(line);
140: }
141: reader.close();
142: return (String[]) lineList.toArray(new String[lineList.size()]);
143: }
144:
145: /**
146: * Verify output lines from auditStart to auditEnd.
147: * Take into consideration checkstyle element (first and last lines).
148: * @param aExpectedLines expected error report lines
149: */
150: private void verifyLines(String[] aExpectedLines)
151: throws IOException {
152: final String[] lines = getOutStreamLines();
153: assertEquals("length.", aExpectedLines.length + 3, lines.length);
154: assertEquals("first line.",
155: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>", lines[0]);
156: Pattern checkstyleOpenTag = Utils
157: .getPattern("^<checkstyle version=\".*\">$");
158: assertTrue("second line.", checkstyleOpenTag.matcher(lines[1])
159: .matches());
160: for (int i = 0; i < aExpectedLines.length; i++) {
161: assertEquals("line " + i + ".", aExpectedLines[i],
162: lines[i + 2]);
163: }
164: assertEquals("last line.", "</checkstyle>",
165: lines[lines.length - 1]);
166: }
167:
168: private class TestThrowable extends Exception {
169: public void printStackTrace(PrintWriter s) {
170: s.print("stackTrace");
171: }
172: }
173: }
|