1 //
2 // Copyright (c) 2003,2004 , Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 // * Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J.
14 // nor the names of its contributors may be used to endorse or promote products
15 // derived from this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
21 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 // OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 // POSSIBILITY OF SUCH DAMAGE.
27 //
28 package org.objectledge.utils;
29
30
31 /***
32 *
33 *
34 * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a>
35 * @version $Id: StackTraceTest.java,v 1.3 2004/07/22 16:39:42 zwierzem Exp $
36 */
37 public class StackTraceTest extends LedgeTestCase
38 {
39 private Thingy thingy = new Thingy();
40
41 // enable trace output during the test
42 private boolean printTraces = false;
43
44 public void testTracingException()
45 {
46 String[] trace = new StackTrace(new TracingException(4)).toStringArray();
47 assertEquals(5, trace.length);
48 }
49
50 public void testOrdinaryTrace()
51 {
52 try
53 {
54 thingy.d();
55 }
56 catch(Exception e)
57 {
58 String[] trace = new StackTrace(e).toStringArray();
59 assertEquals(3, trace.length);
60 }
61 }
62
63 public void testNestingTrace()
64 {
65 try
66 {
67 thingy.a();
68 }
69 catch(Exception e)
70 {
71 String[] trace = new StackTrace(e).toStringArray();
72 assertEquals(10, trace.length);
73 }
74 }
75
76 public void testLegacyExceptions()
77 {
78 try
79 {
80 thingy.l();
81 }
82 catch(Exception e)
83 {
84 String[] trace = new StackTrace(e).toStringArray();
85 assertEquals(6, trace.length);
86 }
87 }
88
89 /////////////////////////////////////////////////////////////////////////////////////////////
90
91 public static class Thingy
92 {
93 public void a()
94 throws Exception
95 {
96 try
97 {
98 b();
99 }
100 catch(Exception e)
101 {
102 throw new Exception("exception in b", e);
103 }
104 }
105
106 public void b()
107 throws Exception
108 {
109 try
110 {
111 c();
112 }
113 catch(Exception e)
114 {
115 throw new Exception("exception in c", e);
116 }
117 }
118
119 public void c()
120 throws Exception
121 {
122 d();
123 }
124
125 public void d()
126 throws Exception
127 {
128 throw new Exception("d failed");
129 }
130
131 /////////////////////////////////////////////////////////////////////////////////////////
132
133 public void l()
134 throws Exception
135 {
136 try
137 {
138 m();
139 }
140 catch(Exception e)
141 {
142 throw new LegacyException("exception in m", e);
143 }
144 }
145
146 public void m()
147 throws Exception
148 {
149 throw new Exception("m failed");
150 }
151 }
152
153 public static class LegacyException
154 extends Exception
155 {
156 private Throwable cause;
157
158 public LegacyException(String message, Throwable cause)
159 {
160 super(message);
161 this.cause = cause;
162 }
163
164 public Throwable getRootCause()
165 {
166 return cause;
167 }
168 }
169 }