View Javadoc

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