View Javadoc
1   package org.junit.tests.experimental.theories.runner;
2   
3   import static org.hamcrest.CoreMatchers.is;
4   import static org.junit.Assert.assertEquals;
5   import static org.junit.Assert.assertThat;
6   import static org.junit.Assert.assertTrue;
7   import static org.junit.Assume.assumeTrue;
8   
9   import java.util.ArrayList;
10  import java.util.List;
11  
12  import org.junit.AfterClass;
13  import org.junit.Before;
14  import org.junit.BeforeClass;
15  import org.junit.Test;
16  import org.junit.experimental.runners.Enclosed;
17  import org.junit.experimental.theories.DataPoint;
18  import org.junit.experimental.theories.Theories;
19  import org.junit.experimental.theories.Theory;
20  import org.junit.runner.RunWith;
21  
22  @RunWith(Enclosed.class)
23  public class SuccessfulWithDataPointFields {
24      @RunWith(Theories.class)
25      public static class HasATwoParameterTheory {
26          @DataPoint
27          public static int ONE = 1;
28  
29          @Theory
30          public void allIntsAreEqual(int x, int y) {
31              assertThat(x, is(y));
32          }
33      }
34  
35      @RunWith(Theories.class)
36      public static class BeforeAndAfterOnSameInstance {
37          @DataPoint
38          public static String A = "A";
39  
40          private int befores = 0;
41  
42          @Before
43          public void incrementBefore() {
44              befores++;
45          }
46  
47          @Theory
48          public void stringsAreOK(String string) {
49              assertTrue(befores == 1);
50          }
51      }
52  
53      @RunWith(Theories.class)
54      public static class NewObjectEachTime {
55          @DataPoint
56          public static String A = "A";
57  
58          @DataPoint
59          public static String B = "B";
60  
61          private List<String> list = new ArrayList<String>();
62  
63          @Theory
64          public void addToEmptyList(String string) {
65              list.add(string);
66              assertThat(list.size(), is(1));
67          }
68      }
69  
70      @RunWith(Theories.class)
71      public static class PositiveInts {
72          @DataPoint
73          public static final int ONE = 1;
74  
75          private int x;
76  
77          public PositiveInts(int x) {
78              assumeTrue(x > 0);
79              this.x = x;
80          }
81  
82          @Theory
83          public void haveAPostiveSquare() {
84              assertTrue(x * x > 0);
85          }
86      }
87  
88      @RunWith(Theories.class)
89      public static class PositiveIntsWithNegativeField {
90          @DataPoint
91          public static final int ONE = 1;
92          @DataPoint
93          public static final int NEGONE = -1;
94  
95          private int x;
96  
97          public PositiveIntsWithNegativeField(int x) {
98              assumeTrue(x > 0);
99              this.x = x;
100         }
101 
102         @Theory
103         public void haveAPostiveSquare() {
104             assertTrue(x > 0);
105         }
106     }
107 
108     @RunWith(Theories.class)
109     public static class PositiveIntsWithMethodParams {
110         @DataPoint
111         public static final int ONE = 1;
112 
113         private int x;
114 
115         public PositiveIntsWithMethodParams(int x) {
116             assumeTrue(x > 0);
117             this.x = x;
118         }
119 
120         @Theory
121         public void haveAPostiveSquare(int y) {
122             assumeTrue(y > 0);
123             assertTrue(x * y > 0);
124         }
125     }
126 
127     @RunWith(Theories.class)
128     public static class DifferentTypesInConstructor {
129         @DataPoint
130         public static final int ONE = 1;
131 
132         @DataPoint
133         public static final String A = "A";
134 
135         public DifferentTypesInConstructor(int x) {
136         }
137 
138         @Theory
139         public void yesIndeed(String a) {
140         }
141     }
142 
143     @RunWith(Theories.class)
144     public static class BeforeAndAfterEachTime {
145         public static int befores = 0;
146 
147         @DataPoint
148         public static String A = "A";
149 
150         @DataPoint
151         public static String B = "B";
152 
153         @Before
154         public void incrementBefore() {
155             befores++;
156         }
157 
158         @BeforeClass
159         public static void resetCalls() {
160             befores = 0;
161         }
162 
163         @Theory
164         public void stringsAreOK(String string) {
165         }
166 
167         @AfterClass
168         public static void calledTwice() {
169             assertEquals(2, befores);
170         }
171     }
172 
173     @RunWith(Theories.class)
174     public static class OneTestTwoAnnotations {
175         public static int tests = 0;
176 
177         @DataPoint
178         public static String A = "A";
179 
180         @BeforeClass
181         public static void resetCalls() {
182             tests = 0;
183         }
184 
185         @Theory
186         @Test
187         public void stringsAreOK(String string) {
188             tests++;
189         }
190 
191         @AfterClass
192         public static void calledTwice() {
193             assertEquals(1, tests);
194         }
195     }
196 
197     @RunWith(Theories.class)
198     static public class StaticPublicNonDataPoints {
199         // DataPoint which passes the test
200         @DataPoint
201         public static int ZERO = 0;
202 
203         // Not annotated as a DataPoint and therefore should be ignored:
204         public static int ONE = 1;
205 
206         @Theory
207         public void onlyAnnotatedFields(int i) {
208             assertTrue(i == 0);
209         }
210     }
211 }