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.assertArrayEquals;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertNull;
024import static org.junit.Assert.assertTrue;
025
026import java.util.HashMap;
027import java.util.Map;
028
029import org.apache.commons.beanutils2.testbeans.TestBean;
030import org.junit.After;
031import org.junit.Before;
032import org.junit.Test;
033
034public class PopulateTestCase
035{
036
037    private TestBean target;
038    private TestBean defaults;
039
040    private Map<String, Object> properties;
041
042    @Before
043    public void setUp()
044    {
045        target = new TestBean();
046        defaults = new TestBean();
047        properties = new HashMap<String, Object>();
048    }
049
050    @After
051    public void tearDown()
052    {
053        target = null;
054        defaults = null;
055        properties = null;
056    }
057
058    @Test
059    public void populateEmptry()
060        throws Exception
061    {
062        on( target ).populate( properties );
063        assertTrue( target.equals( defaults ) );
064    }
065
066    @Test( expected = NullPointerException.class )
067    public void populateNull()
068        throws Exception
069    {
070        on( target ).populate( null );
071    }
072
073    @Test
074    public void populateWithNullKey()
075        throws Exception
076    {
077        properties.put( null, new Object() );
078        on( target ).populate( properties );
079        assertTrue( target.equals( defaults ) );
080    }
081
082    @Test
083    public void populateNullValue()
084        throws Exception
085    {
086        target.setStringProperty( "Hello World!" );
087        properties.put( "stringProperty", null );
088        on( target ).populate( properties );
089        assertNull( target.getStringProperty() );
090    }
091
092    @Test
093    public void populateUnknownKey()
094        throws Exception
095    {
096        properties.put( "unkown", "String value" );
097        on( target ).populate( properties );
098        assertEquals( defaults, target ) ;
099    }
100
101    @Test( expected = IllegalArgumentException.class )
102    public void populateNullToPrimitive()
103        throws Exception
104    {
105        properties.put( "intProperty", null );
106        on( target ).populate( properties );
107    }
108
109    @Test
110    public void populatePropertyArrays()
111        throws Exception
112    {
113        setUpArrayProperties();
114        on( target ).populate( properties );
115        validateArrayPropertiesOnTarget();
116    }
117
118    private void setUpArrayProperties()
119    {
120        int intArray[] = new int[] { 123, 456, 789 };
121        String stringArray[] = new String[] { "New String 0", "New String 1" };
122        properties.put( "intArray", intArray );
123        properties.put( "stringArray", stringArray );
124    }
125
126    private void validateArrayPropertiesOnTarget()
127    {
128        int[] expectedInts = (int[]) properties.get( "intArray" );
129        assertArrayEquals( expectedInts, target.getIntArray() );
130
131        String[] expectedStrings = (String[]) properties.get( "stringArray" );
132        assertArrayEquals( expectedStrings, target.getStringArray() );
133    }
134
135    @Test
136    public void populateScalar()
137        throws Exception
138    {
139        setUpScalarProperties();
140        on( target ).populate( properties );
141        validateScalarPropertiesOnTarget();
142    }
143
144    private void setUpScalarProperties()
145    {
146        properties.put( "booleanProperty", false );
147        // booleanSecond is left at default value
148        properties.put( "byteProperty", (byte) 111 );
149        properties.put( "doubleProperty", 432.0 );
150        // floatProperty is left at default value
151        properties.put( "intProperty", 543 );
152        properties.put( "longProperty", 123456789l );
153        properties.put( "nullProperty", "Non-null value" );
154        properties.put( "shortProperty", (short) 654 );
155        // stringProperty is left at default value
156        properties.put( "writeOnlyProperty", "New writeOnlyProperty value" );
157        properties.put( "readOnlyProperty", "New readOnlyProperty value" );
158    }
159
160    private void validateScalarPropertiesOnTarget()
161    {
162        boolean expectedBoolean = (Boolean) properties.get( "booleanProperty" );
163        // was left at default value
164        boolean expectedBooleanSecond = defaults.isBooleanSecond();
165        byte expectedByte = (Byte) properties.get( "byteProperty" );
166        double expectedDouble = (Double) properties.get( "doubleProperty" );
167        // was left at default
168        float expectedFloat = defaults.getFloatProperty();
169        int expectedInt = (Integer) properties.get( "intProperty" );
170        long expectedLong = (Long) properties.get( "longProperty" );
171        String expectedNull = (String) properties.get( "nullProperty" );
172        short expectedShort = (Short) properties.get( "shortProperty" );
173        String expectedString = defaults.getStringProperty();
174        String expectedWriteOnly = (String) properties.get( "writeOnlyProperty" );
175        String expectedReadOnly = defaults.getReadOnlyProperty();
176
177        assertEquals( "booleanProperty has the wrong value!", expectedBoolean, target.getBooleanProperty() );
178        assertEquals( "booleanSecond has the wrong value!", expectedBooleanSecond, target.isBooleanSecond() );
179        assertEquals( "byteProperty has the wrong value!", expectedByte, target.getByteProperty() );
180        assertEquals( "doubleProperty has the wrong value!", expectedDouble, target.getDoubleProperty(), 0.005 );
181        assertEquals( "floatProperty has the wrong value!", expectedFloat, target.getFloatProperty(), (float) 0.005 );
182        assertEquals( "intProperty has the wrong value!", expectedInt, target.getIntProperty() );
183        assertEquals( "longProperty has the wrong value!", expectedLong, target.getLongProperty() );
184        assertEquals( "nullProperty has the wrong value!", expectedNull, target.getNullProperty() );
185        assertEquals( "shortProperty has the wrong value!", expectedShort, target.getShortProperty() );
186        assertEquals( "stringProperty has the wrong value!", expectedString, target.getStringProperty() );
187        assertEquals( "writeOnlyProperty has the wrong value!", expectedWriteOnly,
188                      target.getWriteOnlyPropertyValue() );
189        assertEquals( "readOnlyProperty has the wrong value!", expectedReadOnly,
190                      target.getReadOnlyProperty() );
191    }
192
193    /**
194     * Makes sure that read only properties are ignored
195     */
196    @Test
197    public void populateReadOnly()
198        throws Exception
199    {
200        properties.put( "readOnlyProperty", "Some Value" );
201        on( target ).populate( properties );
202        assertTrue( target.equals( defaults ) );
203    }
204
205    /**
206     * Makes sure, that ignoring read only property does not cause writable properties to be ignored
207     */
208    @Test
209    public void populateReadOnlyAndOther()
210        throws Exception
211    {
212        properties.put( "readOnlyProperty", "Some value" );
213        properties.put( "stringProperty", "Some other value" );
214        on( target ).populate( properties );
215
216        defaults.setStringProperty( "Some other value" );
217        assertTrue( target.equals( defaults ) );
218    }
219}