KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > XMLLoggerTest


1 package com.puppycrawl.tools.checkstyle;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.ByteArrayInputStream JavaDoc;
5 import java.io.ByteArrayOutputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.io.InputStreamReader JavaDoc;
8 import java.io.PrintWriter JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.util.regex.Pattern JavaDoc;
11
12 import com.puppycrawl.tools.checkstyle.api.AuditEvent;
13 import com.puppycrawl.tools.checkstyle.api.LocalizedMessage;
14 import com.puppycrawl.tools.checkstyle.api.SeverityLevel;
15 import com.puppycrawl.tools.checkstyle.api.Utils;
16
17 import junit.framework.TestCase;
18
19 /**
20  * Enter a description of class XMLLoggerTest.java.
21  * @author Rick Giles
22  * @version 11-Dec-2002
23  */

24 public class XMLLoggerTest extends TestCase
25 {
26     private ByteArrayOutputStream JavaDoc outStream;
27
28     public void setUp()
29         throws Exception JavaDoc
30     {
31         outStream = new ByteArrayOutputStream JavaDoc();
32     }
33
34     public void testEncode()
35         throws IOException JavaDoc
36     {
37         final XMLLogger logger = new XMLLogger(outStream, false);
38         final String JavaDoc[][] encodings = {
39             {"<", "&lt;"},
40             {">", "&gt;"},
41             {"'", "&apos;"},
42             {"\"", "&quot;"},
43             {"&", "&amp;"},
44             {"&lt;", "&lt;"},
45             {"abc;", "abc;"},
46         };
47         for (int i = 0; i < encodings.length; i++) {
48             final String JavaDoc encoded = logger.encode(encodings[i][0]);
49             assertEquals("\"" + encodings[i][0] + "\"", encodings[i][1], encoded);
50         }
51         outStream.close();
52     }
53
54     public void testIsReference()
55         throws IOException JavaDoc
56     {
57         final XMLLogger logger = new XMLLogger(outStream, false);
58         final String JavaDoc[] reference = {
59             "&#0;",
60             "&#x0;",
61         };
62         for (int i = 0; i < reference.length; i++) {
63             assertTrue("reference: " + reference[i],
64                        logger.isReference(reference[i]));
65         }
66         final String JavaDoc[] noReference = {
67             "&",
68             "&;",
69             "&#;",
70             "&#a;",
71             "&#X0;",
72             "&#x;",
73             "&#xg;",
74         };
75         for (int i = 0; i < noReference.length; i++) {
76             assertFalse("no reference: " + noReference[i],
77                        logger.isReference(noReference[i]));
78         }
79
80         outStream.close();
81     }
82
83      public void testCloseStream()
84         throws IOException JavaDoc
85     {
86         final XMLLogger logger = new XMLLogger(outStream, true);
87         logger.auditStarted(null);
88         logger.auditFinished(null);
89         final String JavaDoc[] expectedLines = {};
90         verifyLines(expectedLines);
91     }
92
93     public void testNoCloseStream()
94         throws IOException JavaDoc
95     {
96         final XMLLogger logger = new XMLLogger(outStream, false);
97         logger.auditStarted(null);
98         logger.auditFinished(null);
99         outStream.close();
100         final String JavaDoc[] expectedLines = {};
101         verifyLines(expectedLines);
102     }
103
104     public void testFileStarted()
105         throws IOException JavaDoc
106     {
107         final XMLLogger logger = new XMLLogger(outStream, true);
108         logger.auditStarted(null);
109         final AuditEvent ev = new AuditEvent(this, "Test.java");
110         logger.fileStarted(ev);
111         logger.auditFinished(null);
112         final String JavaDoc[] expectedLines = {"<file name=\"Test.java\">"};
113         verifyLines(expectedLines);
114     }
115
116     public void testFileFinished()
117         throws IOException JavaDoc
118     {
119         final XMLLogger logger = new XMLLogger(outStream, true);
120         logger.auditStarted(null);
121         final AuditEvent ev = new AuditEvent(this, "Test.java");
122         logger.fileFinished(ev);
123         logger.auditFinished(null);
124         final String JavaDoc[] expectedLines = {"</file>"};
125         verifyLines(expectedLines);
126     }
127
128     public void testAddError() throws IOException JavaDoc {
129         final XMLLogger logger = new XMLLogger(outStream, true);
130         logger.auditStarted(null);
131         final LocalizedMessage message =
132             new LocalizedMessage(1, 1,
133                 "messages.properties", "key", null, SeverityLevel.ERROR, null,
134                 this.getClass());
135         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
136         logger.addError(ev);
137         logger.auditFinished(null);
138         final String JavaDoc[] expectedLines =
139             {"<error line=\"1\" column=\"1\" severity=\"error\" message=\"key\" source=\"com.puppycrawl.tools.checkstyle.XMLLoggerTest\"/>"};
140         verifyLines(expectedLines);
141     }
142
143     public void testAddException()
144         throws IOException JavaDoc
145     {
146         final XMLLogger logger = new XMLLogger(outStream, true);
147         logger.auditStarted(null);
148         final LocalizedMessage message =
149             new LocalizedMessage(1, 1,
150                 "messages.properties", null, null, null, this.getClass());
151         final AuditEvent ev = new AuditEvent(this, "Test.java", message);
152         logger.addException(ev, new TestThrowable());
153         logger.auditFinished(null);
154         final String JavaDoc[] expectedLines = {
155             "&lt;exception&gt;",
156             "&lt;![CDATA[",
157             "stackTrace]]&gt;",
158             "&lt;/exception&gt;",
159             "",
160         };
161         verifyLines(expectedLines);
162     }
163
164     private String JavaDoc[] getOutStreamLines()
165         throws IOException JavaDoc
166     {
167         final byte[] bytes = outStream.toByteArray();
168         final ByteArrayInputStream JavaDoc inStream =
169             new ByteArrayInputStream JavaDoc(bytes);
170         final BufferedReader JavaDoc reader =
171             new BufferedReader JavaDoc(new InputStreamReader JavaDoc(inStream));
172         final ArrayList JavaDoc lineList = new ArrayList JavaDoc();
173         while (true) {
174             final String JavaDoc line = reader.readLine();
175             if (line == null) {
176                 break;
177             }
178             lineList.add(line);
179         }
180         reader.close();
181         return (String JavaDoc[])lineList.toArray(new String JavaDoc[lineList.size()]);
182     }
183
184     /**
185      * Verify output lines from auditStart to auditEnd.
186      * Take into consideration checkstyle element (first and last lines).
187      * @param aExpectedLines expected error report lines
188      */

189     private void verifyLines(String JavaDoc[] aExpectedLines)
190         throws IOException JavaDoc
191     {
192         final String JavaDoc[] lines = getOutStreamLines();
193         assertEquals("length.", aExpectedLines.length + 3, lines.length);
194         assertEquals("first line.",
195                      "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
196                      lines[0]);
197         Pattern JavaDoc checkstyleOpenTag = Utils.getPattern("^<checkstyle version=\".*\">$");
198         assertTrue("second line.", checkstyleOpenTag.matcher(lines[1]).matches());
199         for (int i = 0; i < aExpectedLines.length; i++) {
200             assertEquals("line " + i + ".", aExpectedLines[i], lines[i + 2]);
201         }
202         assertEquals("last line.", "</checkstyle>", lines[lines.length - 1]);
203     }
204
205     private class TestThrowable extends Exception JavaDoc
206     {
207         public void printStackTrace(PrintWriter JavaDoc s)
208         {
209             s.print("stackTrace");
210         }
211     }
212 }
213
Popular Tags