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 import static org.junit.jupiter.api.Assertions.assertTrue;
21
22 import java.beans.PropertyDescriptor;
23
24 import org.apache.commons.beanutils2.FluentIntrospectionTestBean;
25 import org.apache.commons.beanutils2.FluentPropertyBeanIntrospector;
26 import org.apache.commons.beanutils2.PropertyUtilsBean;
27 import org.junit.jupiter.api.BeforeEach;
28 import org.junit.jupiter.api.Test;
29
30 /**
31 * Write methods for PropertyDescriptors created during custom introspection are lost. See <a href="https://issues.apache.org/jira/browse/BEANUTILS-456">JIRA
32 * issue BEANUTILS-456</a>.
33 */
34 public class Jira456Test {
35 /** Constant for the name of the test property. */
36 private static final String TEST_PROP = "fluentGetProperty";
37
38 /** The PropertyUtilsBean used by the tests. */
39 private PropertyUtilsBean pub;
40
41 /**
42 * Clears the reference to the write method in the property descriptor of the test property. This simulates that the write method reference is freed by the
43 * GC.
44 *
45 * @return the bean instance used for testing
46 * @throws Exception if an error occurs
47 */
48 private FluentIntrospectionTestBean clearWriteMethodRef() throws Exception {
49 final FluentIntrospectionTestBean bean = new FluentIntrospectionTestBean();
50 final PropertyDescriptor pd = pub.getPropertyDescriptor(bean, TEST_PROP);
51
52 // simulate that the write method reference is freed
53 pd.setWriteMethod(null);
54 return bean;
55 }
56
57 @BeforeEach
58 protected void setUp() throws Exception {
59 pub = new PropertyUtilsBean();
60 pub.addBeanIntrospector(new FluentPropertyBeanIntrospector());
61 }
62
63 /**
64 * Tests whether a property is recognized as writable even if the reference to its write method was freed.
65 */
66 @Test
67 public void testPropertyIsWritable() throws Exception {
68 final FluentIntrospectionTestBean bean = clearWriteMethodRef();
69 assertTrue(pub.isWriteable(bean, TEST_PROP), "Not writable");
70 }
71
72 /**
73 * Tests whether a lost write method is automatically recovered and can be invoked.
74 */
75 @Test
76 public void testWriteMethodRecover() throws Exception {
77 final FluentIntrospectionTestBean bean = clearWriteMethodRef();
78 final String value = "Test value";
79 pub.setProperty(bean, TEST_PROP, value);
80 assertEquals(value, bean.getFluentGetProperty(), "Property not set");
81 }
82 }