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.io.File;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.maven.plugin.surefire.StartupReportConfiguration;
29  import org.apache.maven.plugin.surefire.log.api.ConsoleLogger;
30  import org.apache.maven.shared.utils.logging.MessageUtils;
31  import org.apache.maven.surefire.report.RunStatistics;
32  import org.apache.maven.surefire.report.SafeThrowable;
33  import org.apache.maven.surefire.report.StackTraceWriter;
34  
35  import static java.util.Arrays.asList;
36  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.error;
37  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.failure;
38  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.flake;
39  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.skipped;
40  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.success;
41  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.TestResultType.unknown;
42  import static org.apache.maven.plugin.surefire.report.DefaultReporterFactory.getTestResultType;
43  import static org.mockito.Mockito.mock;
44  import static org.mockito.Mockito.when;
45  
46  public class DefaultReporterFactoryTest
47      extends TestCase
48  {
49      private final static String TEST_ONE = "testOne";
50  
51      private final static String TEST_TWO = "testTwo";
52  
53      private final static String TEST_THREE = "testThree";
54  
55      private final static String TEST_FOUR = "testFour";
56  
57      private final static String TEST_FIVE = "testFive";
58  
59      private final static String ASSERTION_FAIL = "assertionFail";
60  
61      private final static String ERROR = "error";
62  
63      public void testMergeTestHistoryResult()
64      {
65          MessageUtils.setColorEnabled( false );
66          File reportsDirectory = new File("target");
67          StartupReportConfiguration reportConfig =
68                  new StartupReportConfiguration( true, true, "PLAIN", false, false, reportsDirectory, false, null,
69                                                        new File( reportsDirectory, "TESTHASH" ), false, 1, null, null );
70  
71          DummyTestReporter reporter = new DummyTestReporter();
72  
73          DefaultReporterFactory factory = new DefaultReporterFactory( reportConfig, reporter );
74  
75          // First run, four tests failed and one passed
76          List<TestMethodStats> firstRunStats = new ArrayList<TestMethodStats>();
77          firstRunStats.add( new TestMethodStats( TEST_ONE, ReportEntryType.ERROR, new DummyStackTraceWriter( ERROR ) ) );
78          firstRunStats.add( new TestMethodStats( TEST_TWO, ReportEntryType.ERROR, new DummyStackTraceWriter( ERROR ) ) );
79          firstRunStats.add(
80              new TestMethodStats( TEST_THREE, ReportEntryType.FAILURE, new DummyStackTraceWriter( ASSERTION_FAIL ) ) );
81          firstRunStats.add(
82              new TestMethodStats( TEST_FOUR, ReportEntryType.FAILURE, new DummyStackTraceWriter( ASSERTION_FAIL ) ) );
83          firstRunStats.add(
84              new TestMethodStats( TEST_FIVE, ReportEntryType.SUCCESS, null ) );
85  
86          // Second run, two tests passed
87          List<TestMethodStats> secondRunStats = new ArrayList<TestMethodStats>();
88          secondRunStats.add(
89              new TestMethodStats( TEST_ONE, ReportEntryType.FAILURE, new DummyStackTraceWriter( ASSERTION_FAIL ) ) );
90          secondRunStats.add( new TestMethodStats( TEST_TWO, ReportEntryType.SUCCESS, null ) );
91          secondRunStats.add(
92              new TestMethodStats( TEST_THREE, ReportEntryType.ERROR, new DummyStackTraceWriter( ERROR ) ) );
93          secondRunStats.add( new TestMethodStats( TEST_FOUR, ReportEntryType.SUCCESS, null ) );
94  
95          // Third run, another test passed
96          List<TestMethodStats> thirdRunStats = new ArrayList<TestMethodStats>();
97          thirdRunStats.add( new TestMethodStats( TEST_ONE, ReportEntryType.SUCCESS, null ) );
98          thirdRunStats.add(
99              new TestMethodStats( TEST_THREE, ReportEntryType.ERROR, new DummyStackTraceWriter( ERROR ) ) );
100 
101         TestSetRunListener firstRunListener = mock( TestSetRunListener.class );
102         TestSetRunListener secondRunListener = mock( TestSetRunListener.class );
103         TestSetRunListener thirdRunListener = mock( TestSetRunListener.class );
104         when( firstRunListener.getTestMethodStats() ).thenReturn( firstRunStats );
105         when( secondRunListener.getTestMethodStats() ).thenReturn( secondRunStats );
106         when( thirdRunListener.getTestMethodStats() ).thenReturn( thirdRunStats );
107 
108         factory.addListener( firstRunListener );
109         factory.addListener( secondRunListener );
110         factory.addListener( thirdRunListener );
111 
112         factory.mergeTestHistoryResult();
113         RunStatistics mergedStatistics = factory.getGlobalRunStatistics();
114 
115         // Only TEST_THREE is a failing test, other three are flaky tests
116         assertEquals( 5, mergedStatistics.getCompletedCount() );
117         assertEquals( 1, mergedStatistics.getErrors() );
118         assertEquals( 0, mergedStatistics.getFailures() );
119         assertEquals( 3, mergedStatistics.getFlakes() );
120         assertEquals( 0, mergedStatistics.getSkipped() );
121 
122         // Now test the result will be printed out correctly
123         factory.printTestFailures( flake );
124         String[] expectedFlakeOutput =
125             { "Flakes: ", TEST_FOUR, "  Run 1: " + ASSERTION_FAIL, "  Run 2: PASS", "", TEST_ONE,
126                 "  Run 1: " + ERROR, "  Run 2: " + ASSERTION_FAIL, "  Run 3: PASS", "", TEST_TWO, "  Run 1: " + ERROR,
127                 "  Run 2: PASS", "" };
128         assertEquals( asList( expectedFlakeOutput ), reporter.getMessages() );
129 
130         reporter.reset();
131         factory.printTestFailures( error );
132         String[] expectedFailureOutput =
133             { "Errors: ", TEST_THREE, "  Run 1: " + ASSERTION_FAIL, "  Run 2: " + ERROR, "  Run 3: " + ERROR, ""
134             };
135         assertEquals( asList( expectedFailureOutput ), reporter.getMessages() );
136 
137         reporter.reset();
138         factory.printTestFailures( failure );
139         String[] expectedErrorOutput = { };
140         assertEquals( asList( expectedErrorOutput ), reporter.getMessages() );
141     }
142 
143     static final class DummyTestReporter implements ConsoleLogger
144     {
145         private final List<String> messages = new ArrayList<String>();
146 
147         @Override
148         public boolean isDebugEnabled()
149         {
150             return true;
151         }
152 
153         @Override
154         public void debug( String message )
155         {
156             messages.add( message );
157         }
158 
159         @Override
160         public boolean isInfoEnabled()
161         {
162             return true;
163         }
164 
165         @Override
166         public void info( String message )
167         {
168             messages.add( message );
169         }
170 
171         @Override
172         public boolean isWarnEnabled()
173         {
174             return true;
175         }
176 
177         @Override
178         public void warning( String message )
179         {
180             messages.add( message );
181         }
182 
183         @Override
184         public boolean isErrorEnabled()
185         {
186             return true;
187         }
188 
189         @Override
190         public void error( String message )
191         {
192             messages.add( message );
193         }
194 
195         @Override
196         public void error( String message, Throwable t )
197         {
198             messages.add( message );
199         }
200 
201         @Override
202         public void error( Throwable t )
203         {
204         }
205 
206         List<String> getMessages()
207         {
208             return messages;
209         }
210 
211         void reset()
212         {
213             messages.clear();
214         }
215     }
216 
217     public void testGetTestResultType()
218     {
219         List<ReportEntryType> emptyList = new ArrayList<ReportEntryType>();
220         assertEquals( unknown, getTestResultType( emptyList, 1 ) );
221 
222         List<ReportEntryType> successList = new ArrayList<ReportEntryType>();
223         successList.add( ReportEntryType.SUCCESS );
224         successList.add( ReportEntryType.SUCCESS );
225         assertEquals( success, getTestResultType( successList, 1 ) );
226 
227         List<ReportEntryType> failureErrorList = new ArrayList<ReportEntryType>();
228         failureErrorList.add( ReportEntryType.FAILURE );
229         failureErrorList.add( ReportEntryType.ERROR );
230         assertEquals( error, getTestResultType( failureErrorList, 1 ) );
231 
232         List<ReportEntryType> errorFailureList = new ArrayList<ReportEntryType>();
233         errorFailureList.add( ReportEntryType.ERROR );
234         errorFailureList.add( ReportEntryType.FAILURE );
235         assertEquals( error, getTestResultType( errorFailureList, 1 ) );
236 
237         List<ReportEntryType> flakeList = new ArrayList<ReportEntryType>();
238         flakeList.add( ReportEntryType.SUCCESS );
239         flakeList.add( ReportEntryType.FAILURE );
240         assertEquals( flake, getTestResultType( flakeList, 1 ) );
241 
242         assertEquals( failure, getTestResultType( flakeList, 0 ) );
243 
244         flakeList = new ArrayList<ReportEntryType>();
245         flakeList.add( ReportEntryType.ERROR );
246         flakeList.add( ReportEntryType.SUCCESS );
247         flakeList.add( ReportEntryType.FAILURE );
248         assertEquals( flake, getTestResultType( flakeList, 1 ) );
249 
250         assertEquals( error, getTestResultType( flakeList, 0 ) );
251 
252         List<ReportEntryType> skippedList = new ArrayList<ReportEntryType>();
253         skippedList.add( ReportEntryType.SKIPPED );
254         assertEquals( skipped, getTestResultType( skippedList, 1 ) );
255     }
256 
257     static class DummyStackTraceWriter
258         implements StackTraceWriter
259     {
260 
261         private final String stackTrace;
262 
263         public DummyStackTraceWriter( String stackTrace )
264         {
265             this.stackTrace = stackTrace;
266         }
267 
268         @Override
269         public String writeTraceToString()
270         {
271             return "";
272         }
273 
274         @Override
275         public String writeTrimmedTraceToString()
276         {
277             return "";
278         }
279 
280         @Override
281         public String smartTrimmedStackTrace()
282         {
283             return stackTrace;
284         }
285 
286         @Override
287         public SafeThrowable getThrowable()
288         {
289             return null;
290         }
291     }
292 }