1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }