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.assertNull;
20  
21  import java.util.Comparator;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  import org.apache.commons.beanutils2.BeanUtils;
26  import org.apache.commons.beanutils2.PropertyUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.junit.jupiter.api.AfterEach;
30  import org.junit.jupiter.api.BeforeEach;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-339">https://issues.apache.org/jira/browse/BEANUTILS-339</a>
35   */
36  public class Jira339Test {
37  
38      /**
39       * Test Bean.
40       */
41      public static class TestBean {
42          private Comparator<?> comparator;
43  
44          /**
45           * Gets the comparator.
46           *
47           * @return the comparator
48           */
49          public Comparator<?> getComparator() {
50              return comparator;
51          }
52  
53          /**
54           * Sets the comparator.
55           *
56           * @param comparator the comparator
57           */
58          public void setComparator(final Comparator<?> comparator) {
59              this.comparator = comparator;
60          }
61  
62      }
63  
64      private static final Log LOG = LogFactory.getLog(Jira339Test.class);
65  
66      /**
67       * Sets up.
68       *
69       * @throws Exception
70       */
71      @BeforeEach
72      protected void setUp() throws Exception {
73      }
74  
75      /**
76       * Tear Down.
77       *
78       * @throws Exception
79       */
80      @AfterEach
81      protected void tearDown() throws Exception {
82      }
83  
84      /**
85       * Test {@link BeanUtils#populate(Object, Map)}
86       */
87      @Test
88      public void testIssue_BEANUTILS_331_BeanUtilsBean_populate() throws Exception {
89          final TestBean bean = new TestBean();
90          final Map<String, Object> properties = new HashMap<>();
91          properties.put("comparator", null);
92          BeanUtils.populate(bean, properties);
93          assertNull(bean.getComparator(), "TestBean comparator should be null");
94      }
95  
96      /**
97       * Test {@link PropertyUtils#setProperty(Object, String, Object)}
98       */
99      @Test
100     public void testIssue_BEANUTILS_339_BeanUtilsBean_setProperty() throws Exception {
101         final TestBean bean = new TestBean();
102         BeanUtils.setProperty(bean, "comparator", null);
103         assertNull(bean.getComparator(), "TestBean comparator should be null");
104     }
105 }