Source Code Cross Referenced for TestSwitch.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 junit.framework.TestCase;
004:        import junit.framework.TestFailure;
005:        import junit.framework.TestResult;
006:
007:        import org.hansel.CoverageDecorator;
008:
009:        /**
010:         * Test the coverage test of switch statements. 
011:         * 
012:         * @author Niklas Mehner
013:         */
014:        public class TestSwitch extends TestCase {
015:            /** Class the tests cover. */
016:            private static final Class[] COVERED = { CoverSwitch.class };
017:
018:            /**
019:             * Create a new test.
020:             * @param name Name of the test.
021:             */
022:            public TestSwitch(String name) {
023:                super (name);
024:            }
025:
026:            /**
027:             * Assert that a failure message contains a given string.
028:             * @param message Message that has to be contained in the failure.
029:             * @param failure Failure, that should contain message.
030:             */
031:            private void assertContains(String message, TestFailure failure) {
032:                String failureMessage = failure.thrownException().getMessage();
033:
034:                int index = failureMessage.indexOf(message);
035:
036:                assertTrue("Failure message '" + failureMessage
037:                        + "' does not contain '" + message + "'.", index > -1);
038:            }
039:
040:            /**
041:             * Returns the test result for running a coverage test on
042:             * COVERED for a given test class.
043:             * 
044:             * @param clazz The class of the test to be run.
045:             * @return Result of the test run.
046:             */
047:            private TestResult getTestResult(Class clazz) {
048:                CoverageDecorator cd = new CoverageDecorator(clazz, COVERED);
049:
050:                TestResult result = new TestResult();
051:
052:                cd.run(result);
053:
054:                return result;
055:            }
056:
057:            /**
058:             * No code is covered.
059:             */
060:            public void testNoCoverage() {
061:                TestResult result = getTestResult(NoCoverage.class);
062:                // Four methods not covered, the switch probes do not report
063:                // an failure, because, they are not hit at all.
064:                assertEquals(4, result.failureCount());
065:                assertEquals(0, result.errorCount());
066:            }
067:
068:            /**
069:             * Test full coverage.
070:             */
071:            public void testFullCoverage() {
072:                TestResult result = getTestResult(FullCoverage.class);
073:
074:                // Four methods not covered, the switch probes do not report
075:                // an failure, because, they are not hit at all.
076:                assertEquals(0, result.failureCount());
077:                assertEquals(0, result.errorCount());
078:            }
079:
080:            /**
081:             * Test part coverage (1).
082:             */
083:            public void testPartCoverage() {
084:                TestResult result = getTestResult(PartCoverage.class);
085:
086:                assertEquals(1, result.failureCount());
087:                assertEquals(0, result.errorCount());
088:
089:                TestFailure failure = (TestFailure) result.failures()
090:                        .nextElement();
091:
092:                assertContains(
093:                        "Switch-cases '0', '1' and '5' have not been covered.",
094:                        failure);
095:            }
096:
097:            /**
098:             * Test part coverage (2).
099:             */
100:            public void testPartCoverage2() {
101:                TestResult result = getTestResult(PartCoverage2.class);
102:
103:                assertEquals(1, result.failureCount());
104:                assertEquals(0, result.errorCount());
105:
106:                TestFailure failure = (TestFailure) result.failures()
107:                        .nextElement();
108:
109:                assertContains("Switch-case 'default' has not been covered.",
110:                        failure);
111:            }
112:
113:            /**
114:             * Test part coverage (3).
115:             */
116:            public void testPartCoverage3() {
117:                TestResult result = getTestResult(PartCoverage3.class);
118:
119:                assertEquals(1, result.failureCount());
120:                assertEquals(0, result.errorCount());
121:
122:                TestFailure failure = (TestFailure) result.failures()
123:                        .nextElement();
124:                assertContains(
125:                        "Switch-cases '4', '5' and '6' have not been covered.",
126:                        failure);
127:            }
128:
129:            /**
130:             * TestCase does covers no code at all.
131:             */
132:            public static class NoCoverage extends TestCase {
133:                /**
134:                 * Creates a new test.
135:                 * @param name Name of the test.
136:                 */
137:                public NoCoverage(String name) {
138:                    super (name);
139:                }
140:
141:                /** Empty method. */
142:                public void testNothing() {
143:                }
144:            }
145:
146:            /**
147:             * Covers the whole CoverSwitch class.
148:             */
149:            public static class FullCoverage extends TestCase {
150:                /**
151:                 * Creates a new test.
152:                 * @param name Name of the test.
153:                 */
154:                public FullCoverage(String name) {
155:                    super (name);
156:                }
157:
158:                /**
159:                 * Single Test of this class.
160:                 */
161:                public void testAll() {
162:                    CoverSwitch cs = new CoverSwitch();
163:                    // Cover simpleSwitch
164:                    assertEquals(1, cs.coverSimpleSwitch(0));
165:                    assertEquals(2, cs.coverSimpleSwitch(1));
166:                    assertEquals(3, cs.coverSimpleSwitch(5));
167:                    assertEquals(4, cs.coverSimpleSwitch(-1));
168:                    assertEquals(4, cs.coverSimpleSwitch(2));
169:
170:                    // Cover NoDefault
171:                    assertEquals(3, cs.coverNoDefault(0));
172:                    assertEquals(2, cs.coverNoDefault(5));
173:
174:                    // Cover simpleSwitch
175:                    assertEquals(1, cs.coverSimpleSwitch2(1));
176:                    assertEquals(2, cs.coverSimpleSwitch2(5));
177:                    assertEquals(3, cs.coverSimpleSwitch2(7));
178:                }
179:            }
180:
181:            /**
182:             * Covers only a part of the CoverSwitch class.
183:             */
184:            public static class PartCoverage extends TestCase {
185:                /**
186:                 * Creates a new test.
187:                 * @param name Name of the test.
188:                 */
189:                public PartCoverage(String name) {
190:                    super (name);
191:                }
192:
193:                /**
194:                 * Single Test of this class.
195:                 */
196:                public void testAll() {
197:                    CoverSwitch cs = new CoverSwitch();
198:                    // Cover simpleSwitch
199:                    // Covers only default case
200:                    assertEquals(4, cs.coverSimpleSwitch(-1));
201:                    assertEquals(4, cs.coverSimpleSwitch(2));
202:
203:                    // Cover NoDefault
204:                    assertEquals(3, cs.coverNoDefault(0));
205:                    assertEquals(2, cs.coverNoDefault(5));
206:
207:                    // Cover simpleSwitch
208:                    assertEquals(1, cs.coverSimpleSwitch2(1));
209:                    assertEquals(2, cs.coverSimpleSwitch2(5));
210:                    assertEquals(3, cs.coverSimpleSwitch2(7));
211:                }
212:            }
213:
214:            /**
215:             * Covers only a part of the CoverSwitch class.
216:             */
217:            public static class PartCoverage2 extends TestCase {
218:                /**
219:                 * Creates a new test.
220:                 * @param name Name of the test.
221:                 */
222:                public PartCoverage2(String name) {
223:                    super (name);
224:                }
225:
226:                /**
227:                 * Single Test of this class.
228:                 */
229:                public void testAll() {
230:                    CoverSwitch cs = new CoverSwitch();
231:                    // Cover simpleSwitch
232:                    assertEquals(1, cs.coverSimpleSwitch(0));
233:                    assertEquals(2, cs.coverSimpleSwitch(1));
234:                    assertEquals(3, cs.coverSimpleSwitch(5));
235:                    assertEquals(4, cs.coverSimpleSwitch(-1));
236:                    assertEquals(4, cs.coverSimpleSwitch(2));
237:
238:                    // Cover NoDefault
239:                    // Does not cover default case
240:                    assertEquals(3, cs.coverNoDefault(0));
241:
242:                    // Cover simpleSwitch
243:                    assertEquals(1, cs.coverSimpleSwitch2(1));
244:                    assertEquals(2, cs.coverSimpleSwitch2(5));
245:                    assertEquals(3, cs.coverSimpleSwitch2(7));
246:                }
247:            }
248:
249:            /**
250:             * Covers only a part of the CoverSwitch class.
251:             */
252:            public static class PartCoverage3 extends TestCase {
253:                /**
254:                 * Creates a new test.
255:                 * @param name Name of the test.
256:                 */
257:                public PartCoverage3(String name) {
258:                    super (name);
259:                }
260:
261:                /**
262:                 * Single Test of this class.
263:                 */
264:                public void testAll() {
265:                    CoverSwitch cs = new CoverSwitch();
266:                    // Cover simpleSwitch
267:                    assertEquals(1, cs.coverSimpleSwitch(0));
268:                    assertEquals(2, cs.coverSimpleSwitch(1));
269:                    assertEquals(3, cs.coverSimpleSwitch(5));
270:                    assertEquals(4, cs.coverSimpleSwitch(-1));
271:                    assertEquals(4, cs.coverSimpleSwitch(2));
272:
273:                    // Cover NoDefault
274:                    // Does not cover default case
275:                    assertEquals(3, cs.coverNoDefault(0));
276:                    assertEquals(2, cs.coverNoDefault(5));
277:
278:                    // Cover simpleSwitch
279:                    assertEquals(1, cs.coverSimpleSwitch2(1));
280:                    assertEquals(3, cs.coverSimpleSwitch2(7));
281:                }
282:            }
283:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.