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