001package org.apache.commons.beanutils2;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one or more
005 * contributor license agreements.  See the NOTICE file distributed with
006 * this work for additional information regarding copyright ownership.
007 * The ASF licenses this file to You under the Apache License, Version 2.0
008 * (the "License"); you may not use this file except in compliance with
009 * the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020import static org.apache.commons.beanutils2.BeanUtils.on;
021import static org.junit.Assert.assertEquals;
022import static org.junit.Assert.assertNotNull;
023import static org.junit.Assert.assertTrue;
024
025import java.io.Serializable;
026import java.util.Map;
027
028import org.apache.commons.beanutils2.testbeans.TestBean;
029import org.junit.After;
030import org.junit.Before;
031import org.junit.Test;
032
033public class DescribeTestCase
034{
035
036    private TestBean testBean;
037
038    @Before
039    public void setUp()
040    {
041        testBean = new TestBean();
042    }
043
044    @After
045    public void tearDown()
046    {
047        testBean = null;
048    }
049
050    /**
051     * The set of properties that should be described.
052     */
053    private final String describes[] =
054    { "booleanProperty",
055      "booleanSecond",
056      "byteProperty",
057      "doubleProperty",
058      "dupProperty",
059      "floatProperty",
060      "intArray",
061//            "intIndexed",
062      "longProperty",
063      "listIndexed",
064      "longProperty",
065//            "mappedProperty",
066//            "mappedIntProperty",
067      "nested",
068      "nullProperty",
069      "readOnlyProperty",
070      "shortProperty",
071      "stringArray",
072//            "stringIndexed",
073      "stringProperty"
074    };
075
076    @Test
077    public void describe()
078        throws Exception
079    {
080        Map<String, Object> properties = on( testBean ).describe();
081        assertNotNull( properties );
082
083        // Verify existence of all the properties that should be present
084        for ( int i = 0; i < describes.length; i++ )
085        {
086            assertTrue( "Property '" + describes[i] + "' is not present", properties.containsKey( describes[i] ) );
087        }
088
089        assertTrue( properties.containsKey( "class" ) );
090
091        assertTrue( "Property 'writeOnlyProperty' is not present", !properties.containsKey( "writeOnlyProperty" ) );
092
093        // Verify the values of scalar properties
094        assertEquals( "Value of 'booleanProperty'", true, properties.get( "booleanProperty" ) );
095        assertEquals( "Value of 'byteProperty'", (byte) 121, properties.get( "byteProperty" ) );
096        assertEquals( "Value of 'doubleProperty'", 321.0d, properties.get( "doubleProperty" ) );
097        assertEquals( "Value of 'floatProperty'", 123.0f, properties.get( "floatProperty" ) );
098        assertEquals( "Value of 'intProperty'", 123, properties.get( "intProperty" ) );
099        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}