Source Code Cross Referenced for NestableDelegateTestCase.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) 


0001:        /*
0002:         * Licensed to the Apache Software Foundation (ASF) under one or more
0003:         * contributor license agreements.  See the NOTICE file distributed with
0004:         * this work for additional information regarding copyright ownership.
0005:         * The ASF licenses this file to You under the Apache License, Version 2.0
0006:         * (the "License"); you may not use this file except in compliance with
0007:         * the License.  You may obtain a copy of the License at
0008:         * 
0009:         *      http://www.apache.org/licenses/LICENSE-2.0
0010:         * 
0011:         * Unless required by applicable law or agreed to in writing, software
0012:         * distributed under the License is distributed on an "AS IS" BASIS,
0013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014:         * See the License for the specific language governing permissions and
0015:         * limitations under the License.
0016:         */
0017:        package org.apache.commons.lang.exception;
0018:
0019:        import java.io.ByteArrayOutputStream;
0020:        import java.io.EOFException;
0021:        import java.io.IOException;
0022:        import java.io.PrintStream;
0023:        import java.io.PrintWriter;
0024:
0025:        import junit.framework.Test;
0026:        import junit.framework.TestSuite;
0027:        import junit.textui.TestRunner;
0028:
0029:        /**
0030:         * Tests the org.apache.commons.lang.exception.NestableDelegate class.
0031:         *
0032:         * @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
0033:         * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
0034:         * @version $Id: NestableDelegateTestCase.java 437554 2006-08-28 06:21:41Z bayard $
0035:         */
0036:        public class NestableDelegateTestCase extends junit.framework.TestCase {
0037:            private static final String CONSTRUCTOR_FAILED_MSG = "The Nestable implementation passed to the NestableDelegate(Nestable) constructor must extend java.lang.Throwable";
0038:
0039:            private static final String PARTIAL_STACK_TRACE = "ThrowableNestedNestable partial stack trace place-holder";
0040:
0041:            protected String lineSeparator;
0042:
0043:            /**
0044:             * Construct a new instance of NestableDelegateTestCase with the specified name
0045:             */
0046:            public NestableDelegateTestCase(String name) {
0047:                super (name);
0048:            }
0049:
0050:            /**
0051:             * Set up instance variables required by this test case.
0052:             */
0053:            public void setUp() {
0054:                lineSeparator = System.getProperty("line.separator");
0055:            }
0056:
0057:            public static Test suite() {
0058:                return new TestSuite(NestableDelegateTestCase.class);
0059:            }
0060:
0061:            /**
0062:             * Tear down instance variables required by this test case.
0063:             */
0064:            public void tearDown() {
0065:                lineSeparator = null;
0066:            }
0067:
0068:            /**
0069:             * Test the implementation
0070:             */
0071:            public void testNestableDelegateConstructor() {
0072:                String msg = null;
0073:                boolean constructorFailed = false;
0074:                try {
0075:                    NestableDelegate nonThrowableCause = new NestableDelegate(
0076:                            new NonThrowableNestable());
0077:                } catch (IllegalArgumentException iae) {
0078:                    constructorFailed = true;
0079:                    msg = iae.getMessage();
0080:                }
0081:                assertTrue(
0082:                        "nestable delegate constructor with non-throwable cause failed == true",
0083:                        constructorFailed);
0084:                assertTrue("constructor failed exception msg == "
0085:                        + CONSTRUCTOR_FAILED_MSG, msg
0086:                        .equals(CONSTRUCTOR_FAILED_MSG));
0087:
0088:                constructorFailed = false;
0089:                try {
0090:                    NestableDelegate nd1 = new NestableDelegate(
0091:                            new ThrowableNestable());
0092:                } catch (IllegalArgumentException iae) {
0093:                    constructorFailed = true;
0094:                }
0095:                assertTrue(
0096:                        "nestable delegate constructor with throwable cause failed == false",
0097:                        !constructorFailed);
0098:            }
0099:
0100:            public void testNestableDelegateGetMessage() {
0101:                Nestable ne1 = new ThrowableNestable();
0102:                assertTrue(
0103:                        "ThrowableNestable ne1 getMessage() == ThrowableNestable exception",
0104:                        ne1.getMessage().equals("ThrowableNestable exception"));
0105:                NestableDelegate nd1 = new NestableDelegate(ne1);
0106:                assertTrue(
0107:                        "nd1 getMessage() == " + ne1.getCause().getMessage(),
0108:                        nd1.getMessage("base").equals(
0109:                                "base: " + ne1.getCause().getMessage()));
0110:
0111:                Nestable ne2 = new ThrowableNestedNestable(new Exception(
0112:                        "nested exception 2"));
0113:                NestableDelegate nd2 = new NestableDelegate(ne2);
0114:                assertTrue("nd2 getMessage() == base: "
0115:                        + ne2.getCause().getMessage(), nd2.getMessage("base")
0116:                        .equals("base: " + ne2.getCause().getMessage()));
0117:            }
0118:
0119:            public void testNestableDelegateGetThrowableCount() {
0120:                Nestable n = null;
0121:                NestableDelegate d = null;
0122:
0123:                n = new NestableDelegateTester1();
0124:                d = new NestableDelegate(n);
0125:                doNestableDelegateGetThrowableCount(d, 1);
0126:
0127:                n = new NestableDelegateTester1("level 1");
0128:                d = new NestableDelegate(n);
0129:                doNestableDelegateGetThrowableCount(d, 1);
0130:
0131:                n = new NestableDelegateTester1(new Exception());
0132:                d = new NestableDelegate(n);
0133:                doNestableDelegateGetThrowableCount(d, 2);
0134:
0135:                n = new NestableDelegateTester1(new Exception("level 2"));
0136:                d = new NestableDelegate(n);
0137:                doNestableDelegateGetThrowableCount(d, 2);
0138:
0139:                n = new NestableDelegateTester1("level 1",
0140:                        new NestableDelegateTester2("level 2",
0141:                                new NestableDelegateTester1(
0142:                                        new NestableDelegateTester2("level 4",
0143:                                                new Exception("level 5")))));
0144:                d = new NestableDelegate(n);
0145:                doNestableDelegateGetThrowableCount(d, 5);
0146:            }
0147:
0148:            private void doNestableDelegateGetThrowableCount(
0149:                    NestableDelegate d, int len) {
0150:                // Compare the lengths
0151:                assertEquals("delegate length", len, d.getThrowableCount());
0152:            }
0153:
0154:            public void testNestableDelegateGetMessages() {
0155:                Nestable n = null;
0156:                NestableDelegate d = null;
0157:                String msgs[] = null;
0158:
0159:                msgs = new String[1];
0160:                n = new NestableDelegateTester1();
0161:                d = new NestableDelegate(n);
0162:                doNestableDelegateGetMessages(d, msgs);
0163:
0164:                msgs = new String[1];
0165:                msgs[0] = "level 1";
0166:                n = new NestableDelegateTester1(msgs[0]);
0167:                d = new NestableDelegate(n);
0168:                doNestableDelegateGetMessages(d, msgs);
0169:
0170:                msgs = new String[2];
0171:                n = new NestableDelegateTester1(new Exception());
0172:                d = new NestableDelegate(n);
0173:                doNestableDelegateGetMessages(d, msgs);
0174:
0175:                msgs = new String[2];
0176:                msgs[0] = null;
0177:                msgs[1] = "level 2";
0178:                n = new NestableDelegateTester1(new Exception(msgs[1]));
0179:                d = new NestableDelegate(n);
0180:                doNestableDelegateGetMessages(d, msgs);
0181:
0182:                msgs = new String[5];
0183:                msgs[0] = "level 1";
0184:                msgs[1] = "level 2";
0185:                msgs[2] = null;
0186:                msgs[3] = "level 4";
0187:                msgs[4] = "level 5";
0188:                n = new NestableDelegateTester1(msgs[0],
0189:                        new NestableDelegateTester2(msgs[1],
0190:                                new NestableDelegateTester1(
0191:                                        new NestableDelegateTester2(msgs[3],
0192:                                                new Exception(msgs[4])))));
0193:                d = new NestableDelegate(n);
0194:                doNestableDelegateGetMessages(d, msgs);
0195:            }
0196:
0197:            private void doNestableDelegateGetMessages(NestableDelegate d,
0198:                    String[] nMsgs) {
0199:                // Compare the messages
0200:                String[] dMsgs = d.getMessages();
0201:                assertEquals("messages length", nMsgs.length, dMsgs.length);
0202:                for (int i = 0; i < nMsgs.length; i++) {
0203:                    assertEquals("message " + i, nMsgs[i], dMsgs[i]);
0204:                }
0205:            }
0206:
0207:            public void testGetMessageString() {
0208:                NestableDelegateTester1 ndt1 = new NestableDelegateTester1(
0209:                        new NullPointerException());
0210:                NestableDelegate nd = new NestableDelegate(ndt1);
0211:                assertNull(nd.getMessage((String) null));
0212:
0213:                ndt1 = new NestableDelegateTester1(new NullPointerException(
0214:                        "null pointer"));
0215:                nd = new NestableDelegate(ndt1);
0216:                assertNotNull(nd.getMessage((String) null));
0217:
0218:                ndt1 = new NestableDelegateTester1();
0219:                nd = new NestableDelegate(ndt1);
0220:                assertNull(nd.getMessage((String) null));
0221:
0222:                ndt1 = new NestableDelegateTester1("root");
0223:                nd = new NestableDelegate(ndt1);
0224:                assertNull(nd.getMessage((String) null));
0225:            }
0226:
0227:            public void testNestableDelegateGetMessageN() {
0228:                Nestable n = null;
0229:                NestableDelegate d = null;
0230:                String[] msgs = new String[5];
0231:                msgs[0] = "level 1";
0232:                msgs[1] = "level 2";
0233:                msgs[2] = null;
0234:                msgs[3] = "level 4";
0235:                msgs[4] = "level 5";
0236:                n = new NestableDelegateTester1(msgs[0],
0237:                        new NestableDelegateTester2(msgs[1],
0238:                                new NestableDelegateTester1(
0239:                                        new NestableDelegateTester2(msgs[3],
0240:                                                new Exception(msgs[4])))));
0241:                d = new NestableDelegate(n);
0242:                for (int i = 0; i < msgs.length; i++) {
0243:                    assertEquals("message " + i, msgs[i], d.getMessage(i));
0244:                }
0245:
0246:                // Test for index out of bounds
0247:                try {
0248:                    String msg = d.getMessage(-1);
0249:                    fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
0250:                } catch (IndexOutOfBoundsException ioode) {
0251:                }
0252:                try {
0253:                    String msg = d.getMessage(msgs.length + 100);
0254:                    fail("getMessage(999) should have thrown IndexOutOfBoundsException");
0255:                } catch (IndexOutOfBoundsException ioode) {
0256:                }
0257:            }
0258:
0259:            public void testNestableDelegateGetThrowableN() {
0260:                Nestable n = null;
0261:                NestableDelegate d = null;
0262:                String msgs[] = null;
0263:                Class[] throwables = null;
0264:
0265:                msgs = new String[2];
0266:                msgs[0] = null;
0267:                msgs[1] = "level 2";
0268:                throwables = new Class[2];
0269:                throwables[0] = NestableDelegateTester1.class;
0270:                throwables[1] = Exception.class;
0271:                n = new NestableDelegateTester1(new Exception(msgs[1]));
0272:                d = new NestableDelegate(n);
0273:                doNestableDelegateGetThrowableN(d, throwables, msgs);
0274:
0275:                msgs = new String[5];
0276:                msgs[0] = "level 1";
0277:                msgs[1] = "level 2";
0278:                msgs[2] = null;
0279:                msgs[3] = "level 4";
0280:                msgs[4] = "level 5";
0281:                throwables = new Class[5];
0282:                throwables[0] = NestableDelegateTester1.class;
0283:                throwables[1] = NestableDelegateTester2.class;
0284:                throwables[2] = NestableDelegateTester1.class;
0285:                throwables[3] = NestableDelegateTester2.class;
0286:                throwables[4] = Exception.class;
0287:                n = new NestableDelegateTester1(msgs[0],
0288:                        new NestableDelegateTester2(msgs[1],
0289:                                new NestableDelegateTester1(
0290:                                        new NestableDelegateTester2(msgs[3],
0291:                                                new Exception(msgs[4])))));
0292:                d = new NestableDelegate(n);
0293:                doNestableDelegateGetThrowableN(d, throwables, msgs);
0294:            }
0295:
0296:            private void doNestableDelegateGetThrowableN(NestableDelegate d,
0297:                    Class[] classes, String[] msgs) {
0298:                Throwable t = null;
0299:                String msg = null;
0300:
0301:                for (int i = 0; i < classes.length; i++) {
0302:                    t = d.getThrowable(i);
0303:                    assertEquals("throwable class", classes[i], t.getClass());
0304:                    if (Nestable.class.isInstance(t)) {
0305:                        msg = ((Nestable) t).getMessage(0);
0306:                    } else {
0307:                        msg = t.getMessage();
0308:                    }
0309:                    assertEquals("throwable message", msgs[i], msg);
0310:                }
0311:
0312:                // Test for index out of bounds
0313:                try {
0314:                    t = d.getThrowable(-1);
0315:                    fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
0316:                } catch (IndexOutOfBoundsException ioobe) {
0317:                }
0318:                try {
0319:                    t = d.getThrowable(999);
0320:                    fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
0321:                } catch (IndexOutOfBoundsException ioobe) {
0322:                }
0323:            }
0324:
0325:            public void testNestableDelegateGetThrowables() {
0326:                Nestable n = null;
0327:                NestableDelegate d = null;
0328:                String msgs[] = null;
0329:                Class[] throwables = null;
0330:
0331:                msgs = new String[2];
0332:                msgs[0] = null;
0333:                msgs[1] = "level 2";
0334:                throwables = new Class[2];
0335:                throwables[0] = NestableDelegateTester1.class;
0336:                throwables[1] = Exception.class;
0337:                n = new NestableDelegateTester1(new Exception(msgs[1]));
0338:                d = new NestableDelegate(n);
0339:                doNestableDelegateGetThrowables(d, throwables, msgs);
0340:
0341:                msgs = new String[5];
0342:                msgs[0] = "level 1";
0343:                msgs[1] = "level 2";
0344:                msgs[2] = null;
0345:                msgs[3] = "level 4";
0346:                msgs[4] = "level 5";
0347:                throwables = new Class[5];
0348:                throwables[0] = NestableDelegateTester1.class;
0349:                throwables[1] = NestableDelegateTester2.class;
0350:                throwables[2] = NestableDelegateTester1.class;
0351:                throwables[3] = NestableDelegateTester2.class;
0352:                throwables[4] = Exception.class;
0353:                n = new NestableDelegateTester1(msgs[0],
0354:                        new NestableDelegateTester2(msgs[1],
0355:                                new NestableDelegateTester1(
0356:                                        new NestableDelegateTester2(msgs[3],
0357:                                                new Exception(msgs[4])))));
0358:                d = new NestableDelegate(n);
0359:                doNestableDelegateGetThrowables(d, throwables, msgs);
0360:            }
0361:
0362:            private void doNestableDelegateGetThrowables(NestableDelegate d,
0363:                    Class[] classes, String[] msgs) {
0364:                Throwable[] throwables = null;
0365:                String msg = null;
0366:
0367:                throwables = d.getThrowables();
0368:                assertEquals("throwables length", classes.length,
0369:                        throwables.length);
0370:                for (int i = 0; i < classes.length; i++) {
0371:                    assertEquals("throwable class", classes[i], throwables[i]
0372:                            .getClass());
0373:                    Throwable t = throwables[i];
0374:                    if (Nestable.class.isInstance(t)) {
0375:                        msg = ((Nestable) t).getMessage(0);
0376:                    } else {
0377:                        msg = t.getMessage();
0378:                    }
0379:                    assertEquals("throwable message", msgs[i], msg);
0380:                }
0381:            }
0382:
0383:            public void testIndexOfThrowable() {
0384:                Nestable n = null;
0385:                NestableDelegate d = null;
0386:                String msgs[] = null;
0387:                Class[] throwables = null;
0388:
0389:                msgs = new String[5];
0390:                msgs[0] = "level 1";
0391:                msgs[1] = "level 2";
0392:                msgs[2] = null;
0393:                msgs[3] = "level 4";
0394:                msgs[4] = "level 5";
0395:                throwables = new Class[5];
0396:                throwables[0] = NestableDelegateTester1.class;
0397:                throwables[1] = NestableDelegateTester2.class;
0398:                throwables[2] = NestableDelegateTester1.class;
0399:                throwables[3] = NestableDelegateTester2.class;
0400:                throwables[4] = EOFException.class;
0401:                int[] indexes = { 0, 1, 0, 1, 4 };
0402:                n = new NestableDelegateTester1(msgs[0],
0403:                        new NestableDelegateTester2(msgs[1],
0404:                                new NestableDelegateTester1(
0405:                                        new NestableDelegateTester2(msgs[3],
0406:                                                new EOFException(msgs[4])))));
0407:                d = new NestableDelegate(n);
0408:                for (int i = 0; i < throwables.length; i++) {
0409:                    doNestableDelegateIndexOfThrowable(d, throwables[i], 0,
0410:                            indexes[i], msgs[indexes[i]]);
0411:                }
0412:                doNestableDelegateIndexOfThrowable(d,
0413:                        NestableDelegateTester2.class, 2, 3, msgs[3]);
0414:                doNestableDelegateIndexOfThrowable(d,
0415:                        NestableDelegateTester1.class, 1, 2, msgs[2]);
0416:                doNestableDelegateIndexOfThrowable(d,
0417:                        NestableDelegateTester1.class, 3, -1, null);
0418:                doNestableDelegateIndexOfThrowable(d,
0419:                        NestableDelegateTester1.class, 4, -1, null);
0420:                doNestableDelegateIndexOfThrowable(d, EOFException.class, 2, 4,
0421:                        msgs[4]);
0422:                doNestableDelegateIndexOfThrowable(d, IOException.class, 2, 4,
0423:                        msgs[4]);
0424:                doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 2,
0425:                        msgs[2]);
0426:                doNestableDelegateIndexOfThrowable(d, Exception.class, 0, 0,
0427:                        msgs[0]);
0428:                doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0,
0429:                        -1, null);
0430:                doNestableDelegateIndexOfThrowable(d, null, 0, -1, null);
0431:
0432:                // Test for index out of bounds
0433:                try {
0434:                    int index = d.indexOfThrowable(
0435:                            NestableDelegateTester1.class, -1);
0436:                    fail("method should have thrown IndexOutOfBoundsException");
0437:                } catch (IndexOutOfBoundsException iooob) {
0438:                }
0439:                try {
0440:                    int index = d.indexOfThrowable(
0441:                            NestableDelegateTester1.class, 5);
0442:                    fail("method should have thrown IndexOutOfBoundsException");
0443:                } catch (IndexOutOfBoundsException iooob) {
0444:                }
0445:            }
0446:
0447:            private void doNestableDelegateIndexOfThrowable(NestableDelegate d,
0448:                    Class type, int fromIndex, int expectedIndex,
0449:                    String expectedMsg) {
0450:                Throwable t = null;
0451:
0452:                int index = d.indexOfThrowable(type, fromIndex);
0453:                assertEquals("index of throwable "
0454:                        + (type == null ? "null" : type.getName()),
0455:                        expectedIndex, index);
0456:                if (expectedIndex > -1) {
0457:                    t = d.getThrowable(index);
0458:                    if (expectedMsg != null) {
0459:                        String msg = null;
0460:                        if (Nestable.class.isInstance(t)) {
0461:                            msg = ((Nestable) t).getMessage(0);
0462:                        } else {
0463:                            msg = t.getMessage();
0464:                        }
0465:                        assertEquals("message of indexed throwable",
0466:                                expectedMsg, msg);
0467:                    }
0468:                }
0469:            }
0470:
0471:            public void testNestableDelegetePrintStackTrace() {
0472:                int lineSepLen = lineSeparator.length();
0473:                int partialStackTraceLen = PARTIAL_STACK_TRACE.length();
0474:                Nestable ne3 = new ThrowableNestedNestable(new Exception(
0475:                        "nested exception 3"));
0476:                NestableDelegate nd3 = new NestableDelegate(ne3);
0477:
0478:                ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
0479:                PrintStream ps1 = new PrintStream(baos1);
0480:                nd3.printStackTrace(ps1);
0481:                String stack1 = baos1.toString();
0482:                assertTrue("stack trace startsWith", stack1
0483:                        .startsWith(PARTIAL_STACK_TRACE));
0484:
0485:                Nestable n = new NestableDelegateTester1("level 1",
0486:                        new NestableDelegateTester2("level 2",
0487:                                new NestableDelegateTester1(
0488:                                        new NestableDelegateTester2("level 4",
0489:                                                new Exception("level 5")))));
0490:                NestableDelegate d = new NestableDelegate(n);
0491:
0492:                // Only testing the flags for jdk1.3 and below
0493:                if (!ExceptionUtils.isThrowableNested()) {
0494:                    NestableDelegate.topDown = true;
0495:                    NestableDelegate.trimStackFrames = true;
0496:                    checkStackTrace(d, true, true,
0497:                            NestableDelegateTester1.class.getName()
0498:                                    + ": level 1", 24);
0499:                    NestableDelegate.topDown = true;
0500:                    NestableDelegate.trimStackFrames = false;
0501:                    checkStackTrace(d, true, false,
0502:                            NestableDelegateTester1.class.getName()
0503:                                    + ": level 1", 80);
0504:                    NestableDelegate.topDown = false;
0505:                    NestableDelegate.trimStackFrames = true;
0506:                    checkStackTrace(d, false, true,
0507:                            "java.lang.Exception: level 5", 24);
0508:                    NestableDelegate.topDown = false;
0509:                    NestableDelegate.trimStackFrames = false;
0510:                    checkStackTrace(d, false, false,
0511:                            "java.lang.Exception: level 5", 80);
0512:                    NestableDelegate.topDown = true;
0513:                    NestableDelegate.trimStackFrames = true;
0514:                }
0515:            }
0516:
0517:            private void checkStackTrace(NestableDelegate d, boolean topDown,
0518:                    boolean trimStackFrames, String startsWith, int expCount) {
0519:                ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
0520:                PrintStream ps1 = new PrintStream(baos1);
0521:                d.printStackTrace(ps1);
0522:                String stack1 = baos1.toString();
0523:                int actCount = countLines(stack1);
0524:                assertTrue("topDown: " + topDown + ", trimStackFrames: "
0525:                        + trimStackFrames + " startsWith", stack1
0526:                        .startsWith(startsWith));
0527:                // test is unreliable, as count varies depending on JUnit version/where main method is
0528:                //        assertEquals("topDown: "+topDown+", trimStackFrames: "+trimStackFrames+" lineCount",
0529:                //            expCount, actCount);
0530:            }
0531:
0532:            private int countLines(String s) {
0533:                if (s == null)
0534:                    return 0;
0535:
0536:                int i = 0, ndx = -1;
0537:                while ((ndx = s.indexOf("\n", ndx + 1)) != -1) {
0538:                    i++;
0539:                }
0540:                return i;
0541:            }
0542:
0543:            public static void main(String args[]) {
0544:                TestRunner.run(suite());
0545:            }
0546:        }
0547:
0548:        /**
0549:         * Nestable and Throwable class which can be passed to the NestableDelegate
0550:         * constructor. Used for testing various methods which iterate through the
0551:         * nested causes.
0552:         */
0553:        class NestableDelegateTester1 extends Exception implements  Nestable {
0554:            private Throwable cause = null;
0555:
0556:            public NestableDelegateTester1() {
0557:                super ();
0558:            }
0559:
0560:            public NestableDelegateTester1(String reason, Throwable cause) {
0561:                super (reason);
0562:                this .cause = cause;
0563:            }
0564:
0565:            public NestableDelegateTester1(String reason) {
0566:                super (reason);
0567:            }
0568:
0569:            public NestableDelegateTester1(Throwable cause) {
0570:                super ();
0571:                this .cause = cause;
0572:            }
0573:
0574:            /**
0575:             * @see Nestable#getThrowables()
0576:             * Returns zero-length <code>Throwable</code> array for this test.
0577:             */
0578:            public Throwable[] getThrowables() {
0579:                return new Throwable[0];
0580:            }
0581:
0582:            /**
0583:             * @see Nestable#getMessages()
0584:             * Returns zero-length String array for this test.
0585:             */
0586:            public String[] getMessages() {
0587:                return new String[0];
0588:            }
0589:
0590:            /**
0591:             * @see Nestable#indexOfThrowable(Class)
0592:             * Returns -1 for this test.
0593:             */
0594:            public int indexOfThrowable(Class type) {
0595:                return -1;
0596:            }
0597:
0598:            /**
0599:             * @see Nestable#getThrowable(int)
0600:             * Returns <code>null</code> for this test.
0601:             */
0602:            public Throwable getThrowable(int index) {
0603:                return null;
0604:            }
0605:
0606:            /**
0607:             * @see Nestable#getThrowableCount()
0608:             * Returns 1 for this test.
0609:             */
0610:            public int getThrowableCount() {
0611:                return 1;
0612:            }
0613:
0614:            /**
0615:             * @see Nestable#getCause()
0616:             */
0617:            public Throwable getCause() {
0618:                return cause;
0619:            }
0620:
0621:            /**
0622:             * Empty method to satisfy the implemented interface. Does nothing
0623:             * in this test.
0624:             *
0625:             * @param out The writer to use.
0626:             */
0627:            public void printPartialStackTrace(PrintWriter out) {
0628:                super .printStackTrace(out);
0629:            }
0630:
0631:            /**
0632:             * @see Nestable#getMessage(int)
0633:             */
0634:            public String getMessage(int index) {
0635:                if (index == 0) {
0636:                    return super .getMessage();
0637:                } else {
0638:                    return "";
0639:                }
0640:            }
0641:
0642:            /**
0643:             * @see Nestable#indexOfThrowable(Class, int)
0644:             * Returns -1 for this test.
0645:             */
0646:            public int indexOfThrowable(Class type, int fromIndex) {
0647:                return -1;
0648:            }
0649:
0650:        }
0651:
0652:        /**
0653:         * Nestable and Throwable class which can be passed to the NestableDelegate
0654:         * constructor. Used for testing various methods which iterate through the
0655:         * nested causes.
0656:         */
0657:        class NestableDelegateTester2 extends Throwable implements  Nestable {
0658:            private Throwable cause = null;
0659:
0660:            public NestableDelegateTester2() {
0661:                super ();
0662:            }
0663:
0664:            public NestableDelegateTester2(String reason, Throwable cause) {
0665:                super (reason);
0666:                this .cause = cause;
0667:            }
0668:
0669:            public NestableDelegateTester2(String reason) {
0670:                super (reason);
0671:            }
0672:
0673:            public NestableDelegateTester2(Throwable cause) {
0674:                super ();
0675:                this .cause = cause;
0676:            }
0677:
0678:            /**
0679:             * @see Nestable#getThrowables()
0680:             * Returns zero-length <code>Throwable</code> array for this test.
0681:             */
0682:            public Throwable[] getThrowables() {
0683:                return new Throwable[0];
0684:            }
0685:
0686:            /**
0687:             * @see Nestable#getMessages()
0688:             * Returns zero-length String array for this test.
0689:             */
0690:            public String[] getMessages() {
0691:                return new String[0];
0692:            }
0693:
0694:            /**
0695:             * @see Nestable#indexOfThrowable(Class)
0696:             * Returns -1 for this test.
0697:             */
0698:            public int indexOfThrowable(Class type) {
0699:                return -1;
0700:            }
0701:
0702:            /**
0703:             * @see Nestable#getThrowable(int)
0704:             * Returns <code>null</code> for this test.
0705:             */
0706:            public Throwable getThrowable(int index) {
0707:                return null;
0708:            }
0709:
0710:            /**
0711:             * @see Nestable#getThrowableCount()
0712:             * Returns 1 for this test.
0713:             *
0714:             * @return 1
0715:             */
0716:            public int getThrowableCount() {
0717:                return 1;
0718:            }
0719:
0720:            /**
0721:             * @see Nestable#getCause()
0722:             */
0723:            public Throwable getCause() {
0724:                return cause;
0725:            }
0726:
0727:            /**
0728:             * Empty method to satisfy the implemented interface. Does nothing
0729:             * in this test.
0730:             *
0731:             * @param out The writer to use.
0732:             */
0733:            public void printPartialStackTrace(PrintWriter out) {
0734:                super .printStackTrace(out);
0735:            }
0736:
0737:            /**
0738:             * @see Nestable#getMessage(int)
0739:             */
0740:            public String getMessage(int index) {
0741:                if (index == 0) {
0742:                    return super .getMessage();
0743:                } else {
0744:                    return "";
0745:                }
0746:            }
0747:
0748:            /**
0749:             * @see Nestable#indexOfThrowable(Class, int)     
0750:             * Returns -1 for this test.
0751:             */
0752:            public int indexOfThrowable(Class type, int fromIndex) {
0753:                return -1;
0754:            }
0755:
0756:        }
0757:
0758:        /**
0759:         * Used to test that the constructor passes when passed a throwable cause
0760:         * And, the NestableDelegate.getMessage() returns the message from underlying 
0761:         * nestable (which also has to be a Throwable).
0762:         */
0763:        class ThrowableNestable extends Throwable implements  Nestable {
0764:            private Throwable cause = new Exception("ThrowableNestable cause");
0765:
0766:            /**
0767:             * @see Nestable#getThrowableCount()
0768:             * Returns 1 for this test.
0769:             */
0770:            public int getThrowableCount() {
0771:                return 1;
0772:            }
0773:
0774:            /**
0775:             * @see Nestable#getMessage()
0776:             * Returns the hard-coded string "ThrowableNestable exception" for this
0777:             * test.
0778:             */
0779:            public String getMessage() {
0780:                return "ThrowableNestable exception";
0781:            }
0782:
0783:            /**
0784:             * @see Nestable#getMessage(int)
0785:             * Returns the hard-coded string "ThrowableNestable exception" for this
0786:             * test.
0787:             */
0788:            public String getMessage(int index) {
0789:                return getMessage();
0790:            }
0791:
0792:            /**
0793:             * @see Nestable#getMessages()
0794:             * Returns single-element string array with "ThrowableNestable exception".
0795:             */
0796:            public String[] getMessages() {
0797:                String msgs[] = new String[1];
0798:                msgs[0] = getMessage();
0799:                return msgs;
0800:            }
0801:
0802:            /**
0803:             * @see Nestable#getCause()
0804:             */
0805:            public Throwable getCause() {
0806:                return cause;
0807:            }
0808:
0809:            /**
0810:             * @see Nestable#printStackTrace(PrintWriter)
0811:             * Empty method to satisfy the implemented interface. Does nothing
0812:             * in this test.
0813:             */
0814:            public void printStackTrace(PrintWriter out) {
0815:            }
0816:
0817:            /**
0818:             * @see Nestable#printPartialStackTrace(PrintWriter)
0819:             * Empty method to satisfy the implemented interface. Does nothing
0820:             * in this test.
0821:             */
0822:            public void printPartialStackTrace(PrintWriter out) {
0823:            }
0824:
0825:            /**
0826:             * @see Nestable#getThrowable(int)
0827:             */
0828:            public Throwable getThrowable(int index) {
0829:                return cause;
0830:            }
0831:
0832:            /**
0833:             * @see Nestable#getThrowables()
0834:             */
0835:            public Throwable[] getThrowables() {
0836:                Throwable throwables[] = new Throwable[1];
0837:                throwables[0] = cause;
0838:                return throwables;
0839:            }
0840:
0841:            /**
0842:             * @see Nestable#indexOfThrowable(Class)
0843:             */
0844:            public int indexOfThrowable(Class type) {
0845:                if (Exception.class.isInstance(type)) {
0846:                    return 0;
0847:                }
0848:                return -1;
0849:            }
0850:
0851:            /**
0852:             * @see Nestable#indexOfThrowable(Class,int)
0853:             */
0854:            public int indexOfThrowable(Class type, int fromIndex) {
0855:                return indexOfThrowable(type);
0856:            }
0857:
0858:        }
0859:
0860:        /**
0861:         * Nestable and Throwable class which takes in a 'cause' object.
0862:         * Returns a message wrapping the 'cause' message
0863:         * Prints a fixed stack trace and partial stack trace.
0864:         */
0865:        class ThrowableNestedNestable extends Throwable implements  Nestable {
0866:            private Throwable cause = null;
0867:
0868:            public ThrowableNestedNestable(Throwable cause) {
0869:                this .cause = cause;
0870:            }
0871:
0872:            /**
0873:             * @see Nestable#getThrowableCount()
0874:             * Returns 1 for this test.
0875:             */
0876:            public int getThrowableCount() {
0877:                return 1;
0878:            }
0879:
0880:            /**
0881:             * @see Nestable#getMessage()
0882:             * For this test, returns "ThrowableNestable exception (" appended to the
0883:             * message of the cause specified in the constructor.
0884:             */
0885:            public String getMessage() {
0886:                return "ThrowableNestedNestable exception ("
0887:                        + cause.getMessage() + ")";
0888:            }
0889:
0890:            /**
0891:             * @see Nestable#getMessage(int)
0892:             * For this test, returns "ThrowableNestable exception (" appended to the
0893:             * message of the cause specified in the constructor.
0894:             */
0895:            public String getMessage(int index) {
0896:                return "ThrowableNestedNestable exception ("
0897:                        + cause.getMessage() + ")";
0898:            }
0899:
0900:            /**
0901:             * @see Nestable#getMessages()
0902:             * For this test, returns a single-element string array containing
0903:             * "ThrowableNestable exception (" appended to the
0904:             * message of the cause specified in the constructor.
0905:             */
0906:            public String[] getMessages() {
0907:                String[] msgs = new String[1];
0908:                msgs[0] = "ThrowableNestedNestable exception ("
0909:                        + cause.getMessage() + ")";
0910:                return msgs;
0911:            }
0912:
0913:            /**
0914:             * @see Nestable#getCause()
0915:             */
0916:            public Throwable getCause() {
0917:                return cause;
0918:            }
0919:
0920:            /**
0921:             * @see Nestable#printStackTrace(PrintWriter)
0922:             * For this test, writes the string
0923:             * "ThrowableNestedNestable stack trace place-holder" to the print writer.
0924:             */
0925:            public void printStackTrace(PrintWriter out) {
0926:                out.println("ThrowableNestedNestable stack trace place-holder");
0927:            }
0928:
0929:            /**
0930:             * @see Nestable#printPartialStackTrace(PrintWriter)
0931:             * For this test, writes the string
0932:             * "ThrowableNestedNestable partial stack trace place-holder" to the print
0933:             * writer.
0934:             */
0935:            public void printPartialStackTrace(PrintWriter out) {
0936:                out
0937:                        .println("ThrowableNestedNestable partial stack trace place-holder");
0938:            }
0939:
0940:            /**
0941:             * @see Nestable#getThrowable(int)
0942:             */
0943:            public Throwable getThrowable(int index) {
0944:                return cause;
0945:            }
0946:
0947:            /**
0948:             * @see Nestable#getThrowableS()
0949:             */
0950:            public Throwable[] getThrowables() {
0951:                Throwable throwables[] = new Throwable[1];
0952:                throwables[0] = cause;
0953:                return throwables;
0954:            }
0955:
0956:            /**
0957:             * @see Nestable#indexOfThrowable(Class)
0958:             */
0959:            public int indexOfThrowable(Class type) {
0960:                if (Exception.class.isInstance(type)) {
0961:                    return 0;
0962:                }
0963:                return -1;
0964:            }
0965:
0966:            /**
0967:             * @see Nestable#indexOfThrowable(Class, int)
0968:             */
0969:            public int indexOfThrowable(Class type, int fromIndex) {
0970:                return indexOfThrowable(type);
0971:            }
0972:
0973:        }
0974:
0975:        /**
0976:         * Used to test that the constructor fails when passed a non-throwable cause
0977:         */
0978:        class NonThrowableNestable implements  Nestable {
0979:            /**
0980:             * @see Nestable#getThrowableCount()
0981:             * Returns 1 for this test.
0982:             */
0983:            public int getThrowableCount() {
0984:                return 1;
0985:            }
0986:
0987:            /**
0988:             * @see Nestable#getMessage()
0989:             * Returns the string "non-throwable" for this test.
0990:             */
0991:            public String getMessage() {
0992:                return "non-throwable";
0993:            }
0994:
0995:            /**
0996:             * @see Nestable#getMessage(int)
0997:             * Returns the string "non-throwable" for this test.
0998:             */
0999:            public String getMessage(int index) {
1000:                return "non-throwable";
1001:            }
1002:
1003:            /**
1004:             * @see Nestable#getMessage()
1005:             * Returns a single-element array containing the string "non-throwable" for
1006:             * this test.
1007:             */
1008:            public String[] getMessages() {
1009:                String[] msgs = new String[1];
1010:                msgs[0] = "non-throwable";
1011:                return msgs;
1012:            }
1013:
1014:            /**
1015:             * @see Nestable#getCause()
1016:             * Returns <code>null</code> for this test.
1017:             */
1018:            public Throwable getCause() {
1019:                return null;
1020:            }
1021:
1022:            /**
1023:             * @see Nestable#printStackTrace(PrintWriter)
1024:             * Empty method to satisfy the implemented interface. Does nothing
1025:             * in this test.
1026:             */
1027:            public void printStackTrace(PrintWriter out) {
1028:            }
1029:
1030:            /**
1031:             * @see Nestable#printStackTrace(PrintStream)
1032:             * Empty method to satisfy the implemented interface. Does nothing
1033:             * in this test.
1034:             */
1035:            public void printStackTrace(PrintStream out) {
1036:            }
1037:
1038:            /**
1039:             * @see Nestable#printPartialStackTrace(PrintWriter)
1040:             * Empty method to satisfy the implemented interface. Does nothing
1041:             * in this test.
1042:             */
1043:            public void printPartialStackTrace(PrintWriter out) {
1044:            }
1045:
1046:            /**
1047:             * @see Nestable#getThrowable(int)
1048:             * Returns <code>null</code> for this test.
1049:             */
1050:            public Throwable getThrowable(int index) {
1051:                return null;
1052:            }
1053:
1054:            /**
1055:             * @see Nestable#getThrowables()
1056:             * Returns zero-length <code>Throwable</code> array.
1057:             */
1058:            public Throwable[] getThrowables() {
1059:                return new Throwable[0];
1060:            }
1061:
1062:            /**
1063:             * @see Nestable#indexOfThrowable(Class)
1064:             * Returns -1 for this test.
1065:             */
1066:            public int indexOfThrowable(Class type) {
1067:                return -1;
1068:            }
1069:
1070:            /**
1071:             * @see Nestable#indexOfThrowable(Class, int)
1072:             * Returns -1 for this test.
1073:             */
1074:            public int indexOfThrowable(Class type, int fromIndex) {
1075:                return -1;
1076:            }
1077:
1078:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.