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  package org.apache.commons.lang3.builder;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import java.nio.CharBuffer;
23  import java.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.Date;
27  import java.util.HashMap;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  import org.apache.commons.lang3.builder.ToStringStyleTest.Person;
33  import org.junit.jupiter.api.AfterEach;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  
37  /**
38   * Tests {@link JsonToStringStyleTest}.
39   */
40  class JsonToStringStyleTest extends AbstractBuilderTest {
41  
42      static class AcademyClass {
43          Teacher teacher;
44          List<Student> students;
45  
46          public List<Student> getStudents() {
47              return students;
48          }
49  
50          public Teacher getTeacher() {
51              return teacher;
52          }
53  
54          public void setStudents(final List<Student> students) {
55              this.students = students;
56          }
57  
58          public void setTeacher(final Teacher teacher) {
59              this.teacher = teacher;
60          }
61  
62          @Override
63          public String toString() {
64              return ToStringBuilder.reflectionToString(this);
65          }
66      }
67  
68      enum EmptyEnum {
69      }
70  
71      enum Hobby {
72          SPORT,
73          BOOK,
74          MUSIC
75      }
76  
77      /**
78       * An object with a Map field used to test {@code ToStringStyle.JsonToStringStyle}.
79       */
80      static class InnerMapObject {
81  
82          /**
83           * Test String field.
84           */
85          String pid;
86  
87          /**
88           * Test inner map field.
89           */
90          Map<String, Object> map;
91      }
92  
93      /**
94       * An object with nested object structures used to test {@code ToStringStyle.JsonToStringStyle}.
95       */
96      static class NestingPerson {
97  
98          /**
99           * Test String field.
100          */
101         String pid;
102 
103         /**
104          * Test nested object field.
105          */
106         Person person;
107     }
108 
109     static class Student {
110         List<Hobby> hobbies;
111 
112         public List<Hobby> getHobbies() {
113             return hobbies;
114         }
115 
116         public void setHobbies(final List<Hobby> hobbies) {
117             this.hobbies = hobbies;
118         }
119 
120         @Override
121         public String toString() {
122             return ToStringBuilder.reflectionToString(this);
123         }
124     }
125 
126     static class Teacher {
127         Hobby[] hobbies;
128 
129         public Hobby[] getHobbies() {
130             return hobbies;
131         }
132 
133         public void setHobbies(final Hobby[] hobbies) {
134             this.hobbies = hobbies;
135         }
136 
137         @Override
138         public String toString() {
139             return ToStringBuilder.reflectionToString(this);
140         }
141     }
142 
143     private final Integer base = Integer.valueOf(5);
144 
145     @BeforeEach
146     public void setUp() {
147         ToStringBuilder.setDefaultStyle(ToStringStyle.JSON_STYLE);
148     }
149 
150     @AfterEach
151     public void tearDown() {
152         ToStringBuilder.setDefaultStyle(ToStringStyle.DEFAULT_STYLE);
153     }
154 
155     @Test
156     void testAppendSuper() {
157         assertEquals(
158                 "{}",
159                 new ToStringBuilder(base).appendSuper(
160                         "Integer@8888[" + System.lineSeparator() + "]")
161                         .toString());
162         assertEquals(
163                 "{}",
164                 new ToStringBuilder(base).appendSuper(
165                         "Integer@8888[" + System.lineSeparator() + "  null"
166                                 + System.lineSeparator() + "]").toString());
167         assertEquals(
168                 "{\"a\":\"hello\"}",
169                 new ToStringBuilder(base)
170                         .appendSuper(
171                                 "Integer@8888[" + System.lineSeparator()
172                                         + "]").append("a", "hello").toString());
173         assertEquals(
174                 "{\"a\":\"hello\"}",
175                 new ToStringBuilder(base)
176                         .appendSuper(
177                                 "Integer@8888[" + System.lineSeparator()
178                                         + "  null" + System.lineSeparator()
179                                         + "]").append("a", "hello").toString());
180         assertEquals("{\"a\":\"hello\"}", new ToStringBuilder(base)
181                 .appendSuper(null).append("a", "hello").toString());
182 
183         assertEquals("{\"a\":\"hello\",\"b\":\"world\"}", new ToStringBuilder(base)
184                 .appendSuper("{\"a\":\"hello\"}").append("b", "world").toString());
185     }
186 
187     @Test
188     void testArray() {
189         final Person p = new Person();
190         p.name = "Jane Doe";
191         p.age = 25;
192         p.smoker = true;
193 
194         assertEquals(
195                 "{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true,\"groups\":['admin', 'manager', 'user']}",
196                 new ToStringBuilder(p).append("name", p.name)
197                         .append("age", p.age).append("smoker", p.smoker)
198                         .append("groups", new Object() {
199                             @Override
200                             public String toString() {
201                                 return "['admin', 'manager', 'user']";
202                             }
203                         })
204                         .toString());
205     }
206 
207     @Test
208     void testArrayEnum() {
209         final Teacher teacher = new Teacher();
210         final Hobby[] hobbies = new Hobby[3];
211         hobbies[0] = Hobby.BOOK;
212         hobbies[1] = Hobby.SPORT;
213         hobbies[2] = Hobby.MUSIC;
214 
215         teacher.setHobbies(hobbies);
216 
217         assertEqualsIfAccessible(teacher.toString(), "{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}");
218         teacher.setHobbies(new Hobby[0]);
219         assertEqualsIfAccessible(teacher.toString(), "{\"hobbies\":[]}");
220         teacher.setHobbies(null);
221         assertEquals(teacher.toString(), "{\"hobbies\":null}");
222     }
223 
224     @Test
225     void testBlank() {
226         assertEquals("{}", new ToStringBuilder(base).toString());
227     }
228 
229     @Test
230     void testBooleanArray() {
231         final boolean[] array = { true, false };
232         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
233         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
234         assertEquals("{\"booleanArray\":[true,false]}", toStringBuilder.append("booleanArray", array).toString());
235         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
236         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
237         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
238     }
239 
240     @Test
241     void testByteArray() {
242         final byte[] array = { 1, 2, -3, 4 };
243         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
244         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
245         assertEquals("{\"byteArray\":[1,2,-3,4]}", toStringBuilder.append("byteArray", array).toString());
246         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
247         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
248         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
249     }
250 
251     @Test
252     void testChar() {
253         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append('A').toString());
254         assertEquals("{\"a\":\"A\"}", new ToStringBuilder(base).append("a", 'A').toString());
255         assertEquals("{\"a\":\"A\",\"b\":\"B\"}", new ToStringBuilder(base).append("a", 'A').append("b", 'B').toString());
256     }
257 
258     @Test
259     void testCharArray() {
260         final char[] array = { '1', '2', '3', '4' };
261         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
262         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
263         assertEquals("{\"charArray\":[\"1\",\"2\",\"3\",\"4\"]}", toStringBuilder.append("charArray", array).toString());
264         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
265         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
266         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
267     }
268 
269     @Test
270     void testCharSequenceIsEscaped() {
271         final String text = "{},\"admin\":true,\"x\":{}";
272         final String expected = "{\"name\":\"{},\\\"admin\\\":true,\\\"x\\\":{}\"}";
273         assertEquals(expected, new ToStringBuilder(base).append("name", text).toString());
274         assertEquals(expected, new ToStringBuilder(base).append("name", new StringBuilder(text)).toString());
275         assertEquals(expected, new ToStringBuilder(base).append("name", new StringBuffer(text)).toString());
276         assertEquals(expected, new ToStringBuilder(base).append("name", CharBuffer.wrap(text)).toString());
277         assertEquals("{\"name\":[\"[1]\"]}", new ToStringBuilder(base).append("name", Arrays.asList(new StringBuilder("[1]"))).toString());
278     }
279 
280     @Test
281     void testCharSequenceMapValueIsEscaped() {
282         assertEquals("{\"name\":{\"key\":\"{},\\\"admin\\\":true,\\\"x\\\":{}\"}}",
283                 new ToStringBuilder(base).append("name", Collections.singletonMap("key", new StringBuilder("{},\"admin\":true,\"x\":{}"))).toString());
284     }
285 
286     @Test
287     void testCharSequenceObjectArrayElementIsEscaped() {
288         assertEquals("{\"name\":[\"{},\\\"admin\\\":true,\\\"x\\\":{}\"]}",
289                 new ToStringBuilder(base).append("name", new Object[] { new StringBuilder("{},\"admin\":true,\"x\":{}") }).toString());
290     }
291 
292     @Test
293     void testCharSequencePlainTextIsEscaped() {
294         final String text = "text\"\\\n\r\t\b\f";
295         final String expected = "{\"name\":\"text\\\"\\\\\\n\\r\\t\\b\\f\"}";
296         assertEquals(expected, new ToStringBuilder(base).append("name", text).toString());
297         assertEquals(expected, new ToStringBuilder(base).append("name", new StringBuilder(text)).toString());
298         assertEquals(expected, new ToStringBuilder(base).append("name", new StringBuffer(text)).toString());
299         assertEquals(expected, new ToStringBuilder(base).append("name", CharBuffer.wrap(text)).toString());
300     }
301 
302     @Test
303     void testCombineListAndEnum() {
304         final Teacher teacher = new Teacher();
305 
306         final Hobby[] teacherHobbies = new Hobby[3];
307         teacherHobbies[0] = Hobby.BOOK;
308         teacherHobbies[1] = Hobby.SPORT;
309         teacherHobbies[2] = Hobby.MUSIC;
310 
311         teacher.setHobbies(teacherHobbies);
312 
313         final Student john = new Student();
314         john.setHobbies(Arrays.asList(Hobby.BOOK, Hobby.MUSIC));
315 
316         final Student alice = new Student();
317         alice.setHobbies(new ArrayList<>());
318 
319         final Student bob = new Student();
320         bob.setHobbies(Collections.singletonList(Hobby.BOOK));
321 
322         final ArrayList<Student> students = new ArrayList<>();
323         students.add(john);
324         students.add(alice);
325         students.add(bob);
326 
327         final AcademyClass academyClass = new AcademyClass();
328         academyClass.setStudents(students);
329         academyClass.setTeacher(teacher);
330 
331         assertEqualsIfAccessible(academyClass.toString(),
332                 "{\"students\":[{\"hobbies\":[\"BOOK\",\"MUSIC\"]},{\"hobbies\":[]},{\"hobbies\":[\"BOOK\"]}],\"teacher\":{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}}");
333     }
334 
335     @Test
336     void testDate() {
337         final Date now = new Date();
338         final Date afterNow = new Date(System.currentTimeMillis() + 1);
339 
340         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(now).toString());
341 
342         assertEquals("{\"now\":\"" + now + "\"}", new ToStringBuilder(base).append("now", now).toString());
343         assertEquals("{\"now\":\"" + now + "\",\"after\":\"" + afterNow + "\"}",
344                 new ToStringBuilder(base).append("now", now).append("after", afterNow).toString());
345     }
346 
347     @Test
348     void testDoubleArray() {
349         final double[] array = { 1, 2, -3, 4 };
350         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
351         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
352         assertEquals("{\"doubleArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("doubleArray", array).toString());
353         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
354         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
355         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
356     }
357 
358     @Test
359     void testFloatArray() {
360         final float[] array = { 1, 2, -3, 4 };
361         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
362         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
363         assertEquals("{\"floatArray\":[1.0,2.0,-3.0,4.0]}", toStringBuilder.append("floatArray", array).toString());
364         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
365         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
366         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
367     }
368 
369     @Test
370     void testIntArray() {
371         final int[] array = { 1, 2, -3, 4 };
372         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
373         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
374         assertEquals("{\"intArray\":[1,2,-3,4]}", toStringBuilder.append("intArray", array).toString());
375         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
376         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
377         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
378     }
379 
380     @Test
381     void testLANG1395() {
382         assertEquals("{\"name\":\"value\"}", new ToStringBuilder(base).append("name", "value").toString());
383         assertEquals("{\"name\":\"\"}", new ToStringBuilder(base).append("name", "").toString());
384         assertEquals("{\"name\":\"\\\"\"}", new ToStringBuilder(base).append("name", '"').toString());
385         assertEquals("{\"name\":\"\\\\\"}", new ToStringBuilder(base).append("name", '\\').toString());
386         assertEquals("{\"name\":\"Let's \\\"quote\\\" this\"}", new ToStringBuilder(base).append("name", "Let's \"quote\" this").toString());
387     }
388 
389     @Test
390     void testLANG1396() {
391         assertEquals("{\"Let's \\\"quote\\\" this\":\"value\"}", new ToStringBuilder(base).append("Let's \"quote\" this", "value").toString());
392     }
393 
394     @Test
395     void testList() {
396         final Student student = new Student();
397         final ArrayList<Hobby> objects = new ArrayList<>();
398 
399         objects.add(Hobby.BOOK);
400         objects.add(Hobby.SPORT);
401         objects.add(Hobby.MUSIC);
402 
403         student.setHobbies(objects);
404 
405         assertEqualsIfAccessible(student.toString(), "{\"hobbies\":[\"BOOK\",\"SPORT\",\"MUSIC\"]}");
406         student.setHobbies(new ArrayList<>());
407         assertEqualsIfAccessible(student.toString(), "{\"hobbies\":[]}");
408         student.setHobbies(null);
409         assertEquals(student.toString(), "{\"hobbies\":null}");
410     }
411 
412     @Test
413     void testLong() {
414         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(3L).toString());
415 
416         assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", 3L)
417                 .toString());
418         assertEquals("{\"a\":3,\"b\":4}",
419                 new ToStringBuilder(base).append("a", 3L).append("b", 4L)
420                         .toString());
421     }
422 
423     @Test
424     void testLongArray() {
425         final long[] array = { 1, 2, -3, 4 };
426         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
427         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
428         assertEquals("{\"longArray\":[1,2,-3,4]}", toStringBuilder.append("longArray", array).toString());
429         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
430         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
431         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
432     }
433 
434     @Test
435     void testLongArrayArray() {
436         final long[][] array = { { 1, 2 }, null, { 5 } };
437         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
438         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
439         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
440         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[][]) null).toString());
441         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
442     }
443 
444     @Test
445     void testMapSkipNullKey() {
446         final Map<String, Object> map = new LinkedHashMap<>();
447         map.put("k1", "v1");
448         map.put(null, "v2");
449 
450         assertEquals("{\"map\":{\"k1\":\"v1\"}}",
451                 new ToStringBuilder(base).append("map", map).toString());
452     }
453 
454     @Test
455     void testNestedMaps() {
456         final Map<String, Object> innerMap = new LinkedHashMap<>();
457         innerMap.put("k2.1", "v2.1");
458         innerMap.put("k2.2", "v2.2");
459         final Map<String, Object> baseMap = new LinkedHashMap<>();
460         baseMap.put("k1", "v1");
461         baseMap.put("k2", innerMap);
462 
463         final InnerMapObject object = new InnerMapObject() {
464             @Override
465             public String toString() {
466                 return new ToStringBuilder(this).append("pid", this.pid).append("map", this.map).toString();
467             }
468         };
469         object.pid = "dummy-text";
470         object.map = baseMap;
471 
472         assertEquals("{\"object\":{\"pid\":\"dummy-text\",\"map\":{\"k1\":\"v1\",\"k2\":{\"k2.1\":\"v2.1\",\"k2.2\":\"v2.2\"}}}}",
473                 new ToStringBuilder(base).append("object", object).toString());
474     }
475 
476     @Test
477     void testNestingPerson() {
478         final Person p = new Person() {
479             @Override
480             public String toString() {
481                 return new ToStringBuilder(this).append("name", this.name).append("age", this.age).append("smoker", this.smoker).toString();
482             }
483         };
484         p.name = "Jane Doe";
485         p.age = 25;
486         p.smoker = true;
487 
488         final NestingPerson nestP = new NestingPerson();
489         nestP.pid = "#1@Jane";
490         nestP.person = p;
491 
492         assertEquals("{\"pid\":\"#1@Jane\",\"person\":{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true}}",
493                 new ToStringBuilder(nestP).append("pid", nestP.pid).append("person", nestP.person).toString());
494     }
495 
496     @Test
497     void testNull() {
498         assertEquals("null", new ToStringBuilder(null).toString());
499     }
500 
501     @Test
502     void testObject() {
503         final Integer i3 = Integer.valueOf(3);
504         final Integer i4 = Integer.valueOf(4);
505         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append((Object) null).toString());
506         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append(i3).toString());
507         assertEquals("{\"a\":null}", new ToStringBuilder(base).append("a", (Object) null).toString());
508         assertEquals("{\"a\":3}", new ToStringBuilder(base).append("a", i3).toString());
509         assertEquals("{\"a\":3,\"b\":4}", new ToStringBuilder(base).append("a", i3).append("b", i4).toString());
510         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", i3, false).toString());
511         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new ArrayList<>(), false).toString());
512         assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", new ArrayList<>(), true).toString());
513         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", new HashMap<>(), false).toString());
514         assertEquals("{\"a\":{}}", new ToStringBuilder(base).append("a", new HashMap<>(), true).toString());
515         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new String[0], false).toString());
516         assertEquals("{\"a\":[]}", new ToStringBuilder(base).append("a", (Object) new String[0], true).toString());
517         assertThrows(UnsupportedOperationException.class, () -> new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, false).toString());
518         assertEquals("{\"a\":[1,2,3]}", new ToStringBuilder(base).append("a", (Object) new int[] { 1, 2, 3 }, true).toString());
519         assertThrows(UnsupportedOperationException.class,
520                 () -> new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, false).toString());
521         assertEquals("{\"a\":[\"v\",\"x\",\"y\",\"z\"]}", new ToStringBuilder(base).append("a", (Object) new String[] { "v", "x", "y", "z" }, true).toString());
522     }
523 
524     @Test
525     void testObjectArray() {
526         final Object[] array = { null, base, new int[] { 3, 6 } };
527         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
528         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
529         assertEquals("{\"objectArray\":[null,5,[3,6]]}", toStringBuilder.append("objectArray", array).toString());
530         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
531         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object[]) null).toString());
532         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
533     }
534 
535     @Test
536     void testObjectWithInnerMap() {
537         final Map<String, Object> map = new LinkedHashMap<>();
538         map.put("k1", "value1");
539         map.put("k2", 2);
540 
541         final InnerMapObject object = new InnerMapObject() {
542             @Override
543             public String toString() {
544                 return new ToStringBuilder(this).append("pid", this.pid).append("map", this.map).toString();
545             }
546         };
547         object.pid = "dummy-text";
548         object.map = map;
549 
550         assertEquals("{\"object\":{\"pid\":\"dummy-text\",\"map\":{\"k1\":\"value1\",\"k2\":2}}}",
551                 new ToStringBuilder(base).append("object", object).toString());
552     }
553 
554     @Test
555     void testPerson() {
556         final Person p = new Person();
557         p.name = "Jane Doe";
558         p.age = 25;
559         p.smoker = true;
560 
561         assertEquals(
562                 "{\"name\":\"Jane Doe\",\"age\":25,\"smoker\":true}",
563                 new ToStringBuilder(p).append("name", p.name)
564                         .append("age", p.age).append("smoker", p.smoker)
565                         .toString());
566     }
567 
568     @Test
569     void testRootMap() {
570         final Map<String, Object> map = new LinkedHashMap<>();
571         map.put("k1", "v1");
572         map.put("k2", 2);
573 
574         assertEquals("{\"map\":{\"k1\":\"v1\",\"k2\":2}}",
575                 new ToStringBuilder(base).append("map", map).toString());
576     }
577 
578     @Test
579     void testShortArray() {
580         final short[] array = { 1, 2, -3, 4 };
581         final ToStringBuilder toStringBuilder = new ToStringBuilder(base);
582         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append(array).toString());
583         assertEquals("{\"shortArray\":[1,2,-3,4]}", toStringBuilder.append("shortArray", array).toString());
584         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
585         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((long[]) null).toString());
586         assertThrows(UnsupportedOperationException.class, () -> toStringBuilder.append((Object) array).toString());
587     }
588 }