View Javadoc

1   package org.apache.maven.plugin.surefire.report;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.NumberFormat;
23  import java.util.Locale;
24  import org.apache.maven.surefire.report.ReportEntry;
25  import org.apache.maven.surefire.report.StackTraceWriter;
26  
27  /**
28   * @author Kristian Rosenvold
29   */
30  public class WrappedReportEntry
31      implements ReportEntry
32  {
33      private final ReportEntry original;
34  
35      private final ReportEntryType reportEntryType;
36  
37      private final Integer elapsed;
38  
39      private final String stdout;
40  
41      private final String stdErr;
42  
43      private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
44  
45      private static final int MS_PER_SEC = 1000;
46  
47      static final String NL = System.getProperty( "line.separator" );
48  
49      public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
50                                 String stdout, String stdErr )
51      {
52          this.original = original;
53          this.reportEntryType = reportEntryType;
54          this.elapsed = estimatedElapsed;
55          this.stdout = stdout;
56          this.stdErr = stdErr;
57      }
58  
59      public Integer getElapsed()
60      {
61          return elapsed;
62      }
63  
64      public ReportEntryType getReportEntryType()
65      {
66          return reportEntryType;
67      }
68  
69      public String getStdout()
70      {
71          return stdout;
72      }
73  
74      public String getStdErr()
75      {
76          return stdErr;
77      }
78  
79      public String getSourceName()
80      {
81          return original.getSourceName();
82      }
83  
84      public String getName()
85      {
86          return original.getName();
87      }
88  
89      public String getGroup()
90      {
91          return original.getGroup();
92      }
93  
94      public StackTraceWriter getStackTraceWriter()
95      {
96          return original.getStackTraceWriter();
97      }
98  
99      public String getMessage()
100     {
101         return original.getMessage();
102     }
103 
104     public String getStackTrace( boolean trimStackTrace )
105     {
106         StackTraceWriter writer = original.getStackTraceWriter();
107         if ( writer == null )
108         {
109             return null;
110         }
111         return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
112     }
113 
114     public String elapsedTimeAsString()
115     {
116         return elapsedTimeAsString( getElapsed() );
117     }
118 
119     String elapsedTimeAsString( long runTime )
120     {
121         return numberFormat.format( (double) runTime / MS_PER_SEC );
122     }
123 
124     public String getReportName()
125     {
126         final int i = getName().lastIndexOf( "(" );
127         return i > 0 ? getName().substring( 0, i ) : getName();
128     }
129 
130     public String getReportName( String suffix )
131     {
132         return suffix != null && suffix.length() > 0 ? getReportName() + "(" + suffix + ")" : getReportName();
133     }
134 
135     public String getOutput( boolean trimStackTrace )
136     {
137         StringBuilder buf = new StringBuilder();
138 
139         buf.append( getElapsedTimeSummary() );
140 
141         buf.append( "  <<< " ).append( getReportEntryType().toString().toUpperCase() ).append( "!" ).append( NL );
142 
143         buf.append( getStackTrace( trimStackTrace ) );
144 
145         return buf.toString();
146     }
147 
148     public String getElapsedTimeSummary()
149     {
150         StringBuilder reportContent = new StringBuilder();
151         reportContent.append( getName() );
152         reportContent.append( "  Time elapsed: " );
153         reportContent.append( elapsedTimeAsString() );
154         reportContent.append( " sec" );
155 
156         return reportContent.toString();
157     }
158 
159     public boolean isErrorOrFailure()
160     {
161         ReportEntryType thisType = getReportEntryType();
162         return ReportEntryType.failure == thisType || ReportEntryType.error == thisType;
163     }
164 
165     public boolean isSkipped()
166     {
167         return ReportEntryType.skipped == getReportEntryType();
168     }
169 
170     public boolean isSucceeded()
171     {
172         return ReportEntryType.success == getReportEntryType();
173     }
174 }