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 */ 25package org.slf4j.helpers; 26 27import java.util.Arrays; 28 29import org.junit.Test; 30 31import static org.junit.Assert.*; 32 33/** 34 * @author Ceki Gulcu 35 */ 36public class MessageFormatterTest { 37 38 Integer i1 = new Integer(1); 39 Integer i2 = new Integer(2); 40 Integer i3 = new Integer(3); 41 Integer[] ia0 = new Integer[] { i1, i2, i3 }; 42 Integer[] ia1 = new Integer[] { new Integer(10), new Integer(20), new Integer(30) }; 43 44 String result; 45 46 @Test 47 public void testNull() { 48 result = MessageFormatter.format(null, i1).getMessage(); 49 assertEquals(null, result); 50 } 51 52 @Test 53 public void nullParametersShouldBeHandledWithoutBarfing() { 54 result = MessageFormatter.format("Value is {}.", null).getMessage(); 55 assertEquals("Value is null.", result); 56 57 result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, null).getMessage(); 58 assertEquals("Val1 is null, val2 is null.", result); 59 60 result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1, null).getMessage(); 61 assertEquals("Val1 is 1, val2 is null.", result); 62 63 result = MessageFormatter.format("Val1 is {}, val2 is {}.", null, i2).getMessage(); 64 assertEquals("Val1 is null, val2 is 2.", result); 65 66 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, null }).getMessage(); 67 assertEquals("Val1 is null, val2 is null, val3 is null", result); 68 69 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, i2, i3 }).getMessage(); 70 assertEquals("Val1 is null, val2 is 2, val3 is 3", result); 71 72 result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[] { null, null, i3 }).getMessage(); 73 assertEquals("Val1 is null, val2 is null, val3 is 3", result); 74 } 75 76 @Test 77 public void verifyOneParameterIsHandledCorrectly() { 78 result = MessageFormatter.format("Value is {}.", i3).getMessage(); 79 assertEquals("Value is 3.", result); 80 81 result = MessageFormatter.format("Value is {", i3).getMessage(); 82 assertEquals("Value is {", result); 83 84 result = MessageFormatter.format("{} is larger than 2.", i3).getMessage(); 85 assertEquals("3 is larger than 2.", result); 86 87 result = MessageFormatter.format("No subst", i3).getMessage(); 88 assertEquals("No subst", result); 89 90 result = MessageFormatter.format("Incorrect {subst", i3).getMessage(); 91 assertEquals("Incorrect {subst", result); 92 93 result = MessageFormatter.format("Value is {bla} {}", i3).getMessage(); 94 assertEquals("Value is {bla} 3", result); 95 96 result = MessageFormatter.format("Escaped \\{} subst", i3).getMessage(); 97 assertEquals("Escaped {} subst", result); 98 99 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 -- " + t.toString() + " .", ft.getMessage()); 316 assertTrue(Arrays.equals(ia, ft.getArgArray())); 317 assertNull(ft.getThrowable()); 318 319 ft = MessageFormatter.arrayFormat("{}{}{}{}", ia); 320 assertEquals("123" + t.toString(), ft.getMessage()); 321 assertTrue(Arrays.equals(ia, ft.getArgArray())); 322 assertNull(ft.getThrowable()); 323 } 324} 325