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.assertFalse;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.apache.commons.beanutils2.PropertyUtils;
23  import org.apache.commons.beanutils2.bugs.other.Jira18BeanFactory;
24  import org.junit.jupiter.api.BeforeAll;
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Test case for Jira issue# BEANUTILS-18.
29   *
30   * <p>
31   * This test case demonstrates the issue.
32   *
33   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-18">https://issues.apache.org/jira/browse/BEANUTILS-18</a>
34   */
35  public class Jira18Test {
36  
37      private static Object bean;
38  
39      @BeforeAll
40      public static void beforeAll() {
41          bean = Jira18BeanFactory.createBean();
42      }
43  
44      /**
45       * Test {@link PropertyUtils#getProperty(Object, String)} for simple properties.
46       */
47      @Test
48      public void testIssue_BEANUTILS_18_PropertyUtils_getProperty() {
49          assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "simple"));
50      }
51  
52      /**
53       * Test {@link PropertyUtils#getProperty(Object, String)} for indexed properties.
54       */
55      @Test
56      public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Indexed() {
57          assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "indexed[0]"));
58      }
59  
60      /**
61       * Test {@link PropertyUtils#getProperty(Object, String)} for mapped properties.
62       */
63      @Test
64      public void testIssue_BEANUTILS_18_PropertyUtils_getProperty_Mapped() {
65          assertThrows(NoSuchMethodException.class, () -> PropertyUtils.getProperty(bean, "mapped(foo-key)"));
66      }
67  
68      /**
69       * Test {@link PropertyUtils#isReadable(Object, String)} for simple properties.
70       */
71      @Test
72      public void testIssue_BEANUTILS_18_PropertyUtils_isReadable() {
73          assertFalse(PropertyUtils.isReadable(bean, "simple"));
74      }
75  
76      /**
77       * Test {@link PropertyUtils#isReadable(Object, String)} for indexed properties.
78       */
79      @Test
80      public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Indexed() {
81          assertFalse(PropertyUtils.isReadable(bean, "indexed"));
82      }
83  
84      /**
85       * Test {@link PropertyUtils#isReadable(Object, String)} for Mapped properties.
86       */
87      @Test
88      public void testIssue_BEANUTILS_18_PropertyUtils_isReadable_Mapped() {
89          assertFalse(PropertyUtils.isReadable(bean, "mapped"));
90      }
91  
92      /**
93       * Test {@link PropertyUtils#isWriteable(Object, String)} for simple properties.
94       */
95      @Test
96      public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable() {
97          assertFalse(PropertyUtils.isWriteable(bean, "simple"));
98      }
99  
100     /**
101      * Test {@link PropertyUtils#isWriteable(Object, String)} for indexed properties.
102      */
103     @Test
104     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Indexed() {
105         assertFalse(PropertyUtils.isWriteable(bean, "indexed"));
106     }
107 
108     /**
109      * Test {@link PropertyUtils#isWriteable(Object, String)} for Mapped properties.
110      */
111     @Test
112     public void testIssue_BEANUTILS_18_PropertyUtils_isWriteable_Mapped() {
113         assertFalse(PropertyUtils.isWriteable(bean, "mapped"));
114     }
115 
116     /**
117      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for simple properties.
118      */
119     @Test
120     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty() {
121         assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "simple", "BAR"));
122     }
123 
124     /**
125      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for indexed properties.
126      */
127     @Test
128     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Indexed() {
129         assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "indexed[0]", "BAR"));
130     }
131 
132     /**
133      * Test {@link PropertyUtils#setProperty(Object, String, Object)} for mapped properties.
134      */
135     @Test
136     public void testIssue_BEANUTILS_18_PropertyUtils_setProperty_Mapped() {
137         assertThrows(NoSuchMethodException.class, () -> PropertyUtils.setProperty(bean, "mapped(foo-key)", "BAR"));
138     }
139 }