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 junit.framework.TestCase;
37  
38  /**
39   * @author Ceki Gulcu
40   * 
41   */
42  public class MessageFormatterTest extends TestCase {
43  
44    Integer i1 = new Integer(1);
45    Integer i2 = new Integer(2);
46    Integer i3 = new Integer(3);
47    Integer[] ia0 = new Integer[] { i1, i2, i3 };
48    Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20),
49        new Integer(30) };
50    
51    String result;
52    
53    
54    public void testNull() {
55      result = MessageFormatter.format(null, i1);
56      assertEquals(null, result);
57    }
58  
59    public void testNullParam() {
60      result = MessageFormatter.format("Value is {}.", null);
61      assertEquals("Value is null.", result);
62  
63      result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null);
64      assertEquals("Val1 is null, val2 is null.", result);
65  
66      result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null);
67      assertEquals("Val1 is 1, val2 is null.", result);
68  
69      result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2);
70      assertEquals("Val1 is null, val2 is 2.", result);
71  
72      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
73          new Integer[] { null, null, null });
74      assertEquals("Val1 is null, val2 is null, val3 is null", result);
75  
76      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
77          new Integer[] { null, i2, i3 });
78      assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
79  
80      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}",
81          new Integer[] { null, null, i3 });
82      assertEquals("Val1 is null, val2 is null, val3 is 3", result);
83    }
84  
85    public void testOneParameter() {
86      result = MessageFormatter.format("Value is {}.", i3);
87      assertEquals("Value is 3.", result);
88  
89      result = MessageFormatter.format("Value is {", i3);
90      assertEquals("Value is {", result);
91  
92      result = MessageFormatter.format("{} is larger than 2.", i3);
93      assertEquals("3 is larger than 2.", result);
94  
95      result = MessageFormatter.format("No subst", i3);
96      assertEquals("No subst", result);
97  
98      result = MessageFormatter.format("Incorrect {subst", i3);
99      assertEquals("Incorrect {subst", result);
100 
101     result = MessageFormatter.format("Value is {bla} {}", i3);
102     assertEquals("Value is {bla} 3", result);
103 
104     result = MessageFormatter.format("Escaped \\{} subst", i3);
105     assertEquals("Escaped {} subst", result);
106 
107     result = MessageFormatter.format("{Escaped", i3);
108     assertEquals("{Escaped", result);
109 
110     result = MessageFormatter.format("\\{}Escaped", i3);
111     assertEquals("{}Escaped", result);
112 
113     result = MessageFormatter.format("File name is {{}}.", "App folder.zip");
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");
119     assertEquals("File name is C:\\App folder.zip.", result);
120   }
121 
122   public void testTwoParameters() {
123     result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2);
124     assertEquals("Value 1 is smaller than 2.", result);
125 
126     result = MessageFormatter.format("Value {} is smaller than {}", i1, i2);
127     assertEquals("Value 1 is smaller than 2", result);
128 
129     result = MessageFormatter.format("{}{}", i1, i2);
130     assertEquals("12", result);
131 
132     result = MessageFormatter.format("Val1={}, Val2={", i1, i2);
133     assertEquals("Val1=1, Val2={", result);
134 
135     result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2);
136     assertEquals("Value 1 is smaller than {}", result);
137 
138     result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1,
139         i2);
140     assertEquals("Value 1 is smaller than {} tail", result);
141 
142     result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2);
143     assertEquals("Value 1 is smaller than \\{", result);
144 
145     result = MessageFormatter
146         .format("Value {} is smaller than {tail", i1, i2);
147     assertEquals("Value 1 is smaller than {tail", result);
148 
149     result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2);
150     assertEquals("Value {} is smaller than 1", result);
151   }
152 
153   
154   public void testExceptionInToString() {
155     Object o = new Object() {
156       public String toString() {
157         throw new IllegalStateException("a");
158       }
159     };
160     result = MessageFormatter.format("Troublesome object {}", o);
161     assertEquals("Troublesome object [FAILED toString()]", result);
162     
163   }
164   
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);
174     assertEquals(msg0, result);
175 
176     result = MessageFormatter.arrayFormat(msg1, args);
177     assertEquals(msg1, result);
178 
179     result = MessageFormatter.arrayFormat(msg2, args);
180     assertEquals(msg2, result);
181 
182     result = MessageFormatter.arrayFormat(msg3, args);
183     assertEquals(msg3, result);
184   }
185 
186   // tests the case when the parameters are supplied in a single array
187   public void testArrayFormat() {
188     result = MessageFormatter.arrayFormat(
189         "Value {} is smaller than {} and {}.", ia0);
190     assertEquals("Value 1 is smaller than 2 and 3.", result);
191 
192     result = MessageFormatter.arrayFormat("{}{}{}", ia0);
193     assertEquals("123", result);
194 
195     result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0);
196     assertEquals("Value 1 is smaller than 2.", result);
197 
198     result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0);
199     assertEquals("Value 1 is smaller than 2", result);
200 
201     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0);
202     assertEquals("Val=1, {, Val=2", result);
203 
204     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0);
205     assertEquals("Val=1, {, Val=2", result);
206 
207     result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0);
208     assertEquals("Val1=1, Val2={", result);
209   }
210 
211   public void testArrayValues() {
212     Integer p0 = i1;
213     Integer[] p1 = new Integer[] { i2, i3 };
214 
215     result = MessageFormatter.format("{}{}", p0, p1);
216     assertEquals("1[2, 3]", result);
217 
218     // Integer[]
219     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 });
220     assertEquals("a[2, 3]", result);
221 
222     // byte[]
223     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
224         new byte[] { 1, 2 } });
225     assertEquals("a[1, 2]", result);
226 
227     // int[]
228     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
229         new int[] { 1, 2 } });
230     assertEquals("a[1, 2]", result);
231 
232     // float[]
233     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
234         new float[] { 1, 2 } });
235     assertEquals("a[1.0, 2.0]", result);
236 
237     // double[]
238     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
239         new double[] { 1, 2 } });
240     assertEquals("a[1.0, 2.0]", result);
241 
242   }
243 
244   public void testMultiDimensionalArrayValues() {
245     Integer[][] multiIntegerA = new Integer[][] { ia0, ia1 };
246     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
247         multiIntegerA });
248     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
249 
250     int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
251     result = MessageFormatter.arrayFormat("{}{}",
252         new Object[] { "a", multiIntA });
253     assertEquals("a[[1, 2], [10, 20]]", result);
254 
255     float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
256     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
257         multiFloatA });
258     assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
259 
260     Object[][] multiOA = new Object[][] { ia0, ia1 };
261     result = MessageFormatter
262         .arrayFormat("{}{}", new Object[] { "a", multiOA });
263     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
264 
265     Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
266     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA });
267     assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]",
268         result);
269   }
270 
271   public void testCyclicArrays() {
272     {
273       Object[] cyclicA = new Object[1];
274       cyclicA[0] = cyclicA;
275       assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA));
276     }
277     {
278       Object[] a = new Object[2];
279       a[0] = i1;
280       Object[] c = new Object[] {i3, a};
281       Object[] b = new Object[] {i2, c};
282       a[1] = b;
283       assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a));
284     }
285   }
286 }