View Javadoc

1   package org.apache.maven.surefire.report;
2   
3   import java.util.Collection;
4   
5   import junit.framework.TestCase;
6   
7   /*
8    * Copyright 2002-2009 the original author or authors.
9    *
10   * Licensed under the Apache License, Version 2.0 (the "License");
11   * you may not use this file except in compliance with the License.
12   * You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   * Unless required by applicable law or agreed to in writing, software
17   * distributed under the License is distributed on an "AS IS" BASIS,
18   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   * See the License for the specific language governing permissions and
20   * limitations under the License.
21   *
22   */
23  
24  public class RunStatisticsTest
25      extends TestCase
26  {
27      private static final String Method = "AClass#AMethod";
28  
29      private static final String DUMMY_ERROR_SOURCE = Method + " RuntimeException";
30  
31      private static final String DUMMY_FAILURE_SOURCE = "dummy failure source";
32  
33      private static final String DUMMY_MESSAGE = "foo";
34  
35      public void testAddErrorSourceWithThrowableMessage()
36      {
37          RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
38          RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( throwable );
39          assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE + " " + DUMMY_MESSAGE );
40      }
41  
42      public void testAddErrorSourceWithoutThrowable()
43      {
44          RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( null );
45          assertRunStatisticsHasErrorSource( statistics, Method );
46      }
47  
48      public void testAddErrorSourceWithThrowableWithoutMessage()
49      {
50          RuntimeException throwable = new RuntimeException();
51          RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( throwable );
52          assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
53      }
54  
55      public void testAddFailureSourceWithThrowableMessage()
56      {
57          RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
58          RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
59          assertRunStatisticsHasFailureSource( statistics, DUMMY_ERROR_SOURCE + " " + DUMMY_MESSAGE );
60      }
61  
62      public void testAddFailureSourceWithoutThrowable()
63      {
64          RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( null );
65          assertRunStatisticsHasFailureSource( statistics, Method );
66      }
67  
68      public void testAddFailureSourceWithThrowableWithoutMessage()
69      {
70          RuntimeException throwable = new RuntimeException();
71          RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
72          assertRunStatisticsHasFailureSource( statistics, DUMMY_ERROR_SOURCE );
73      }
74  
75      private RunStatistics createRunStatisticsAndAddErrorSourceWithThrowable( Throwable throwable )
76      {
77          StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "AClass", "AMethod", throwable );
78          RunStatistics statistics = new RunStatistics();
79          statistics.addErrorSource( stackTraceWriter );
80  
81          return statistics;
82      }
83  
84      private RunStatistics createRunStatisticsAndAddFailureSourceWithThrowable( Throwable throwable )
85      {
86          StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "AClass", "AMethod", throwable );
87          RunStatistics statistics = new RunStatistics();
88          statistics.addFailureSource( stackTraceWriter );
89  
90          return statistics;
91      }
92  
93      private void assertRunStatisticsHasErrorSource( RunStatistics statistics, String expectedErrorSource )
94      {
95          Collection errorSources = statistics.getErrorSources();
96          assertNotNull( "No error sources.", errorSources );
97          assertEquals( "Wrong number of error sources.", 1, errorSources.size() );
98          assertEquals( "Wrong error sources.", expectedErrorSource, errorSources.iterator().next() );
99      }
100 
101     private void assertRunStatisticsHasFailureSource( RunStatistics statistics, String expectedFailureSource )
102     {
103         Collection failureSources = statistics.getFailureSources();
104         assertNotNull( "No failure sources.", failureSources );
105         assertEquals( "Wrong number of failure sources.", 1, failureSources.size() );
106         assertEquals( "Wrong failure sources.", expectedFailureSource, failureSources.iterator().next() );
107     }
108 }