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