|
| 1 | +package org.junit.testsupport; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 5 | +import static org.hamcrest.CoreMatchers.is; |
| 6 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 7 | +import static org.junit.rules.ExpectedException.none; |
| 8 | + |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.junit.Rule; |
| 12 | +import org.junit.Test; |
| 13 | +import org.junit.rules.ExpectedException; |
| 14 | +import org.junit.runner.Description; |
| 15 | +import org.junit.runner.Result; |
| 16 | +import org.junit.runner.notification.Failure; |
| 17 | + |
| 18 | +public class EventCollectorTest { |
| 19 | + private static final Description DUMMY_DESCRIPTION = Description.EMPTY; |
| 20 | + |
| 21 | + private static final Failure DUMMY_FAILURE = new Failure(null, null); |
| 22 | + |
| 23 | + private static final Result DUMMY_RESULT = new Result(); |
| 24 | + |
| 25 | + @Rule |
| 26 | + public final ExpectedException thrown = none(); |
| 27 | + |
| 28 | + private final EventCollector collector = new EventCollector(); |
| 29 | + |
| 30 | + @Test |
| 31 | + public void collectsTestRunsStarted() throws Exception { |
| 32 | + collector.testRunStarted(DUMMY_DESCRIPTION); |
| 33 | + assertThat(collector.getTestRunsStarted(), |
| 34 | + is(equalTo(asList(DUMMY_DESCRIPTION)))); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void returnsUnmodifiableListOfTestRunsStarted() throws Exception { |
| 39 | + assertNoDescriptionCanBeAddedToList(collector.getTestRunsStarted()); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void collectsTestRunsFinished() throws Exception { |
| 44 | + collector.testRunFinished(DUMMY_RESULT); |
| 45 | + assertThat(collector.getTestRunsFinished(), |
| 46 | + is(equalTo(asList(DUMMY_RESULT)))); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void returnsUnmodifiableListOfTestRunsFinished() throws Exception { |
| 51 | + assertNoResultCanBeAddedToList(collector.getTestRunsFinished()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void collectsTestsStarted() throws Exception { |
| 56 | + collector.testStarted(DUMMY_DESCRIPTION); |
| 57 | + assertThat(collector.getTestsStarted(), |
| 58 | + is(equalTo(asList(DUMMY_DESCRIPTION)))); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void returnsUnmodifiableListOfTestsStarted() throws Exception { |
| 63 | + assertNoDescriptionCanBeAddedToList(collector.getTestsStarted()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void collectsTestsFinished() throws Exception { |
| 68 | + collector.testFinished(DUMMY_DESCRIPTION); |
| 69 | + assertThat(collector.getTestsFinished(), |
| 70 | + is(equalTo(asList(DUMMY_DESCRIPTION)))); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void returnsUnmodifiableListOfTestsFinished() throws Exception { |
| 75 | + assertNoDescriptionCanBeAddedToList(collector.getTestsFinished()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void collectsFailures() throws Exception { |
| 80 | + collector.testFailure(DUMMY_FAILURE); |
| 81 | + assertThat(collector.getFailures(), is(equalTo(asList(DUMMY_FAILURE)))); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void returnsUnmodifiableListOfFailures() throws Exception { |
| 86 | + assertNoFailureCanBeAddedToList(collector.getFailures()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void collectsAssumptionFailures() throws Exception { |
| 91 | + collector.testAssumptionFailure(DUMMY_FAILURE); |
| 92 | + assertThat(collector.getAssumptionFailures(), |
| 93 | + is(equalTo(asList(DUMMY_FAILURE)))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void returnsUnmodifiableListOfAssumptionFailures() throws Exception { |
| 98 | + assertNoFailureCanBeAddedToList(collector.getAssumptionFailures()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void collectsTestsIgnored() throws Exception { |
| 103 | + collector.testIgnored(DUMMY_DESCRIPTION); |
| 104 | + assertThat(collector.getTestsIgnored(), |
| 105 | + is(equalTo(asList(DUMMY_DESCRIPTION)))); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void returnsUnmodifiableListOfTestsIgnored() throws Exception { |
| 110 | + assertNoDescriptionCanBeAddedToList(collector.getTestsIgnored()); |
| 111 | + } |
| 112 | + |
| 113 | + private void assertNoDescriptionCanBeAddedToList(List<Description> list) { |
| 114 | + thrown.expect(Exception.class); |
| 115 | + list.add(DUMMY_DESCRIPTION); |
| 116 | + } |
| 117 | + |
| 118 | + private void assertNoFailureCanBeAddedToList(List<Failure> list) { |
| 119 | + thrown.expect(Exception.class); |
| 120 | + list.add(DUMMY_FAILURE); |
| 121 | + } |
| 122 | + |
| 123 | + private void assertNoResultCanBeAddedToList(List<Result> list) { |
| 124 | + thrown.expect(Exception.class); |
| 125 | + list.add(DUMMY_RESULT); |
| 126 | + } |
| 127 | +} |
0 commit comments