aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyleTest.java
blob: 372e2d849804b78897a719a4be2891f62161b941 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.lang3.builder;

import static org.junit.Assert.assertEquals;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

/**
 */
public class MultilineRecursiveToStringStyleTest {

    private final String BR = System.lineSeparator();

    @Test
    public void simpleObject() {
        final Transaction tx = new Transaction("2014.10.15", 100);
        final String expected = getClassPrefix(tx) + "[" + BR
                        + "  amount=100.0," + BR
                        + "  date=2014.10.15" + BR
                        + "]";
        assertEquals(expected, toString(tx));
    }

    @Test
    public void nestedElements() {
        final Customer customer = new Customer("Douglas Adams");
        final Bank bank = new Bank("ASF Bank");
        customer.bank = bank;
        final String exp = getClassPrefix(customer) + "[" + BR
                   + "  name=Douglas Adams," + BR
                   + "  bank=" + getClassPrefix(bank) + "[" + BR
                   + "    name=ASF Bank" + BR
                   + "  ]," + BR
                   + "  accounts=<null>" + BR
                   + "]";
        assertEquals(exp, toString(customer));
    }

    @Test
    public void nestedAndArray() {
        final Account acc = new Account();
        final Transaction tx1 = new Transaction("2014.10.14", 100);
        final Transaction tx2 = new Transaction("2014.10.15", 50);
        acc.transactions.add(tx1);
        acc.transactions.add(tx2);
        final String expected = getClassPrefix(acc) + "[" + BR
                        + "  owner=<null>," + BR
                        + "  transactions=" + getClassPrefix(acc.transactions) + "{" + BR
                        + "    " + getClassPrefix(tx1) + "[" + BR
                        + "      amount=100.0," + BR
                        + "      date=2014.10.14" + BR
                        + "    ]," + BR
                        + "    " + getClassPrefix(tx2) + "[" + BR
                        + "      amount=50.0," + BR
                        + "      date=2014.10.15" + BR
                        + "    ]" + BR
                        + "  }" + BR
                        + "]";
        assertEquals(expected, toString(acc));
    }

    @Test
    public void noArray() {
        final WithArrays wa = new WithArrays();
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void boolArray() {
        final WithArrays wa = new WithArrays();
        wa.boolArray = new boolean[] { true, false, true };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray={" + BR
                   + "    true," + BR
                   + "    false," + BR
                   + "    true" + BR
                   + "  }," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void charArray() {
        final WithArrays wa = new WithArrays();
        wa.charArray = new char[] { 'a', 'A' };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray={" + BR
                   + "    a," + BR
                   + "    A" + BR
                   + "  }," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void intArray() {
        final WithArrays wa = new WithArrays();
        wa.intArray = new int[] { 1, 2 };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray={" + BR
                   + "    1," + BR
                   + "    2" + BR
                   + "  }," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void doubleArray() {
        final WithArrays wa = new WithArrays();
        wa.doubleArray = new double[] { 1, 2 };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray={" + BR
                   + "    1.0," + BR
                   + "    2.0" + BR
                   + "  }," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void longArray() {
        final WithArrays wa = new WithArrays();
        wa.longArray = new long[] { 1L, 2L };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray={" + BR
                   + "    1," + BR
                   + "    2" + BR
                   + "  }," + BR
                   + "  stringArray=<null>" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }

    @Test
    public void stringArray() {
        final WithArrays wa = new WithArrays();
        wa.stringArray = new String[] { "a", "A" };
        final String exp = getClassPrefix(wa) + "[" + BR
                   + "  boolArray=<null>," + BR
                   + "  charArray=<null>," + BR
                   + "  intArray=<null>," + BR
                   + "  doubleArray=<null>," + BR
                   + "  longArray=<null>," + BR
                   + "  stringArray={" + BR
                   + "    a," + BR
                   + "    A" + BR
                   + "  }" + BR
                   + "]";
        assertEquals(exp, toString(wa));
    }


    @Test
    public void testLANG1319() throws Exception {
        final String[] stringArray = {"1", "2"};

        final String exp = getClassPrefix(stringArray) + "[" + BR
                + "  {" + BR
                + "    1," + BR
                + "    2" + BR
                + "  }" + BR
                + "]";
        assertEquals(exp, toString(stringArray));
    }

    private String getClassPrefix(final Object object) {
        return object.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(object));
    }

    private String toString(final Object object) {
        return new ReflectionToStringBuilder(object, new MultilineRecursiveToStringStyle()).toString();
    }

    static class WithArrays {
        boolean[] boolArray;
        char[] charArray;
        int[] intArray;
        double[] doubleArray;
        long[] longArray;
        String[] stringArray;
    }

    static class Bank {
        String name;

        Bank(final String name) {
            this.name = name;
        }
    }

    static class Customer {
        String name;
        Bank bank;
        List<Account> accounts;

        Customer(final String name) {
            this.name = name;
        }
    }

    static class Account {
        Customer owner;
        List<Transaction> transactions = new ArrayList<>();

        public double getBalance() {
            double balance = 0;
            for (final Transaction tx : transactions) {
                balance += tx.amount;
            }
            return balance;
        }
    }

    static class Transaction {
        double amount;
        String date;

        Transaction(final String datum, final double betrag) {
            this.date = datum;
            this.amount = betrag;
        }
    }

}