Source Code Cross Referenced for UtilTest.java in  » Test-Coverage » Hansel » org » hanseltest » 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 » Test Coverage » Hansel » org.hanseltest 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.hanseltest;
002:
003:        import java.io.ByteArrayOutputStream;
004:        import java.io.PrintStream;
005:        import java.util.Arrays;
006:
007:        import junit.framework.AssertionFailedError;
008:        import junit.framework.Test;
009:        import junit.framework.TestCase;
010:        import junit.framework.TestResult;
011:
012:        import org.hansel.CoverageDecorator;
013:        import org.hansel.Util;
014:
015:        /**
016:         * Tests for the  org.hansel.Util class.
017:         * @author Niklas Mehner
018:         */
019:        public class UtilTest extends TestCase {
020:            /** Line separator */
021:            private static final String NEW_LINE = System
022:                    .getProperty("line.separator");
023:
024:            /** This holds the old value of System.out, 
025:             * after startRedirect() has been called.
026:             */
027:            private PrintStream oldOut;
028:
029:            /** This holds the old value of System.out, 
030:             * after startRedirect() has been called.
031:             */
032:            private PrintStream oldErr;
033:
034:            /** This contains the redirectied stdio,
035:             *  after startRedirect() has been called. */
036:            private ByteArrayOutputStream bout;
037:            /** This contains the redirectied stderr,
038:             *  after startRedirect() has been called. */
039:            private ByteArrayOutputStream berr;
040:
041:            /**
042:             * Create a new Test.
043:             * @param name Name of the test.
044:             */
045:            public UtilTest(String name) {
046:                super (name);
047:            }
048:
049:            /**
050:             * Start redirecting stdio/stderr to bytearrays.
051:             */
052:            public void startRedirect() {
053:                oldOut = System.out;
054:                oldErr = System.err;
055:                bout = new ByteArrayOutputStream();
056:                berr = new ByteArrayOutputStream();
057:                System.setOut(new PrintStream(bout));
058:                System.setErr(new PrintStream(berr));
059:            }
060:
061:            /**
062:             * End redirecting stdio/stderr to bytearrays.
063:             */
064:            public void endRedirect() {
065:                System.setOut(oldOut);
066:                System.setErr(oldErr);
067:            }
068:
069:            /**
070:             * Test the constructor. Trying to instantiate
071:             * Util should always cause an exception.
072:             */
073:            public void testConstructor() {
074:                try {
075:                    new Util();
076:                    fail();
077:                } catch (UnsupportedOperationException e) {
078:                    // This exception is expected to be thrown
079:                }
080:            }
081:
082:            /**
083:             * Test the concat method.
084:             */
085:            public void testConcat() {
086:                String[] a = new String[] { "a", "a" };
087:                String[] b = new String[] { "b", "b" };
088:
089:                String[] c = Util.concat(a, b);
090:
091:                assertTrue(Arrays
092:                        .equals(new String[] { "a", "a", "b", "b" }, c));
093:            }
094:
095:            /**
096:             * Test the redirection fo stido/stderr to strings.
097:             * This is needed for the dump() result tests.
098:             */
099:            public void testRedirection() {
100:                startRedirect();
101:                System.out.println("test");
102:                assertEquals("test" + NEW_LINE, bout.toString());
103:                endRedirect();
104:            }
105:
106:            /**
107:             * Test dump() result with a result containing no errors or failures.
108:             */
109:            public void testEmptyResult() {
110:                startRedirect();
111:                TestResult result = new TestResult();
112:                Util.dumpResult(result);
113:                assertEquals("", bout.toString());
114:                endRedirect();
115:            }
116:
117:            /**
118:             * Test dumpResult() with a single failure.
119:             */
120:            public void testSingleFailure() {
121:                startRedirect();
122:                TestResult result = new TestResult();
123:                result.addFailure(this , new MockError("SingleFailure"));
124:                Util.dumpResult(result);
125:                assertEquals("", bout.toString());
126:                assertEquals("SingleFailure" + NEW_LINE, berr.toString());
127:                endRedirect();
128:            }
129:
130:            /**
131:             * Test dumpResult() with two failures.
132:             */
133:            public void testTwoFailures() {
134:                startRedirect();
135:                TestResult result = new TestResult();
136:                result.addFailure(this , new MockError("FirstFailure"));
137:                result.addFailure(this , new MockError("SecondFailure"));
138:                Util.dumpResult(result);
139:                assertEquals("", bout.toString());
140:                assertEquals("FirstFailure" + NEW_LINE + "SecondFailure"
141:                        + NEW_LINE, berr.toString());
142:                endRedirect();
143:            }
144:
145:            /**
146:             * Test dumpResult() with a single error.
147:             */
148:            public void testSingleError() {
149:                startRedirect();
150:                TestResult result = new TestResult();
151:                result.addError(this , new MockError("SingleError"));
152:                Util.dumpResult(result);
153:                assertEquals("", bout.toString());
154:                assertEquals("SingleError" + NEW_LINE, berr.toString());
155:                endRedirect();
156:            }
157:
158:            /**
159:             * Test dumpResult(), when two errors are present.
160:             */
161:            public void testTwoErrors() {
162:                startRedirect();
163:                TestResult result = new TestResult();
164:                result.addError(this , new MockError("FirstError"));
165:                result.addError(this , new MockError("SecondError"));
166:                Util.dumpResult(result);
167:                assertEquals("", bout.toString());
168:                assertEquals(
169:                        "FirstError" + NEW_LINE + "SecondError" + NEW_LINE,
170:                        berr.toString());
171:                endRedirect();
172:            }
173:
174:            /**
175:             * Test dumpResult(), when failureas and errors are present.
176:             */
177:            public void testMixed() {
178:                startRedirect();
179:                TestResult result = new TestResult();
180:                result.addError(this , new MockError("Error"));
181:                result.addFailure(this , new MockError("Failure"));
182:                Util.dumpResult(result);
183:                assertEquals("", bout.toString());
184:                assertEquals("Failure" + NEW_LINE + "Error" + NEW_LINE, berr
185:                        .toString());
186:                endRedirect();
187:            }
188:
189:            /**
190:             * Test the leftFill method.
191:             */
192:            public void testLeftFill() {
193:                assertEquals("    1", Util.leftFill(5, "1"));
194:                assertEquals("   12", Util.leftFill(5, "12"));
195:                assertEquals("12345", Util.leftFill(5, "12345"));
196:                assertEquals("1234567", Util.leftFill(5, "1234567"));
197:            }
198:
199:            /**
200:             * Returns a coverage decorator for this test.
201:             * @return CoverageTest.
202:             */
203:            public static Test suite() {
204:                CoverageDecorator result = new CoverageDecorator(
205:                        UtilTest.class, new Class[] { Util.class });
206:
207:                return result;
208:            }
209:
210:            /**
211:             * AssertionFailedError, that prints a simple message instead
212:             * of a stack trace.
213:             */
214:            private class MockError extends AssertionFailedError {
215:                /** String to print out. */
216:                private String msg;
217:
218:                /** 
219:                 * Creates a new MockError.
220:                 * @param msg String to print out. 
221:                 */
222:                public MockError(String msg) {
223:                    this .msg = msg;
224:                }
225:
226:                /**
227:                 * Prints a message instead of a stack trace to stderr.
228:                 */
229:                public void printStackTrace() {
230:                    System.err.println(msg);
231:                }
232:            }
233:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.