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  package org.apache.commons.lang3.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  
21  import java.util.Arrays;
22  import java.util.Collections;
23  
24  import org.apache.commons.lang3.AbstractLangTest;
25  import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Unit tests {@link org.apache.commons.lang3.builder.NoFieldNamesToStringStyleTest}.
32   */
33  public class NoFieldNamesToStringStyleTest extends AbstractLangTest {
34  
35      private final Integer base = Integer.valueOf(5);
36      private final String baseStr = base.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(base));
37  
38      @BeforeEach
39      public void setUp() {
40          ToStringBuilder.setDefaultStyle(ToStringStyle.NO_FIELD_NAMES_STYLE);
41      }
42  
43      @AfterEach
44      public void tearDown() {
45          ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
46      }
47  
48      @Test
49      public void testAppendSuper() {
50          assertEquals(baseStr + "[]", new ToStringBuilder(base).appendSuper("Integer@8888[]").toString());
51          assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").toString());
52  
53          assertEquals(baseStr + "[hello]", new ToStringBuilder(base).appendSuper("Integer@8888[]").append("a", "hello").toString());
54          assertEquals(baseStr + "[<null>,hello]", new ToStringBuilder(base).appendSuper("Integer@8888[<null>]").append("a", "hello").toString());
55          assertEquals(baseStr + "[hello]", new ToStringBuilder(base).appendSuper(null).append("a", "hello").toString());
56      }
57  
58      @Test
59      public void testArray() {
60          final Integer i3 = Integer.valueOf(3);
61          final Integer i4 = Integer.valueOf(4);
62          assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", (Object) new Integer[0], false).toString());
63          assertEquals(baseStr + "[{}]", new ToStringBuilder(base).append("a", (Object) new Integer[0], true).toString());
64          assertEquals(baseStr + "[<size=1>]", new ToStringBuilder(base).append("a", (Object) new Integer[] {i3}, false).toString());
65          assertEquals(baseStr + "[{3}]", new ToStringBuilder(base).append("a", (Object) new Integer[] {i3}, true).toString());
66          assertEquals(baseStr + "[<size=2>]", new ToStringBuilder(base).append("a", (Object) new Integer[] {i3, i4}, false).toString());
67          assertEquals(baseStr + "[{3,4}]", new ToStringBuilder(base).append("a", (Object) new Integer[] {i3, i4}, true).toString());
68      }
69  
70      @Test
71      public void testBlank() {
72          assertEquals(baseStr + "[]", new ToStringBuilder(base).toString());
73      }
74  
75      @Test
76      public void testCollection() {
77          final Integer i3 = Integer.valueOf(3);
78          final Integer i4 = Integer.valueOf(4);
79          assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", Collections.emptyList(), false).toString());
80          assertEquals(baseStr + "[[]]", new ToStringBuilder(base).append("a", Collections.emptyList(), true).toString());
81          assertEquals(baseStr + "[<size=1>]", new ToStringBuilder(base).append("a", Collections.singletonList(i3), false).toString());
82          assertEquals(baseStr + "[[3]]", new ToStringBuilder(base).append("a", Collections.singletonList(i3), true).toString());
83          assertEquals(baseStr + "[<size=2>]", new ToStringBuilder(base).append("a", Arrays.asList(i3, i4), false).toString());
84          assertEquals(baseStr + "[[3, 4]]", new ToStringBuilder(base).append("a", Arrays.asList(i3, i4), true).toString());
85      }
86  
87      @Test
88      public void testLong() {
89          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(3L).toString());
90          assertEquals(baseStr + "[3]", new ToStringBuilder(base).append("a", 3L).toString());
91          assertEquals(baseStr + "[3,4]", new ToStringBuilder(base).append("a", 3L).append("b", 4L).toString());
92      }
93  
94      @Test
95      public void testLongArray() {
96          long[] array = {1, 2, -3, 4};
97          assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append(array).toString());
98          assertEquals(baseStr + "[{1,2,-3,4}]", new ToStringBuilder(base).append((Object) array).toString());
99          array = null;
100         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
101         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
102     }
103 
104     @Test
105     public void testLongArrayArray() {
106         long[][] array = {{1, 2}, null, {5}};
107         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append(array).toString());
108         assertEquals(baseStr + "[{{1,2},<null>,{5}}]", new ToStringBuilder(base).append((Object) array).toString());
109         array = null;
110         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
111         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
112     }
113 
114     @Test
115     public void testMap() {
116         assertEquals(baseStr + "[<size=0>]", new ToStringBuilder(base).append("a", Collections.emptyMap(), false).toString());
117         assertEquals(baseStr + "[{}]", new ToStringBuilder(base).append("a", Collections.emptyMap(), true).toString());
118         assertEquals(baseStr + "[<size=1>]", new ToStringBuilder(base).append("a", Collections.singletonMap("k", "v"), false).toString());
119         assertEquals(baseStr + "[{k=v}]", new ToStringBuilder(base).append("a", Collections.singletonMap("k", "v"), true).toString());
120     }
121 
122     @Test
123     public void testObject() {
124         final Integer i3 = Integer.valueOf(3);
125         final Integer i4 = Integer.valueOf(4);
126         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) null).toString());
127         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append(i3).toString());
128         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append("a", (Object) null).toString());
129         assertEquals(baseStr + "[3]", new ToStringBuilder(base).append("a", i3).toString());
130         assertEquals(baseStr + "[3,4]", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
131         assertEquals(baseStr + "[<Integer>]", new ToStringBuilder(base).append("a", i3, false).toString());
132     }
133 
134     @Test
135     public void testObjectArray() {
136         Object[] array = {null, base, new int[] {3, 6}};
137         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append(array).toString());
138         assertEquals(baseStr + "[{<null>,5,{3,6}}]", new ToStringBuilder(base).append((Object) array).toString());
139         array = null;
140         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append(array).toString());
141         assertEquals(baseStr + "[<null>]", new ToStringBuilder(base).append((Object) array).toString());
142     }
143 
144     @Test
145     public void testPerson() {
146         final Person p = new Person();
147         p.name = "Ron Paul";
148         p.age = 72;
149         p.smoker = false;
150         final String pBaseStr = p.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(p));
151         assertEquals(pBaseStr + "[Ron Paul,72,false]", new ToStringBuilder(p).append("name", p.name).append("age", p.age).append("smoker", p.smoker).toString());
152     }
153 
154 }