aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/apache/commons/lang3/builder/MultilineRecursiveToStringStyleTest.java
blob: 0353a2fc0712fd75b54240a54459bc3c84968f0b (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
 * 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.jupiter.api.Assertions.assertEquals;

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

import org.apache.commons.lang3.AbstractLangTest;
import org.junit.jupiter.api.Test;

/**
 */
public class MultilineRecursiveToStringStyleTest extends AbstractLangTest {

    private static final String LS = System.lineSeparator();
    private static final String BASE_WITH_ARRAYS_TO_STRING = "[" + LS
            + "  boolArray=#BOOLEAN#," + LS
            + "  byteArray=#BYTE#," + LS
            + "  charArray=#CHAR#," + LS
            + "  doubleArray=#DOUBLE#," + LS
            + "  floatArray=#FLOAT#," + LS
            + "  intArray=#INT#," + LS
            + "  longArray=#LONG#," + LS
            + "  shortArray=#SHORT#," + LS
            + "  stringArray=#STRING#" + LS
            + "]";

    @Test
    public void simpleObject() {
        final Transaction tx = new Transaction("2014.10.15", 100);
        final String expected = getClassPrefix(tx) + "[" + LS
                        + "  amount=100.0," + LS
                        + "  date=2014.10.15" + LS
                        + "]";
        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) + "[" + LS
                   + "  accounts=<null>," + LS
                   + "  bank=" + getClassPrefix(bank) + "[" + LS
                   + "    name=ASF Bank" + LS
                   + "  ]," + LS
                   + "  name=Douglas Adams" + LS
                + "]";
        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) + "[" + LS
                        + "  owner=<null>," + LS
                        + "  transactions=" + getClassPrefix(acc.transactions) + "{" + LS
                        + "    " + getClassPrefix(tx1) + "[" + LS
                        + "      amount=100.0," + LS
                        + "      date=2014.10.14" + LS
                        + "    ]," + LS
                        + "    " + getClassPrefix(tx2) + "[" + LS
                        + "      amount=50.0," + LS
                        + "      date=2014.10.15" + LS
                        + "    ]" + LS
                        + "  }" + LS
                        + "]";
        assertEquals(expected, toString(acc));
    }

    @Test
    public void noArray() {
        final WithArrays wa = new WithArrays();
        final String exp = getExpectedToString(wa, WithArraysTestType.NONE, "");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void boolArray() {
        final WithArrays wa = new WithArrays();
        wa.boolArray = new boolean[] { true, false, true };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.BOOLEAN,
                "{" + LS
                + "    true," + LS
                + "    false," + LS
                + "    true" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void byteArray() {
        final WithArrays wa = new WithArrays();
        wa.byteArray = new byte[] { 1, 2 };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.BYTE,
                "{" + LS
                + "    1," + LS
                + "    2" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void charArray() {
        final WithArrays wa = new WithArrays();
        wa.charArray = new char[] { 'a', 'A' };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.CHAR,
                "{" + LS
                + "    a," + LS
                + "    A" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void intArray() {
        final WithArrays wa = new WithArrays();
        wa.intArray = new int[] { 1, 2 };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.INT,
                "{" + LS
                + "    1," + LS
                + "    2" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void doubleArray() {
        final WithArrays wa = new WithArrays();
        wa.doubleArray = new double[] { 1, 2 };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.DOUBLE,
                "{" + LS
                + "    1.0," + LS
                + "    2.0" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void floatArray() {
        final WithArrays wa = new WithArrays();
        wa.floatArray = new float[] { 1f, 2f };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.FLOAT,
                "{" + LS
                + "    1.0," + LS
                + "    2.0" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void longArray() {
        final WithArrays wa = new WithArrays();
        wa.longArray = new long[] { 1L, 2L };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.LONG,
                "{" + LS
                + "    1," + LS
                + "    2" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void stringArray() {
        final WithArrays wa = new WithArrays();
        wa.stringArray = new String[] { "a", "A" };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.STRING,
                "{" + LS
                + "    a," + LS
                + "    A" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

    @Test
    public void shortArray() {
        final WithArrays wa = new WithArrays();
        wa.shortArray = new short[] { 1, 2 };
        final String exp = getExpectedToString(
                wa, WithArraysTestType.SHORT,
                "{" + LS
                + "    1," + LS
                + "    2" + LS
                + "  }");
        assertEquals(exp, toString(wa));
    }

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

        final String exp = getClassPrefix(stringArray) + "[" + LS
                + "  {" + LS
                + "    1," + LS
                + "    2" + LS
                + "  }" + LS
                + "]";
        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;
        byte[] byteArray;
        char[] charArray;
        double[] doubleArray;
        float[] floatArray;
        int[] intArray;
        long[] longArray;
        short[] shortArray;
        String[] stringArray;
    }

    /**
     * Create an expected to String for the given WithArraysInstance
     * @param wa                 Instance
     * @param arrayType          Type - empty used to indicate expect all nulls
     * @param expectedArrayValue Expected value for the array type
     * @return expected toString
     */
    private String getExpectedToString(final WithArrays wa, final WithArraysTestType arrayType, final String expectedArrayValue) {
        return getClassPrefix(wa)
                + BASE_WITH_ARRAYS_TO_STRING
                .replace("#" + arrayType + "#", expectedArrayValue)
                .replaceAll("#[A-Z]+#", "<null>");
    }

    private enum WithArraysTestType {
        NONE, BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT, STRING
    }

    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;
        }
    }

}