View Javadoc
1   package org.junit.tests.experimental.theories;
2   
3   import static org.hamcrest.CoreMatchers.isA;
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.lang.annotation.Annotation;
10  import java.lang.reflect.Method;
11  import java.util.List;
12  
13  import org.hamcrest.CoreMatchers;
14  import org.junit.Test;
15  import org.junit.experimental.theories.DataPoint;
16  import org.junit.experimental.theories.ParameterSignature;
17  import org.junit.experimental.theories.Theories;
18  import org.junit.experimental.theories.Theory;
19  import org.junit.experimental.theories.suppliers.TestedOn;
20  import org.junit.runner.RunWith;
21  
22  @RunWith(Theories.class)
23  public class ParameterSignatureTest {
24      @DataPoint
25      public static Method getType() throws SecurityException,
26              NoSuchMethodException {
27          return ParameterSignatureTest.class.getMethod("getType", Method.class,
28                  int.class);
29      }
30  
31      @DataPoint
32      public static int ZERO = 0;
33  
34      @DataPoint
35      public static int ONE = 1;
36  
37      @Theory
38      public void getType(Method method, int index) {
39          assumeTrue(index < method.getParameterTypes().length);
40          assertEquals(method.getParameterTypes()[index], ParameterSignature
41                  .signatures(method).get(index).getType());
42      }
43  
44      public void foo(@TestedOn(ints = {1, 2, 3}) int x) {
45      }
46  
47      @Test
48      public void getAnnotations() throws SecurityException,
49              NoSuchMethodException {
50          Method method = getClass().getMethod("foo", int.class);
51          List<Annotation> annotations = ParameterSignature.signatures(method)
52                  .get(0).getAnnotations();
53          assertThat(annotations,
54                  CoreMatchers.<TestedOn>hasItem(isA(TestedOn.class)));
55      }
56      
57      public void intMethod(int param) {
58      }
59      
60      public void integerMethod(Integer param) {
61      }
62      
63      public void numberMethod(Number param) {
64      }
65  
66      @Test
67      public void primitiveTypesShouldBeAcceptedAsWrapperTypes() throws Exception {
68          List<ParameterSignature> signatures = ParameterSignature
69                  .signatures(getClass().getMethod("integerMethod", Integer.class));
70          ParameterSignature integerSignature = signatures.get(0);
71  
72          assertTrue(integerSignature.canAcceptType(int.class));
73      }
74      
75      @Test
76      public void primitiveTypesShouldBeAcceptedAsWrapperTypeAssignables() throws Exception {
77          List<ParameterSignature> signatures = ParameterSignature
78                  .signatures(getClass().getMethod("numberMethod", Number.class));
79          ParameterSignature numberSignature = signatures.get(0);
80  
81          assertTrue(numberSignature.canAcceptType(int.class));
82      }
83      
84      @Test
85      public void wrapperTypesShouldBeAcceptedAsPrimitiveTypes() throws Exception {
86          List<ParameterSignature> signatures = ParameterSignature
87                  .signatures(getClass().getMethod("intMethod", int.class));
88          ParameterSignature intSignature = signatures.get(0);
89  
90          assertTrue(intSignature.canAcceptType(Integer.class));
91      }
92  }