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.assertEquals;
21
22 import org.apache.commons.lang3.AbstractLangTest;
23 import org.apache.commons.lang3.ObjectUtils;
24 import org.apache.commons.lang3.StringUtils;
25 import org.junit.jupiter.api.Test;
26
27 /**
28 * Tests that nested reflection calls preserve visited objects until the enclosing output is complete.
29 */
30 class ReflectionToStringBuilderNestedTraversalTest extends AbstractLangTest {
31
32 private static final class Holder {
33
34 private final Object child;
35
36 private Holder(final Object child) {
37 this.child = child;
38 }
39
40 @Override
41 public String toString() {
42 return ReflectionToStringBuilder.toString(this, new RecursiveToStringStyle());
43 }
44 }
45
46 private final Object shared = new Object();
47
48 private void assertSharedChildDetailedOnce(final String output) {
49 // Expect "child=java.lang.Object@<id>[]" in the first holder and "child=java.lang.Object@<id>" in the second.
50 // The empty brackets are the detailed rendering of an Object with no fields. The second occurrence
51 // must use only the identity because the same child has already been visited in this outer output.
52 final String childField = "child=" + ObjectUtils.identityToString(shared);
53 assertEquals(2, StringUtils.countMatches(output, childField), output);
54 assertEquals(1, StringUtils.countMatches(output, childField + "[]"), output);
55 }
56
57 @Test
58 void testNestedReflectionInManualBuilder() {
59 // The outer style does not mark the holders visited, so the first nested call starts with an empty visited set.
60 // Both append calls belong to one outer output, so the first holder's nested reflection call
61 // must preserve the shared child's visited mark for the second holder.
62 // @formatter:off
63 final String output = new ToStringBuilder(new Object(), ToStringStyle.DEFAULT_STYLE)
64 .append("first", new Holder(shared))
65 .append("second", new Holder(shared))
66 .toString();
67 // @formatter:on
68 assertSharedChildDetailedOnce(output);
69 }
70
71 @Test
72 void testNestedReflectionInReflectionBuilder() {
73 // Both array elements belong to one outer reflection call. Completing the first holder's nested call
74 // must preserve the shared child's visited mark so only the first element renders that child in detail.
75 final Object[] holders = { new Holder(shared), new Holder(shared) };
76 final String output = ReflectionToStringBuilder.toString(holders, ToStringStyle.DEFAULT_STYLE);
77 assertSharedChildDetailedOnce(output);
78 }
79 }