View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import static java.lang.String.format;
21  import static java.lang.reflect.Modifier.isPublic;
22  import static org.junit.Assert.assertArrayEquals;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertNotNull;
25  import static org.junit.Assert.assertNull;
26  import static org.junit.Assert.assertTrue;
27  
28  import java.lang.reflect.Constructor;
29  
30  import org.apache.commons.beanutils2.testbeans.TestBean;
31  import org.junit.Test;
32  
33  public class ConstructorRegistryTestCase
34  {
35  
36      private static final String WRONG_FOUND = "ConstructorRegistry resolved wrong constructor!";
37  
38      private static final String NON_FOUND = "ConstructorRegistry could not resolve constructor %s!";
39  
40      private final AccessibleObjectsRegistry<Constructor<?>> constructorRegistry =
41          AccessibleObjectsRegistry.getConstructorsRegistry();
42  
43      @Test
44      public void get()
45      {
46          Constructor<?>[] constructors = TestBean.class.getConstructors();
47          for ( Constructor<?> constructor : constructors )
48          {
49              Constructor<?> resolved = constructorRegistry.get( false, TestBean.class, constructor.getParameterTypes() );
50              assertNotNull( format( NON_FOUND, constructor ), resolved );
51              assertEquals( WRONG_FOUND, constructor, resolved );
52          }
53      }
54  
55      @Test
56      public void getDifferentParameter()
57          throws Exception
58      {
59          Constructor<TestBean> floatConstructor = TestBean.class.getConstructor( float.class );
60          Constructor<?> resolved = constructorRegistry.get( false, TestBean.class, Float.class );
61          assertNotNull( format( NON_FOUND, floatConstructor ), resolved );
62          assertEquals( WRONG_FOUND, floatConstructor, resolved );
63      }
64  
65      @Test
66      public void getExact()
67      {
68          Constructor<?>[] constructors = TestBean.class.getConstructors();
69          for ( Constructor<?> constructor : constructors )
70          {
71              Constructor<?> resolved = constructorRegistry.get( true, TestBean.class, constructor.getParameterTypes() );
72              assertNotNull( format( NON_FOUND, constructor ), resolved );
73              assertEquals( WRONG_FOUND, constructor, resolved );
74          }
75      }
76  
77      @Test
78      public void getExactDifferentParameter()
79          throws Exception
80      {
81          Constructor<?> resolved = constructorRegistry.get( true, TestBean.class, Float.class );
82          assertNull( format( "Constructor resolved constructor [%s] for parameter type Float on type TestBean although TestBean does not describe such a constructor.",
83                              resolved ), resolved );
84      }
85  
86      @Test
87      public void getWithName()
88      {
89          // TODO does the constructorRegistry need this method? constructors don't have names.
90          Constructor<?>[] constructors = TestBean.class.getConstructors();
91          for ( Constructor<?> constructor : constructors )
92          {
93              Constructor<?> resolved = constructorRegistry.get( true, TestBean.class,
94                                                                 "name is ignored by Constructors Registry",
95                                                                 constructor.getParameterTypes() );
96              assertNotNull( format( NON_FOUND, constructor ), resolved );
97              assertEquals( WRONG_FOUND, constructor, resolved );
98          }
99      }
100 
101     @Test
102     public void resolveDirectly()
103         throws Exception
104     {
105         Constructor<?>[] constructors = TestBean.class.getConstructors();
106         for ( Constructor<?> constructor : constructors )
107         {
108             Constructor<?> resolved =
109                 constructorRegistry.resolveDirectly( TestBean.class, null, constructor.getParameterTypes() );
110             assertNotNull( format( NON_FOUND, constructor ), resolved );
111             assertEquals( WRONG_FOUND, constructor, resolved );
112         }
113     }
114 
115     @Test
116     public void getAccessibleObjectsArrayForTestBean()
117     {
118         Constructor<?>[] constructors = constructorRegistry.getAccessibleObjectsArray( TestBean.class );
119         assertNotNull( constructors );
120         assertArrayEquals( TestBean.class.getConstructors(), constructors );
121     }
122 
123     @Test
124     public void matchesNull()
125     {
126         // this method always returns true
127         boolean matches = constructorRegistry.matches( null, null );
128         assertTrue( matches );
129     }
130 
131     @Test
132     public void matches()
133     {
134         // this method always returns true
135         Constructor<?>[] constructors = TestBean.class.getConstructors();
136         for ( Constructor<?> constructor : constructors )
137         {
138             boolean matches = constructorRegistry.matches( constructor, "this returns true" );
139             assertTrue( matches );
140         }
141     }
142 
143     @Test
144     public void getParameterTypes()
145     {
146         Constructor<?>[] constructors = TestBean.class.getDeclaredConstructors();
147         for ( Constructor<?> constructor : constructors )
148         {
149             Class<?>[] typesFromRegistry = constructorRegistry.getParameterTypes( constructor );
150             assertArrayEquals( constructor.getParameterTypes(), typesFromRegistry );
151         }
152     }
153 
154     @Test
155     public void resolveAccessibleNull()
156     {
157         assertNull( constructorRegistry.resolveAccessible( Object.class, null ) );
158     }
159 
160     @Test
161     public void resolveAccessible()
162     {
163         Constructor<?>[] constructors = TestBean.class.getDeclaredConstructors();
164         for ( Constructor<?> constructor : constructors )
165         {
166             Constructor<?> resolved = constructorRegistry.resolveAccessible( TestBean.class, constructor );
167             if ( isPublic( constructor.getModifiers() ) )
168             {
169                 assertEquals( constructor, resolved );
170             }
171             else
172             {
173                 assertNull( resolved );
174             }
175         }
176     }
177 
178     @Test
179     public void resolveAccessiblePrivateClass()
180     {
181         Constructor<?>[] constructors = PrivateTestBean.class.getDeclaredConstructors();
182         for ( Constructor<?> constructor : constructors )
183         {
184             Constructor<?> resolved = constructorRegistry.resolveAccessible( TestBean.class, constructor );
185             // PrivateTestBean is a private class, so no constructor is accessible.
186             assertNull( resolved );
187         }
188     }
189 
190     @SuppressWarnings( "unused" ) // class is used for tests only
191     private static class PrivateTestBean
192     {
193 
194         public PrivateTestBean()
195         {
196             // empty default constructor
197         }
198 
199         private PrivateTestBean( int i )
200         {
201             // int constructor
202         }
203     }
204 
205 }