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 org.slf4j.helpers.MessageFormatter;
37  
38  import junit.framework.TestCase;
39  
40  
41  /**
42   * @author Ceki Gulcu
43   *
44   */
45  public class MessageFormatterTest extends TestCase {
46    
47    Integer i1 = new Integer(1);
48    Integer i2 = new Integer(2);
49    Integer i3 = new Integer(3);
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 {}", new Integer[]{null, null, null});
73      assertEquals("Val1 is null, val2 is null, val3 is null", result);
74      
75      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[]{null, i2, i3});
76      assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
77      
78      result = MessageFormatter.arrayFormat("Val1 is {}, val2 is {}, val3 is {}", new Integer[]{null, null, i3});
79      assertEquals("Val1 is null, val2 is null, val3 is 3", result);
80    }
81    
82    
83    public void testOneParameter() {
84      String result;
85      
86      result = MessageFormatter.format("Value is {}.", i3);
87      assertEquals("Value is 3.", result);
88  
89      result = MessageFormatter.format("Value is {", i3);
90      assertEquals("Value is {", result);
91  
92      result = MessageFormatter.format("{} is larger than 2.", i3);
93      assertEquals("3 is larger than 2.", result);
94  
95      result = MessageFormatter.format("No subst", i3);
96      assertEquals("No subst", result);
97      
98      result = MessageFormatter.format("Incorrect {subst", i3);
99      assertEquals("Incorrect {subst", result);
100     
101     result = MessageFormatter.format("Value is \\{bla} {}", i3);
102     assertEquals("Value is {bla} 3", result);
103     
104     result = MessageFormatter.format("Escaped \\{} subst", i3);
105     assertEquals("Escaped {} subst", result);
106 
107     result = MessageFormatter.format("\\{Escaped", i3);
108     assertEquals("{Escaped", result);
109 
110     result = MessageFormatter.format("\\{}Escaped", i3);
111     assertEquals("{}Escaped", result);
112     
113     result = MessageFormatter.format("File name is \\{{}}.", "App folder.zip");
114     assertEquals("File name is {App folder.zip}.", result);
115     
116     // escaping the escape character
117     result = MessageFormatter.format("File name is C:\\\\{}.", "App folder.zip");
118     assertEquals("File name is C:\\App folder.zip.", result);
119   }
120   
121   public void testTwoParameters() {
122     String result;
123 
124     
125     result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2);
126     assertEquals("Value 1 is smaller than 2.", result);
127     
128     result = MessageFormatter.format("Value {} is smaller than {}", i1, i2);
129     assertEquals("Value 1 is smaller than 2", result);
130     
131     result = MessageFormatter.format("{}{}", i1, i2);
132     assertEquals("12", result);
133     
134     result = MessageFormatter.format("Val1={}, Val2={", i1, i2);
135     assertEquals("Val1=1, Val2={", result);
136 
137     result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2);
138     assertEquals("Value 1 is smaller than {}", result);
139     
140     result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2);
141     assertEquals("Value 1 is smaller than {} tail", result);    
142 
143     result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2);
144     assertEquals("Value 1 is smaller than \\{", result);  
145     
146     result = MessageFormatter.format("Value {} is smaller than \\{tail", i1, i2);
147     assertEquals("Value 1 is smaller than {tail", result);  
148   
149     
150     result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2);
151     assertEquals("Value {} is smaller than 1", result);    
152   }
153   
154   public void testNullArray() {
155     String result;
156     
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);
165     assertEquals(msg0, result);
166     
167     result = MessageFormatter.arrayFormat(msg1, args);
168     assertEquals(msg1, result);
169     
170     result = MessageFormatter.arrayFormat(msg2, args);
171     assertEquals(msg2, result);
172     
173     result = MessageFormatter.arrayFormat(msg3, args);
174     assertEquals(msg3, result);
175   }
176   public void testArray() {
177     String result;
178 
179     Integer[] ia = new Integer[] {i1, i2, i3};
180 
181     result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);
182     assertEquals("Value 1 is smaller than 2 and 3.", result);
183     
184     result = MessageFormatter.arrayFormat("{}{}{}", ia);
185     assertEquals("123", result);
186     
187     result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
188     assertEquals("Value 1 is smaller than 2.", result);
189     
190     result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
191     assertEquals("Value 1 is smaller than 2", result);
192    
193     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
194     assertEquals("Val=1, {, Val={}", result);
195    
196     result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
197     assertEquals("Val=1, {, Val=2", result);
198    
199     
200     result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
201     assertEquals("Val1=1, Val2={", result);
202     
203 
204   }
205 
206 }