View Javadoc
1   package org.junit.tests.experimental.rules;
2   
3   import static org.hamcrest.CoreMatchers.allOf;
4   import static org.hamcrest.core.IsEqual.equalTo;
5   
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.hamcrest.Matcher;
10  import org.hamcrest.TypeSafeMatcher;
11  import org.junit.runner.Description;
12  import org.junit.runner.Result;
13  import org.junit.runner.notification.Failure;
14  import org.junit.runner.notification.RunListener;
15  
16  class EventCollector extends RunListener {
17      static Matcher<EventCollector> everyTestRunSuccessful() {
18          return allOf(hasNoFailure(), hasNoAssumptionFailure());
19      }
20  
21      private static Matcher<EventCollector> hasNumberOfFailures(
22              final int numberOfFailures) {
23          return new TypeSafeMatcher<EventCollector>() {
24              @Override
25              public boolean matchesSafely(EventCollector item) {
26                  return item.fFailures.size() == numberOfFailures;
27              }
28  
29              public void describeTo(org.hamcrest.Description description) {
30                  description.appendText("has ");
31                  description.appendValue(numberOfFailures);
32                  description.appendText(" failures");
33              }
34  
35              @Override
36              protected void describeMismatchSafely(EventCollector item,
37                      org.hamcrest.Description description) {
38                  description.appendValue(item.fFailures.size());
39                  description.appendText(" failures");
40              }
41          };
42      }
43  
44      static Matcher<EventCollector> hasSingleFailure() {
45          return hasNumberOfFailures(1);
46      }
47  
48      static Matcher<EventCollector> hasNoFailure() {
49          return hasNumberOfFailures(0);
50      }
51  
52      private static Matcher<EventCollector> hasNumberOfAssumptionFailures(
53              final int numberOfFailures) {
54          return new TypeSafeMatcher<EventCollector>() {
55              @Override
56              public boolean matchesSafely(EventCollector item) {
57                  return item.fAssumptionFailures.size() == numberOfFailures;
58              }
59  
60              public void describeTo(org.hamcrest.Description description) {
61                  description.appendText("has ");
62                  description.appendValue(numberOfFailures);
63                  description.appendText(" assumption failures");
64              }
65          };
66      }
67  
68      static Matcher<EventCollector> hasSingleAssumptionFailure() {
69          return hasNumberOfAssumptionFailures(1);
70      }
71  
72      static Matcher<EventCollector> hasNoAssumptionFailure() {
73          return hasNumberOfAssumptionFailures(0);
74      }
75  
76      static Matcher<EventCollector> hasSingleFailureWithMessage(String message) {
77          return hasSingleFailureWithMessage(equalTo(message));
78      }
79  
80      static Matcher<EventCollector> hasSingleFailureWithMessage(
81              final Matcher<String> messageMatcher) {
82          return new TypeSafeMatcher<EventCollector>() {
83              @Override
84              public boolean matchesSafely(EventCollector item) {
85                  return hasSingleFailure().matches(item)
86                          && messageMatcher.matches(item.fFailures.get(0)
87                          .getMessage());
88              }
89  
90              public void describeTo(org.hamcrest.Description description) {
91                  description.appendText("has single failure with message ");
92                  messageMatcher.describeTo(description);
93              }
94  
95              @Override
96              protected void describeMismatchSafely(EventCollector item,
97                      org.hamcrest.Description description) {
98                  description.appendText("was ");
99                  hasSingleFailure().describeMismatch(item, description);
100                 description.appendText(": ");
101                 boolean first= true;
102                 for (Failure f : item.fFailures) {
103                     if (!first) {
104                         description.appendText(" ,");
105                     }
106                     description.appendText("'");
107                     description.appendText(f.getMessage());
108                     description.appendText("'");
109                     first= false;
110                 }
111             }
112         };
113     }
114 
115     static Matcher<EventCollector> failureIs(final Matcher<? super Throwable> exceptionMatcher) {
116         return new TypeSafeMatcher<EventCollector>() {
117             @Override
118             public boolean matchesSafely(EventCollector item) {
119                 for (Failure f : item.fFailures) {
120                     return exceptionMatcher.matches(f.getException());
121                 }
122                 return false;
123             }
124 
125             public void describeTo(org.hamcrest.Description description) {
126                 description.appendText("failure is ");
127                 exceptionMatcher.describeTo(description);
128             }
129         };
130     }
131 
132     private final List<Description> fTestRunsStarted = new ArrayList<Description>();
133 
134     private final List<Result> fTestRunsFinished = new ArrayList<Result>();
135 
136     private final List<Description> fTestsStarted = new ArrayList<Description>();
137 
138     private final List<Description> fTestsFinished = new ArrayList<Description>();
139 
140     private final List<Failure> fFailures = new ArrayList<Failure>();
141 
142     private final List<Failure> fAssumptionFailures = new ArrayList<Failure>();
143 
144     private final List<Description> fTestsIgnored = new ArrayList<Description>();
145 
146     @Override
147     public void testRunStarted(Description description) throws Exception {
148         fTestRunsStarted.add(description);
149     }
150 
151     @Override
152     public void testRunFinished(Result result) throws Exception {
153         fTestRunsFinished.add(result);
154     }
155 
156     @Override
157     public void testStarted(Description description) throws Exception {
158         fTestsStarted.add(description);
159     }
160 
161     @Override
162     public void testFinished(Description description) throws Exception {
163         fTestsFinished.add(description);
164     }
165 
166     @Override
167     public void testFailure(Failure failure) throws Exception {
168         fFailures.add(failure);
169     }
170 
171     @Override
172     public void testAssumptionFailure(Failure failure) {
173         fAssumptionFailures.add(failure);
174     }
175 
176     @Override
177     public void testIgnored(Description description) throws Exception {
178         fTestsIgnored.add(description);
179     }
180 
181     @Override
182     public String toString() {
183         return fTestRunsStarted.size() + " test runs started, "
184             + fTestRunsFinished.size() + " test runs finished, "
185             + fTestsStarted.size() + " tests started, "
186             + fTestsFinished.size() + " tests finished, "
187             + fFailures.size() + " failures, "
188             + fAssumptionFailures.size() + " assumption failures, "
189             + fTestsIgnored.size() + " tests ignored";
190     }
191 }