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 = Integer.valueOf(1);
39      Integer i2 = Integer.valueOf(2);
40      Integer i3 = Integer.valueOf(3);
41      Integer[] ia0 = new Integer[] { i1, i2, i3 };
42      Integer[] ia1 = new Integer[] { Integer.valueOf(10), Integer.valueOf(20), Integer.valueOf(30) };
43  
44      String result;
45  
46      @Test
47      public void testNull() {
48          result = MessageFormatter.format(null, i1).getMessage();
49          assertEquals(null, result);
50      }
51  
52      @Test
53      public void testParamaterContainingAnAnchor() {
54          result = MessageFormatter.format("Value is {}.", "[{}]").getMessage();
55          assertEquals("Value is [{}].", result);
56  
57          result = MessageFormatter.format("Values are {} and {}.", i1, "[{}]").getMessage();
58          assertEquals("Values are 1 and [{}].", result);
59      }
60      
61      @Test
62      public void nullParametersShouldBeHandledWithoutBarfing() {
63          result = MessageFormatter.format("Value is {}.", null).getMessage();
64          assertEquals("Value is null.", result);
65  
66          result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null).getMessage();
67          assertEquals("Val1 is null, val2 is null.", result);
68  
69          result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null).getMessage();
70          assertEquals("Val1 is 1, val2 is null.", result);
71  
72          result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2).getMessage();
73          assertEquals("Val1 is null, val2 is 2.", result);
74  
75          result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, null }).getMessage();
76          assertEquals("Val1 is null, val2 is null, val3 is null", result);
77  
78          result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, i2, i3 }).getMessage();
79          assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
80  
81          result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, i3 }).getMessage();
82          assertEquals("Val1 is null, val2 is null, val3 is 3", result);
83      }
84  
85      @Test
86      public void verifyOneParameterIsHandledCorrectly() {
87          result = MessageFormatter.format("Value is {}.", i3).getMessage();
88          assertEquals("Value is 3.", result);
89  
90          result = MessageFormatter.format("Value is {", i3).getMessage();
91          assertEquals("Value is {", result);
92  
93          result = MessageFormatter.format("{} is larger than 2.", i3).getMessage();
94          assertEquals("3 is larger than 2.", result);
95  
96          result = MessageFormatter.format("No subst", i3).getMessage();
97          assertEquals("No subst", result);
98  
99          result = MessageFormatter.format("Incorrect {subst", i3).getMessage();
100         assertEquals("Incorrect {subst", result);
101 
102         result = MessageFormatter.format("Value is {bla} {}", i3).getMessage();
103         assertEquals("Value is {bla} 3", result);
104 
105         result = MessageFormatter.format("Escaped \\{} subst", i3).getMessage();
106         assertEquals("Escaped {} subst", result);
107 
108         result = MessageFormatter.format("{Escaped", i3).getMessage();
109         assertEquals("{Escaped", result);
110 
111         result = MessageFormatter.format("\\{}Escaped", i3).getMessage();
112         assertEquals("{}Escaped", result);
113 
114         result = MessageFormatter.format("File name is {{}}.", "App folder.zip").getMessage();
115         assertEquals("File name is {App folder.zip}.", result);
116 
117         // escaping the escape character
118         result = MessageFormatter.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).getMessage();
125         assertEquals("Value 1 is smaller than 2.", result);
126 
127         result = MessageFormatter.format("Value {} is smaller than {}", i1, i2).getMessage();
128         assertEquals("Value 1 is smaller than 2", result);
129 
130         result = MessageFormatter.format("{}{}", i1, i2).getMessage();
131         assertEquals("12", result);
132 
133         result = MessageFormatter.format("Val1={}, Val2={", i1, i2).getMessage();
134         assertEquals("Val1=1, Val2={", result);
135 
136         result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2).getMessage();
137         assertEquals("Value 1 is smaller than {}", result);
138 
139         result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2).getMessage();
140         assertEquals("Value 1 is smaller than {} tail", result);
141 
142         result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2).getMessage();
143         assertEquals("Value 1 is smaller than \\{", result);
144 
145         result = MessageFormatter.format("Value {} is smaller than {tail", i1, i2).getMessage();
146         assertEquals("Value 1 is smaller than {tail", result);
147 
148         result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2).getMessage();
149         assertEquals("Value {} is smaller than 1", result);
150     }
151 
152     @Test
153     public void testExceptionIn_toString() {
154         Object o = new Object() {
155             public String toString() {
156                 throw new IllegalStateException("a");
157             }
158         };
159         result = MessageFormatter.format("Troublesome object {}", o).getMessage();
160         assertEquals("Troublesome object [FAILED toString()]", result);
161 
162     }
163 
164     @Test
165     public void testNullArray() {
166         String msg0 = "msg0";
167         String msg1 = "msg1 {}";
168         String msg2 = "msg2 {} {}";
169         String msg3 = "msg3 {} {} {}";
170 
171         Object[] args = null;
172 
173         result = MessageFormatter.arrayFormat(msg0, args).getMessage();
174         assertEquals(msg0, result);
175 
176         result = MessageFormatter.arrayFormat(msg1, args).getMessage();
177         assertEquals(msg1, result);
178 
179         result = MessageFormatter.arrayFormat(msg2, args).getMessage();
180         assertEquals(msg2, result);
181 
182         result = MessageFormatter.arrayFormat(msg3, args).getMessage();
183         assertEquals(msg3, result);
184     }
185 
186     // tests the case when the parameters are supplied in a single array
187     @Test
188     public void testArrayFormat() {
189         result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia0).getMessage();
190         assertEquals("Value 1 is smaller than 2 and 3.", result);
191 
192         result = MessageFormatter.arrayFormat("{}{}{}", ia0).getMessage();
193         assertEquals("123", result);
194 
195         result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0).getMessage();
196         assertEquals("Value 1 is smaller than 2.", result);
197 
198         result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0).getMessage();
199         assertEquals("Value 1 is smaller than 2", result);
200 
201         result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage();
202         assertEquals("Val=1, {, Val=2", result);
203 
204         result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage();
205         assertEquals("Val=1, {, Val=2", result);
206 
207         result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0).getMessage();
208         assertEquals("Val1=1, Val2={", result);
209     }
210 
211     @Test
212     public void testArrayValues() {
213         Integer p0 = i1;
214         Integer[] p1 = new Integer[] { i2, i3 };
215 
216         result = MessageFormatter.format("{}{}", p0, p1).getMessage();
217         assertEquals("1[2, 3]", result);
218 
219         // Integer[]
220         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 }).getMessage();
221         assertEquals("a[2, 3]", result);
222 
223         // byte[]
224         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new byte[] { 1, 2 } }).getMessage();
225         assertEquals("a[1, 2]", result);
226 
227         // int[]
228         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new int[] { 1, 2 } }).getMessage();
229         assertEquals("a[1, 2]", result);
230 
231         // float[]
232         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new float[] { 1, 2 } }).getMessage();
233         assertEquals("a[1.0, 2.0]", result);
234 
235         // double[]
236         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new double[] { 1, 2 } }).getMessage();
237         assertEquals("a[1.0, 2.0]", result);
238 
239     }
240 
241     @Test
242     public void testMultiDimensionalArrayValues() {
243         Integer[][] multiIntegerA = new Integer[][] { ia0, ia1 };
244         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntegerA }).getMessage();
245         assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
246 
247         int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
248         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntA }).getMessage();
249         assertEquals("a[[1, 2], [10, 20]]", result);
250 
251         float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
252         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiFloatA }).getMessage();
253         assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
254 
255         Object[][] multiOA = new Object[][] { ia0, ia1 };
256         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiOA }).getMessage();
257         assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
258 
259         Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
260         result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA }).getMessage();
261         assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]", result);
262     }
263 
264     @Test
265     public void testCyclicArrays() {
266         {
267             Object[] cyclicA = new Object[1];
268             cyclicA[0] = cyclicA;
269             assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA).getMessage());
270         }
271         {
272             Object[] a = new Object[2];
273             a[0] = i1;
274             Object[] c = new Object[] { i3, a };
275             Object[] b = new Object[] { i2, c };
276             a[1] = b;
277             assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a).getMessage());
278         }
279     }
280 
281     @Test
282     public void testArrayThrowable() {
283         FormattingTuple ft;
284         Throwable t = new Throwable();
285         Object[] ia = new Object[] { i1, i2, i3, t };
286         Object[] iaWitness = new Object[] { i1, i2, i3 };
287 
288         ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);
289         assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage());
290         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
291         assertEquals(t, ft.getThrowable());
292 
293         ft = MessageFormatter.arrayFormat("{}{}{}", ia);
294         assertEquals("123", ft.getMessage());
295         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
296         assertEquals(t, ft.getThrowable());
297 
298         ft = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
299         assertEquals("Value 1 is smaller than 2.", ft.getMessage());
300         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
301         assertEquals(t, ft.getThrowable());
302 
303         ft = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
304         assertEquals("Value 1 is smaller than 2", ft.getMessage());
305         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
306         assertEquals(t, ft.getThrowable());
307 
308         ft = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
309         assertEquals("Val=1, {, Val=2", ft.getMessage());
310         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
311         assertEquals(t, ft.getThrowable());
312 
313         ft = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
314         assertEquals("Val=1, \\{, Val=2", ft.getMessage());
315         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
316         assertEquals(t, ft.getThrowable());
317 
318         ft = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
319         assertEquals("Val1=1, Val2={", ft.getMessage());
320         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
321         assertEquals(t, ft.getThrowable());
322 
323         ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);
324         assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage());
325         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
326         assertEquals(t, ft.getThrowable());
327 
328         ft = MessageFormatter.arrayFormat("{}{}{}{}", ia);
329         assertEquals("123{}", ft.getMessage());
330         assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
331         assertEquals(t, ft.getThrowable());
332 
333         ft = MessageFormatter.arrayFormat("1={}", new Object[] { i1 }, t);
334         assertEquals("1=1", ft.getMessage());
335         assertTrue(Arrays.equals(new Object[] { i1 }, ft.getArgArray()));
336         assertEquals(t, ft.getThrowable());
337 
338     }
339 }