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 java.util.Comparator;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import junit.framework.Test;
24  import junit.framework.TestCase;
25  import junit.framework.TestSuite;
26  
27  import org.apache.commons.beanutils.BeanUtils;
28  import org.apache.commons.beanutils.PropertyUtils;
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  
32  /**
33   * See https://issues.apache.org/jira/browse/BEANUTILS-339
34   * <p />
35   *
36   * @version $Revision: 800650 $ $Date: 2009-08-03 21:38:16 -0400 (Mon, 03 Aug 2009) $
37   */
38  public class Jira339TestCase extends TestCase {
39  
40      private Log log = LogFactory.getLog(Jira339TestCase.class);
41  
42      /**
43       * Create a test case with the specified name.
44       *
45       * @param name The name of the test
46       */
47      public Jira339TestCase(String name) {
48          super(name);
49      }
50  
51      /**
52       * Run the Test.
53       *
54       * @param args Arguments
55       */
56      public static void main(String[] args) {
57          junit.textui.TestRunner.run(suite());
58      }
59  
60      /**
61       * Create a test suite for this test.
62       *
63       * @return a test suite
64       */
65      public static Test suite() {
66          return (new TestSuite(Jira339TestCase.class));
67      }
68  
69      /**
70       * Set up.
71       *
72       * @throws java.lang.Exception
73       */
74      protected void setUp() throws Exception {
75          super.setUp();
76      }
77  
78      /**
79       * Tear Down.
80       *
81       * @throws java.lang.Exception
82       */
83      protected void tearDown() throws Exception {
84          super.tearDown();
85      }
86  
87      /**
88       * Test {@link PropertyUtils#setProperty(Object, String, Object)}
89       */
90      public void testIssue_BEANUTILS_339_BeanUtilsBean_setProperty() {
91          
92          TestBean bean = new TestBean();
93          try {
94              BeanUtils.setProperty(bean, "comparator", null);
95          } catch (Throwable t) {
96              log.error("Failed: " + t.getMessage(), t);
97              fail("Threw exception: " + t);
98          }
99          assertNull("TestBean comparator should be null", bean.getComparator());
100     }
101 
102     /**
103      * Test {@link BeanUtils#populate(Object, Map)}
104      */
105     public void testIssue_BEANUTILS_331_BeanUtilsBean_populate() {
106         
107         TestBean bean = new TestBean();
108         try {
109             Map properties = new HashMap();
110             properties.put("comparator", null);
111             BeanUtils.populate(bean, properties);
112         } catch (Throwable t) {
113             log.error("Failed: " + t.getMessage(), t);
114             fail("Threw exception: " + t);
115         }
116         assertNull("TestBean comparator should be null", bean.getComparator());
117     }
118 
119     /**
120      * Test Bean.
121      */
122     public static class TestBean {
123         private Comparator comparator;
124 
125         /**
126          * Return the comparator.
127          *
128          * @return the comparator
129          */
130         public Comparator getComparator() {
131             return comparator;
132         }
133 
134         /**
135          * Set the comparator.
136          *
137          * @param comparator the comparator
138          */
139         public void setComparator(Comparator comparator) {
140             this.comparator = comparator;
141         }
142         
143     }
144 }