001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.configuration.beanutils;
018    
019    import static org.junit.Assert.assertEquals;
020    import static org.junit.Assert.assertNotNull;
021    import static org.junit.Assert.assertNull;
022    import static org.junit.Assert.assertTrue;
023    
024    import java.util.HashMap;
025    import java.util.Map;
026    
027    import org.apache.commons.configuration.PropertiesConfiguration;
028    import org.junit.Before;
029    import org.junit.Test;
030    
031    /**
032     * Test class for DefaultBeanFactory.
033     *
034     * @since 1.3
035     * @author <a
036     * href="http://commons.apache.org/configuration/team-list.html">Commons
037     * Configuration team</a>
038     * @version $Id: TestDefaultBeanFactory.java 1225642 2011-12-29 20:31:38Z oheger $
039     */
040    public class TestDefaultBeanFactory
041    {
042        /** The object to be tested. */
043        DefaultBeanFactory factory;
044    
045        @Before
046        public void setUp() throws Exception
047        {
048            factory = new DefaultBeanFactory();
049        }
050    
051        /**
052         * Tests obtaining the default class. This should be null.
053         */
054        @Test
055        public void testGetDefaultBeanClass()
056        {
057            assertNull("Default class is not null", factory.getDefaultBeanClass());
058        }
059    
060        /**
061         * Tests creating a bean.
062         */
063        @Test
064        public void testCreateBean() throws Exception
065        {
066            Object bean = factory.createBean(PropertiesConfiguration.class,
067                    new TestBeanDeclaration(), null);
068            assertNotNull("New bean is null", bean);
069            assertEquals("Bean is of wrong class", PropertiesConfiguration.class,
070                    bean.getClass());
071            PropertiesConfiguration config = (PropertiesConfiguration) bean;
072            assertTrue("Bean was not initialized", config
073                    .isThrowExceptionOnMissing());
074        }
075    
076        /**
077         * A simple implementation of BeanDeclaration used for testing purposes.
078         */
079        static class TestBeanDeclaration implements BeanDeclaration
080        {
081            public String getBeanFactoryName()
082            {
083                return null;
084            }
085    
086            public Object getBeanFactoryParameter()
087            {
088                return null;
089            }
090    
091            public String getBeanClassName()
092            {
093                return null;
094            }
095    
096            public Map<String, Object> getBeanProperties()
097            {
098                Map<String, Object> props = new HashMap<String, Object>();
099                props.put("throwExceptionOnMissing", Boolean.TRUE);
100                return props;
101            }
102    
103            public Map<String, Object> getNestedBeanDeclarations()
104            {
105                return null;
106            }
107        }
108    }