001/**
002 * Copyright (c) 2004-2011 QOS.ch
003 * All rights reserved.
004 *
005 * Permission is hereby granted, free  of charge, to any person obtaining
006 * a  copy  of this  software  and  associated  documentation files  (the
007 * "Software"), to  deal in  the Software without  restriction, including
008 * without limitation  the rights to  use, copy, modify,  merge, publish,
009 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
010 * permit persons to whom the Software  is furnished to do so, subject to
011 * the following conditions:
012 *
013 * The  above  copyright  notice  and  this permission  notice  shall  be
014 * included in all copies or substantial portions of the Software.
015 *
016 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
017 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
018 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
019 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
020 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
021 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
022 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023 *
024 */
025package org.slf4j.helpers;
026
027import java.util.Arrays;
028
029import org.junit.Test;
030
031import static org.junit.Assert.*;
032
033/**
034 * @author Ceki Gulcu
035 */
036public class MessageFormatterTest {
037
038    Integer i1 = Integer.valueOf(1);
039    Integer i2 = Integer.valueOf(2);
040    Integer i3 = Integer.valueOf(3);
041    Integer[] ia0 = new Integer[] { i1, i2, i3 };
042    Integer[] ia1 = new Integer[] { Integer.valueOf(10), Integer.valueOf(20), Integer.valueOf(30) };
043
044    String result;
045
046    @Test
047    public void testNull() {
048        result = MessageFormatter.format(null, i1).getMessage();
049        assertEquals(null, result);
050    }
051
052    @Test
053    public void testParamaterContainingAnAnchor() {
054        result = MessageFormatter.format("Value is {}.", "[{}]").getMessage();
055        assertEquals("Value is [{}].", result);
056
057        result = MessageFormatter.format("Values are {} and {}.", i1, "[{}]").getMessage();
058        assertEquals("Values are 1 and [{}].", result);
059    }
060    
061    @Test
062    public void nullParametersShouldBeHandledWithoutBarfing() {
063        result = MessageFormatter.format("Value is {}.", null).getMessage();
064        assertEquals("Value is null.", result);
065
066        result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null).getMessage();
067        assertEquals("Val1 is null, val2 is null.", result);
068
069        result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null).getMessage();
070        assertEquals("Val1 is 1, val2 is null.", result);
071
072        result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2).getMessage();
073        assertEquals("Val1 is null, val2 is 2.", result);
074
075        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, null }).getMessage();
076        assertEquals("Val1 is null, val2 is null, val3 is null", result);
077
078        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, i2, i3 }).getMessage();
079        assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
080
081        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, i3 }).getMessage();
082        assertEquals("Val1 is null, val2 is null, val3 is 3", result);
083    }
084
085    @Test
086    public void verifyOneParameterIsHandledCorrectly() {
087        result = MessageFormatter.format("Value is {}.", i3).getMessage();
088        assertEquals("Value is 3.", result);
089
090        result = MessageFormatter.format("Value is {", i3).getMessage();
091        assertEquals("Value is {", result);
092
093        result = MessageFormatter.format("{} is larger than 2.", i3).getMessage();
094        assertEquals("3 is larger than 2.", result);
095
096        result = MessageFormatter.format("No subst", i3).getMessage();
097        assertEquals("No subst", result);
098
099        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}