    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 = new Integer(1);
    039    Integer i2 = new Integer(2);
    040    Integer i3 = new Integer(3);
    041    Integer[] ia0 = new Integer[] { i1, i2, i3 };
    042    Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20), new Integer(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 nullParametersShouldBeHandledWithoutBarfing() {
    054        result = MessageFormatter.format("Value is {}.", null).getMessage();
    055        assertEquals("Value is null.", result);
    056
    057        result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null).getMessage();
    058        assertEquals("Val1 is null, val2 is null.", result);
    059
    060        result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null).getMessage();
    061        assertEquals("Val1 is 1, val2 is null.", result);
    062
    063        result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2).getMessage();
    064        assertEquals("Val1 is null, val2 is 2.", result);
    065
    066        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, null }).getMessage();
    067        assertEquals("Val1 is null, val2 is null, val3 is null", result);
    068
    069        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, i2, i3 }).getMessage();
    070        assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
    071
    072        result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, i3 }).getMessage();
    073        assertEquals("Val1 is null, val2 is null, val3 is 3", result);
    074    }
    075
    076    @Test
    077    public void verifyOneParameterIsHandledCorrectly() {
    078        result = MessageFormatter.format("Value is {}.", i3).getMessage();
    079        assertEquals("Value is 3.", result);
    080
    081        result = MessageFormatter.format("Value is {", i3).getMessage();
    082        assertEquals("Value is {", result);
    083
    084        result = MessageFormatter.format("{} is larger than 2.", i3).getMessage();
    085        assertEquals("3 is larger than 2.", result);
    086
    087        result = MessageFormatter.format("No subst", i3).getMessage();
    088        assertEquals("No subst", result);
    089
    090        result = MessageFormatter.format("Incorrect {subst", i3).getMessage();
    091        assertEquals("Incorrect {subst", result);
    092
    093        result = MessageFormatter.format("Value is {bla} {}", i3).getMessage();
    094        assertEquals("Value is {bla} 3", result);
    095
    096        result = MessageFormatter.format("Escaped \\{} subst", i3).getMessage();
    097        assertEquals("Escaped {} subst", result);
    098
    099        result = MessageFormatter.format("{Escaped", i3).getMessage();
    100        assertEquals("{Escaped", result);
    101
    102        result = MessageFormatter.format("\\{}Escaped", i3).getMessage();
    103        assertEquals("{}Escaped", result);
    104
    105        result = MessageFormatter.format("File name is {{}}.", "App folder.zip").getMessage();
    106        assertEquals("File name is {App folder.zip}.", result);
    107
    108        // escaping the escape character
    109        result = MessageFormatter.format("File name is C:\\\\{}.", "App folder.zip").getMessage();
    110        assertEquals("File name is C:\\App folder.zip.", result);
    111    }
    112
    113    @Test
    114    public void testTwoParameters() {
    115        result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2).getMessage();
    116        assertEquals("Value 1 is smaller than 2.", result);
    117
    118        result = MessageFormatter.format("Value {} is smaller than {}", i1, i2).getMessage();
    119        assertEquals("Value 1 is smaller than 2", result);
    120
    121        result = MessageFormatter.format("{}{}", i1, i2).getMessage();
    122        assertEquals("12", result);
    123
    124        result = MessageFormatter.format("Val1={}, Val2={", i1, i2).getMessage();
    125        assertEquals("Val1=1, Val2={", result);
    126
    127        result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2).getMessage();
    128        assertEquals("Value 1 is smaller than {}", result);
    129
    130        result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2).getMessage();
    131        assertEquals("Value 1 is smaller than {} tail", result);
    132
    133        result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2).getMessage();
    134        assertEquals("Value 1 is smaller than \\{", result);
    135
    136        result = MessageFormatter.format("Value {} is smaller than {tail", i1, i2).getMessage();
    137        assertEquals("Value 1 is smaller than {tail", result);
    138
    139        result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2).getMessage();
    140        assertEquals("Value {} is smaller than 1", result);
    141    }
    142
    143    @Test
    144    public void testExceptionIn_toString() {
    145        Object o = new Object() {
    146            public String toString() {
    147                throw new IllegalStateException("a");
    148            }
    149        };
    150        result = MessageFormatter.format("Troublesome object {}", o).getMessage();
    151        assertEquals("Troublesome object [FAILED toString()]", result);
    152
    153    }
    154
    155    @Test
    156    public void testNullArray() {
    157        String msg0 = "msg0";
    158        String msg1 = "msg1 {}";
    159        String msg2 = "msg2 {} {}";
    160        String msg3 = "msg3 {} {} {}";
    161
    162        Object[] args = null;
    163
    164        result = MessageFormatter.arrayFormat(msg0, args).getMessage();
    165        assertEquals(msg0, result);
    166
    167        result = MessageFormatter.arrayFormat(msg1, args).getMessage();
    168        assertEquals(msg1, result);
    169
    170        result = MessageFormatter.arrayFormat(msg2, args).getMessage();
    171        assertEquals(msg2, result);
    172
    173        result = MessageFormatter.arrayFormat(msg3, args).getMessage();
    174        assertEquals(msg3, result);
    175    }
    176
    177    // tests the case when the parameters are supplied in a single array
    178    @Test
    179    public void testArrayFormat() {
    180        result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia0).getMessage();
    181        assertEquals("Value 1 is smaller than 2 and 3.", result);
    182
    183        result = MessageFormatter.arrayFormat("{}{}{}", ia0).getMessage();
    184        assertEquals("123", result);
    185
    186        result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia0).getMessage();
    187        assertEquals("Value 1 is smaller than 2.", result);
    188
    189        result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia0).getMessage();
    190        assertEquals("Value 1 is smaller than 2", result);
    191
    192        result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage();
    193        assertEquals("Val=1, {, Val=2", result);
    194
    195        result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia0).getMessage();
    196        assertEquals("Val=1, {, Val=2", result);
    197
    198        result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia0).getMessage();
    199        assertEquals("Val1=1, Val2={", result);
    200    }
    201
    202    @Test
    203    public void testArrayValues() {
    204        Integer p0 = i1;
    205        Integer[] p1 = new Integer[] { i2, i3 };
    206
    207        result = MessageFormatter.format("{}{}", p0, p1).getMessage();
    208        assertEquals("1[2, 3]", result);
    209
    210        // Integer[]
    211        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", p1 }).getMessage();
    212        assertEquals("a[2, 3]", result);
    213
    214        // byte[]
    215        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new byte[] { 1, 2 } }).getMessage();
    216        assertEquals("a[1, 2]", result);
    217
    218        // int[]
    219        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new int[] { 1, 2 } }).getMessage();
    220        assertEquals("a[1, 2]", result);
    221
    222        // float[]
    223        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new float[] { 1, 2 } }).getMessage();
    224        assertEquals("a[1.0, 2.0]", result);
    225
    226        // double[]
    227        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", new double[] { 1, 2 } }).getMessage();
    228        assertEquals("a[1.0, 2.0]", result);
    229
    230    }
    231
    232    @Test
    233    public void testMultiDimensionalArrayValues() {
    234        Integer[][] multiIntegerA = new Integer[][] { ia0, ia1 };
    235        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntegerA }).getMessage();
    236        assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
    237
    238        int[][] multiIntA = new int[][] { { 1, 2 }, { 10, 20 } };
    239        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiIntA }).getMessage();
    240        assertEquals("a[[1, 2], [10, 20]]", result);
    241
    242        float[][] multiFloatA = new float[][] { { 1, 2 }, { 10, 20 } };
    243        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiFloatA }).getMessage();
    244        assertEquals("a[[1.0, 2.0], [10.0, 20.0]]", result);
    245
    246        Object[][] multiOA = new Object[][] { ia0, ia1 };
    247        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", multiOA }).getMessage();
    248        assertEquals("a[[1, 2, 3], [10, 20, 30]]", result);
    249
    250        Object[][][] _3DOA = new Object[][][] { multiOA, multiOA };
    251        result = MessageFormatter.arrayFormat("{}{}", new Object[] { "a", _3DOA }).getMessage();
    252        assertEquals("a[[[1, 2, 3], [10, 20, 30]], [[1, 2, 3], [10, 20, 30]]]", result);
    253    }
    254
    255    @Test
    256    public void testCyclicArrays() {
    257        {
    258            Object[] cyclicA = new Object[1];
    259            cyclicA[0] = cyclicA;
    260            assertEquals("[[...]]", MessageFormatter.arrayFormat("{}", cyclicA).getMessage());
    261        }
    262        {
    263            Object[] a = new Object[2];
    264            a[0] = i1;
    265            Object[] c = new Object[] { i3, a };
    266            Object[] b = new Object[] { i2, c };
    267            a[1] = b;
    268            assertEquals("1[2, [3, [1, [...]]]]", MessageFormatter.arrayFormat("{}{}", a).getMessage());
    269        }
    270    }
    271
    272    @Test
    273    public void testArrayThrowable() {
    274        FormattingTuple ft;
    275        Throwable t = new Throwable();
    276        Object[] ia = new Object[] { i1, i2, i3, t };
    277        Object[] iaWitness = new Object[] { i1, i2, i3 };
    278
    279        ft = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);
    280        assertEquals("Value 1 is smaller than 2 and 3.", ft.getMessage());
    281        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    282        assertEquals(t, ft.getThrowable());
    283
    284        ft = MessageFormatter.arrayFormat("{}{}{}", ia);
    285        assertEquals("123", ft.getMessage());
    286        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    287        assertEquals(t, ft.getThrowable());
    288
    289        ft = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
    290        assertEquals("Value 1 is smaller than 2.", ft.getMessage());
    291        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    292        assertEquals(t, ft.getThrowable());
    293
    294        ft = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
    295        assertEquals("Value 1 is smaller than 2", ft.getMessage());
    296        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    297        assertEquals(t, ft.getThrowable());
    298
    299        ft = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
    300        assertEquals("Val=1, {, Val=2", ft.getMessage());
    301        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    302        assertEquals(t, ft.getThrowable());
    303
    304        ft = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
    305        assertEquals("Val=1, \\{, Val=2", ft.getMessage());
    306        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    307        assertEquals(t, ft.getThrowable());
    308
    309        ft = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
    310        assertEquals("Val1=1, Val2={", ft.getMessage());
    311        assertTrue(Arrays.equals(iaWitness, ft.getArgArray()));
    312        assertEquals(t, ft.getThrowable());
    313
    314        ft = MessageFormatter.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("1={}", new Object[] { i1 }, t);
    325        assertEquals("1=1", ft.getMessage());
    326        assertTrue(Arrays.equals(new Object[] { i1 }, ft.getArgArray()));
    327        assertEquals(t, ft.getThrowable());
    328
    329    }
    330}