1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import junit.framework.TestCase;
21
22
23 /***
24 * Tests for NameAbbrevator.
25 *
26 * @author Curt Arnold
27 *
28 */
29 public class NameAbbreviatorTest extends TestCase {
30 /***
31 * Create a new instance.
32 *
33 * @param name test name
34 */
35 public NameAbbreviatorTest(final String name) {
36 super(name);
37 }
38
39 /***
40 * Check that getDefaultAbbreviator does not return null.
41 *
42 */
43 public void testGetDefault() {
44 NameAbbreviator abbrev = NameAbbreviator.getDefaultAbbreviator();
45 assertNotNull(abbrev);
46 }
47
48 /***
49 * Check that "0" drops all name content.
50 *
51 */
52 public void testZero() {
53 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("0");
54 StringBuffer buf = new StringBuffer("DEBUG - ");
55 int fieldStart = buf.length();
56 buf.append("org.example.foo.bar");
57 abbrev.abbreviate(fieldStart, buf);
58 assertEquals("DEBUG - ", buf.toString());
59 }
60
61 /***
62 * Check that getAbbreviator(" ") returns default abbreviator.
63 *
64 */
65 public void testBlank() {
66 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator(" ");
67 NameAbbreviator defaultAbbrev = NameAbbreviator.getDefaultAbbreviator();
68 assertTrue(abbrev == defaultAbbrev);
69 }
70
71 /***
72 * Check that getAbbreviator("1").abbreviate() drops all but the final name element.
73 *
74 */
75 public void testOne() {
76 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1");
77 StringBuffer buf = new StringBuffer("DEBUG - ");
78 int fieldStart = buf.length();
79 buf.append("org.example.foo.bar");
80 abbrev.abbreviate(fieldStart, buf);
81 assertEquals("DEBUG - bar", buf.toString());
82
83 buf.setLength(0);
84 buf.append("DEBUG - ");
85 fieldStart = buf.length();
86 buf.append("bar");
87 abbrev.abbreviate(fieldStart, buf);
88 assertEquals("DEBUG - bar", buf.toString());
89
90 buf.setLength(0);
91 buf.append("DEBUG - ");
92 fieldStart = buf.length();
93 abbrev.abbreviate(fieldStart, buf);
94 assertEquals("DEBUG - ", buf.toString());
95 }
96
97 /***
98 * Check that blanks are trimmed in evaluating abbreviation pattern.
99 */
100 public void testBlankOne() {
101 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator(" 1 ");
102 StringBuffer buf = new StringBuffer("DEBUG - ");
103 int fieldStart = buf.length();
104 buf.append("org.example.foo.bar");
105 abbrev.abbreviate(fieldStart, buf);
106 assertEquals("DEBUG - bar", buf.toString());
107
108 buf.setLength(0);
109 buf.append("DEBUG - ");
110 fieldStart = buf.length();
111 buf.append("bar");
112 abbrev.abbreviate(fieldStart, buf);
113 assertEquals("DEBUG - bar", buf.toString());
114
115 buf.setLength(0);
116 buf.append("DEBUG - ");
117 fieldStart = buf.length();
118 abbrev.abbreviate(fieldStart, buf);
119 assertEquals("DEBUG - ", buf.toString());
120 }
121
122 /***
123 * Check that getAbbreviator("2").abbreviate drops all but the last two elements.
124 *
125 */
126 public void testTwo() {
127 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("2");
128 StringBuffer buf = new StringBuffer("DEBUG - ");
129 int fieldStart = buf.length();
130 buf.append("org.example.foo.bar");
131 abbrev.abbreviate(fieldStart, buf);
132 assertEquals("DEBUG - foo.bar", buf.toString());
133
134 buf.setLength(0);
135 buf.append("DEBUG - ");
136 fieldStart = buf.length();
137 buf.append("foo.bar");
138 abbrev.abbreviate(fieldStart, buf);
139 assertEquals("DEBUG - foo.bar", buf.toString());
140
141 buf.setLength(0);
142 buf.append("DEBUG - ");
143 fieldStart = buf.length();
144 buf.append("bar");
145 abbrev.abbreviate(fieldStart, buf);
146 assertEquals("DEBUG - bar", buf.toString());
147 }
148
149 /***
150 * Check that getAbbreviator("1.").abbreviate abbreviates non-final elements
151 * to one character.
152 *
153 */
154 public void testOneDot() {
155 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1.");
156 StringBuffer buf = new StringBuffer("DEBUG - ");
157 int fieldStart = buf.length();
158 buf.append("org.example.foo.bar");
159 abbrev.abbreviate(fieldStart, buf);
160 assertEquals("DEBUG - o.e.f.bar", buf.toString());
161
162 buf.setLength(0);
163 buf.append("DEBUG - ");
164 fieldStart = buf.length();
165 buf.append("org.example.foo.");
166 abbrev.abbreviate(fieldStart, buf);
167 assertEquals("DEBUG - o.e.f.", buf.toString());
168
169
170 buf.setLength(0);
171 buf.append("DEBUG - ");
172 fieldStart = buf.length();
173 buf.append("foo.bar");
174 abbrev.abbreviate(fieldStart, buf);
175 assertEquals("DEBUG - f.bar", buf.toString());
176
177 buf.setLength(0);
178 buf.append("DEBUG - ");
179 fieldStart = buf.length();
180 buf.append("bar");
181 abbrev.abbreviate(fieldStart, buf);
182 assertEquals("DEBUG - bar", buf.toString());
183
184 buf.setLength(0);
185 buf.append("DEBUG - ");
186 fieldStart = buf.length();
187 abbrev.abbreviate(fieldStart, buf);
188 assertEquals("DEBUG - ", buf.toString());
189
190 buf.setLength(0);
191 buf.append("DEBUG - ");
192 fieldStart = buf.length();
193 buf.append(".");
194 abbrev.abbreviate(fieldStart, buf);
195 assertEquals("DEBUG - .", buf.toString());
196 }
197
198 /***
199 * Check that getAbbreviator("1~.").abbreviate abbreviates non-final elements
200 * to one character and a tilde.
201 *
202 */
203 public void testOneTildeDot() {
204 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1~.");
205 StringBuffer buf = new StringBuffer("DEBUG - ");
206 int fieldStart = buf.length();
207 buf.append("org.example.foo.bar");
208 abbrev.abbreviate(fieldStart, buf);
209 assertEquals("DEBUG - o~.e~.f~.bar", buf.toString());
210
211 buf.setLength(0);
212 buf.append("DEBUG - ");
213 fieldStart = buf.length();
214 buf.append("org.example.foo.");
215 abbrev.abbreviate(fieldStart, buf);
216 assertEquals("DEBUG - o~.e~.f~.", buf.toString());
217
218 buf.setLength(0);
219 buf.append("DEBUG - ");
220 fieldStart = buf.length();
221 buf.append("foo.bar");
222 abbrev.abbreviate(fieldStart, buf);
223 assertEquals("DEBUG - f~.bar", buf.toString());
224
225 buf.setLength(0);
226 buf.append("DEBUG - ");
227 fieldStart = buf.length();
228 buf.append("bar");
229 abbrev.abbreviate(fieldStart, buf);
230 assertEquals("DEBUG - bar", buf.toString());
231
232 buf.setLength(0);
233 buf.append("DEBUG - ");
234 fieldStart = buf.length();
235 abbrev.abbreviate(fieldStart, buf);
236 assertEquals("DEBUG - ", buf.toString());
237
238
239 buf.setLength(0);
240 buf.append("DEBUG - ");
241 fieldStart = buf.length();
242 buf.append(".");
243 abbrev.abbreviate(fieldStart, buf);
244 assertEquals("DEBUG - .", buf.toString());
245
246
247 buf.setLength(0);
248 buf.append("DEBUG - ");
249 fieldStart = buf.length();
250 buf.append("o.e.f.bar");
251 abbrev.abbreviate(fieldStart, buf);
252 assertEquals("DEBUG - o.e.f.bar", buf.toString());
253 }
254
255 /***
256 * Check that getAbbreviator("1.*.2").abbreviate drops all but the first
257 * character from the first element, uses all of the second element and
258 * drops all but the first two characters of the rest of the non-final elements.
259 *
260 */
261 public void testMulti() {
262 NameAbbreviator abbrev = NameAbbreviator.getAbbreviator("1.*.2");
263 StringBuffer buf = new StringBuffer("DEBUG - ");
264 int fieldStart = buf.length();
265 buf.append("org.example.foo.bar");
266 abbrev.abbreviate(fieldStart, buf);
267 assertEquals("DEBUG - o.example.fo.bar", buf.toString());
268
269 buf.setLength(0);
270 buf.append("DEBUG - ");
271 fieldStart = buf.length();
272 buf.append("org.example.foo.");
273 abbrev.abbreviate(fieldStart, buf);
274 assertEquals("DEBUG - o.example.fo.", buf.toString());
275
276 buf.setLength(0);
277 buf.append("DEBUG - ");
278 fieldStart = buf.length();
279 buf.append("foo.bar");
280 abbrev.abbreviate(fieldStart, buf);
281 assertEquals("DEBUG - f.bar", buf.toString());
282
283 buf.setLength(0);
284 buf.append("DEBUG - ");
285 fieldStart = buf.length();
286 buf.append("bar");
287 abbrev.abbreviate(fieldStart, buf);
288 assertEquals("DEBUG - bar", buf.toString());
289
290 buf.setLength(0);
291 buf.append("DEBUG - ");
292 fieldStart = buf.length();
293 abbrev.abbreviate(fieldStart, buf);
294 assertEquals("DEBUG - ", buf.toString());
295
296 buf.setLength(0);
297 buf.append("DEBUG - ");
298 fieldStart = buf.length();
299 buf.append(".");
300 abbrev.abbreviate(fieldStart, buf);
301 assertEquals("DEBUG - .", buf.toString());
302 }
303 }