KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > TextualTrace


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 /**
12  *
13  */

14 package org.eclipse.jdt.internal.junit.ui;
15
16 import java.io.BufferedReader JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.io.PrintWriter JavaDoc;
19 import java.io.StringReader JavaDoc;
20 import java.io.StringWriter JavaDoc;
21
22 public class TextualTrace {
23     public static final int LINE_TYPE_EXCEPTION = 1;
24
25     public static final int LINE_TYPE_NORMAL = 0;
26
27     public static final int LINE_TYPE_STACKFRAME = 2;
28
29     private final String JavaDoc fTrace;
30
31     public TextualTrace(String JavaDoc trace, String JavaDoc[] filterPatterns) {
32         super();
33         fTrace = filterStack(trace, filterPatterns);
34     }
35
36     public void display(ITraceDisplay display, int maxLabelLength) {
37         StringReader JavaDoc stringReader = new StringReader JavaDoc(fTrace);
38         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(stringReader);
39         String JavaDoc line;
40
41         try {
42             // first line contains the thrown exception
43
line = readLine(bufferedReader);
44             if (line == null)
45                 return;
46
47             displayWrappedLine(display, maxLabelLength, line,
48                     LINE_TYPE_EXCEPTION);
49
50             // the stack frames of the trace
51
while ((line = readLine(bufferedReader)) != null) {
52                 int type = isAStackFrame(line) ? LINE_TYPE_STACKFRAME
53                         : LINE_TYPE_NORMAL;
54                 displayWrappedLine(display, maxLabelLength, line, type);
55             }
56         } catch (IOException JavaDoc e) {
57             display.addTraceLine(LINE_TYPE_NORMAL, fTrace);
58         }
59     }
60
61     private void displayWrappedLine(ITraceDisplay display, int maxLabelLength,
62             String JavaDoc line, int type) {
63         final int labelLength = line.length();
64         if (labelLength < maxLabelLength) {
65             display.addTraceLine(type, line);
66         } else {
67             // workaround for bug 74647: JUnit view truncates
68
// failure message
69
display.addTraceLine(type, line.substring(0, maxLabelLength));
70             int offset = maxLabelLength;
71             while (offset < labelLength) {
72                 int nextOffset = Math.min(labelLength, offset + maxLabelLength);
73                 display.addTraceLine(LINE_TYPE_NORMAL, line.substring(offset,
74                         nextOffset));
75                 offset = nextOffset;
76             }
77         }
78     }
79
80     private boolean filterLine(String JavaDoc[] patterns, String JavaDoc line) {
81         String JavaDoc pattern;
82         int len;
83         for (int i = (patterns.length - 1); i >= 0; --i) {
84             pattern = patterns[i];
85             len = pattern.length() - 1;
86             if (pattern.charAt(len) == '*') {
87                 // strip trailing * from a package filter
88
pattern = pattern.substring(0, len);
89             } else if (Character.isUpperCase(pattern.charAt(0))) {
90                 // class in the default package
91
pattern = FailureTrace.FRAME_PREFIX + pattern + '.';
92             } else {
93                 // class names start w/ an uppercase letter after the .
94
final int lastDotIndex = pattern.lastIndexOf('.');
95                 if ((lastDotIndex != -1)
96                     && (lastDotIndex != len)
97                     && Character.isUpperCase(pattern.charAt(lastDotIndex + 1)))
98                     pattern += '.'; // append . to a class filter
99
}
100
101             if (line.indexOf(pattern) > 0)
102                 return true;
103         }
104         return false;
105     }
106
107     private String JavaDoc filterStack(String JavaDoc stackTrace, String JavaDoc[] filterPatterns) {
108         if (filterPatterns.length == 0 || stackTrace == null)
109             return stackTrace;
110
111         StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
112         PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(stringWriter);
113         StringReader JavaDoc stringReader = new StringReader JavaDoc(stackTrace);
114         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(stringReader);
115
116         String JavaDoc line;
117         String JavaDoc[] patterns = filterPatterns;
118         try {
119             while ((line = bufferedReader.readLine()) != null) {
120                 if (!filterLine(patterns, line))
121                     printWriter.println(line);
122             }
123         } catch (IOException JavaDoc e) {
124             return stackTrace; // return the stack unfiltered
125
}
126         return stringWriter.toString();
127     }
128
129     private boolean isAStackFrame(String JavaDoc itemLabel) {
130         // heuristic for detecting a stack frame - works for JDK
131
return itemLabel.indexOf(" at ") >= 0; //$NON-NLS-1$
132
}
133
134     private String JavaDoc readLine(BufferedReader JavaDoc bufferedReader) throws IOException JavaDoc {
135         String JavaDoc readLine = bufferedReader.readLine();
136         return readLine == null ? null : readLine.replace('\t', ' ');
137     }
138 }
139
Popular Tags