View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.beanutils;
18  
19  import java.beans.IntrospectionException;
20  import java.beans.PropertyDescriptor;
21  import java.util.HashSet;
22  import java.util.Set;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * Test class for {@code IntrospectionContext}.
28   *
29   * @version $Id$
30   */
31  public class DefaultIntrospectionContextTestCase extends TestCase {
32      /** Constant for the name of a property. */
33      private static final String PROP = "foo";
34  
35      /** The context to be tested. */
36      private DefaultIntrospectionContext context;
37  
38      @Override
39      protected void setUp() throws Exception {
40          super.setUp();
41          context = new DefaultIntrospectionContext(getClass());
42      }
43  
44      /**
45       * Creates a property descriptor object for a property with the given name.
46       *
47       * @param propName the property name
48       * @return the descriptor for this property
49       */
50      private static PropertyDescriptor createDescriptor(final String propName) {
51          try {
52              return new PropertyDescriptor(propName,
53                      DefaultIntrospectionContextTestCase.class, null, null);
54          } catch (final IntrospectionException e) {
55              throw new IllegalStateException("Unexpected exception: " + e);
56          }
57      }
58  
59      /**
60       * Tests a newly created instance.
61       */
62      public void testInit() {
63          assertEquals("Wrong current class", getClass(),
64                  context.getTargetClass());
65          assertTrue("Got property names", context.propertyNames().isEmpty());
66      }
67  
68      /**
69       * Tests whether a property descriptor can be added.
70       */
71      public void testAddPropertyDescriptor() {
72          final PropertyDescriptor desc = createDescriptor(PROP);
73          context.addPropertyDescriptor(desc);
74          assertTrue("Property not found", context.hasProperty(PROP));
75          assertSame("Wrong descriptor", desc,
76                  context.getPropertyDescriptor(PROP));
77      }
78  
79      /**
80       * Tries to add a null descriptor.
81       */
82      public void testAddPropertyDescriptorNull() {
83          try {
84              context.addPropertyDescriptor(null);
85              fail("Could add null descriptor!");
86          } catch (final IllegalArgumentException iex) {
87              // ok
88          }
89      }
90  
91      /**
92       * Tests whether multiple descriptors can be added.
93       */
94      public void testAddPropertyDescriptors() {
95          final int count = 4;
96          final PropertyDescriptor[] descs = new PropertyDescriptor[count];
97          final Set<PropertyDescriptor> descSet = new HashSet<PropertyDescriptor>();
98          for (int i = 0; i < count; i++) {
99              descs[i] = createDescriptor(PROP + i);
100             descSet.add(descs[i]);
101         }
102         context.addPropertyDescriptors(descs);
103         final PropertyDescriptor d = createDescriptor(PROP);
104         context.addPropertyDescriptor(d);
105         descSet.add(d);
106         final Set<String> names = context.propertyNames();
107         assertEquals("Wrong number of property names", count + 1, names.size());
108         assertTrue("Property not found: " + PROP, names.contains(PROP));
109         for (int i = 0; i < count; i++) {
110             assertTrue("Property not found: " + (PROP + i),
111                     names.contains(PROP + i));
112         }
113         final PropertyDescriptor[] addedDescs = context.getPropertyDescriptors();
114         assertEquals("Wrong number of added descriptors", count + 1,
115                 addedDescs.length);
116         for (final PropertyDescriptor pd : addedDescs) {
117             assertTrue("Unexpected descriptor: " + pd, descSet.remove(pd));
118         }
119     }
120 
121     /**
122      * Tries to add a null array with property descriptors.
123      */
124     public void testAddPropertyDescriptorsNull() {
125         try {
126             context.addPropertyDescriptors(null);
127             fail("Could add a null array with descriptors!");
128         } catch (final IllegalArgumentException iex) {
129             // ok
130         }
131     }
132 
133     /**
134      * Tests hasProperty() if the expected result is false.
135      */
136     public void testHasPropertyFalse() {
137         assertFalse("Wrong result (1)", context.hasProperty(PROP));
138         context.addPropertyDescriptor(createDescriptor(PROP));
139         assertFalse("Wrong result (2)", context.hasProperty("other"));
140     }
141 
142     /**
143      * Tests getPropertyDescriptor() if the property name is unknown.
144      */
145     public void testGetPropertyDescriptorUnknown() {
146         assertNull("Got a property (1)", context.getPropertyDescriptor(PROP));
147         context.addPropertyDescriptor(createDescriptor(PROP));
148         assertNull("Got a property (2)", context.getPropertyDescriptor("other"));
149     }
150 
151     /**
152      * Tests that the set with property names cannot be changed.
153      */
154     public void testPropertyNamesModify() {
155         final Set<String> names = context.propertyNames();
156         try {
157             names.add(PROP);
158             fail("Could modify property names set!");
159         } catch (final UnsupportedOperationException uex) {
160             // ok
161         }
162     }
163 
164     /**
165      * Tests whether a descriptor can be removed.
166      */
167     public void testRemovePropertyDescriptor() {
168         context.addPropertyDescriptor(createDescriptor(PROP));
169         context.removePropertyDescriptor(PROP);
170         assertTrue("Got property names", context.propertyNames().isEmpty());
171         assertEquals("Got descriptors", 0,
172                 context.getPropertyDescriptors().length);
173     }
174 }