1 package org.apache.maven.surefire.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.Test;
23
24 import static org.fest.assertions.Assertions.assertThat;
25
26
27
28
29
30
31
32 public class ReflectionUtilsTest
33 {
34 @Test(expected = RuntimeException.class)
35 public void shouldNotInvokeStaticMethod()
36 {
37 ReflectionUtils.invokeStaticMethod( ReflectionUtilsTest.class, "notCallable",
38 new Class<?>[0], new Object[0] );
39 }
40
41 @Test
42 public void shouldInvokeStaticMethod()
43 {
44 Object o = ReflectionUtils.invokeStaticMethod( ReflectionUtilsTest.class, "callable",
45 new Class<?>[0], new Object[0] );
46 assertThat( o )
47 .isEqualTo( 3L );
48 }
49
50 @Test
51 public void shouldInvokeMethodChain()
52 {
53 Class<?>[] classes1 = { A.class, A.class };
54 String[] chain = { "current", "pid" };
55 Object o = ReflectionUtils.invokeMethodChain( classes1, chain, null );
56 assertThat( o )
57 .isEqualTo( 3L );
58
59 Class<?>[] classes2 = { A.class, A.class, B.class };
60 String[] longChain = { "current", "createB", "pid" };
61 o = ReflectionUtils.invokeMethodChain( classes2, longChain, null );
62 assertThat( o )
63 .isEqualTo( 1L );
64 }
65
66 @Test
67 public void shouldInvokeFallbackOnMethodChain()
68 {
69 Class<?>[] classes1 = { A.class, A.class };
70 String[] chain = { "current", "abc" };
71 Object o = ReflectionUtils.invokeMethodChain( classes1, chain, 5L );
72 assertThat( o )
73 .isEqualTo( 5L );
74
75 Class<?>[] classes2 = { A.class, B.class, B.class };
76 String[] longChain = { "current", "createB", "abc" };
77 o = ReflectionUtils.invokeMethodChain( classes2, longChain, 6L );
78 assertThat( o )
79 .isEqualTo( 6L );
80 }
81
82 private static void notCallable()
83 {
84 }
85
86 public static long callable()
87 {
88 return 3L;
89 }
90
91 public static class A
92 {
93 public static A current()
94 {
95 return new A();
96 }
97
98 public long pid()
99 {
100 return 3L;
101 }
102
103 public B createB()
104 {
105 return new B();
106 }
107 }
108
109 public static class B
110 {
111 public long pid()
112 {
113 return 1L;
114 }
115 }
116 }