View Javadoc
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.configuration2.builder.combined;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotNull;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertThrows;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Map;
28  
29  import org.apache.commons.beanutils.DynaClass;
30  import org.apache.commons.beanutils.LazyDynaBean;
31  import org.apache.commons.beanutils.PropertyUtils;
32  import org.apache.commons.configuration2.builder.BasicBuilderParameters;
33  import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
34  import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
35  import org.apache.commons.configuration2.convert.ListDelimiterHandler;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Test class for {@code MultiWrapDynaBean} and {@code MultiWrapDynaClass}.
40   */
41  public class TestMultiWrapDynaBean {
42      /**
43       * A simple test bean class used as wrapped bean.
44       */
45      public static class WrappedBeanTestImpl {
46          /** Stores mapped properties. */
47          private final Map<String, String> mapProperties = new HashMap<>();
48  
49          /** Stores indexed properties. */
50          private final int[] indexedValues = new int[8];
51  
52          /** A simple property. */
53          private String text;
54  
55          public int getIndexedProperty(final int idx) {
56              return indexedValues[idx];
57          }
58  
59          public String getMapProperty(final String key) {
60              return mapProperties.get(key);
61          }
62  
63          public String getText() {
64              return text;
65          }
66  
67          public void setIndexedProperty(final int idx, final int value) {
68              indexedValues[idx] = value;
69          }
70  
71          public void setMapProperty(final String key, final String value) {
72              mapProperties.put(key, value);
73          }
74  
75          public void setText(final String text) {
76              this.text = text;
77          }
78      }
79  
80      /** Constant for a mapped property. */
81      private static final String MAPPED_PROPERTY = "testMappedProperty";
82  
83      /** A test wrapped bean. */
84      private BasicBuilderParameters params;
85  
86      /** Another test wrapped bean. */
87      private WrappedBeanTestImpl wrapBean;
88  
89      /** A wrapped DynaBean. */
90      private LazyDynaBean wrapDynaBean;
91  
92      /**
93       * Creates a new test object with a list of wrapped beans.
94       *
95       * @param withDynaBean a flag whether also a DynaBean should be added to the wrapped beans
96       * @return the test bean
97       */
98      private MultiWrapDynaBean createBean(final boolean withDynaBean) {
99          params = new BasicBuilderParameters();
100         wrapBean = new WrappedBeanTestImpl();
101         final Collection<Object> beans = new ArrayList<>();
102         beans.add(params);
103         beans.add(wrapBean);
104         if (withDynaBean) {
105             wrapDynaBean = new LazyDynaBean();
106             wrapDynaBean.set(MAPPED_PROPERTY, "someKey", "somValue");
107             beans.add(wrapDynaBean);
108         }
109         return new MultiWrapDynaBean(beans);
110     }
111 
112     /**
113      * Tests the contains() implementation. This operation is not available.
114      */
115     @Test
116     public void testContains() {
117         final MultiWrapDynaBean bean = createBean(false);
118         assertThrows(UnsupportedOperationException.class, () -> bean.contains(MAPPED_PROPERTY, "someKey"));
119     }
120 
121     /**
122      * Tests whether the class of bean can be queried.
123      */
124     @Test
125     public void testGetDynaClass() {
126         final DynaClass cls = createBean(false).getDynaClass();
127         assertNotNull(cls.getDynaProperty("throwExceptionOnMissing"));
128         assertNotNull(cls.getDynaProperty("text"));
129     }
130 
131     /**
132      * Checks the name of the DynaClass.
133      */
134     @Test
135     public void testGetDynaClassName() {
136         assertNull(createBean(false).getDynaClass().getName());
137     }
138 
139     /**
140      * Tries to create a new instance of the DynaClass. This is not possible.
141      */
142     @Test
143     public void testGetDynaClassNewInstance() {
144         final DynaClass dynaClass = createBean(false).getDynaClass();
145         assertThrows(UnsupportedOperationException.class, dynaClass::newInstance);
146     }
147 
148     /**
149      * Tests whether an indexed property can be read.
150      */
151     @Test
152     public void testGetIndexedProperty() throws Exception {
153         final MultiWrapDynaBean bean = createBean(false);
154         wrapBean.setIndexedProperty(3, 20121117);
155         assertEquals(20121117, PropertyUtils.getIndexedProperty(bean, "indexedProperty", 3));
156     }
157 
158     /**
159      * Tests whether a map property can be read.
160      */
161     @Test
162     public void testGetMappedProperty() throws Exception {
163         final MultiWrapDynaBean bean = createBean(true);
164         final String key = "testKey";
165         final String value = "Hello World";
166         wrapDynaBean.set(MAPPED_PROPERTY, key, value);
167         assertEquals(value, PropertyUtils.getMappedProperty(bean, MAPPED_PROPERTY, key));
168     }
169 
170     /**
171      * Tries to access an unknown property.
172      */
173     @Test
174     public void testGetPropertyUnknown() {
175         final MultiWrapDynaBean bean = createBean(false);
176         assertThrows(IllegalArgumentException.class, () -> bean.get("unknown property"));
177     }
178 
179     /**
180      * Tests whether a simple property can be read.
181      */
182     @Test
183     public void testGetSimpleProperty() throws Exception {
184         final MultiWrapDynaBean bean = createBean(false);
185         final String text = "testText";
186         wrapBean.setText(text);
187         assertEquals(text, PropertyUtils.getProperty(bean, "text"));
188     }
189 
190     /**
191      * Tests that the order of properties is relevant when adding beans to a MultiWrapDynaBean.
192      */
193     @Test
194     public void testOrderOfProperties() throws Exception {
195         final Collection<Object> beans = new ArrayList<>();
196         params = new BasicBuilderParameters();
197         beans.add(params);
198         beans.add(new FileBasedBuilderParametersImpl());
199         for (int i = 0; i < 32; i++) {
200             beans.add(new BasicBuilderParameters());
201         }
202         final MultiWrapDynaBean bean = new MultiWrapDynaBean(beans);
203         final ListDelimiterHandler listHandler = new DefaultListDelimiterHandler('+');
204         PropertyUtils.setProperty(bean, "throwExceptionOnMissing", Boolean.TRUE);
205         PropertyUtils.setProperty(bean, "listDelimiterHandler", listHandler);
206         final Map<String, Object> map = params.getParameters();
207         assertEquals(Boolean.TRUE, map.get("throwExceptionOnMissing"));
208         assertEquals(listHandler, map.get("listDelimiterHandler"));
209     }
210 
211     /**
212      * Tests the remove() implementation. This operation is not available.
213      */
214     @Test
215     public void testRemove() {
216         final MultiWrapDynaBean bean = createBean(false);
217         assertThrows(UnsupportedOperationException.class, () -> bean.remove(MAPPED_PROPERTY, "someKey"));
218     }
219 
220     /**
221      * Tests whether an indexed property can be set.
222      */
223     @Test
224     public void testSetIndexedProperty() throws Exception {
225         PropertyUtils.setIndexedProperty(createBean(false), "indexedProperty", 1, 42);
226         assertEquals(42, wrapBean.getIndexedProperty(1));
227     }
228 
229     /**
230      * Tests whether a map property can be set.
231      */
232     @Test
233     public void testSetMappedProperty() throws Exception {
234         final MultiWrapDynaBean bean = createBean(true);
235         final String key = "testKey";
236         final String text = "Hello World";
237         PropertyUtils.setMappedProperty(bean, MAPPED_PROPERTY, key, text);
238         assertEquals(text, wrapDynaBean.get(MAPPED_PROPERTY, key));
239     }
240 
241     /**
242      * Tests whether a simple property can be set.
243      */
244     @Test
245     public void testSetSimpleProperty() throws Exception {
246         PropertyUtils.setProperty(createBean(false), "throwExceptionOnMissing", Boolean.TRUE);
247         assertEquals(Boolean.TRUE, params.getParameters().get("throwExceptionOnMissing"));
248     }
249 }