View Javadoc
1   package org.junit.tests.experimental.theories.internal;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertNotEquals;
6   import static org.junit.Assert.assertThat;
7   import static org.junit.tests.experimental.theories.TheoryTestUtils.potentialAssignments;
8   
9   import java.util.Arrays;
10  import java.util.List;
11  
12  import org.junit.Rule;
13  import org.junit.Test;
14  import org.junit.experimental.theories.DataPoint;
15  import org.junit.experimental.theories.DataPoints;
16  import org.junit.experimental.theories.ParameterSignature;
17  import org.junit.experimental.theories.PotentialAssignment;
18  import org.junit.experimental.theories.Theory;
19  import org.junit.experimental.theories.internal.AllMembersSupplier;
20  import org.junit.rules.ExpectedException;
21  import org.junit.runners.model.TestClass;
22  
23  public class AllMembersSupplierTest {
24      @Rule
25      public ExpectedException expected = ExpectedException.none();
26      
27      public static class HasDataPointsArrayField {
28          @DataPoints
29          public static String[] list = new String[] { "qwe", "asd" };
30  
31          @Theory
32          public void theory(String param) {
33          }
34      }
35      
36      @Test
37      public void dataPointsArrayShouldBeRecognized() throws Throwable {
38          List<PotentialAssignment> assignments = potentialAssignments(
39                  HasDataPointsArrayField.class.getMethod("theory", String.class));
40          
41          assertEquals(2, assignments.size());
42      }
43      
44      public static class HasDataPointsArrayWithMatchingButInaccurateTypes {
45          @DataPoints
46          public static Object[] objects = {1, "string!", 2};
47  
48          @Theory
49          public void theory(Integer param) {
50          }
51      }
52  
53      @Test
54      public void dataPointsArrayShouldBeRecognizedOnValueTypeNotFieldType() throws Throwable {
55          List<PotentialAssignment> assignments = potentialAssignments(
56                  HasDataPointsArrayWithMatchingButInaccurateTypes.class.getMethod("theory", Integer.class));
57          
58          assertEquals(2, assignments.size());
59      }
60      
61      public static class HasDataPointMethodWithOverlyGeneralTypes {
62          @DataPoint
63          public static Integer object() {
64              return 1;
65          }
66  
67          @Theory
68          public void theory(Object param) {
69          }
70      }
71  
72      @Test
73      public void dataPointMethodShouldBeRecognizedForOverlyGeneralParameters() throws Throwable {
74          List<PotentialAssignment> assignments = potentialAssignments(
75                  HasDataPointMethodWithOverlyGeneralTypes.class.getMethod("theory", Object.class));
76          
77          assertEquals(1, assignments.size());
78      }
79      
80      public static class HasDataPointsWithObjectParameter {
81          @DataPoints
82          public static Object[] objectField = {1, 2};
83  
84          @Theory
85          public void theory(Object obj) {
86          }
87      }
88  
89      @Test
90      public void dataPointsAnnotationMeansTreatAsArrayOnly() throws Throwable {
91          List<PotentialAssignment> assignments = potentialAssignments(
92                  HasDataPointsWithObjectParameter.class.getMethod("theory", Object.class));
93          
94          assertEquals(2, assignments.size());
95          for (PotentialAssignment assignment : assignments) {
96              assertNotEquals(HasDataPointsWithObjectParameter.objectField, assignment.getValue());
97          }
98      }
99  
100     public static class HasDataPointsFieldWithNullValue {
101         @DataPoints
102         public static Object[] objects = {null, "a"};
103 
104         public HasDataPointsFieldWithNullValue(Object obj) {
105         }
106     }
107 
108     @Test
109     public void dataPointsArrayFieldMayContainNullValue() throws Throwable {
110         List<PotentialAssignment> valueSources = allMemberValuesFor(
111                 HasDataPointsFieldWithNullValue.class, Object.class);
112         assertThat(valueSources.size(), is(2));
113     }
114 
115     public static class HasDataPointsMethodWithNullValue {
116         @DataPoints
117         public static Integer[] getObjects() {
118             return new Integer[] {null, 1};
119         }
120 
121         public HasDataPointsMethodWithNullValue(Integer i) {
122         }
123     }
124 
125     @Test
126     public void dataPointsArrayMethodMayContainNullValue() throws Throwable {
127         List<PotentialAssignment> valueSources = allMemberValuesFor(
128                 HasDataPointsMethodWithNullValue.class, Integer.class);
129         assertThat(valueSources.size(), is(2));
130     }
131     
132     public static class HasFailingDataPointsArrayMethod {
133         @DataPoints
134         public static Object[] objects() {
135             throw new RuntimeException("failing method");
136         }
137 
138         public HasFailingDataPointsArrayMethod(Object obj) {
139         }
140     }
141 
142     @Test
143     public void allMembersFailsOnFailingDataPointsArrayMethod() throws Throwable {
144         expected.expect(RuntimeException.class);
145         expected.expectMessage("failing method");
146         allMemberValuesFor(HasFailingDataPointsArrayMethod.class, Object.class);
147     }
148 
149     private List<PotentialAssignment> allMemberValuesFor(Class<?> testClass,
150             Class<?>... constructorParameterTypes) throws Throwable {
151         return new AllMembersSupplier(new TestClass(testClass))
152                 .getValueSources(ParameterSignature.signatures(
153                         testClass.getConstructor(constructorParameterTypes))
154                         .get(0));
155     }
156     
157     public static class HasDataPointsListField {
158         @DataPoints
159         public static List<String> list = Arrays.asList("one", "two");
160 
161         @Theory
162         public void theory(String param) {
163         }
164     }
165 
166     @Test
167     public void dataPointsCollectionFieldsShouldBeRecognized() throws Throwable {
168         List<PotentialAssignment> assignments = potentialAssignments(
169             HasDataPointsListField.class.getMethod("theory", String.class));
170 
171         assertEquals(2, assignments.size());
172     }
173     
174     public static class HasDataPointsListMethod {
175         @DataPoints
176         public static List<String> getList() {
177             return Arrays.asList("one", "two");
178         }
179 
180         @Theory
181         public void theory(String param) {
182         }
183     }
184 
185     @Test
186     public void dataPointsCollectionMethodShouldBeRecognized() throws Throwable {
187         List<PotentialAssignment> assignments = potentialAssignments(
188             HasDataPointsListMethod.class.getMethod("theory", String.class));
189 
190         assertEquals(2, assignments.size());
191     }
192     
193     public static class HasDataPointsListFieldWithOverlyGenericTypes {
194         @DataPoints
195         public static List<Object> list = Arrays.asList("string", new Object());
196 
197         @Theory
198         public void theory(String param) {
199         }
200     }
201 
202     @Test
203     public void dataPointsCollectionShouldBeRecognizedIgnoringStrangeTypes() throws Throwable {
204         List<PotentialAssignment> assignments = potentialAssignments(
205             HasDataPointsListFieldWithOverlyGenericTypes.class.getMethod("theory", String.class));
206 
207         assertEquals(1, assignments.size());
208     }
209 }