Source Code Cross Referenced for XMLLoggerTest.java in  » Code-Analyzer » checkstyle » com » puppycrawl » tools » checkstyle » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Code Analyzer » checkstyle » com.puppycrawl.tools.checkstyle 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 = { { "<", "&lt;" },
034:                        { ">", "&gt;" }, { "'", "&apos;" }, { "\"", "&quot;" },
035:                        { "&", "&amp;" }, { "&lt;", "&lt;" },
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 = { "&#0;", "&#x0;", };
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:                        "&#X0;", "&#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 = { "&lt;exception&gt;",
122:                        "&lt;![CDATA[", "stackTrace]]&gt;",
123:                        "&lt;/exception&gt;", "", };
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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.