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 test1Param() {
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("Escaped \\{} subst", i3);
102     assertEquals("Escaped {} subst", result);
103 
104     result = MessageFormatter.format("\\{Escaped", i3);
105     assertEquals("{Escaped", result);
106 
107     result = MessageFormatter.format("\\{}Escaped", i3);
108     assertEquals("{}Escaped", result);
109     
110     result = MessageFormatter.format("File name is \\{{}}.", "App folder.zip");
111     assertEquals("File name is {App folder.zip}.", result);
112   }
113   
114   public void test2Param() {
115     String result;
116 
117     
118     result = MessageFormatter.format("Value {} is smaller than {}.", i1, i2);
119     assertEquals("Value 1 is smaller than 2.", result);
120     
121     result = MessageFormatter.format("Value {} is smaller than {}", i1, i2);
122     assertEquals("Value 1 is smaller than 2", result);
123     
124     result = MessageFormatter.format("{}{}", i1, i2);
125     assertEquals("12", result);
126     
127     result = MessageFormatter.format("Val1={}, Val2={", i1, i2);
128     assertEquals("Val1=1, Val2={", result);
129 
130     result = MessageFormatter.format("Value {} is smaller than \\{}", i1, i2);
131     assertEquals("Value 1 is smaller than {}", result);
132     
133     result = MessageFormatter.format("Value {} is smaller than \\{} tail", i1, i2);
134     assertEquals("Value 1 is smaller than {} tail", result);    
135 
136     result = MessageFormatter.format("Value {} is smaller than \\{", i1, i2);
137     assertEquals("Value 1 is smaller than \\{", result);  
138     
139     result = MessageFormatter.format("Value {} is smaller than \\{tail", i1, i2);
140     assertEquals("Value 1 is smaller than {tail", result);  
141   
142     
143     result = MessageFormatter.format("Value \\{} is smaller than {}", i1, i2);
144     assertEquals("Value {} is smaller than 1", result);    
145   }
146   
147   public void testArray() {
148     String result;
149 
150     Integer[] ia = new Integer[] {i1, i2, i3};
151 
152     result = MessageFormatter.arrayFormat("Value {} is smaller than {} and {}.", ia);
153     assertEquals("Value 1 is smaller than 2 and 3.", result);
154     
155     result = MessageFormatter.arrayFormat("{}{}{}", ia);
156     assertEquals("123", result);
157     
158     result = MessageFormatter.arrayFormat("Value {} is smaller than {}.", ia);
159     assertEquals("Value 1 is smaller than 2.", result);
160     
161     result = MessageFormatter.arrayFormat("Value {} is smaller than {}", ia);
162     assertEquals("Value 1 is smaller than 2", result);
163    
164     result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
165     assertEquals("Val=1, {, Val={}", result);
166    
167     result = MessageFormatter.arrayFormat("Val={}, \\{, Val={}", ia);
168     assertEquals("Val=1, {, Val=2", result);
169    
170     
171     result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
172     assertEquals("Val1=1, Val2={", result);
173     
174 
175   }
176 
177 }