1 package com.puppycrawl.tools.checkstyle; 2 3 import java.io.BufferedReader ; 4 import java.io.ByteArrayInputStream ; 5 import java.io.ByteArrayOutputStream ; 6 import java.io.IOException ; 7 import java.io.InputStreamReader ; 8 import java.io.PrintWriter ; 9 import java.util.ArrayList ; 10 import java.util.regex.Pattern ; 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 24 public class XMLLoggerTest extends TestCase 25 { 26 private ByteArrayOutputStream outStream; 27 28 public void setUp() 29 throws Exception 30 { 31 outStream = new ByteArrayOutputStream (); 32 } 33 34 public void testEncode() 35 throws IOException 36 { 37 final XMLLogger logger = new XMLLogger(outStream, false); 38 final String [][] encodings = { 39 {"<", "<"}, 40 {">", ">"}, 41 {"'", "'"}, 42 {"\"", """}, 43 {"&", "&"}, 44 {"<", "<"}, 45 {"abc;", "abc;"}, 46 }; 47 for (int i = 0; i < encodings.length; i++) { 48 final String 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 56 { 57 final XMLLogger logger = new XMLLogger(outStream, false); 58 final String [] reference = { 59 "�", 60 "�", 61 }; 62 for (int i = 0; i < reference.length; i++) { 63 assertTrue("reference: " + reference[i], 64 logger.isReference(reference[i])); 65 } 66 final String [] noReference = { 67 "&", 68 "&;", 69 "&#;", 70 "&#a;", 71 "�", 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 85 { 86 final XMLLogger logger = new XMLLogger(outStream, true); 87 logger.auditStarted(null); 88 logger.auditFinished(null); 89 final String [] expectedLines = {}; 90 verifyLines(expectedLines); 91 } 92 93 public void testNoCloseStream() 94 throws IOException 95 { 96 final XMLLogger logger = new XMLLogger(outStream, false); 97 logger.auditStarted(null); 98 logger.auditFinished(null); 99 outStream.close(); 100 final String [] expectedLines = {}; 101 verifyLines(expectedLines); 102 } 103 104 public void testFileStarted() 105 throws IOException 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 [] expectedLines = {"<file name=\"Test.java\">"}; 113 verifyLines(expectedLines); 114 } 115 116 public void testFileFinished() 117 throws IOException 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 [] expectedLines = {"</file>"}; 125 verifyLines(expectedLines); 126 } 127 128 public void testAddError() throws IOException { 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 [] 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 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 [] expectedLines = { 155 "<exception>", 156 "<![CDATA[", 157 "stackTrace]]>", 158 "</exception>", 159 "", 160 }; 161 verifyLines(expectedLines); 162 } 163 164 private String [] getOutStreamLines() 165 throws IOException 166 { 167 final byte[] bytes = outStream.toByteArray(); 168 final ByteArrayInputStream inStream = 169 new ByteArrayInputStream (bytes); 170 final BufferedReader reader = 171 new BufferedReader (new InputStreamReader (inStream)); 172 final ArrayList lineList = new ArrayList (); 173 while (true) { 174 final String line = reader.readLine(); 175 if (line == null) { 176 break; 177 } 178 lineList.add(line); 179 } 180 reader.close(); 181 return (String [])lineList.toArray(new String [lineList.size()]); 182 } 183 184 189 private void verifyLines(String [] aExpectedLines) 190 throws IOException 191 { 192 final String [] 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 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 206 { 207 public void printStackTrace(PrintWriter s) 208 { 209 s.print("stackTrace"); 210 } 211 } 212 } 213 | Popular Tags |