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  
18  package org.apache.commons.lang3.builder;
19  
20  import static org.junit.jupiter.api.Assertions.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.apache.commons.lang3.ArrayUtils;
24  import org.junit.jupiter.api.Test;
25  
26  /**
27   * Tests {@link ReflectionToStringBuilder} allowing overriding annotations, for example in third party code.
28   */
29  class ReflectionToStringBuilderOverrideExcludeTest {
30  
31      static class SensitiveBean {
32  
33          String email = INCLUDE_2;
34          @ToStringExclude
35          String password = EXCLUDE;
36          String username = INCLUDE_1;
37      }
38      private static final String EXCLUDE = "s3cr3t!";
39  
40      private static final String INCLUDE_1 = "alice";
41  
42      private static final String INCLUDE_2 = "alice@example.com";
43  
44      @Test
45      void toStringExcludeFieldNotExposedViaIncludeList() {
46          final SensitiveBean bean = new SensitiveBean();
47          // Explicitly include the @ToStringExclude field by name
48          final String result = ReflectionToStringBuilder.toStringExclude(bean, ArrayUtils.EMPTY_STRING_ARRAY);
49          assertFalse(result.contains(EXCLUDE));
50          assertTrue(result.contains(INCLUDE_1));
51          assertTrue(result.contains(INCLUDE_2));
52      }
53  
54      @Test
55      void toStringIncludeDoesNotExposeExcludedField() {
56          final SensitiveBean bean = new SensitiveBean();
57          // Include both username and password explicitly (password is @ToStringExclude)
58          final ReflectionToStringBuilder builder = new ReflectionToStringBuilder(bean, ToStringStyle.SHORT_PREFIX_STYLE);
59          // By default, @ToStringExclude kicks in.
60          assertFalse(builder.toString().contains(EXCLUDE));
61          assertTrue(builder.toString().contains(INCLUDE_1));
62          // But I really want it
63          builder.setIncludeFieldNames("username", "password");
64          assertTrue(builder.toString().contains(EXCLUDE));
65          assertTrue(builder.toString().contains(INCLUDE_1));
66          assertTrue(builder.toString().contains(INCLUDE_2));
67      }
68  
69      @Test
70      void toStringIncludeExposesNonExcludedFields() {
71          final SensitiveBean bean = new SensitiveBean();
72          final ReflectionToStringBuilder builder = new ReflectionToStringBuilder(bean, ToStringStyle.SHORT_PREFIX_STYLE);
73          builder.setIncludeFieldNames("username", "email");
74          final String result = builder.toString();
75          assertTrue(result.contains(INCLUDE_1));
76          assertTrue(result.contains(INCLUDE_2));
77      }
78  }