MessageFormatterTest xref

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