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