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