View Javadoc

1   // Copyright (C) 2002 - 2011 Philip Aston
2   // All rights reserved.
3   //
4   // This file is part of The Grinder software distribution. Refer to
5   // the file LICENSE which is part of The Grinder distribution for
6   // licensing details. The Grinder distribution is available on the
7   // Internet at http://grinder.sourceforge.net/
8   //
9   // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10  // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11  // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
12  // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
13  // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
14  // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
15  // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
16  // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
17  // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
18  // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
19  // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
20  // OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Unit test for {@link GrinderException}.
39   *
40   * @author Philip Aston
41   */
42  public class TestGrinderExceptions {
43  
44    // Calculate the callers method name. This is so the tests still work when
45    // this class is instrumented by Clover.
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 }