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