001: /*
002: * Copyright (c) 2004-2005 SLF4J.ORG
003: * Copyright (c) 2004-2005 QOS.CH
004: *
005: * All rights reserved.
006: *
007: * Permission is hereby granted, free of charge, to any person obtaining
008: * a copy of this software and associated documentation files (the
009: * "Software"), to deal in the Software without restriction, including
010: * without limitation the rights to use, copy, modify, merge, publish,
011: * distribute, and/or sell copies of the Software, and to permit persons
012: * to whom the Software is furnished to do so, provided that the above
013: * copyright notice(s) and this permission notice appear in all copies of
014: * the Software and that both the above copyright notice(s) and this
015: * permission notice appear in supporting documentation.
016: *
017: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
018: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
019: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
020: * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
021: * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
022: * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
023: * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
024: * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
025: * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
026: *
027: * Except as contained in this notice, the name of a copyright holder
028: * shall not be used in advertising or otherwise to promote the sale, use
029: * or other dealings in this Software without prior written authorization
030: * of the copyright holder.
031: *
032: */
033:
034: package org.slf4j.helpers;
035:
036: import org.slf4j.helpers.MessageFormatter;
037:
038: import junit.framework.TestCase;
039:
040: /**
041: * @author Ceki Gulcu
042: *
043: */
044: public class MessageFormatterTest extends TestCase {
045:
046: Integer i1 = new Integer(1);
047: Integer i2 = new Integer(2);
048: Integer i3 = new Integer(3);
049:
050: public void testNull() {
051: String result;
052: result = MessageFormatter.format(null, i1);
053: assertEquals(null, result);
054: }
055:
056: public void testNullParam() {
057: String result;
058:
059: result = MessageFormatter.format("Value is {}.", null);
060: assertEquals("Value is null.", result);
061:
062: result = MessageFormatter.format("Val1 is {}, val2 is {}.",
063: null, null);
064: assertEquals("Val1 is null, val2 is null.", result);
065:
066: result = MessageFormatter.format("Val1 is {}, val2 is {}.", i1,
067: null);
068: assertEquals("Val1 is 1, val2 is null.", result);
069:
070: result = MessageFormatter.format("Val1 is {}, val2 is {}.",
071: null, i2);
072: assertEquals("Val1 is null, val2 is 2.", result);
073:
074: result = MessageFormatter.arrayFormat(
075: "Val1 is {}, val2 is {}, val3 is {}", new Integer[] {
076: null, null, null });
077: assertEquals("Val1 is null, val2 is null, val3 is null", result);
078:
079: result = MessageFormatter.arrayFormat(
080: "Val1 is {}, val2 is {}, val3 is {}", new Integer[] {
081: null, i2, i3 });
082: assertEquals("Val1 is null, val2 is 2, val3 is 3", result);
083:
084: result = MessageFormatter.arrayFormat(
085: "Val1 is {}, val2 is {}, val3 is {}", new Integer[] {
086: null, null, i3 });
087: assertEquals("Val1 is null, val2 is null, val3 is 3", result);
088: }
089:
090: public void testOneParameter() {
091: String result;
092:
093: result = MessageFormatter.format("Value is {}.", i3);
094: assertEquals("Value is 3.", result);
095:
096: result = MessageFormatter.format("Value is {", i3);
097: assertEquals("Value is {", result);
098:
099: result = MessageFormatter.format("{} is larger than 2.", i3);
100: assertEquals("3 is larger than 2.", result);
101:
102: result = MessageFormatter.format("No subst", i3);
103: assertEquals("No subst", result);
104:
105: result = MessageFormatter.format("Incorrect {subst", i3);
106: assertEquals("Incorrect {subst", result);
107:
108: result = MessageFormatter.format("Value is \\{bla} {}", i3);
109: assertEquals("Value is {bla} 3", result);
110:
111: result = MessageFormatter.format("Escaped \\{} subst", i3);
112: assertEquals("Escaped {} subst", result);
113:
114: result = MessageFormatter.format("\\{Escaped", i3);
115: assertEquals("{Escaped", result);
116:
117: result = MessageFormatter.format("\\{}Escaped", i3);
118: assertEquals("{}Escaped", result);
119:
120: result = MessageFormatter.format("File name is \\{{}}.",
121: "App folder.zip");
122: assertEquals("File name is {App folder.zip}.", result);
123:
124: // escaping the escape character
125: result = MessageFormatter.format("File name is C:\\\\{}.",
126: "App folder.zip");
127: assertEquals("File name is C:\\App folder.zip.", result);
128: }
129:
130: public void testTwoParameters() {
131: String result;
132:
133: result = MessageFormatter.format(
134: "Value {} is smaller than {}.", i1, i2);
135: assertEquals("Value 1 is smaller than 2.", result);
136:
137: result = MessageFormatter.format("Value {} is smaller than {}",
138: i1, i2);
139: assertEquals("Value 1 is smaller than 2", result);
140:
141: result = MessageFormatter.format("{}{}", i1, i2);
142: assertEquals("12", result);
143:
144: result = MessageFormatter.format("Val1={}, Val2={", i1, i2);
145: assertEquals("Val1=1, Val2={", result);
146:
147: result = MessageFormatter.format(
148: "Value {} is smaller than \\{}", i1, i2);
149: assertEquals("Value 1 is smaller than {}", result);
150:
151: result = MessageFormatter.format(
152: "Value {} is smaller than \\{} tail", i1, i2);
153: assertEquals("Value 1 is smaller than {} tail", result);
154:
155: result = MessageFormatter.format(
156: "Value {} is smaller than \\{", i1, i2);
157: assertEquals("Value 1 is smaller than \\{", result);
158:
159: result = MessageFormatter.format(
160: "Value {} is smaller than \\{tail", i1, i2);
161: assertEquals("Value 1 is smaller than {tail", result);
162:
163: result = MessageFormatter.format(
164: "Value \\{} is smaller than {}", i1, i2);
165: assertEquals("Value {} is smaller than 1", result);
166: }
167:
168: public void testArray() {
169: String result;
170:
171: Integer[] ia = new Integer[] { i1, i2, i3 };
172:
173: result = MessageFormatter.arrayFormat(
174: "Value {} is smaller than {} and {}.", ia);
175: assertEquals("Value 1 is smaller than 2 and 3.", result);
176:
177: result = MessageFormatter.arrayFormat("{}{}{}", ia);
178: assertEquals("123", result);
179:
180: result = MessageFormatter.arrayFormat(
181: "Value {} is smaller than {}.", ia);
182: assertEquals("Value 1 is smaller than 2.", result);
183:
184: result = MessageFormatter.arrayFormat(
185: "Value {} is smaller than {}", ia);
186: assertEquals("Value 1 is smaller than 2", result);
187:
188: result = MessageFormatter.arrayFormat("Val={}, {, Val={}", ia);
189: assertEquals("Val=1, {, Val={}", result);
190:
191: result = MessageFormatter
192: .arrayFormat("Val={}, \\{, Val={}", ia);
193: assertEquals("Val=1, {, Val=2", result);
194:
195: result = MessageFormatter.arrayFormat("Val1={}, Val2={", ia);
196: assertEquals("Val1=1, Val2={", result);
197:
198: }
199:
200: }
|