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.assertEquals;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.concurrent.ExecutorService;
24  import java.util.concurrent.Executors;
25  import java.util.concurrent.Future;
26  
27  import org.apache.commons.beanutils2.FluentPropertyBeanIntrospector;
28  import org.apache.commons.beanutils2.PropertyUtilsBean;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   * Fix BEANUTILS-541
33   *
34   * @see <a href="https://issues.apache.org/jira/browse/BEANUTILS-541">https://issues.apache.org/jira/browse/BEANUTILS-541</a>
35   */
36  public class Jira541Test {
37  
38      public static class BaseType {
39  
40          private String field;
41  
42          public String getField() {
43              return field;
44          }
45  
46          public BaseType setField(final String objectName) {
47              this.field = objectName;
48              return this;
49          }
50      }
51  
52      public static class SubTypeA extends BaseType {
53  
54          @Override
55          public SubTypeA setField(final String field) {
56              super.setField(field);
57              return this;
58          }
59      }
60  
61      public static class SubTypeB extends BaseType {
62  
63      }
64  
65      private static final String FIELD_NAME = "field";
66  
67      private static final String FIELD_VALUE = "name";
68  
69      private static void testImpl() throws ReflectiveOperationException {
70          final PropertyUtilsBean propertyUtilsBean = new PropertyUtilsBean();
71          propertyUtilsBean.addBeanIntrospector(new FluentPropertyBeanIntrospector());
72  
73          // note: we should setProperty first on SubTypeA (with overridden setter), then on subTypeB
74          // but not vice versa
75          final SubTypeA subTypeA = new SubTypeA();
76          propertyUtilsBean.setProperty(subTypeA, FIELD_NAME, FIELD_VALUE);
77  
78          final SubTypeB subTypeB = new SubTypeB();
79          propertyUtilsBean.setProperty(subTypeB, FIELD_NAME, FIELD_VALUE);
80  
81          assertEquals(FIELD_VALUE, subTypeA.getField());
82          assertEquals(FIELD_VALUE, subTypeB.getField());
83      }
84  
85      @Test
86      public void testFluentBeanIntrospectorOnOverriddenSetter() throws Exception {
87          testImpl();
88      }
89  
90      @Test
91      public void testFluentBeanIntrospectorOnOverriddenSetterConcurrent() throws Exception {
92          final ExecutorService executionService = Executors.newFixedThreadPool(256);
93          try {
94              final List<Future<?>> futures = new ArrayList<>();
95              for (int i = 0; i < 10000; i++) {
96                  futures.add(executionService.submit(() -> {
97                      testImpl();
98                      return null;
99                  }));
100             }
101             for (final Future<?> future : futures) {
102                 future.get();
103             }
104         } finally {
105             executionService.shutdown();
106         }
107     }
108 }