1   /* 
2    * Copyright (c) 2004-2005 SLF4J.ORG
3    * Copyright (c) 2004-2005 QOS.CH
4    * 
5    * All rights reserved.
6    * 
7    * Permission is hereby granted, free of charge, to any person obtaining
8    * a copy of this software and associated documentation files (the
9    * "Software"), to  deal in  the Software without  restriction, including
10   * without limitation  the rights to  use, copy, modify,  merge, publish,
11   * distribute, and/or sell copies of  the Software, and to permit persons
12   * to whom  the Software is furnished  to do so, provided  that the above
13   * copyright notice(s) and this permission notice appear in all copies of
14   * the  Software and  that both  the above  copyright notice(s)  and this
15   * permission notice appear in supporting documentation.
16   * 
17   * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
18   * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
19   * MERCHANTABILITY, FITNESS FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT
20   * OF  THIRD PARTY  RIGHTS. IN  NO EVENT  SHALL THE  COPYRIGHT  HOLDER OR
21   * HOLDERS  INCLUDED IN  THIS  NOTICE BE  LIABLE  FOR ANY  CLAIM, OR  ANY
22   * SPECIAL INDIRECT  OR CONSEQUENTIAL DAMAGES, OR  ANY DAMAGES WHATSOEVER
23   * RESULTING FROM LOSS  OF USE, DATA OR PROFITS, WHETHER  IN AN ACTION OF
24   * CONTRACT, NEGLIGENCE  OR OTHER TORTIOUS  ACTION, ARISING OUT OF  OR IN
25   * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26   * 
27   * Except as  contained in  this notice, the  name of a  copyright holder
28   * shall not be used in advertising or otherwise to promote the sale, use
29   * or other dealings in this Software without prior written authorization
30   * of the copyright holder.
31   *
32   */
33  
34  package org.slf4j.helpers;
35  
36  import java.util.Arrays;
37  
38  import junit.framework.TestCase;
39  
40  /**
41   * @author Ceki Gulcu
42   * 
43   */
44  public class MessageFormatterTest extends TestCase {
45  
46    Integer i1 = new Integer(1);
47    Integer i2 = new Integer(2);
48    Integer i3 = new Integer(3);
49    Integer[] ia0 = new Integer[] { i1, i2, i3 };
50    Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20),
51        new Integer(30) };
52  
53    String result;
54  
55    public void testNull() {
56      result = MessageFormatter.format(null, i1).getMessage();
57      assertEquals(null, result);
58    }
59  
60    public void testNullParam() {
61      result = MessageFormatter.format("Value is {}.", null).getMessage();
62      assertEquals("Value is null.", result);
63  
64      result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null)
65          .getMessage();
66      assertEquals("Val1 is null, val2 is null.", result);
67  
68      result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null)
69          .getMessage();
70      assertEquals("Val1 is 1, val2 is null.", result);
71  
72      result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2)
73          .getMessage();
74      assertEquals("Val1 is null, val2 is 2.", result);
75  
76      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
77          new Integer[] { null, null, null }).getMessage();
78      assertEquals("Val1 is null, val2 is null, val3 is null", result);
79  
80      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
81          new Integer[] { null, i2, i3 }).getMessage();
82      assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
83  
84      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
85          new Integer[] { null, null, i3 }).getMessage();
86      assertEquals("Val1 is null, val2 is null, val3 is 3", result);
87    }
88  
89    public void testOneParameter() {
90      result = MessageFormatter.format("Value is {}.", i3).getMessage();
91      assertEquals("Value is 3.", result);
92  
93      result = MessageFormatter.format("Value is {", i3).getMessage();
94      assertEquals("Value is {", result);
95  
96      result = MessageFormatter.format("{} is larger than 2.", i3).getMessage();
97      assertEquals("3 is larger than 2.", result);
98  
99      result = MessageFormatter.format("No subst", i3).getMessage();
100     assertEquals("No subst", result);
101 
102     result = MessageFormatter.format("Incorrect {subst", i3).getMessage();
103     assertEquals("Incorrect {subst", result);
104 
105     result = MessageFormatter.format("Value is {bla} {}", i3).getMessage();
106     assertEquals("Value is {bla} 3", result);
107 
108     result = MessageFormatter.format("Escaped \\{} subst", i3).getMessage();
109     assertEquals("Escaped {} subst", result);
110 
111     result = MessageFormatter.format("{Escaped", i3).getMessage();
112     assertEquals("{Escaped", result);
113 
114     result = MessageFormatter.format("\\{}Escaped", i3).getMessage();
115     assertEquals("{}Escaped", result);
116 
117     result = MessageFormatter.format("File name is {{}}.", "App folder.zip")
118         .getMessage();
119     assertEquals("File name is {App folder.zip}.", result);
120 
121     // escaping the escape character
122     result = MessageFormatter
123         .format("File name is C:\\\\{}.", "App folder.zip").getMessage();
124     assertEquals("File name is C:\\App folder.zip.", result);
125   }
126 
127   public void testTwoParameters() {
128     result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2)
129         .getMessage();
130     assertEquals("Value 1 is smaller than 2.", result);
131 
132     result = MessageFormatter.format("Value {} is smaller than {}", i1, i2)
133         .getMessage();
134     assertEquals("Value 1 is smaller than 2", result);
135 
136     result = MessageFormatter.format("{}{}", i1, i2).getMessage();
137     assertEquals("12", result);
138 
139     result = MessageFormatter.format("Val1={}, Val2={", i1, i2).getMessage();
140     assertEquals("Val1=1, Val2={", result);
141 
142     result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2)
143         .getMessage();
144     assertEquals("Value 1 is smaller than {}", result);
145 
146     result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1,
147         i2).getMessage();
148     assertEquals("Value 1 is smaller than {} tail", result);
149 
150     result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2)
151         .getMessage();
152     assertEquals("Value 1 is smaller than \\{", result);
153 
154     result = MessageFormatter.format("Value {} is smaller than {tail", i1, i2)
155         .getMessage();
156     assertEquals("Value 1 is smaller than {tail", result);
157 
158     result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2)
159         .getMessage();
160     assertEquals("Value {} is smaller than 1", result);
161   }
162 
163   public void testExceptionIn_toString() {
164     Object o = new Object() {
165       public String toString() {
166         throw new IllegalStateException("a");
167       }
168     };
169     result = MessageFormatter.format("Troublesome object {}", o).getMessage();
170     assertEquals("Troublesome object [FAILED toString()]", result);
171 
172   }
173 
174   public void testNullArray() {
175     String msg0 = "msg0";
176     String msg1 = "msg1 {}";
177     String msg2 = "msg2 {} {}";
178     String msg3 = "msg3 {} {} {}";
179 
180     Object[] args = null;
181 
182     result = MessageFormatter.arrayFormat(msg0, args).getMessage();
183     assertEquals(msg0, result);
184 
185     result = MessageFormatter.arrayFormat(msg1, args).getMessage();
186     assertEquals(msg1, result);
187 
188     result = MessageFormatter.arrayFormat(msg2, args).getMessage();
189     assertEquals(msg2, result);
190 
191     result = MessageFormatter.arrayFormat(msg3, args).getMessage();
192     assertEquals(msg3, result);
193   }
194 
195   // tests the case when the parameters are supplied in a single array
196   public void testArrayFormat() {
197     result = MessageFormatter.arrayFormat(
198         "Value {} is smaller than {} and {}.", ia0).getMessage();
199     assertEquals("Value 1 is smaller than 2 and 3.", result);
200 
201     result = MessageFormatter.arrayFormat("{}{}{}", ia0).getMessage();
202     assertEquals("123", result);
203 
204     result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0)
205         .getMessage();
206     assertEquals("Value 1 is smaller than 2.", result);
207 
208     result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0)
209         .getMessage();
210     assertEquals("Value 1 is smaller than 2", result);
211 
212     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0)
213         .getMessage();
214     assertEquals("Val=1, {, Val=2", result);
215 
216     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0)
217         .getMessage();
218     assertEquals("Val=1, {, Val=2", result);
219 
220     result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0).getMessage();
221     assertEquals("Val1=1, Val2={", result);
222   }
223 
224   public void testArrayValues() {
225     Integer p0 = i1;
226     Integer[] p1 = new Integer[] { i2, i3 };
227 
228     result = MessageFormatter.format("{}{}", p0, p1).getMessage();
229     assertEquals("1[2, 3]", result);
230 
231     // Integer[]
232     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 })
233         .getMessage();
234     assertEquals("a[2, 3]", result);
235 
236     // byte[]
237     result = MessageFormatter.arrayFormat("{}{}",
238         new Object[] { "a", new byte[] { 1, 2 } }).getMessage();
239     assertEquals("a[1, 2]", result);
240 
241     // int[]
242     result = MessageFormatter.arrayFormat("{}{}",
243         new Object[] { "a", new int[] { 1, 2 } }).getMessage();
244     assertEquals("a[1, 2]", result);
245 
246     // float[]
247     result = MessageFormatter.arrayFormat("{}{}",
248         new Object[] { "a", new float[] { 1, 2 } }).getMessage();
249     assertEquals("a[1.0, 2.0]", result);
250 
251     // double[]
252     result = MessageFormatter.arrayFormat("{}{}",
253         new Object[] { "a", new double[] { 1, 2 } }).getMessage();
254     assertEquals("a[1.0, 2.0]", result);
255 
256   }
257 
258   public void testMultiDimensionalArrayValues() {
259     Integer[][] multiIntegerA = new Integer[][] { ia0, ia1 };
260     result = MessageFormatter.arrayFormat("{}{}",
261         new Object[] { "a", multiIntegerA }).getMessage();
262     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
263 
264     int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
265     result = MessageFormatter.arrayFormat("{}{}",
266         new Object[] { "a", multiIntA }).getMessage();
267     assertEquals("a[[1, 2], [10, 20]]", result);
268 
269     float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
270     result = MessageFormatter.arrayFormat("{}{}",
271         new Object[] { "a", multiFloatA }).getMessage();
272     assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
273 
274     Object[][] multiOA = new Object[][] { ia0, ia1 };
275     result = MessageFormatter
276         .arrayFormat("{}{}", new Object[] { "a", multiOA }).getMessage();
277     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
278 
279     Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
280     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA })
281         .getMessage();
282     assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]",
283         result);
284   }
285 
286   public void testCyclicArrays() {
287     {
288       Object[] cyclicA = new Object[1];
289       cyclicA[0] = cyclicA;
290       assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA)
291           .getMessage());
292     }
293     {
294       Object[] a = new Object[2];
295       a[0] = i1;
296       Object[] c = new Object[] { i3, a };
297       Object[] b = new Object[] { i2, c };
298       a[1] = b;
299       assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat(
300           "{}{}", a).getMessage());
301     }
302   }
303 
304   public void testArrayThrowable() {
305     FormattingTuple ft;
306     Throwable t = new Throwable();
307     Object[] ia = new Object[] { i1, i2, i3, t };
308     Object[] iaWitness = new Object[] { i1, i2, i3 };
309 
310     ft = MessageFormatter
311         .arrayFormat("Value {} is smaller than {} and {}.", ia);
312     assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage());
313     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
314     assertEquals(t, ft.getThrowable());
315 
316     ft = MessageFormatter.arrayFormat("{}{}{}", ia);
317     assertEquals("123", ft.getMessage());
318     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
319     assertEquals(t, ft.getThrowable());
320 
321     ft = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
322     assertEquals("Value 1 is smaller than 2.", ft.getMessage());
323     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
324     assertEquals(t, ft.getThrowable());
325 
326     ft = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
327     assertEquals("Value 1 is smaller than 2", ft.getMessage());
328     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
329     assertEquals(t, ft.getThrowable());
330 
331     ft = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
332     assertEquals("Val=1, {, Val=2", ft.getMessage());
333     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
334     assertEquals(t, ft.getThrowable());
335 
336     ft = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
337     assertEquals("Val=1, \\{, Val=2", ft.getMessage());
338     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
339     assertEquals(t, ft.getThrowable());
340 
341     ft = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
342     assertEquals("Val1=1, Val2={", ft.getMessage());
343     assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
344     assertEquals(t, ft.getThrowable());
345 
346     ft = MessageFormatter.arrayFormat(
347         "Value {} is smaller than {} and {} -- {} .", ia);
348     assertEquals("Value 1 is smaller than 2 and 3 -- " + t.toString() + " .",
349         ft.getMessage());
350     assertTrue(Arrays.equals(ia, ft.getArgArray()));
351     assertNull(ft.getThrowable());
352 
353     ft = MessageFormatter.arrayFormat("{}{}{}{}", ia);
354     assertEquals("123" + t.toString(), ft.getMessage());
355     assertTrue(Arrays.equals(ia, ft.getArgArray()));
356     assertNull(ft.getThrowable());
357   }
358 }