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.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.List;
26  
27  import org.apache.commons.lang3.ArrayUtils;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   */
32  class ReflectionToStringBuilderExcludeTest extends AbstractBuilderTest {
33  
34      final class TestFixture {
35          @SuppressWarnings("unused")
36          private final String secretField = SECRET_VALUE;
37  
38          @SuppressWarnings("unused")
39          private final String showField = NOT_SECRET_VALUE;
40      }
41  
42      private static final String NOT_SECRET_FIELD = "showField";
43  
44      private static final String NOT_SECRET_VALUE = "Hello World!";
45  
46      private static final String SECRET_FIELD = "secretField";
47  
48      private static final String SECRET_VALUE = "secret value";
49  
50      @Test
51      void test_toStringExclude() {
52          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), SECRET_FIELD);
53          validateSecretFieldAbsent(toString);
54      }
55  
56      @Test
57      void test_toStringExcludeArray() {
58          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), SECRET_FIELD);
59          validateSecretFieldAbsent(toString);
60      }
61  
62      @Test
63      void test_toStringExcludeArrayWithNull() {
64          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new String[]{null});
65          validateSecretFieldPresent(toString);
66      }
67  
68      @Test
69      void test_toStringExcludeArrayWithNulls() {
70          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), null, null);
71          validateSecretFieldPresent(toString);
72      }
73  
74      @Test
75      void test_toStringExcludeCollection() {
76          final List<String> excludeList = new ArrayList<>();
77          excludeList.add(SECRET_FIELD);
78          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList);
79          validateSecretFieldAbsent(toString);
80      }
81  
82      @Test
83      void test_toStringExcludeCollectionWithNull() {
84          final List<String> excludeList = new ArrayList<>();
85          excludeList.add(null);
86          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList);
87          validateSecretFieldPresent(toString);
88      }
89  
90      @Test
91      void test_toStringExcludeCollectionWithNulls() {
92          final List<String> excludeList = new ArrayList<>();
93          excludeList.add(null);
94          excludeList.add(null);
95          final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), excludeList);
96          validateSecretFieldPresent(toString);
97      }
98  
99      @Test
100     void test_toStringExcludeEmptyArray() {
101         final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), ArrayUtils.EMPTY_STRING_ARRAY);
102         validateSecretFieldPresent(toString);
103     }
104 
105     @Test
106     void test_toStringExcludeEmptyCollection() {
107         final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), new ArrayList<>());
108         validateSecretFieldPresent(toString);
109     }
110 
111     @Test
112     void test_toStringExcludeNullArray() {
113         final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), (String[]) null);
114         validateSecretFieldPresent(toString);
115     }
116 
117     @Test
118     void test_toStringExcludeNullCollection() {
119         final String toString = ReflectionToStringBuilder.toStringExclude(new TestFixture(), (Collection<String>) null);
120         validateSecretFieldPresent(toString);
121     }
122 
123     private void validateNonSecretField(final String toString) {
124         assertTrue(toString.contains(NOT_SECRET_FIELD));
125         assertTrueIfAccessible(toString.contains(NOT_SECRET_VALUE));
126     }
127 
128     private void validateSecretFieldAbsent(final String toString) {
129         assertEquals(ArrayUtils.INDEX_NOT_FOUND, toString.indexOf(SECRET_VALUE));
130         validateNonSecretField(toString);
131     }
132 
133     private void validateSecretFieldPresent(final String toString) {
134         assertTrueIfAccessible(toString.indexOf(SECRET_VALUE) > 0);
135         validateNonSecretField(toString);
136     }
137 }