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