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 org.apache.commons.beanutils2.BeanUtils.on;
21  import static org.junit.Assert.assertEquals;
22  import static org.junit.Assert.assertNotNull;
23  import static org.junit.Assert.assertTrue;
24  
25  import java.io.Serializable;
26  import java.util.Map;
27  
28  import org.apache.commons.beanutils2.testbeans.TestBean;
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
32  
33  public class DescribeTestCase
34  {
35  
36      private TestBean testBean;
37  
38      @Before
39      public void setUp()
40      {
41          testBean = new TestBean();
42      }
43  
44      @After
45      public void tearDown()
46      {
47          testBean = null;
48      }
49  
50      /**
51       * The set of properties that should be described.
52       */
53      private final String describes[] =
54      { "booleanProperty",
55        "booleanSecond",
56        "byteProperty",
57        "doubleProperty",
58        "dupProperty",
59        "floatProperty",
60        "intArray",
61  //            "intIndexed",
62        "longProperty",
63        "listIndexed",
64        "longProperty",
65  //            "mappedProperty",
66  //            "mappedIntProperty",
67        "nested",
68        "nullProperty",
69        "readOnlyProperty",
70        "shortProperty",
71        "stringArray",
72  //            "stringIndexed",
73        "stringProperty"
74      };
75  
76      @Test
77      public void describe()
78          throws Exception
79      {
80          Map<String, Object> properties = on( testBean ).describe();
81          assertNotNull( properties );
82  
83          // Verify existence of all the properties that should be present
84          for ( int i = 0; i < describes.length; i++ )
85          {
86              assertTrue( "Property '" + describes[i] + "' is not present", properties.containsKey( describes[i] ) );
87          }
88  
89          assertTrue( properties.containsKey( "class" ) );
90  
91          assertTrue( "Property 'writeOnlyProperty' is not present", !properties.containsKey( "writeOnlyProperty" ) );
92  
93          // Verify the values of scalar properties
94          assertEquals( "Value of 'booleanProperty'", true, properties.get( "booleanProperty" ) );
95          assertEquals( "Value of 'byteProperty'", (byte) 121, properties.get( "byteProperty" ) );
96          assertEquals( "Value of 'doubleProperty'", 321.0d, properties.get( "doubleProperty" ) );
97          assertEquals( "Value of 'floatProperty'", 123.0f, properties.get( "floatProperty" ) );
98          assertEquals( "Value of 'intProperty'", 123, properties.get( "intProperty" ) );
99          assertEquals( "Value of 'longProperty'", 321l, properties.get( "longProperty" ) );
100         assertEquals( "Value of 'shortProperty'", (short) 987, properties.get( "shortProperty" ) );
101         assertEquals( "Value of 'stringProperty'", "This is a string", properties.get( "stringProperty" ) );
102     }
103 
104     /**
105      * Test with an private class that overrides a public method of a "grand parent" public class.
106      * <p />
107      * See Jira issue# BEANUTILS-157.
108      */
109     @Test
110     public void describeSerializable()
111         throws Exception
112     {
113         Object bean = new Serializable()
114         {
115             private static final long serialVersionUID = 1L;
116 
117             @SuppressWarnings( "unused" )
118             public String getX()
119             {
120                 return "x-value";
121             }
122 
123             @SuppressWarnings( "unused" )
124             public String getY()
125             {
126                 return "y-value";
127             }
128         };
129         Map<String, Object> result = on( bean ).describe();
130         // 2 properties + getClass()
131         assertEquals( "Check Size", 3, result.size() );
132         assertTrue( "Class", result.containsKey( "class" ) );
133     }
134 
135     /**
136      * Test with an private class that overrides a public method of a "grand parent" public class.
137      * <p />
138      * See Jira issue# BEANUTILS-157.
139      */
140     @Test
141     public void describeInterface()
142         throws Exception
143     {
144         Object bean = new XY()
145         {
146             public String getX()
147             {
148                 return "x-value";
149             }
150 
151             public String getY()
152             {
153                 return "y-value";
154             }
155         };
156         Map<String, Object> result = on( bean ).describe();
157         assertEquals( "Check Size", 3, result.size() );
158         assertTrue( "Class", result.containsKey( "class" ) );
159         assertTrue( "X Key", result.containsKey( "x" ) );
160         assertTrue( "Y Key", result.containsKey( "y" ) );
161         assertEquals( "X Value", "x-value", result.get( "x" ) );
162         assertEquals( "Y Value", "y-value", result.get( "y" ) );
163     }
164 
165     /**
166      * Test with an private class that overrides a public method of a "grand parent" public class.
167      * <p />
168      * See Jira issue# BEANUTILS-157.
169      */
170     @Test
171     public void describeBean()
172         throws Exception
173     {
174         Object bean = new FooBar();
175         Map<String, Object> result = on( bean ).describe();
176         assertEquals( "Check Size", 2, result.size() );
177         assertTrue( "Class", result.containsKey( "class" ) );
178         assertTrue( "publicFoo Key", result.containsKey( "publicFoo" ) );
179         assertEquals( "publicFoo Value", "PublicFoo Value", result.get( "publicFoo" ) );
180     }
181 
182     public static interface XY
183     {
184         String getX();
185 
186         String getY();
187     }
188 
189     public static class FooBar
190     {
191         String getPackageFoo()
192         {
193             return "Package Value";
194         }
195 
196         @SuppressWarnings( "unused" )
197         private String getPrivateFoo()
198         {
199             return "PrivateFoo Value";
200         }
201 
202         protected String getProtectedFoo()
203         {
204             return "ProtectedFoo Value";
205         }
206 
207         public String getPublicFoo()
208         {
209             return "PublicFoo Value";
210         }
211     }
212 
213 }