View Javadoc
1   package org.junit.runners.model;
2   
3   import static org.hamcrest.CoreMatchers.hasItem;
4   import static org.hamcrest.CoreMatchers.is;
5   import static org.hamcrest.CoreMatchers.nullValue;
6   import static org.junit.Assert.assertEquals;
7   import static org.junit.Assert.assertFalse;
8   import static org.junit.Assert.assertThat;
9   import static org.junit.Assert.assertTrue;
10  
11  import java.lang.annotation.Annotation;
12  import java.util.List;
13  
14  import org.junit.Ignore;
15  import org.junit.Rule;
16  import org.junit.Test;
17  import org.junit.rules.ExpectedException;
18  import org.junit.rules.TestRule;
19  import org.junit.runner.RunWith;
20  
21  public class TestClassTest {
22  
23      @Rule
24      public ExpectedException exception = ExpectedException.none();
25  
26      public static class TwoConstructors {
27          public TwoConstructors() {
28          }
29  
30          public TwoConstructors(int x) {
31          }
32      }
33  
34      @Test(expected = IllegalArgumentException.class)
35      public void complainIfMultipleConstructors() {
36          new TestClass(TwoConstructors.class);
37      }
38  
39      public static class SuperclassWithField {
40          @Rule
41          public TestRule x;
42      }
43  
44      public static class SubclassWithField extends SuperclassWithField {
45          @Rule
46          public TestRule x;
47      }
48  
49      @Test
50      public void fieldsOnSubclassesShadowSuperclasses() {
51          assertThat(new TestClass(SubclassWithField.class).getAnnotatedFields(
52                  Rule.class).size(), is(1));
53      }
54  
55      public static class OuterClass {
56          public class NonStaticInnerClass {
57          }
58      }
59  
60      @Test
61      public void identifyNonStaticInnerClass() {
62          assertThat(
63                  new TestClass(OuterClass.NonStaticInnerClass.class)
64                          .isANonStaticInnerClass(),
65                  is(true));
66      }
67  
68      public static class OuterClass2 {
69          public static class StaticInnerClass {
70          }
71      }
72  
73      @Test
74      public void dontMarkStaticInnerClassAsNonStatic() {
75          assertThat(
76                  new TestClass(OuterClass2.StaticInnerClass.class)
77                          .isANonStaticInnerClass(),
78                  is(false));
79      }
80  
81      public static class SimpleClass {
82      }
83  
84      @Test
85      public void dontMarkNonInnerClassAsInnerClass() {
86          assertThat(new TestClass(SimpleClass.class).isANonStaticInnerClass(),
87                  is(false));
88      }
89  
90      public static class FieldAnnotated {
91          @Rule
92          public String fieldC= "andromeda";
93  
94          @Rule
95          public boolean fieldA;
96  
97          @Rule
98          public boolean fieldB;
99      }
100 
101     @Test
102     public void providesAnnotatedFieldsSortedByName() {
103         TestClass tc= new TestClass(FieldAnnotated.class);
104         List<FrameworkField> annotatedFields= tc.getAnnotatedFields();
105         assertThat("Wrong number of annotated fields.", annotatedFields.size(), is(3));
106         assertThat("First annotated field is wrong.", annotatedFields
107             .iterator().next().getName(), is("fieldA"));
108     }
109 
110     @Test
111     public void annotatedFieldValues() {
112         TestClass tc = new TestClass(FieldAnnotated.class);
113         List<String> values = tc.getAnnotatedFieldValues(new FieldAnnotated(), Rule.class, String.class);
114         assertThat(values, hasItem("andromeda"));
115         assertThat(values.size(), is(1));
116     }
117 
118     public static class MethodsAnnotated {
119         @Ignore
120         @Test
121         public int methodC() {
122             return 0;
123         }
124 
125         @Ignore
126         @Test
127         public String methodA() {
128             return "jupiter";
129         }
130 
131         @Ignore
132         @Test
133         public int methodB() {
134             return 0;
135     	}
136     }
137 
138     @Test
139     public void providesAnnotatedMethodsSortedByName() {
140     	TestClass tc = new TestClass(MethodsAnnotated.class);
141     	List<FrameworkMethod> annotatedMethods = tc.getAnnotatedMethods();
142     	assertThat("Wrong number of annotated methods.",
143     	    annotatedMethods.size(), is(3));
144     	assertThat("First annotated method is wrong.", annotatedMethods
145     	    .iterator().next().getName(), is("methodA"));
146     }
147 
148     @Test
149     public void annotatedMethodValues() {
150     	TestClass tc = new TestClass(MethodsAnnotated.class);
151     	List<String> values = tc.getAnnotatedMethodValues(
152     	    new MethodsAnnotated(), Ignore.class, String.class);
153     	assertThat(values, hasItem("jupiter"));
154     	assertThat(values.size(), is(1));
155     }
156 
157     @Test
158     public void isEqualToTestClassThatWrapsSameJavaClass() {
159         TestClass testClass = new TestClass(DummyClass.class);
160         TestClass testClassThatWrapsSameJavaClass = new TestClass(
161                 DummyClass.class);
162         assertTrue(testClass.equals(testClassThatWrapsSameJavaClass));
163     }
164 
165     @Test
166     public void isEqualToTestClassThatWrapsNoJavaClassToo() {
167         TestClass testClass = new TestClass(null);
168         TestClass testClassThatWrapsNoJavaClassToo = new TestClass(null);
169         assertTrue(testClass.equals(testClassThatWrapsNoJavaClassToo));
170     }
171 
172     @Test
173     public void isNotEqualToTestClassThatWrapsADifferentJavaClass() {
174         TestClass testClass = new TestClass(DummyClass.class);
175         TestClass testClassThatWrapsADifferentJavaClass = new TestClass(
176                 AnotherDummyClass.class);
177         assertFalse(testClass.equals(testClassThatWrapsADifferentJavaClass));
178     }
179 
180     @Test
181     public void isNotEqualToNull() {
182         TestClass testClass = new TestClass(DummyClass.class);
183         assertFalse(testClass.equals(null));
184     }
185 
186     private static class DummyClass {
187     }
188 
189     private static class AnotherDummyClass {
190     }
191 
192     @Test
193     public void hasSameHashCodeAsTestClassThatWrapsSameJavaClass() {
194         TestClass testClass = new TestClass(DummyClass.class);
195         TestClass testClassThatWrapsSameJavaClass = new TestClass(
196                 DummyClass.class);
197         assertEquals(testClass.hashCode(),
198                 testClassThatWrapsSameJavaClass.hashCode());
199     }
200 
201     @Test
202     public void hasHashCodeWithoutJavaClass() {
203         TestClass testClass = new TestClass(null);
204         testClass.hashCode();
205         // everything is fine if no exception is thrown.
206     }
207 
208     public static class PublicClass {
209 
210     }
211 
212     @Test
213     public void identifiesPublicModifier() {
214         TestClass tc = new TestClass(PublicClass.class);
215         assertEquals("Wrong flag 'public',", true, tc.isPublic());
216     }
217 
218     static class NonPublicClass {
219 
220     }
221     
222     @Test
223     public void identifiesNonPublicModifier() {
224         TestClass tc = new TestClass(NonPublicClass.class);
225         assertEquals("Wrong flag 'public',", false, tc.isPublic());
226     }
227 
228     @Ignore
229     static class AnnotatedClass {
230     }
231 
232     @Test
233     public void presentAnnotationIsAvailable() {
234         TestClass tc = new TestClass(AnnotatedClass.class);
235         Annotation annotation = tc.getAnnotation(Ignore.class);
236         assertTrue(Ignore.class.isAssignableFrom(annotation.getClass()));
237     }
238 
239     @Test
240     public void missingAnnotationIsNotAvailable() {
241         TestClass tc = new TestClass(AnnotatedClass.class);
242         Annotation annotation = tc.getAnnotation(RunWith.class);
243         assertThat(annotation, is(nullValue()));
244     }
245 }