View Javadoc
1   package org.apache.maven.surefire.util;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.Test;
23  
24  import static org.fest.assertions.Assertions.assertThat;
25  
26  /**
27   * Unit test for {@link ReflectionUtils}.
28   *
29   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
30   * @since 2.20.1
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 }