1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.common;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.fail;
26
27 import java.io.ByteArrayOutputStream;
28 import java.io.PrintStream;
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31
32 import net.grinder.testutility.RedirectStandardStreams;
33
34 import org.junit.Test;
35
36
37
38
39
40
41
42 public class TestGrinderExceptions {
43
44
45
46 private static final String getMethodName() {
47 final StackTraceElement callersFrame = new Exception().getStackTrace()[1];
48 return callersFrame.toString().replaceAll("\\(.*", "");
49 }
50
51 @Test public void testPrintStackTrace() throws Exception {
52 final StringWriter stringWriter = new StringWriter();
53 final PrintWriter printWriter = new PrintWriter(stringWriter);
54
55 final GrinderException e1 = createDeeperException();
56 final GrinderException e2 = new MyGrinderException("Exception 2", e1);
57
58 e2.printStackTrace(printWriter);
59 final String s = stringWriter.toString();
60
61 assertEquals(1, countOccurrences("createException", s));
62 assertEquals(1, countOccurrences("createDeeperException", s));
63 assertEquals(2, countOccurrences(getMethodName(), s));
64 }
65
66 @Test public void testPrintStackTraceWithNestedNonGrinderException()
67 throws Exception {
68
69 final StringWriter stringWriter = new StringWriter();
70 final PrintWriter printWriter = new PrintWriter(stringWriter);
71
72 final Exception e1 = new RuntimeException();
73 final GrinderException e2 = new MyGrinderException("Exception 2", e1);
74
75 e2.printStackTrace(printWriter);
76 final String s = stringWriter.toString();
77
78 assertEquals(1, countOccurrences("RuntimeException", s));
79 assertEquals(2, countOccurrences(getMethodName(), s));
80 }
81
82 private static class WeirdException extends RuntimeException {
83 public void printStackTrace(PrintWriter w) {
84 w.println("Unconventional stack trace");
85 w.flush();
86 }
87 }
88
89 @Test public void testPrintStackTraceWithNestedUnconventionalException()
90 throws Exception {
91
92 final StringWriter stringWriter = new StringWriter();
93 final PrintWriter printWriter = new PrintWriter(stringWriter);
94
95 final Exception e1 = new WeirdException();
96 final GrinderException e2 = new MyGrinderException("Exception 2", e1);
97
98 e2.printStackTrace(printWriter);
99 final String s = stringWriter.toString();
100
101 assertEquals(2, countOccurrences(getMethodName(), s));
102 assertEquals(1, countOccurrences("...", s));
103 }
104
105 @Test public void testPrintStackTraceWithPrintStream() throws Exception {
106 final ByteArrayOutputStream byteArrayOutputStream =
107 new ByteArrayOutputStream();
108 final PrintStream printStream = new PrintStream(byteArrayOutputStream);
109
110 final GrinderException e1 = createDeeperException();
111 final GrinderException e2 = new MyGrinderException("Exception 2", e1);
112
113 e2.printStackTrace(printStream);
114 final String s = new String(byteArrayOutputStream.toByteArray());
115
116 assertEquals(1, countOccurrences("createException", s));
117 assertEquals(1, countOccurrences("createDeeperException", s));
118 assertEquals(2, countOccurrences(getMethodName(), s));
119 }
120
121 @Test public void testPrintStackTraceWithDefaultStream() throws Exception {
122
123 final GrinderException e1 = createDeeperException();
124 final GrinderException e2 = new MyGrinderException("Exception 2", e1);
125
126 final RedirectStandardStreams streams = new RedirectStandardStreams() {
127 protected void runWithRedirectedStreams() throws Exception {
128 e2.printStackTrace();
129 }
130 };
131
132 streams.run();
133
134 final String s = new String(streams.getStderrBytes());
135
136 assertEquals(1, countOccurrences("createException", s));
137 assertEquals(1, countOccurrences("createDeeperException", s));
138 assertEquals(2, countOccurrences(getMethodName(), s));
139 }
140
141 private GrinderException createException() {
142 return new MyGrinderException("an exception");
143 }
144
145 private GrinderException createDeeperException() {
146 return createException();
147 }
148
149 private int countOccurrences(String pattern, String original) {
150 int result = 0;
151 int p = -1;
152
153 while ((p=original.indexOf(pattern, p + 1)) >= 0) {
154 ++result;
155 }
156
157 return result;
158 }
159
160 private static final class MyGrinderException extends GrinderException {
161
162 public MyGrinderException(String message) {
163 super(message);
164 }
165
166 public MyGrinderException(String string, Throwable e1) {
167 super(string, e1);
168 }
169 }
170
171 @Test public void testUncheckedGrinderException() throws Exception {
172 final RuntimeException e1 = new UncheckedGrinderException("test") {};
173 assertEquals("test", e1.getMessage());
174
175 final UncheckedGrinderException e2 =
176 new UncheckedGrinderException("test2", e1) {};
177 assertEquals("test2", e2.getMessage());
178 assertEquals(e1, e2.getCause());
179
180 try {
181 UncheckedGrinderException.class.newInstance();
182 fail("UncheckedGrinderException is not abstract");
183 }
184 catch (InstantiationException e) {
185 }
186 }
187 }