create changelog entry
[debian/openrocket] / core / test / net / sf / openrocket / logging / TraceExceptionTest.java
1 package net.sf.openrocket.logging;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Test;
6
7 public class TraceExceptionTest {
8         
9         private TraceException getViaAccess() {
10                 
11                 /*
12                  * The SubClass test bases on the fact that getViaAccess method is defined on a row number < 20
13                  * and the return statement is on a row number > 20.
14                  * 
15                  * The JRE sometimes adds an additional "access$NNN" method call between the calls with the
16                  * row number equal to the method definition line.
17                  * 
18                  * 
19                  * 
20                  * 
21                  * 
22                  * 
23                  * 
24                  */
25
26                 return new TraceException(0, 1);
27         }
28         
29         
30         @Test
31         public void testBasic() {
32                 TraceException trace = new TraceException();
33                 assertMatch("\\(TraceExceptionTest.java:[2-9][0-9]\\)", trace);
34         }
35         
36         
37         @Test
38         public void testOneLevelUp() {
39                 // @formatter:off - these need to be on the same line number
40                 TraceException trace = getOneLevelUp(); TraceException ref = new TraceException();
41                 // @formatter:on
42                 assertEquals(ref.getMessage(), trace.getMessage());
43         }
44         
45         private TraceException getOneLevelUp() {
46                 return new TraceException(1);
47         }
48         
49         
50         @Test
51         public void testTwoLevels() {
52                 TraceException trace = getTwoLevels();
53                 assertMatch("\\(TraceExceptionTest.java:[2-9][0-9] TraceExceptionTest.java:[2-9][0-9]\\)", trace);
54         }
55         
56         private TraceException getTwoLevels() {
57                 return new TraceException(0, 1);
58         }
59         
60         
61         @Test
62         public void testViaSubclass() {
63                 /*
64                  * This tests that TraceException.getMessage ignores the synthetic "access$0" method calls.
65                  */
66
67                 TraceException trace = new SubClass().getTrace();
68                 assertMatch("\\(TraceExceptionTest.java:[2-9][0-9] TraceExceptionTest.java:[2-9][0-9]\\)", trace);
69         }
70         
71         private class SubClass {
72                 private TraceException getTrace() {
73                         return getViaAccess();
74                 }
75         }
76         
77         
78
79
80
81         private void assertMatch(String regex, TraceException trace) {
82                 boolean match = trace.getMessage().matches(regex);
83                 if (!match) {
84                         trace.printStackTrace();
85                         assertTrue("Was: " + trace.getMessage(), match);
86                 }
87         }
88         
89 }