Source Code Cross Referenced for ExceptionUtilsTestCase.java in  » Library » Apache-common-lang » org » apache » commons » lang » exception » 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 » Library » Apache common lang » org.apache.commons.lang.exception 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.lang.exception;
018:
019:        import java.lang.reflect.Constructor;
020:        import java.lang.reflect.Modifier;
021:        import java.io.ByteArrayOutputStream;
022:        import java.io.IOException;
023:        import java.io.PrintStream;
024:        import java.io.PrintWriter;
025:        import java.io.StringWriter;
026:        import java.lang.reflect.InvocationTargetException;
027:        import java.sql.SQLException;
028:        import java.util.List;
029:
030:        import junit.framework.Assert;
031:        import junit.framework.Test;
032:        import junit.framework.TestSuite;
033:
034:        import org.apache.commons.lang.SystemUtils;
035:
036:        /**
037:         * Tests {@link org.apache.commons.lang.exception.ExceptionUtils}.
038:         * 
039:         * <h3>Notes</h3>
040:         * <p>
041:         * Make sure this exception code does not depend on Java 1.4 nested exceptions. SVN revision 38990 does not compile with
042:         * Java 1.3.1.
043:         * </p>
044:         * <ul>
045:         * <li>Compiled with Sun Java 1.3.1_15</li>
046:         * <li>Tested with Sun Java 1.3.1_15</li>
047:         * <li>Tested with Sun Java 1.4.2_12</li>
048:         * <li>Tested with Sun Java 1.5.0_08</li>
049:         * <li>All of the above on Windows XP SP2 + patches.</li>
050:         * </ul>
051:         * <p>
052:         * Gary Gregory; August 16, 2006.
053:         * </p>
054:         * 
055:         * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
056:         * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
057:         * @author Stephen Colebourne
058:         * @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
059:         * @since 1.0
060:         */
061:        public class ExceptionUtilsTestCase extends junit.framework.TestCase {
062:
063:            private NestableException nested;
064:            private Throwable withCause;
065:            private Throwable withoutCause;
066:            private Throwable jdkNoCause;
067:            private ExceptionWithCause selfCause;
068:            private ExceptionWithCause cyclicCause;
069:
070:            public ExceptionUtilsTestCase(String name) {
071:                super (name);
072:            }
073:
074:            public static Test suite() {
075:                return new TestSuite(ExceptionUtilsTestCase.class);
076:            }
077:
078:            public void setUp() {
079:                withoutCause = createExceptionWithoutCause();
080:                nested = new NestableException(withoutCause);
081:                withCause = new ExceptionWithCause(nested);
082:                jdkNoCause = new NullPointerException();
083:                selfCause = new ExceptionWithCause(null);
084:                selfCause.setCause(selfCause);
085:                ExceptionWithCause a = new ExceptionWithCause(null);
086:                ExceptionWithCause b = new ExceptionWithCause(a);
087:                a.setCause(b);
088:                cyclicCause = new ExceptionWithCause(a);
089:            }
090:
091:            protected void tearDown() throws Exception {
092:                withoutCause = null;
093:                nested = null;
094:                withCause = null;
095:                jdkNoCause = null;
096:                selfCause = null;
097:                cyclicCause = null;
098:            }
099:
100:            //-----------------------------------------------------------------------
101:            private Throwable createExceptionWithoutCause() {
102:                try {
103:                    throw new ExceptionWithoutCause();
104:                } catch (Throwable t) {
105:                    return t;
106:                }
107:            }
108:
109:            private Throwable createExceptionWithCause() {
110:                try {
111:                    try {
112:                        throw new ExceptionWithCause(
113:                                createExceptionWithoutCause());
114:                    } catch (Throwable t) {
115:                        throw new ExceptionWithCause(t);
116:                    }
117:                } catch (Throwable t) {
118:                    return t;
119:                }
120:            }
121:
122:            //-----------------------------------------------------------------------
123:
124:            public void testConstructor() {
125:                assertNotNull(new ExceptionUtils());
126:                Constructor[] cons = ExceptionUtils.class
127:                        .getDeclaredConstructors();
128:                assertEquals(1, cons.length);
129:                assertEquals(true, Modifier.isPublic(cons[0].getModifiers()));
130:                assertEquals(true, Modifier.isPublic(ExceptionUtils.class
131:                        .getModifiers()));
132:                assertEquals(false, Modifier.isFinal(ExceptionUtils.class
133:                        .getModifiers()));
134:            }
135:
136:            //-----------------------------------------------------------------------
137:
138:            public void testCauseMethodNameOps() {
139:                this .testCauseMethodNameOps(null);
140:                this .testCauseMethodNameOps("");
141:                this .testCauseMethodNameOps(" ");
142:                this .testCauseMethodNameOps("\t\r\n\t");
143:                this .testCauseMethodNameOps("testMethodName");
144:            }
145:
146:            void testCauseMethodNameOps(String name) {
147:                String methodName = "testMethodName";
148:                try {
149:                    Assert.assertFalse(ExceptionUtils
150:                            .isCauseMethodName(methodName));
151:                    ExceptionUtils.addCauseMethodName(methodName);
152:                    ExceptionUtils.addCauseMethodName(methodName);
153:                    Assert.assertTrue(ExceptionUtils
154:                            .isCauseMethodName(methodName));
155:                } finally {
156:                    ExceptionUtils.removeCauseMethodName(methodName);
157:                    Assert.assertFalse("The method name " + methodName
158:                            + " should not be in the array", ExceptionUtils
159:                            .isCauseMethodName(methodName));
160:                }
161:            }
162:
163:            public void testGetCause_Throwable() {
164:                assertSame(null, ExceptionUtils.getCause(null));
165:                assertSame(null, ExceptionUtils.getCause(withoutCause));
166:                assertSame(withoutCause, ExceptionUtils.getCause(nested));
167:                assertSame(nested, ExceptionUtils.getCause(withCause));
168:                assertSame(null, ExceptionUtils.getCause(jdkNoCause));
169:                assertSame(selfCause, ExceptionUtils.getCause(selfCause));
170:                assertSame(cyclicCause.getCause(), ExceptionUtils
171:                        .getCause(cyclicCause));
172:                assertSame(((ExceptionWithCause) cyclicCause.getCause())
173:                        .getCause(), ExceptionUtils.getCause(cyclicCause
174:                        .getCause()));
175:                assertSame(cyclicCause.getCause(), ExceptionUtils
176:                        .getCause(((ExceptionWithCause) cyclicCause.getCause())
177:                                .getCause()));
178:            }
179:
180:            public void testGetCause_ThrowableArray() {
181:                assertSame(null, ExceptionUtils.getCause(null, null));
182:                assertSame(null, ExceptionUtils.getCause(null, new String[0]));
183:
184:                // match because known type        
185:                assertSame(withoutCause, ExceptionUtils.getCause(nested, null));
186:                assertSame(withoutCause, ExceptionUtils.getCause(nested,
187:                        new String[0]));
188:                assertSame(withoutCause, ExceptionUtils.getCause(nested,
189:                        new String[] { "getCause" }));
190:
191:                // not known type, so match on supplied method names
192:                assertSame(nested, ExceptionUtils.getCause(withCause, null)); // default names
193:                assertSame(null, ExceptionUtils.getCause(withCause,
194:                        new String[0]));
195:                assertSame(null, ExceptionUtils.getCause(withCause,
196:                        new String[] { null }));
197:                assertSame(nested, ExceptionUtils.getCause(withCause,
198:                        new String[] { "getCause" }));
199:
200:                // not known type, so match on supplied method names
201:                assertSame(null, ExceptionUtils.getCause(withoutCause, null));
202:                assertSame(null, ExceptionUtils.getCause(withoutCause,
203:                        new String[0]));
204:                assertSame(null, ExceptionUtils.getCause(withoutCause,
205:                        new String[] { null }));
206:                assertSame(null, ExceptionUtils.getCause(withoutCause,
207:                        new String[] { "getCause" }));
208:                assertSame(null, ExceptionUtils.getCause(withoutCause,
209:                        new String[] { "getTargetException" }));
210:            }
211:
212:            public void testGetRootCause_Throwable() {
213:                assertSame(null, ExceptionUtils.getRootCause(null));
214:                assertSame(null, ExceptionUtils.getRootCause(withoutCause));
215:                assertSame(withoutCause, ExceptionUtils.getRootCause(nested));
216:                assertSame(withoutCause, ExceptionUtils.getRootCause(withCause));
217:                assertSame(null, ExceptionUtils.getRootCause(jdkNoCause));
218:                assertSame(null, ExceptionUtils.getRootCause(selfCause));
219:                assertSame(((ExceptionWithCause) cyclicCause.getCause())
220:                        .getCause(), ExceptionUtils.getRootCause(cyclicCause));
221:            }
222:
223:            public void testSetCause() {
224:                Exception cause = new ExceptionWithoutCause();
225:                assertEquals(true, ExceptionUtils.setCause(
226:                        new ExceptionWithCause(null), cause));
227:                if (SystemUtils.isJavaVersionAtLeast(140)) {
228:                    assertEquals(true, ExceptionUtils.setCause(
229:                            new ExceptionWithoutCause(), cause));
230:                }
231:            }
232:
233:            /**
234:             * Tests overriding a cause to <code>null</code>.
235:             */
236:            public void testSetCauseToNull() {
237:                Exception ex = new ExceptionWithCause(new IOException());
238:                assertEquals(true, ExceptionUtils.setCause(ex,
239:                        new IllegalStateException()));
240:                assertNotNull(ExceptionUtils.getCause(ex));
241:                assertEquals(true, ExceptionUtils.setCause(ex, null));
242:                assertNull(ExceptionUtils.getCause(ex));
243:            }
244:
245:            //-----------------------------------------------------------------------
246:            public void testIsThrowableNested() {
247:                if (SystemUtils.isJavaVersionAtLeast(140)) {
248:                    assertEquals(true, ExceptionUtils.isThrowableNested());
249:                } else {
250:                    assertEquals(false, ExceptionUtils.isThrowableNested());
251:                }
252:            }
253:
254:            public void testIsNestedThrowable_Throwable() {
255:                assertEquals(true, ExceptionUtils
256:                        .isNestedThrowable(new SQLException()));
257:                assertEquals(true, ExceptionUtils
258:                        .isNestedThrowable(new InvocationTargetException(
259:                                new Exception())));
260:                assertEquals(true, ExceptionUtils
261:                        .isNestedThrowable(new NestableRuntimeException()));
262:                assertEquals(true, ExceptionUtils.isNestedThrowable(withCause));
263:                assertEquals(true, ExceptionUtils.isNestedThrowable(nested));
264:                if (SystemUtils.isJavaVersionAtLeast(140)) {
265:                    assertEquals(true, ExceptionUtils
266:                            .isNestedThrowable(withoutCause));
267:                    assertEquals(true, ExceptionUtils
268:                            .isNestedThrowable(new Throwable()));
269:                } else {
270:                    assertEquals(false, ExceptionUtils
271:                            .isNestedThrowable(withoutCause));
272:                    assertEquals(false, ExceptionUtils
273:                            .isNestedThrowable(new Throwable()));
274:                }
275:            }
276:
277:            //-----------------------------------------------------------------------
278:            public void testGetThrowableCount_Throwable() {
279:                assertEquals(0, ExceptionUtils.getThrowableCount(null));
280:                assertEquals(1, ExceptionUtils.getThrowableCount(withoutCause));
281:                assertEquals(2, ExceptionUtils.getThrowableCount(nested));
282:                assertEquals(3, ExceptionUtils.getThrowableCount(withCause));
283:                assertEquals(1, ExceptionUtils.getThrowableCount(jdkNoCause));
284:                assertEquals(1, ExceptionUtils.getThrowableCount(selfCause));
285:                assertEquals(3, ExceptionUtils.getThrowableCount(cyclicCause));
286:            }
287:
288:            //-----------------------------------------------------------------------
289:            public void testGetThrowables_Throwable_null() {
290:                assertEquals(0, ExceptionUtils.getThrowables(null).length);
291:            }
292:
293:            public void testGetThrowables_Throwable_withoutCause() {
294:                Throwable[] throwables = ExceptionUtils
295:                        .getThrowables(withoutCause);
296:                assertEquals(1, throwables.length);
297:                assertSame(withoutCause, throwables[0]);
298:            }
299:
300:            public void testGetThrowables_Throwable_nested() {
301:                Throwable[] throwables = ExceptionUtils.getThrowables(nested);
302:                assertEquals(2, throwables.length);
303:                assertSame(nested, throwables[0]);
304:                assertSame(withoutCause, throwables[1]);
305:            }
306:
307:            public void testGetThrowables_Throwable_withCause() {
308:                Throwable[] throwables = ExceptionUtils
309:                        .getThrowables(withCause);
310:                assertEquals(3, throwables.length);
311:                assertSame(withCause, throwables[0]);
312:                assertSame(nested, throwables[1]);
313:                assertSame(withoutCause, throwables[2]);
314:            }
315:
316:            public void testGetThrowables_Throwable_jdkNoCause() {
317:                Throwable[] throwables = ExceptionUtils
318:                        .getThrowables(jdkNoCause);
319:                assertEquals(1, throwables.length);
320:                assertSame(jdkNoCause, throwables[0]);
321:            }
322:
323:            public void testGetThrowables_Throwable_selfCause() {
324:                Throwable[] throwables = ExceptionUtils
325:                        .getThrowables(selfCause);
326:                assertEquals(1, throwables.length);
327:                assertSame(selfCause, throwables[0]);
328:            }
329:
330:            public void testGetThrowables_Throwable_recursiveCause() {
331:                Throwable[] throwables = ExceptionUtils
332:                        .getThrowables(cyclicCause);
333:                assertEquals(3, throwables.length);
334:                assertSame(cyclicCause, throwables[0]);
335:                assertSame(cyclicCause.getCause(), throwables[1]);
336:                assertSame(((ExceptionWithCause) cyclicCause.getCause())
337:                        .getCause(), throwables[2]);
338:            }
339:
340:            //-----------------------------------------------------------------------
341:            public void testGetThrowableList_Throwable_null() {
342:                List throwables = ExceptionUtils.getThrowableList(null);
343:                assertEquals(0, throwables.size());
344:            }
345:
346:            public void testGetThrowableList_Throwable_withoutCause() {
347:                List throwables = ExceptionUtils.getThrowableList(withoutCause);
348:                assertEquals(1, throwables.size());
349:                assertSame(withoutCause, throwables.get(0));
350:            }
351:
352:            public void testGetThrowableList_Throwable_nested() {
353:                List throwables = ExceptionUtils.getThrowableList(nested);
354:                assertEquals(2, throwables.size());
355:                assertSame(nested, throwables.get(0));
356:                assertSame(withoutCause, throwables.get(1));
357:            }
358:
359:            public void testGetThrowableList_Throwable_withCause() {
360:                List throwables = ExceptionUtils.getThrowableList(withCause);
361:                assertEquals(3, throwables.size());
362:                assertSame(withCause, throwables.get(0));
363:                assertSame(nested, throwables.get(1));
364:                assertSame(withoutCause, throwables.get(2));
365:            }
366:
367:            public void testGetThrowableList_Throwable_jdkNoCause() {
368:                List throwables = ExceptionUtils.getThrowableList(jdkNoCause);
369:                assertEquals(1, throwables.size());
370:                assertSame(jdkNoCause, throwables.get(0));
371:            }
372:
373:            public void testGetThrowableList_Throwable_selfCause() {
374:                List throwables = ExceptionUtils.getThrowableList(selfCause);
375:                assertEquals(1, throwables.size());
376:                assertSame(selfCause, throwables.get(0));
377:            }
378:
379:            public void testGetThrowableList_Throwable_recursiveCause() {
380:                List throwables = ExceptionUtils.getThrowableList(cyclicCause);
381:                assertEquals(3, throwables.size());
382:                assertSame(cyclicCause, throwables.get(0));
383:                assertSame(cyclicCause.getCause(), throwables.get(1));
384:                assertSame(((ExceptionWithCause) cyclicCause.getCause())
385:                        .getCause(), throwables.get(2));
386:            }
387:
388:            //-----------------------------------------------------------------------
389:            public void testIndexOf_ThrowableClass() {
390:                assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null));
391:                assertEquals(-1, ExceptionUtils.indexOfThrowable(null,
392:                        NestableException.class));
393:
394:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
395:                        null));
396:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
397:                        ExceptionWithCause.class));
398:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
399:                        NestableException.class));
400:                assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause,
401:                        ExceptionWithoutCause.class));
402:
403:                assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null));
404:                assertEquals(-1, ExceptionUtils.indexOfThrowable(nested,
405:                        ExceptionWithCause.class));
406:                assertEquals(0, ExceptionUtils.indexOfThrowable(nested,
407:                        NestableException.class));
408:                assertEquals(1, ExceptionUtils.indexOfThrowable(nested,
409:                        ExceptionWithoutCause.class));
410:
411:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
412:                        null));
413:                assertEquals(0, ExceptionUtils.indexOfThrowable(withCause,
414:                        ExceptionWithCause.class));
415:                assertEquals(1, ExceptionUtils.indexOfThrowable(withCause,
416:                        NestableException.class));
417:                assertEquals(2, ExceptionUtils.indexOfThrowable(withCause,
418:                        ExceptionWithoutCause.class));
419:
420:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
421:                        Exception.class));
422:            }
423:
424:            public void testIndexOf_ThrowableClassInt() {
425:                assertEquals(-1, ExceptionUtils.indexOfThrowable(null, null, 0));
426:                assertEquals(-1, ExceptionUtils.indexOfThrowable(null,
427:                        NestableException.class, 0));
428:
429:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
430:                        null));
431:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
432:                        ExceptionWithCause.class, 0));
433:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withoutCause,
434:                        NestableException.class, 0));
435:                assertEquals(0, ExceptionUtils.indexOfThrowable(withoutCause,
436:                        ExceptionWithoutCause.class, 0));
437:
438:                assertEquals(-1, ExceptionUtils.indexOfThrowable(nested, null,
439:                        0));
440:                assertEquals(-1, ExceptionUtils.indexOfThrowable(nested,
441:                        ExceptionWithCause.class, 0));
442:                assertEquals(0, ExceptionUtils.indexOfThrowable(nested,
443:                        NestableException.class, 0));
444:                assertEquals(1, ExceptionUtils.indexOfThrowable(nested,
445:                        ExceptionWithoutCause.class, 0));
446:
447:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
448:                        null));
449:                assertEquals(0, ExceptionUtils.indexOfThrowable(withCause,
450:                        ExceptionWithCause.class, 0));
451:                assertEquals(1, ExceptionUtils.indexOfThrowable(withCause,
452:                        NestableException.class, 0));
453:                assertEquals(2, ExceptionUtils.indexOfThrowable(withCause,
454:                        ExceptionWithoutCause.class, 0));
455:
456:                assertEquals(0, ExceptionUtils.indexOfThrowable(withCause,
457:                        ExceptionWithCause.class, -1));
458:                assertEquals(0, ExceptionUtils.indexOfThrowable(withCause,
459:                        ExceptionWithCause.class, 0));
460:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
461:                        ExceptionWithCause.class, 1));
462:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
463:                        ExceptionWithCause.class, 9));
464:
465:                assertEquals(-1, ExceptionUtils.indexOfThrowable(withCause,
466:                        Exception.class, 0));
467:            }
468:
469:            //-----------------------------------------------------------------------
470:            public void testIndexOfType_ThrowableClass() {
471:                assertEquals(-1, ExceptionUtils.indexOfType(null, null));
472:                assertEquals(-1, ExceptionUtils.indexOfType(null,
473:                        NestableException.class));
474:
475:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
476:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause,
477:                        ExceptionWithCause.class));
478:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause,
479:                        NestableException.class));
480:                assertEquals(0, ExceptionUtils.indexOfType(withoutCause,
481:                        ExceptionWithoutCause.class));
482:
483:                assertEquals(-1, ExceptionUtils.indexOfType(nested, null));
484:                assertEquals(-1, ExceptionUtils.indexOfType(nested,
485:                        ExceptionWithCause.class));
486:                assertEquals(0, ExceptionUtils.indexOfType(nested,
487:                        NestableException.class));
488:                assertEquals(1, ExceptionUtils.indexOfType(nested,
489:                        ExceptionWithoutCause.class));
490:
491:                assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
492:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
493:                        ExceptionWithCause.class));
494:                assertEquals(1, ExceptionUtils.indexOfType(withCause,
495:                        NestableException.class));
496:                assertEquals(2, ExceptionUtils.indexOfType(withCause,
497:                        ExceptionWithoutCause.class));
498:
499:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
500:                        Exception.class));
501:            }
502:
503:            public void testIndexOfType_ThrowableClassInt() {
504:                assertEquals(-1, ExceptionUtils.indexOfType(null, null, 0));
505:                assertEquals(-1, ExceptionUtils.indexOfType(null,
506:                        NestableException.class, 0));
507:
508:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause, null));
509:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause,
510:                        ExceptionWithCause.class, 0));
511:                assertEquals(-1, ExceptionUtils.indexOfType(withoutCause,
512:                        NestableException.class, 0));
513:                assertEquals(0, ExceptionUtils.indexOfType(withoutCause,
514:                        ExceptionWithoutCause.class, 0));
515:
516:                assertEquals(-1, ExceptionUtils.indexOfType(nested, null, 0));
517:                assertEquals(-1, ExceptionUtils.indexOfType(nested,
518:                        ExceptionWithCause.class, 0));
519:                assertEquals(0, ExceptionUtils.indexOfType(nested,
520:                        NestableException.class, 0));
521:                assertEquals(1, ExceptionUtils.indexOfType(nested,
522:                        ExceptionWithoutCause.class, 0));
523:
524:                assertEquals(-1, ExceptionUtils.indexOfType(withCause, null));
525:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
526:                        ExceptionWithCause.class, 0));
527:                assertEquals(1, ExceptionUtils.indexOfType(withCause,
528:                        NestableException.class, 0));
529:                assertEquals(2, ExceptionUtils.indexOfType(withCause,
530:                        ExceptionWithoutCause.class, 0));
531:
532:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
533:                        ExceptionWithCause.class, -1));
534:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
535:                        ExceptionWithCause.class, 0));
536:                assertEquals(-1, ExceptionUtils.indexOfType(withCause,
537:                        ExceptionWithCause.class, 1));
538:                assertEquals(-1, ExceptionUtils.indexOfType(withCause,
539:                        ExceptionWithCause.class, 9));
540:
541:                assertEquals(0, ExceptionUtils.indexOfType(withCause,
542:                        Exception.class, 0));
543:            }
544:
545:            //-----------------------------------------------------------------------
546:            public void testPrintRootCauseStackTrace_Throwable()
547:                    throws Exception {
548:                ExceptionUtils.printRootCauseStackTrace(null);
549:                // could pipe system.err to a known stream, but not much point as
550:                // internally this method calls stram method anyway
551:            }
552:
553:            public void testPrintRootCauseStackTrace_ThrowableStream()
554:                    throws Exception {
555:                ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
556:                ExceptionUtils.printRootCauseStackTrace(null,
557:                        (PrintStream) null);
558:                ExceptionUtils.printRootCauseStackTrace(null, new PrintStream(
559:                        out));
560:                assertEquals(0, out.toString().length());
561:
562:                out = new ByteArrayOutputStream(1024);
563:                try {
564:                    ExceptionUtils.printRootCauseStackTrace(withCause,
565:                            (PrintStream) null);
566:                    fail();
567:                } catch (IllegalArgumentException ex) {
568:                }
569:
570:                out = new ByteArrayOutputStream(1024);
571:                Throwable withCause = createExceptionWithCause();
572:                ExceptionUtils.printRootCauseStackTrace(withCause,
573:                        new PrintStream(out));
574:                String stackTrace = out.toString();
575:                assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) != -1);
576:
577:                out = new ByteArrayOutputStream(1024);
578:                ExceptionUtils.printRootCauseStackTrace(withoutCause,
579:                        new PrintStream(out));
580:                stackTrace = out.toString();
581:                assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) == -1);
582:            }
583:
584:            public void testPrintRootCauseStackTrace_ThrowableWriter()
585:                    throws Exception {
586:                StringWriter writer = new StringWriter(1024);
587:                ExceptionUtils.printRootCauseStackTrace(null,
588:                        (PrintWriter) null);
589:                ExceptionUtils.printRootCauseStackTrace(null, new PrintWriter(
590:                        writer));
591:                assertEquals(0, writer.getBuffer().length());
592:
593:                writer = new StringWriter(1024);
594:                try {
595:                    ExceptionUtils.printRootCauseStackTrace(withCause,
596:                            (PrintWriter) null);
597:                    fail();
598:                } catch (IllegalArgumentException ex) {
599:                }
600:
601:                writer = new StringWriter(1024);
602:                Throwable withCause = createExceptionWithCause();
603:                ExceptionUtils.printRootCauseStackTrace(withCause,
604:                        new PrintWriter(writer));
605:                String stackTrace = writer.toString();
606:                assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) != -1);
607:
608:                writer = new StringWriter(1024);
609:                ExceptionUtils.printRootCauseStackTrace(withoutCause,
610:                        new PrintWriter(writer));
611:                stackTrace = writer.toString();
612:                assertTrue(stackTrace.indexOf(ExceptionUtils.WRAPPED_MARKER) == -1);
613:            }
614:
615:            //-----------------------------------------------------------------------
616:            public void testGetRootCauseStackTrace_Throwable() throws Exception {
617:                assertEquals(0,
618:                        ExceptionUtils.getRootCauseStackTrace(null).length);
619:
620:                Throwable withCause = createExceptionWithCause();
621:                String[] stackTrace = ExceptionUtils
622:                        .getRootCauseStackTrace(withCause);
623:                boolean match = false;
624:                for (int i = 0; i < stackTrace.length; i++) {
625:                    if (stackTrace[i].startsWith(ExceptionUtils.WRAPPED_MARKER)) {
626:                        match = true;
627:                        break;
628:                    }
629:                }
630:                assertEquals(true, match);
631:
632:                stackTrace = ExceptionUtils
633:                        .getRootCauseStackTrace(withoutCause);
634:                match = false;
635:                for (int i = 0; i < stackTrace.length; i++) {
636:                    if (stackTrace[i].startsWith(ExceptionUtils.WRAPPED_MARKER)) {
637:                        match = true;
638:                        break;
639:                    }
640:                }
641:                assertEquals(false, match);
642:            }
643:
644:            public void testRemoveCommonFrames_ListList() throws Exception {
645:                try {
646:                    ExceptionUtils.removeCommonFrames(null, null);
647:                    fail();
648:                } catch (IllegalArgumentException ex) {
649:                }
650:            }
651:
652:            public void test_getMessage_Throwable() {
653:                Throwable th = null;
654:                assertEquals("", ExceptionUtils.getMessage(th));
655:
656:                th = new IllegalArgumentException("Base");
657:                assertEquals("IllegalArgumentException: Base", ExceptionUtils
658:                        .getMessage(th));
659:
660:                th = new ExceptionWithCause("Wrapper", th);
661:                assertEquals(
662:                        "ExceptionUtilsTestCase.ExceptionWithCause: Wrapper",
663:                        ExceptionUtils.getMessage(th));
664:            }
665:
666:            public void test_getRootCauseMessage_Throwable() {
667:                Throwable th = null;
668:                assertEquals("", ExceptionUtils.getRootCauseMessage(th));
669:
670:                th = new IllegalArgumentException("Base");
671:                assertEquals("IllegalArgumentException: Base", ExceptionUtils
672:                        .getRootCauseMessage(th));
673:
674:                th = new ExceptionWithCause("Wrapper", th);
675:                assertEquals("IllegalArgumentException: Base", ExceptionUtils
676:                        .getRootCauseMessage(th));
677:            }
678:
679:            //-----------------------------------------------------------------------
680:            /**
681:             * Provides a method with a well known chained/nested exception
682:             * name which matches the full signature (e.g. has a return value
683:             * of <code>Throwable</code>.
684:             */
685:            private static class ExceptionWithCause extends Exception {
686:                private Throwable cause;
687:
688:                public ExceptionWithCause(String str, Throwable cause) {
689:                    super (str);
690:                    setCause(cause);
691:                }
692:
693:                public ExceptionWithCause(Throwable cause) {
694:                    super ();
695:                    setCause(cause);
696:                }
697:
698:                public Throwable getCause() {
699:                    return cause;
700:                }
701:
702:                public void setCause(Throwable cause) {
703:                    this .cause = cause;
704:                }
705:            }
706:
707:            /**
708:             * Provides a method with a well known chained/nested exception
709:             * name which does not match the full signature (e.g. lacks a
710:             * return value of <code>Throwable</code>.
711:             */
712:            private static class ExceptionWithoutCause extends Exception {
713:                public void getTargetException() {
714:                }
715:            }
716:
717:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.