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.AbstractLangTest;
24  import org.junit.jupiter.api.Test;
25  
26  class ReflectionToStringBuilderExcludeNullValuesTest extends AbstractLangTest {
27  
28      static class TestFixture {
29          @SuppressWarnings("unused")
30          private final Integer testIntegerField;
31          @SuppressWarnings("unused")
32          private final String testStringField;
33  
34          TestFixture(final Integer a, final String b) {
35              this.testIntegerField = a;
36              this.testStringField = b;
37          }
38      }
39  
40      private static final String INTEGER_FIELD_NAME = "testIntegerField";
41      private static final String STRING_FIELD_NAME = "testStringField";
42      private final TestFixture BOTH_NON_NULL = new TestFixture(0, "str");
43      private final TestFixture FIRST_NULL = new TestFixture(null, "str");
44      private final TestFixture SECOND_NULL = new TestFixture(0, null);
45      private final TestFixture BOTH_NULL = new TestFixture(null, null);
46  
47      @Test
48      void test_ConstructorOption() {
49          ReflectionToStringBuilder builder = new ReflectionToStringBuilder(BOTH_NON_NULL, null, null, null, false, false, true);
50          assertTrue(builder.isExcludeNullValues());
51          String toString = builder.toString();
52          assertTrue(toString.contains(INTEGER_FIELD_NAME));
53          assertTrue(toString.contains(STRING_FIELD_NAME));
54  
55          builder = new ReflectionToStringBuilder(FIRST_NULL, null, null, null, false, false, true);
56          toString = builder.toString();
57          assertFalse(toString.contains(INTEGER_FIELD_NAME));
58          assertTrue(toString.contains(STRING_FIELD_NAME));
59  
60          builder = new ReflectionToStringBuilder(SECOND_NULL, null, null, null, false, false, true);
61          toString = builder.toString();
62          assertTrue(toString.contains(INTEGER_FIELD_NAME));
63          assertFalse(toString.contains(STRING_FIELD_NAME));
64  
65          builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, true);
66          toString = builder.toString();
67          assertFalse(toString.contains(INTEGER_FIELD_NAME));
68          assertFalse(toString.contains(STRING_FIELD_NAME));
69      }
70  
71      @Test
72      void test_ConstructorOption_ExcludeNull() {
73          ReflectionToStringBuilder builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, false);
74          builder.setExcludeNullValues(true);
75          assertTrue(builder.isExcludeNullValues());
76          String toString = builder.toString();
77          assertFalse(toString.contains(STRING_FIELD_NAME));
78          assertFalse(toString.contains(INTEGER_FIELD_NAME));
79  
80          builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, true);
81          toString = builder.toString();
82          assertFalse(toString.contains(STRING_FIELD_NAME));
83          assertFalse(toString.contains(INTEGER_FIELD_NAME));
84  
85          final ReflectionToStringBuilder oldBuilder = new ReflectionToStringBuilder(BOTH_NULL);
86          oldBuilder.setExcludeNullValues(true);
87          assertTrue(oldBuilder.isExcludeNullValues());
88          toString = oldBuilder.toString();
89          assertFalse(toString.contains(STRING_FIELD_NAME));
90          assertFalse(toString.contains(INTEGER_FIELD_NAME));
91      }
92  
93      @Test
94      void test_ConstructorOptionNormal() {
95          final ReflectionToStringBuilder builder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false, false);
96          assertFalse(builder.isExcludeNullValues());
97          String toString = builder.toString();
98          assertTrue(toString.contains(STRING_FIELD_NAME));
99          assertTrue(toString.contains(INTEGER_FIELD_NAME));
100 
101         //regression test older constructors
102         ReflectionToStringBuilder oldBuilder = new ReflectionToStringBuilder(BOTH_NULL);
103         toString = oldBuilder.toString();
104         assertTrue(toString.contains(STRING_FIELD_NAME));
105         assertTrue(toString.contains(INTEGER_FIELD_NAME));
106 
107         oldBuilder = new ReflectionToStringBuilder(BOTH_NULL, null, null, null, false, false);
108         toString = oldBuilder.toString();
109         assertTrue(toString.contains(STRING_FIELD_NAME));
110         assertTrue(toString.contains(INTEGER_FIELD_NAME));
111 
112         oldBuilder = new ReflectionToStringBuilder(BOTH_NULL, null, null);
113         toString = oldBuilder.toString();
114         assertTrue(toString.contains(STRING_FIELD_NAME));
115         assertTrue(toString.contains(INTEGER_FIELD_NAME));
116     }
117 
118     @Test
119     void test_excludeNull() {
120 
121         //test normal case
122         String toString = ReflectionToStringBuilder.toString(BOTH_NON_NULL, null, false, false, true, null);
123         assertTrue(toString.contains(INTEGER_FIELD_NAME));
124         assertTrue(toString.contains(STRING_FIELD_NAME));
125 
126         //make one null
127         toString = ReflectionToStringBuilder.toString(FIRST_NULL, null, false, false, true, null);
128         assertFalse(toString.contains(INTEGER_FIELD_NAME));
129         assertTrue(toString.contains(STRING_FIELD_NAME));
130 
131         //other one null
132         toString = ReflectionToStringBuilder.toString(SECOND_NULL, null, false, false, true, null);
133         assertTrue(toString.contains(INTEGER_FIELD_NAME));
134         assertFalse(toString.contains(STRING_FIELD_NAME));
135 
136         //both null
137         toString = ReflectionToStringBuilder.toString(BOTH_NULL, null, false, false, true, null);
138         assertFalse(toString.contains(INTEGER_FIELD_NAME));
139         assertFalse(toString.contains(STRING_FIELD_NAME));
140     }
141 
142     @Test
143     void test_NonExclude() {
144         //normal case=
145         String toString = ReflectionToStringBuilder.toString(BOTH_NON_NULL, null, false, false, false, null);
146         assertTrue(toString.contains(INTEGER_FIELD_NAME));
147         assertTrue(toString.contains(STRING_FIELD_NAME));
148 
149         //make one null
150         toString = ReflectionToStringBuilder.toString(FIRST_NULL, null, false, false, false, null);
151         assertTrue(toString.contains(INTEGER_FIELD_NAME));
152         assertTrue(toString.contains(STRING_FIELD_NAME));
153 
154         //other one null
155         toString = ReflectionToStringBuilder.toString(SECOND_NULL, null, false, false, false, null);
156         assertTrue(toString.contains(INTEGER_FIELD_NAME));
157         assertTrue(toString.contains(STRING_FIELD_NAME));
158 
159         //make the both null
160         toString = ReflectionToStringBuilder.toString(BOTH_NULL, null, false, false, false, null);
161         assertTrue(toString.contains(INTEGER_FIELD_NAME));
162         assertTrue(toString.contains(STRING_FIELD_NAME));
163     }
164 
165 }