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