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  
18  package org.apache.commons.lang3.builder;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   */
29  class MultilineRecursiveToStringStyleTest extends AbstractBuilderTest {
30  
31      static class Account {
32          Customer owner;
33          List<Transaction> transactions = new ArrayList<>();
34  
35          public double getBalance() {
36              double balance = 0;
37              for (final Transaction tx : transactions) {
38                  balance += tx.amount;
39              }
40              return balance;
41          }
42      }
43      static class Bank {
44          String name;
45  
46          Bank(final String name) {
47              this.name = name;
48          }
49      }
50  
51      static class Customer {
52          String name;
53          Bank bank;
54          List<Account> accounts;
55  
56          Customer(final String name) {
57              this.name = name;
58          }
59      }
60  
61      static class Transaction {
62          double amount;
63          String date;
64  
65          Transaction(final String datum, final double betrag) {
66              this.date = datum;
67              this.amount = betrag;
68          }
69      }
70  
71      static class WithArrays {
72          boolean[] boolArray;
73          byte[] byteArray;
74          char[] charArray;
75          double[] doubleArray;
76          float[] floatArray;
77          int[] intArray;
78          long[] longArray;
79          short[] shortArray;
80          String[] stringArray;
81      }
82  
83      private enum WithArraysTestType {
84          NONE, BOOLEAN, BYTE, CHAR, DOUBLE, FLOAT, INT, LONG, SHORT, STRING
85      }
86  
87      private static final String LS = System.lineSeparator();
88  
89      private static final String BASE_WITH_ARRAYS_TO_STRING = "[" + LS
90              + "  boolArray=#BOOLEAN#," + LS
91              + "  byteArray=#BYTE#," + LS
92              + "  charArray=#CHAR#," + LS
93              + "  doubleArray=#DOUBLE#," + LS
94              + "  floatArray=#FLOAT#," + LS
95              + "  intArray=#INT#," + LS
96              + "  longArray=#LONG#," + LS
97              + "  shortArray=#SHORT#," + LS
98              + "  stringArray=#STRING#" + LS
99              + "]";
100 
101     @Test
102     public void boolArray() {
103         final WithArrays wa = new WithArrays();
104         wa.boolArray = new boolean[] { true, false, true };
105         final String exp = getExpectedToString(
106                 wa, WithArraysTestType.BOOLEAN,
107                 "{" + LS
108                 + "    true," + LS
109                 + "    false," + LS
110                 + "    true" + LS
111                 + "  }");
112         assertEqualsIfAccessible(exp, toString(wa));
113     }
114 
115     @Test
116     public void byteArray() {
117         final WithArrays wa = new WithArrays();
118         wa.byteArray = new byte[] { 1, 2 };
119         final String exp = getExpectedToString(
120                 wa, WithArraysTestType.BYTE,
121                 "{" + LS
122                 + "    1," + LS
123                 + "    2" + LS
124                 + "  }");
125         assertEqualsIfAccessible(exp, toString(wa));
126     }
127 
128     @Test
129     public void charArray() {
130         final WithArrays wa = new WithArrays();
131         wa.charArray = new char[] { 'a', 'A' };
132         final String exp = getExpectedToString(
133                 wa, WithArraysTestType.CHAR,
134                 "{" + LS
135                 + "    a," + LS
136                 + "    A" + LS
137                 + "  }");
138         assertEqualsIfAccessible(exp, toString(wa));
139     }
140 
141     @Test
142     public void doubleArray() {
143         final WithArrays wa = new WithArrays();
144         wa.doubleArray = new double[] { 1, 2 };
145         final String exp = getExpectedToString(
146                 wa, WithArraysTestType.DOUBLE,
147                 "{" + LS
148                 + "    1.0," + LS
149                 + "    2.0" + LS
150                 + "  }");
151         assertEqualsIfAccessible(exp, toString(wa));
152     }
153 
154     @Test
155     public void floatArray() {
156         final WithArrays wa = new WithArrays();
157         wa.floatArray = new float[] { 1f, 2f };
158         final String exp = getExpectedToString(
159                 wa, WithArraysTestType.FLOAT,
160                 "{" + LS
161                 + "    1.0," + LS
162                 + "    2.0" + LS
163                 + "  }");
164         assertEqualsIfAccessible(exp, toString(wa));
165     }
166 
167     private String getClassPrefix(final Object object) {
168         return object.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(object));
169     }
170 
171     /**
172      * Gets the expected string representation for the given WithArrays instance.
173      * @param wa                 Instance
174      * @param arrayType          Type - empty used to indicate expect all nulls
175      * @param expectedArrayValue Expected value for the array type
176      * @return expected toString
177      */
178     private String getExpectedToString(final WithArrays wa, final WithArraysTestType arrayType, final String expectedArrayValue) {
179         return getClassPrefix(wa)
180                 + BASE_WITH_ARRAYS_TO_STRING
181                 .replace("#" + arrayType + "#", expectedArrayValue)
182                 .replaceAll("#[A-Z]+#", "<null>");
183     }
184 
185     @Test
186     public void intArray() {
187         final WithArrays wa = new WithArrays();
188         wa.intArray = new int[] { 1, 2 };
189         final String exp = getExpectedToString(
190                 wa, WithArraysTestType.INT,
191                 "{" + LS
192                 + "    1," + LS
193                 + "    2" + LS
194                 + "  }");
195         assertEqualsIfAccessible(exp, toString(wa));
196     }
197 
198     @Test
199     public void longArray() {
200         final WithArrays wa = new WithArrays();
201         wa.longArray = new long[] { 1L, 2L };
202         final String exp = getExpectedToString(
203                 wa, WithArraysTestType.LONG,
204                 "{" + LS
205                 + "    1," + LS
206                 + "    2" + LS
207                 + "  }");
208         assertEqualsIfAccessible(exp, toString(wa));
209     }
210 
211     @Test
212     public void nestedAndArray() {
213         final Account acc = new Account();
214         final Transaction tx1 = new Transaction("2014.10.14", 100);
215         final Transaction tx2 = new Transaction("2014.10.15", 50);
216         acc.transactions.add(tx1);
217         acc.transactions.add(tx2);
218         final String expected = getClassPrefix(acc) + "[" + LS
219                         + "  owner=<null>," + LS
220                         + "  transactions=" + getClassPrefix(acc.transactions) + "{" + LS
221                         + "    " + getClassPrefix(tx1) + "[" + LS
222                         + "      amount=100.0," + LS
223                         + "      date=2014.10.14" + LS
224                         + "    ]," + LS
225                         + "    " + getClassPrefix(tx2) + "[" + LS
226                         + "      amount=50.0," + LS
227                         + "      date=2014.10.15" + LS
228                         + "    ]" + LS
229                         + "  }" + LS
230                         + "]";
231         assertEqualsIfAccessible(expected, toString(acc));
232     }
233 
234     @Test
235     public void nestedElements() {
236         final Customer customer = new Customer("Douglas Adams");
237         final Bank bank = new Bank("ASF Bank");
238         customer.bank = bank;
239         final String exp = getClassPrefix(customer) + "[" + LS
240                    + "  accounts=<null>," + LS
241                    + "  bank=" + getClassPrefix(bank) + "[" + LS
242                    + "    name=ASF Bank" + LS
243                    + "  ]," + LS
244                    + "  name=Douglas Adams" + LS
245                 + "]";
246         assertEqualsIfAccessible(exp, toString(customer));
247     }
248 
249     @Test
250     public void noArray() {
251         final WithArrays wa = new WithArrays();
252         final String exp = getExpectedToString(wa, WithArraysTestType.NONE, "");
253         assertEquals(exp, toString(wa));
254     }
255 
256     @Test
257     public void shortArray() {
258         final WithArrays wa = new WithArrays();
259         wa.shortArray = new short[] { 1, 2 };
260         final String exp = getExpectedToString(
261                 wa, WithArraysTestType.SHORT,
262                 "{" + LS
263                 + "    1," + LS
264                 + "    2" + LS
265                 + "  }");
266         assertEqualsIfAccessible(exp, toString(wa));
267     }
268 
269     @Test
270     public void simpleObject() {
271         final Transaction tx = new Transaction("2014.10.15", 100);
272         final String expected = getClassPrefix(tx) + "[" + LS
273                         + "  amount=100.0," + LS
274                         + "  date=2014.10.15" + LS
275                         + "]";
276         assertEqualsIfAccessible(expected, toString(tx));
277     }
278 
279     @Test
280     public void stringArray() {
281         final WithArrays wa = new WithArrays();
282         wa.stringArray = new String[] { "a", "A" };
283         final String exp = getExpectedToString(
284                 wa, WithArraysTestType.STRING,
285                 "{" + LS
286                 + "    a," + LS
287                 + "    A" + LS
288                 + "  }");
289         assertEqualsIfAccessible(exp, toString(wa));
290     }
291 
292     @Test
293     void testLANG1319() {
294         final String[] stringArray = {"1", "2"};
295 
296         final String exp = getClassPrefix(stringArray) + "[" + LS
297                 + "  {" + LS
298                 + "    1," + LS
299                 + "    2" + LS
300                 + "  }" + LS
301                 + "]";
302         assertEquals(exp, toString(stringArray));
303     }
304 
305     private String toString(final Object object) {
306         return new ReflectionToStringBuilder(object, new MultilineRecursiveToStringStyle()).toString();
307     }
308 
309 }