KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > uitags > tagutil > ElTagBeanInfoTest


1 /**
2  * Jun 27, 2005
3  *
4  * Copyright 2004 - 2005 uitags
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package net.sf.uitags.tagutil;
19
20 import java.beans.BeanInfo JavaDoc;
21 import java.beans.Introspector JavaDoc;
22 import java.beans.PropertyDescriptor JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.TreeMap JavaDoc;
25
26 import net.sf.uitags.tagutil.bean.ElTagBeanInfo;
27
28 import junit.framework.TestCase;
29
30 /**
31  * Tests {@link net.sf.uitags.tagutil.bean.ElTagBeanInfo}.
32  *
33  * @author jonni
34  * @version $Id: ElTagBeanInfoTest.java,v 1.4 2006/06/21 22:47:44 hgani Exp $
35  */

36 public final class ElTagBeanInfoTest extends TestCase {
37   /**
38    * Main method for the tests.
39    *
40    * @param args main arguments
41    */

42   public static void main(String JavaDoc[] args) {
43     junit.textui.TestRunner.run(ElTagBeanInfoTest.class);
44   }
45
46   /** {@inheritDoc} */
47   protected void setUp() throws Exception JavaDoc {
48     super.setUp();
49   }
50
51   /** {@inheritDoc} */
52   protected void tearDown() throws Exception JavaDoc {
53     super.tearDown();
54   }
55
56   /**
57    * Ensures that {@link ElTagBeanInfo#getTagHandlerSetters()} prioritizes
58    * <code>String</code> properties over non-<code>String</code> properties
59    * if they are of the same name.
60    *
61    * @throws Exception if an introspection error occurs
62    */

63   public void testGetTagHandlerSetters() throws Exception JavaDoc {
64     BeanInfo JavaDoc beanInfo = Introspector.getBeanInfo(BeanUnderTest.class);
65     PropertyDescriptor JavaDoc[] props = beanInfo.getPropertyDescriptors();
66     Map JavaDoc sortedProps = mapPropertyDescriptors(props);
67
68     // prop0 is an overloaded property, the String type should be favored
69
PropertyDescriptor JavaDoc prop0 = ((PropertyDescriptor JavaDoc) sortedProps.get("prop0"));
70     assertEquals(prop0.getPropertyType(), String JavaDoc.class);
71
72     // Make sure other properties are successfully read
73
assertNotNull(sortedProps.get("prop1"));
74     assertNotNull(sortedProps.get("prop2"));
75
76     // We should have 3 properties whose name starts with "prop"
77
assertTrue(sortedProps.size() == 3);
78   }
79
80   /**
81    * Places supplied <code>PropertyDescriptor</code> into a <code>Map</code>,
82    * keyed by property name.
83    *
84    * @param props the property descriptors to be stored
85    * @return a <code>Map</code> containing <code>PropertyDescriptor</code>s
86    */

87   private Map JavaDoc mapPropertyDescriptors(PropertyDescriptor JavaDoc[] props) {
88     Map JavaDoc ret = new TreeMap JavaDoc();
89     for (int i = 0; i < props.length; i++) {
90       if (props[i].getName().startsWith("prop")) {
91         ret.put(props[i].getName(), props[i]);
92       }
93     }
94
95     return ret;
96   }
97
98   /**
99    * The bean class to be introspected.
100    */

101   public static final class BeanUnderTest {
102     public void setProp0(Long JavaDoc nonStringProp) {
103       // no implementation needed
104
}
105     public void setProp0(String JavaDoc stringProp) {
106       // no implementation needed
107
}
108     public void setProp1(String JavaDoc prop) {
109       // no implementation needed
110
}
111     public void setProp2(Integer JavaDoc prop) {
112       // no implementation needed
113
}
114   }
115
116   /**
117    * Explicit information about <code>BeanUnderTest</code>.
118    */

119   public static final class BeanUnderTestBeanInfo extends ElTagBeanInfo {
120     // no implementation needed
121
}
122 }
Popular Tags