View Javadoc

1   package org.apache.maven.plugins.junitreport;
2   
3   /* ====================================================================
4    *   Copyright 2006 The Apache Software Foundation.
5    *
6    *   Licensed under the Apache License, Version 2.0 (the "License");
7    *   you may not use this file except in compliance with the License.
8    *   You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing, software
13   *   distributed under the License is distributed on an "AS IS" BASIS,
14   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *   See the License for the specific language governing permissions and
16   *   limitations under the License.
17   * ====================================================================
18   */
19  
20  /*
21   * Created on Jun 5, 2005
22   */
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  /**
28   * <p>
29   * Util class for exceptions
30   * </p>
31   * <p>
32   * Inspired from {@link org.apache.maven.util.StringTool}
33   * </p>
34   *
35   * @author <a href="mailto:cleclerc@pobox.com">Cyrille Le Clerc </a>
36   * @version $Id: ExceptionTool.java 393632 2006-04-12 22:28:03Z ltheussl $
37   */
38  public class ExceptionTool
39  {
40      /**
41       * <p>
42       * Splits the provided text into an array, separator String specified.
43       * </p>
44       * <p>
45       * This method is different of {@link java.util.StringTokenizer} that only
46       * accept chars as separator but not strings
47       * </p>
48       *
49       * @param text
50       *            the string to split
51       * @param separator
52       *            the spearator
53       * @return An array containing the split string
54       */
55      public String[] split( String text, String separator )
56      {
57          if ( text == null )
58          {
59              return null;
60          }
61  
62          List result = new ArrayList();
63          int fromIndex = 0;
64          int idx;
65  
66          while ( ( idx = text.indexOf( separator, fromIndex ) ) != -1 )
67          {
68              String subString = text.substring( fromIndex, idx );
69  
70              result.add( subString );
71              fromIndex = idx + separator.length();
72          }
73  
74          if ( fromIndex != text.length() )
75          {
76              // add last element
77              String subString = text.substring( fromIndex );
78  
79              result.add( subString );
80          }
81  
82          return (String[]) result.toArray( new String[result.size()] );
83      }
84  
85      /**
86       * <p>
87       * Splits the given <code>stacktrace</code> into an array of simple (not
88       * nested) stack traces. The nesting stack trace is included in the result
89       * </p>
90       *
91       * @param nestedStackTrace
92       *            the nested stack trace to split
93       * @return An array of un-nested stacktraces
94       */
95      public String[] splitNestedStackTraces( String nestedStackTrace )
96      {
97          String separator = "Caused by: ";
98          String[] result = split( nestedStackTrace, separator );
99  
100         // prefix nested stack traces by "Caused by: "
101         for ( int i = 1; i < result.length; i++ )
102         {
103             result[i] = separator + result[i];
104         }
105 
106         return result;
107     }
108 
109     /**
110      * <p>
111      * Truncates the given stacktrace just before the first occurance of the
112      * given <code>searchedString</code>
113      * </p>
114      *
115      * @param stackTrace
116      *            the stacktrace to truncate
117      * @param searchedString
118      *            e.g. "at org.apache.commons.jelly.tags.ant.AntTag.doTag"
119      * @return The truncated stacktrace
120      */
121     public String truncateStackTrace( String stackTrace, String searchedString )
122     {
123         StringBuffer result = new StringBuffer();
124         int fromIndex = 0;
125         int idxSearchedString = stackTrace.indexOf( searchedString, fromIndex );
126         String suffix;
127 
128         if ( idxSearchedString == -1 )
129         {
130             idxSearchedString = stackTrace.length();
131             suffix = "";
132         }
133         else
134         {
135             suffix = "... more";
136         }
137 
138         String currentStack =
139             stackTrace.substring( fromIndex, idxSearchedString );
140 
141         result.append( currentStack + suffix );
142 
143         return result.toString();
144     }
145 
146     /**
147      * <p>
148      * Splits the given <code>stackTrace</code> into nested stack traces and
149      * truncates each of the nested stacktrace (including the nesting
150      * stacktrace) then re-concatenates the string to return it
151      * </p>
152      *
153      * @param netstedStackTrace
154      *            the nested stacktrace to truncate
155      * @param searchedString
156      *            e.g. "at org.apache.commons.jelly.tags.ant.AntTag.doTag"
157      * @return The truncated stacktrace
158      */
159     public String truncateNestedStackTrace( String netstedStackTrace,
160         String searchedString )
161     {
162         String[] stackTraces = splitNestedStackTraces( netstedStackTrace );
163         StringBuffer result = new StringBuffer();
164 
165         for ( int i = 0; i < stackTraces.length; i++ )
166         {
167             String nestedStackTrace = stackTraces[i];
168             String truncatedNestedStackTrace =
169                 truncateStackTrace( nestedStackTrace, searchedString );
170 
171             result.append( truncatedNestedStackTrace + "\r\n" );
172         }
173 
174         return result.toString();
175     }
176 }