View Javadoc
1   package org.junit.tests.experimental.theories.runner;
2   
3   import static org.hamcrest.CoreMatchers.containsString;
4   import static org.hamcrest.CoreMatchers.everyItem;
5   import static org.hamcrest.CoreMatchers.is;
6   import static org.hamcrest.CoreMatchers.not;
7   import static org.hamcrest.CoreMatchers.nullValue;
8   import static org.junit.Assert.assertThat;
9   import static org.junit.experimental.results.PrintableResult.testResult;
10  import static org.junit.experimental.results.ResultMatchers.isSuccessful;
11  import static org.junit.tests.experimental.theories.TheoryTestUtils.potentialAssignments;
12  
13  import java.util.ArrayList;
14  import java.util.Date;
15  import java.util.List;
16  
17  import org.hamcrest.Matcher;
18  import org.junit.Test;
19  import org.junit.experimental.theories.DataPoint;
20  import org.junit.experimental.theories.Theories;
21  import org.junit.experimental.theories.Theory;
22  import org.junit.runner.JUnitCore;
23  import org.junit.runner.RunWith;
24  import org.junit.runner.notification.Failure;
25  
26  public class WithDataPointMethod {
27      @RunWith(Theories.class)
28      public static class HasDataPointMethod {
29          @DataPoint
30          public static int oneHundred() {
31              return 100;
32          }
33  
34          @Theory
35          public void allIntsOk(int x) {
36          }
37      }
38  
39      @Test
40      public void pickUpDataPointMethods() {
41          assertThat(testResult(HasDataPointMethod.class), isSuccessful());
42      }
43  
44      @RunWith(Theories.class)
45      public static class DataPointMethodReturnsMutableObject {
46          @DataPoint
47          public static List<Object> empty() {
48              return new ArrayList<Object>();
49          }
50  
51          @DataPoint
52          public static int ONE = 1;
53  
54          @DataPoint
55          public static int TWO = 2;
56  
57          @Theory
58          public void everythingsEmpty(List<Object> first, int number) {
59              assertThat(first.size(), is(0));
60              first.add("a");
61          }
62      }
63  
64      @Test
65      public void mutableObjectsAreCreatedAfresh() {
66          assertThat(failures(DataPointMethodReturnsMutableObject.class), empty());
67      }
68  
69      @RunWith(Theories.class)
70      public static class HasDateMethod {
71          @DataPoint
72          public static int oneHundred() {
73              return 100;
74          }
75  
76          public static Date notADataPoint() {
77              return new Date();
78          }
79  
80          @Theory
81          public void allIntsOk(int x) {
82          }
83  
84          @Theory
85          public void onlyStringsOk(String s) {
86          }
87  
88          @Theory
89          public void onlyDatesOk(Date d) {
90          }
91      }
92  
93      @Test
94      public void ignoreDataPointMethodsWithWrongTypes() throws Throwable {
95          assertThat(potentialAssignments(
96                  HasDateMethod.class.getMethod("onlyStringsOk", String.class))
97                  .toString(), not(containsString("100")));
98      }
99  
100     @Test
101     public void ignoreDataPointMethodsWithoutAnnotation() throws Throwable {
102         assertThat(potentialAssignments(
103                 HasDateMethod.class.getMethod("onlyDatesOk", Date.class))
104                 .size(), is(0));
105     }
106 
107     private List<Failure> failures(Class<?> type) {
108         return JUnitCore.runClasses(type).getFailures();
109     }
110 
111     private Matcher<Iterable<Failure>> empty() {
112         return everyItem(nullValue(Failure.class));
113     }
114 }