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    public void testNull() {
52      String result;
53      result = MessageFormatter.format(null, i1);
54      assertEquals(null, result);
55    }
56  
57    public void testNullParam() {
58      String result;
59  
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      String result;
87  
88      result = MessageFormatter.format("Value is {}.", i3);
89      assertEquals("Value is 3.", result);
90  
91      result = MessageFormatter.format("Value is {", i3);
92      assertEquals("Value is {", result);
93  
94      result = MessageFormatter.format("{} is larger than 2.", i3);
95      assertEquals("3 is larger than 2.", result);
96  
97      result = MessageFormatter.format("No subst", i3);
98      assertEquals("No subst", result);
99  
100     result = MessageFormatter.format("Incorrect {subst", i3);
101     assertEquals("Incorrect {subst", result);
102 
103     result = MessageFormatter.format("Value is {bla} {}", i3);
104     assertEquals("Value is {bla} 3", result);
105 
106     result = MessageFormatter.format("Escaped \\{} subst", i3);
107     assertEquals("Escaped {} subst", result);
108 
109     result = MessageFormatter.format("{Escaped", i3);
110     assertEquals("{Escaped", result);
111 
112     result = MessageFormatter.format("\\{}Escaped", i3);
113     assertEquals("{}Escaped", result);
114 
115     result = MessageFormatter.format("File name is {{}}.", "App folder.zip");
116     assertEquals("File name is {App folder.zip}.", result);
117 
118     // escaping the escape character
119     result = MessageFormatter
120         .format("File name is C:\\\\{}.", "App folder.zip");
121     assertEquals("File name is C:\\App folder.zip.", result);
122   }
123 
124   public void testTwoParameters() {
125     String result;
126 
127     result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2);
128     assertEquals("Value 1 is smaller than 2.", result);
129 
130     result = MessageFormatter.format("Value {} is smaller than {}", i1, i2);
131     assertEquals("Value 1 is smaller than 2", result);
132 
133     result = MessageFormatter.format("{}{}", i1, i2);
134     assertEquals("12", result);
135 
136     result = MessageFormatter.format("Val1={}, Val2={", i1, i2);
137     assertEquals("Val1=1, Val2={", result);
138 
139     result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2);
140     assertEquals("Value 1 is smaller than {}", result);
141 
142     result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1,
143         i2);
144     assertEquals("Value 1 is smaller than {} tail", result);
145 
146     result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2);
147     assertEquals("Value 1 is smaller than \\{", result);
148 
149     result = MessageFormatter
150         .format("Value {} is smaller than {tail", i1, i2);
151     assertEquals("Value 1 is smaller than {tail", result);
152 
153     result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2);
154     assertEquals("Value {} is smaller than 1", result);
155   }
156 
157   public void testNullArray() {
158     String result;
159 
160     String msg0 = "msg0";
161     String msg1 = "msg1 {}";
162     String msg2 = "msg2 {} {}";
163     String msg3 = "msg3 {} {} {}";
164 
165     Object[] args = null;
166 
167     result = MessageFormatter.arrayFormat(msg0, args);
168     assertEquals(msg0, result);
169 
170     result = MessageFormatter.arrayFormat(msg1, args);
171     assertEquals(msg1, result);
172 
173     result = MessageFormatter.arrayFormat(msg2, args);
174     assertEquals(msg2, result);
175 
176     result = MessageFormatter.arrayFormat(msg3, args);
177     assertEquals(msg3, result);
178   }
179 
180   // tests the case when the parameters are supplied in a single array
181   public void testArrayFormat() {
182     String result;
183 
184     result = MessageFormatter.arrayFormat(
185         "Value {} is smaller than {} and {}.", ia0);
186     assertEquals("Value 1 is smaller than 2 and 3.", result);
187 
188     result = MessageFormatter.arrayFormat("{}{}{}", ia0);
189     assertEquals("123", result);
190 
191     result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0);
192     assertEquals("Value 1 is smaller than 2.", result);
193 
194     result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0);
195     assertEquals("Value 1 is smaller than 2", result);
196 
197     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0);
198     assertEquals("Val=1, {, Val=2", result);
199 
200     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0);
201     assertEquals("Val=1, {, Val=2", result);
202 
203     result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0);
204     assertEquals("Val1=1, Val2={", result);
205   }
206 
207   public void testArrayValues() {
208     String result;
209     Integer p0 = i1;
210     Integer[] p1 = new Integer[] { i2, i3 };
211 
212     result = MessageFormatter.format("{}{}", p0, p1);
213     assertEquals("1[2, 3]", result);
214 
215     // Integer[]
216     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 });
217     assertEquals("a[2, 3]", result);
218 
219     // byte[]
220     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
221         new byte[] { 1, 2 } });
222     assertEquals("a[1, 2]", result);
223 
224     // int[]
225     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
226         new int[] { 1, 2 } });
227     assertEquals("a[1, 2]", result);
228 
229     // float[]
230     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
231         new float[] { 1, 2 } });
232     assertEquals("a[1.0, 2.0]", result);
233 
234     // double[]
235     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
236         new double[] { 1, 2 } });
237     assertEquals("a[1.0, 2.0]", result);
238 
239   }
240 
241   public void testMultiDimensionalArrayValues() {
242     String result;
243 
244     Integer[][] multiIntegerA = new Integer[][] { ia0, ia1 };
245     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
246         multiIntegerA });
247     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
248 
249     int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
250     result = MessageFormatter.arrayFormat("{}{}",
251         new Object[] { "a", multiIntA });
252     assertEquals("a[[1, 2], [10, 20]]", result);
253 
254     float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
255     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a",
256         multiFloatA });
257     assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
258 
259     Object[][] multiOA = new Object[][] { ia0, ia1 };
260     result = MessageFormatter
261         .arrayFormat("{}{}", new Object[] { "a", multiOA });
262     assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
263 
264     Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
265     result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA });
266     assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]",
267         result);
268   }
269 
270   public void testCyclicArrays() {
271     {
272       Object[] cyclicA = new Object[1];
273       cyclicA[0] = cyclicA;
274       assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA));
275     }
276     {
277       Object[] a = new Object[2];
278       a[0] = i1;
279       Object[] c = new Object[] {i3, a};
280       Object[] b = new Object[] {i2, c};
281       a[1] = b;
282       assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a));
283     }
284   }
285 }