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.beanutils.bugs;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  import org.apache.commons.beanutils.BeanUtils;
24  import org.apache.commons.beanutils.BeanUtilsBean;
25  import org.apache.commons.beanutils.converters.ArrayConverter;
26  import org.apache.commons.beanutils.converters.StringConverter;
27  
28  /**
29   * @version $Id$
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 Jira359TestCase extends TestCase {
33  
34      /**
35       * Create a test case with the specified name.
36       *
37       * @param name The name of the test
38       */
39      public Jira359TestCase(final String name) {
40          super(name);
41      }
42  
43      /**
44       * Run the Test.
45       *
46       * @param args Arguments
47       */
48      public static void main(final String[] args) {
49          junit.textui.TestRunner.run(suite());
50      }
51  
52      /**
53       * Create a test suite for this test.
54       *
55       * @return a test suite
56       */
57      public static Test suite() {
58          return (new TestSuite(Jira359TestCase.class));
59      }
60  
61      /**
62       * Set up.
63       *
64       * @throws java.lang.Exception
65       */
66      @Override
67      protected void setUp() throws Exception {
68          super.setUp();
69      }
70  
71      /**
72       * Tear Down.
73       *
74       * @throws java.lang.Exception
75       */
76      @Override
77      protected void tearDown() throws Exception {
78          super.tearDown();
79      }
80  
81      /**
82       * Test {@link BeanUtils} setProperty() String to array with colon value
83       */
84      public void testBeanUtilsSetProperty_CustomConvertStringToArray_WithColonValue() throws Exception{
85          final ArrayConverter converter = new ArrayConverter(String[].class, new StringConverter());
86          converter.setAllowedChars(new char[] {'.', '-', ':'});
87  
88          final BeanUtilsBean utils = new BeanUtilsBean();
89          utils.getConvertUtils().register(converter, String[].class);
90  
91          final SimplePojoData simplePojo = new SimplePojoData();
92          utils.setProperty(simplePojo, "jcrMixinTypes", "mix:rereferencible,mix:simple");
93          showArray("Custom WithColonValue", simplePojo.getJcrMixinTypes());
94          assertEquals("array size", 2, simplePojo.getJcrMixinTypes().length);
95          assertEquals("mix:rereferencible", simplePojo.getJcrMixinTypes()[0]);
96          assertEquals("mix:simple", simplePojo.getJcrMixinTypes()[1]);
97      }
98  
99      /**
100      * Test {@link BeanUtils} setProperty() String to array with colon value
101      */
102     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithColonValue() throws Exception{
103         final SimplePojoData simplePojo = new SimplePojoData();
104         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mix:rereferencible,mix:simple");
105         showArray("Default WithColonValue", simplePojo.getJcrMixinTypes());
106         assertEquals("array size", 4, simplePojo.getJcrMixinTypes().length);
107         assertEquals("mix", simplePojo.getJcrMixinTypes()[0]);
108         assertEquals("rereferencible", simplePojo.getJcrMixinTypes()[1]);
109         assertEquals("mix", simplePojo.getJcrMixinTypes()[2]);
110         assertEquals("simple", simplePojo.getJcrMixinTypes()[3]);
111     }
112 
113     /**
114      * Test {@link BeanUtils} setProperty() String to array without colon value
115      */
116     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithoutColonValue() throws Exception{
117         final SimplePojoData simplePojo = new SimplePojoData();
118         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mixrereferencible,mixsimple");
119         showArray("Default WithoutColonValue", simplePojo.getJcrMixinTypes());
120         assertEquals("array size", 2, simplePojo.getJcrMixinTypes().length);
121         assertEquals("mixrereferencible", simplePojo.getJcrMixinTypes()[0]);
122         assertEquals("mixsimple", simplePojo.getJcrMixinTypes()[1]);
123     }
124 
125     /**
126      * Test {@link BeanUtils} setProperty() String to array without colon value and no comma
127      */
128     public void testBeanUtilsSetProperty_DefaultConvertStringToArray_WithoutColonValueAndNocoma() throws Exception{
129         final SimplePojoData simplePojo = new SimplePojoData();
130         BeanUtils.setProperty(simplePojo, "jcrMixinTypes", "mixrereferencible");
131         showArray("Default WithoutColonAndNocoma", simplePojo.getJcrMixinTypes());
132         assertEquals("array size", 1, simplePojo.getJcrMixinTypes().length);
133         assertEquals("mixrereferencible", simplePojo.getJcrMixinTypes()[0]);
134     }
135 
136     /**
137      * Show array contents.
138      */
139     private void showArray(final String text, final String[] array) {
140         if (array == null) {
141             System.out.println(text + " array is null");
142         } else {
143             System.out.println(text + " array length=" + array.length);
144             for (int i = 0; i < array.length; i++) {
145                 System.out.println(text + " array[" + i + "]=" + array[i]);
146             }
147         }
148     }
149 
150     public static class SimplePojoData {
151         private String[] jcrMixinTypes = new String[1];
152         public SimplePojoData() {
153         }
154         public String[] getJcrMixinTypes() {
155             return this.jcrMixinTypes;
156         }
157         public void setJcrMixinTypes(final String[] mixinTypes) {
158             this.jcrMixinTypes = mixinTypes;
159         }
160     }
161 }