1 package org.apache.maven.surefire.report;
2
3 import java.util.Collection;
4 import junit.framework.TestCase;
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class RunStatisticsTest
24 extends TestCase
25 {
26 private static final String DUMMY_ERROR_SOURCE = "dummy error source";
27
28 private static final String DUMMY_FAILURE_SOURCE = "dummy failure source";
29
30 private static final String DUMMY_MESSAGE = "dummy message";
31
32 public void testAddErrorSourceWithThrowableMessage()
33 {
34 RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
35 RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( throwable );
36 assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE + ": " + DUMMY_MESSAGE );
37 }
38
39 public void testAddErrorSourceWithoutStackTraceWriter()
40 {
41 RunStatistics statistics = new RunStatistics();
42 statistics.addErrorSource( DUMMY_ERROR_SOURCE, null );
43 assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
44 }
45
46 public void testAddErrorSourceWithoutThrowable()
47 {
48 RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( null );
49 assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
50 }
51
52 public void testAddErrorSourceWithThrowableWithoutMessage()
53 {
54 RuntimeException throwable = new RuntimeException();
55 RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( throwable );
56 assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
57 }
58
59 public void testAddFailureSourceWithThrowableMessage()
60 {
61 RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
62 RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
63 assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE + ": " + DUMMY_MESSAGE );
64 }
65
66 public void testAddFailureSourceWithoutStackTraceWriter()
67 {
68 RunStatistics statistics = new RunStatistics();
69 statistics.addFailureSource( DUMMY_FAILURE_SOURCE, null );
70 assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
71 }
72
73 public void testAddFailureSourceWithoutThrowable()
74 {
75 RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( null );
76 assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
77 }
78
79 public void testAddFailureSourceWithThrowableWithoutMessage()
80 {
81 RuntimeException throwable = new RuntimeException();
82 RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
83 assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
84 }
85
86 private RunStatistics createRunStatisticsAndAddErrorSourceWithThrowable( Throwable throwable )
87 {
88 StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( null, null, throwable );
89 RunStatistics statistics = new RunStatistics();
90 statistics.addErrorSource( DUMMY_ERROR_SOURCE, stackTraceWriter );
91
92 return statistics;
93 }
94
95 private RunStatistics createRunStatisticsAndAddFailureSourceWithThrowable( Throwable throwable )
96 {
97 StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( null, null, throwable );
98 RunStatistics statistics = new RunStatistics();
99 statistics.addFailureSource( DUMMY_FAILURE_SOURCE, stackTraceWriter );
100
101 return statistics;
102 }
103
104 private void assertRunStatisticsHasErrorSource( RunStatistics statistics, String expectedErrorSource )
105 {
106 Collection errorSources = statistics.getErrorSources();
107 assertNotNull( "No error sources.", errorSources );
108 assertEquals( "Wrong number of error sources.", 1, errorSources.size() );
109 assertEquals( "Wrong error sources.", expectedErrorSource, errorSources.iterator().next() );
110 }
111
112 private void assertRunStatisticsHasFailureSource( RunStatistics statistics, String expectedFailureSource )
113 {
114 Collection failureSources = statistics.getFailureSources();
115 assertNotNull( "No failure sources.", failureSources );
116 assertEquals( "Wrong number of failure sources.", 1, failureSources.size() );
117 assertEquals( "Wrong failure sources.", expectedFailureSource, failureSources.iterator().next() );
118 }
119 }