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.beanutils2.bugs;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import org.apache.commons.beanutils2.BeanUtils;
22  import org.apache.commons.beanutils2.BeanUtilsBean;
23  import org.apache.commons.beanutils2.converters.ArrayConverter;
24  import org.apache.commons.beanutils2.converters.StringConverter;
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.BeforeEach;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-359">https://issues.apache.org/jira/browse/BEANUTILS-359</a>
31   */
32  public class Jira359Test {
33  
34      public static class SimplePojoData {
35          private String[] jcrMixinTypes = new String[1];
36  
37          public SimplePojoData() {
38          }
39  
40          public String[] getJcrMixinTypes() {
41              return this.jcrMixinTypes;
42          }
43  
44          public void setJcrMixinTypes(final String[] mixinTypes) {
45              this.jcrMixinTypes = mixinTypes;
46          }
47      }
48  
49      /**
50       * Sets up.
51       *
52       * @throws Exception
53       */
54      @BeforeEach
55      protected void setUp() throws Exception {
56      }
57  
58      /**
59       * Show array contents.
60       */
61      private void showArray(final String text, final String[] array) {
62          if (array == null) {
63              System.out.println(text + " array is null");
64          } else {
65              System.out.println(text + " array length=" + array.length);
66              for (int i = 0; i < array.length; i++) {
67                  System.out.println(text + " array[" + i + "]=" + array[i]);
68              }
69          }
70      }
71  
72      /**
73       * Tear Down.
74       *
75       * @throws Exception
76       */
77      @AfterEach
78      protected void tearDown() throws Exception {
79      }
80  
81      /**
82       * Test {@link BeanUtils} setProperty() String to array with colon value
83       */
84      @Test
85      public void testBeanUtilsSetProperty_CustomConvertStringToArray_WithColonValue() throws Exception {
86          final ArrayConverter converter = new ArrayConverter(String[].class, new StringConverter());
87          converter.setAllowedChars(new char[] { '.', '-', ':' });
88  
89          final BeanUtilsBean utils = new BeanUtilsBean();
90          utils.getConvertUtils().register(converter, String[].class);
91  
92          final SimplePojoData simplePojo = new SimplePojoData();
93          utils.setProperty(simplePojo, "jcrMixinTypes", "mix:rereferencible,mix:simple");
94          showArray("Custom WithColonValue", simplePojo.getJcrMixinTypes());
95          assertEquals(2, simplePojo.getJcrMixinTypes().length, "array size");
96          assertEquals("mix:rereferencible", simplePojo.getJcrMixinTypes()[0]);
97          assertEquals("mix:simple", simplePojo.getJcrMixinTypes()[1]);
98      }
99  
100     /**
101      * Test {@link BeanUtils} setProperty() String to array with colon value
102      */
103     @Test
104     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithColonValue() throws Exception {
105         final SimplePojoData simplePojo = new SimplePojoData();
106         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mix:rereferencible,mix:simple");
107         showArray("Default WithColonValue", simplePojo.getJcrMixinTypes());
108         assertEquals(4, simplePojo.getJcrMixinTypes().length, "array size");
109         assertEquals("mix", simplePojo.getJcrMixinTypes()[0]);
110         assertEquals("rereferencible", simplePojo.getJcrMixinTypes()[1]);
111         assertEquals("mix", simplePojo.getJcrMixinTypes()[2]);
112         assertEquals("simple", simplePojo.getJcrMixinTypes()[3]);
113     }
114 
115     /**
116      * Test {@link BeanUtils} setProperty() String to array without colon value
117      */
118     @Test
119     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithoutColonValue() throws Exception {
120         final SimplePojoData simplePojo = new SimplePojoData();
121         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mixrereferencible,mixsimple");
122         showArray("Default WithoutColonValue", simplePojo.getJcrMixinTypes());
123         assertEquals(2, simplePojo.getJcrMixinTypes().length, "array size");
124         assertEquals("mixrereferencible", simplePojo.getJcrMixinTypes()[0]);
125         assertEquals("mixsimple", simplePojo.getJcrMixinTypes()[1]);
126     }
127 
128     /**
129      * Test {@link BeanUtils} setProperty() String to array without colon value and no comma
130      */
131     @Test
132     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithoutColonValueAndNocoma() throws Exception {
133         final SimplePojoData simplePojo = new SimplePojoData();
134         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mixrereferencible");
135         showArray("Default WithoutColonAndNocoma", simplePojo.getJcrMixinTypes());
136         assertEquals(1, simplePojo.getJcrMixinTypes().length, "array size");
137         assertEquals("mixrereferencible", simplePojo.getJcrMixinTypes()[0]);
138     }
139 }