001: package org.junit.experimental.test.theories;
002:
003: import static org.hamcrest.CoreMatchers.is;
004: import static org.hamcrest.CoreMatchers.not;
005: import static org.hamcrest.CoreMatchers.notNullValue;
006: import static org.junit.Assert.assertThat;
007: import static org.junit.Assume.assumeThat;
008: import static org.junit.experimental.results.PrintableResult.testResult;
009: import static org.junit.experimental.results.ResultMatchers.failureCountIs;
010: import static org.junit.experimental.results.ResultMatchers.hasSingleFailureContaining;
011: import static org.junit.experimental.results.ResultMatchers.isSuccessful;
012: import static org.junit.matchers.StringContains.containsString;
013: import org.hamcrest.Matcher;
014: import org.junit.Test;
015: import org.junit.experimental.results.ResultMatchers;
016: import org.junit.experimental.theories.DataPoint;
017: import org.junit.experimental.theories.Theories;
018: import org.junit.experimental.theories.Theory;
019: import org.junit.experimental.theories.suppliers.TestedOn;
020: import org.junit.internal.runners.TestClass;
021: import org.junit.runner.RunWith;
022:
023: @SuppressWarnings("restriction")
024: @RunWith(Theories.class)
025: public class TheoriesTest {
026: public static String SPACE = " ";
027:
028: public static String J = "J";
029:
030: public static String NULL_STRING = null;
031:
032: public static int ZERO = 0;
033:
034: public static int ONE = 1;
035:
036: public static int THREE = 3;
037:
038: public static int FIVE = 5;
039:
040: public static int INDEPENDENCE = 1776;
041:
042: public static Matcher<Integer> NOT_ZERO = not(0);
043:
044: public static Matcher<Integer> IS_ONE = is(1);
045:
046: @RunWith(Theories.class)
047: public static class HasATheory {
048: public static int ONE = 1;
049:
050: @Theory
051: public void everythingIsZero(int x) {
052: assertThat(x, is(0));
053: }
054: }
055:
056: @Test
057: public void theoryClassMethodsShowUp() throws Exception {
058: assertThat(new Theories(HasATheory.class).getDescription()
059: .getChildren().size(), is(1));
060: }
061:
062: @Test
063: public void theoryAnnotationsAreRetained() throws Exception {
064: assertThat(new TestClass(HasATheory.class).getAnnotatedMethods(
065: Theory.class).size(), is(1));
066: }
067:
068: @Test
069: public void canRunTheories() throws Exception {
070: assertThat(testResult(HasATheory.class),
071: hasSingleFailureContaining("Expected"));
072: }
073:
074: @RunWith(Theories.class)
075: public static class HasATwoParameterTheory {
076: public static int ONE = 1;
077:
078: @Theory
079: public void everythingIsZero(int x, int y) {
080: assertThat(x, is(y));
081: }
082: }
083:
084: @Test
085: public void canRunTwoParameterTheories() throws Exception {
086: assertThat(testResult(HasATwoParameterTheory.class),
087: ResultMatchers.isSuccessful());
088: }
089:
090: @RunWith(Theories.class)
091: public static class DoesntUseParams {
092: public static int ONE = 1;
093:
094: @Theory
095: public void everythingIsZero(int x, int y) {
096: assertThat(2, is(3));
097: }
098: }
099:
100: @Test
101: public void reportBadParams() throws Exception {
102: assertThat(testResult(DoesntUseParams.class),
103: hasSingleFailureContaining("everythingIsZero(1, 1)"));
104: }
105:
106: @RunWith(Theories.class)
107: public static class NullsOK {
108: public static String NULL = null;
109:
110: public static String A = "A";
111:
112: @Theory
113: public void everythingIsA(String a) {
114: assertThat(a, is("A"));
115: }
116: }
117:
118: @Test
119: public void nullsUsedUnlessProhibited() throws Exception {
120: assertThat(testResult(NullsOK.class),
121: hasSingleFailureContaining("null"));
122: }
123:
124: @RunWith(Theories.class)
125: public static class ParameterAnnotations {
126: @Theory
127: public void everythingIsOne(@TestedOn(ints={1})
128: int number) {
129: assertThat(number, is(1));
130: }
131: }
132:
133: @Test
134: public void testedOnLimitsParameters() throws Exception {
135: assertThat(testResult(ParameterAnnotations.class),
136: ResultMatchers.isSuccessful());
137: }
138:
139: @RunWith(Theories.class)
140: public static class HonorExpectedException {
141: @Test(expected=NullPointerException.class)
142: public void shouldThrow() {
143:
144: }
145: }
146:
147: @Test
148: public void honorExpected() throws Exception {
149: assertThat(testResult(HonorExpectedException.class)
150: .getFailures().size(), is(1));
151: }
152:
153: @RunWith(Theories.class)
154: public static class HonorTimeout {
155: @Test(timeout=5)
156: public void shouldStop() {
157: while (true) {
158: try {
159: Thread.sleep(1000);
160: } catch (InterruptedException e) {
161:
162: }
163: }
164: }
165: }
166:
167: @Test
168: public void honorTimeout() throws Exception {
169: assertThat(testResult(HonorTimeout.class), failureCountIs(1));
170: }
171:
172: @RunWith(Theories.class)
173: public static class AssumptionsFail {
174: public static int DATA = 0;
175:
176: public static Matcher<Integer> MATCHER = null;
177:
178: @Theory
179: public void nonZeroIntsAreFun(int x) {
180: assumeThat(x, MATCHER);
181: }
182: }
183:
184: @Theory
185: public void showFailedAssumptionsWhenNoParametersFound(int data,
186: Matcher<Integer> matcher) throws Exception {
187: assumeThat(data, not(matcher));
188: AssumptionsFail.DATA = data;
189: AssumptionsFail.MATCHER = matcher;
190:
191: String result = testResult(AssumptionsFail.class).toString();
192: assertThat(result, containsString(matcher.toString()));
193: assertThat(result, containsString("" + data));
194: }
195:
196: @RunWith(Theories.class)
197: public static class ShouldFilterNull {
198: @DataPoint
199: public static String NULL = null;
200:
201: @DataPoint
202: public static String A = "a";
203:
204: @Theory(nullsAccepted=false)
205: public void allStringsAreNonNull(String s) {
206: assertThat(s, notNullValue());
207: }
208: }
209:
210: @Test
211: public void shouldFilterNull() {
212: assertThat(testResult(ShouldFilterNull.class), isSuccessful());
213: }
214: }
|