View Javadoc
1   package org.junit.tests.experimental.theories.runner;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.tests.experimental.theories.TheoryTestUtils.potentialAssignments;
5   
6   import java.util.ArrayList;
7   import java.util.Arrays;
8   import java.util.List;
9   
10  import org.junit.Rule;
11  import org.junit.Test;
12  import org.junit.experimental.theories.ParameterSignature;
13  import org.junit.experimental.theories.ParameterSupplier;
14  import org.junit.experimental.theories.ParametersSuppliedBy;
15  import org.junit.experimental.theories.PotentialAssignment;
16  import org.junit.experimental.theories.Theories;
17  import org.junit.experimental.theories.Theory;
18  import org.junit.rules.ExpectedException;
19  import org.junit.runner.RunWith;
20  import org.junit.runners.model.InitializationError;
21  import org.junit.runners.model.TestClass;
22  
23  public class WithParameterSupplier {
24      
25      @Rule
26      public ExpectedException expected = ExpectedException.none();
27  
28      private static class SimplePotentialAssignment extends PotentialAssignment {
29          private String description;
30          private Object value;
31  
32          public SimplePotentialAssignment(Object value, String description) {
33              this.value = value;
34              this.description = description;
35          }
36  
37          @Override
38          public Object getValue() throws CouldNotGenerateValueException {
39              return value;
40          }
41  
42          @Override
43          public String getDescription() throws CouldNotGenerateValueException {
44              return description;
45          }
46      }
47  
48      private static final List<String> DATAPOINTS = Arrays.asList("qwe", "asd");
49  
50      public static class SimpleSupplier extends ParameterSupplier {
51  
52          @Override
53          public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
54              List<PotentialAssignment> assignments = new ArrayList<PotentialAssignment>();
55  
56              for (String datapoint : DATAPOINTS) {
57                  assignments.add(new SimplePotentialAssignment(datapoint,
58                          datapoint));
59              }
60  
61              return assignments;
62          }
63  
64      }
65  
66      @RunWith(Theories.class)
67      public static class TestClassUsingParameterSupplier {
68  
69          @Theory
70          public void theoryMethod(@ParametersSuppliedBy(SimpleSupplier.class) String param) {
71          }
72  
73      }
74  
75      @Test
76      public void shouldPickUpDataPointsFromParameterSupplier() throws Throwable {
77          List<PotentialAssignment> assignments = potentialAssignments(TestClassUsingParameterSupplier.class
78                  .getMethod("theoryMethod", String.class));
79  
80          assertEquals(2, assignments.size());
81          assertEquals(DATAPOINTS.get(0), assignments.get(0).getValue());
82          assertEquals(DATAPOINTS.get(1), assignments.get(1).getValue());
83      }
84      
85      public static class SupplierWithUnknownConstructor extends ParameterSupplier {
86          
87          public SupplierWithUnknownConstructor(String param) {
88          }
89  
90          @Override
91          public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
92              return null;
93          }
94  
95      }
96  
97      @RunWith(Theories.class)
98      public static class TestClassUsingSupplierWithUnknownConstructor {
99  
100         @Theory
101         public void theory(@ParametersSuppliedBy(SupplierWithUnknownConstructor.class) String param) {
102         }
103 
104     }
105     
106     @Test
107     public void shouldRejectSuppliersWithUnknownConstructors() throws Exception {
108         expected.expect(InitializationError.class);
109         new Theories(TestClassUsingSupplierWithUnknownConstructor.class);
110     }
111     
112     public static class SupplierWithTwoConstructors extends ParameterSupplier {
113         
114         public SupplierWithTwoConstructors(String param) {
115         }
116 
117         @Override
118         public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
119             return null;
120         }
121 
122     }
123 
124     @RunWith(Theories.class)
125     public static class TestClassUsingSupplierWithTwoConstructors {
126 
127         @Theory
128         public void theory(@ParametersSuppliedBy(SupplierWithTwoConstructors.class) String param) {
129         }
130 
131     }
132     
133     @Test
134     public void shouldRejectSuppliersWithTwoConstructors() throws Exception {
135         expected.expect(InitializationError.class);
136         new Theories(TestClassUsingSupplierWithTwoConstructors.class);
137     }
138     
139     public static class SupplierWithTestClassConstructor extends ParameterSupplier {
140         
141         public SupplierWithTestClassConstructor(TestClass param) {
142         }
143 
144         @Override
145         public List<PotentialAssignment> getValueSources(ParameterSignature sig) {
146             return null;
147         }
148 
149     }
150 
151     @RunWith(Theories.class)
152     public static class TestClassUsingSupplierWithTestClassConstructor {
153 
154         @Theory
155         public void theory(@ParametersSuppliedBy(SupplierWithTestClassConstructor.class) String param) {
156         }
157 
158     }
159     
160     @Test
161     public void shouldAcceptSuppliersWithTestClassConstructor() throws Exception {
162         new Theories(TestClassUsingSupplierWithTestClassConstructor.class);
163     }
164 
165 }