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.configuration.beanutils;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertNull;
22 import static org.junit.Assert.assertTrue;
23
24 import java.util.HashMap;
25 import java.util.Map;
26
27 import org.apache.commons.configuration.PropertiesConfiguration;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 /**
32 * Test class for DefaultBeanFactory.
33 *
34 * @since 1.3
35 * @author <a
36 * href="http://commons.apache.org/configuration/team-list.html">Commons
37 * Configuration team</a>
38 * @version $Id: TestDefaultBeanFactory.java 1225642 2011-12-29 20:31:38Z oheger $
39 */
40 public class TestDefaultBeanFactory
41 {
42 /** The object to be tested. */
43 DefaultBeanFactory factory;
44
45 @Before
46 public void setUp() throws Exception
47 {
48 factory = new DefaultBeanFactory();
49 }
50
51 /**
52 * Tests obtaining the default class. This should be null.
53 */
54 @Test
55 public void testGetDefaultBeanClass()
56 {
57 assertNull("Default class is not null", factory.getDefaultBeanClass());
58 }
59
60 /**
61 * Tests creating a bean.
62 */
63 @Test
64 public void testCreateBean() throws Exception
65 {
66 Object bean = factory.createBean(PropertiesConfiguration.class,
67 new TestBeanDeclaration(), null);
68 assertNotNull("New bean is null", bean);
69 assertEquals("Bean is of wrong class", PropertiesConfiguration.class,
70 bean.getClass());
71 PropertiesConfiguration config = (PropertiesConfiguration) bean;
72 assertTrue("Bean was not initialized", config
73 .isThrowExceptionOnMissing());
74 }
75
76 /**
77 * A simple implementation of BeanDeclaration used for testing purposes.
78 */
79 static class TestBeanDeclaration implements BeanDeclaration
80 {
81 public String getBeanFactoryName()
82 {
83 return null;
84 }
85
86 public Object getBeanFactoryParameter()
87 {
88 return null;
89 }
90
91 public String getBeanClassName()
92 {
93 return null;
94 }
95
96 public Map<String, Object> getBeanProperties()
97 {
98 Map<String, Object> props = new HashMap<String, Object>();
99 props.put("throwExceptionOnMissing", Boolean.TRUE);
100 return props;
101 }
102
103 public Map<String, Object> getNestedBeanDeclarations()
104 {
105 return null;
106 }
107 }
108 }