1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jsieve.util;
20
21 import java.io.StringWriter;
22
23 import junit.framework.TestCase;
24
25 public class ToSieveHandlerFactoryTest extends TestCase {
26
27 ToSieveHandlerFactory factory;
28 StringWriter monitor;
29
30 protected void setUp() throws Exception {
31 super.setUp();
32 factory = new ToSieveHandlerFactory();
33 monitor = new StringWriter();
34 }
35
36 protected void tearDown() throws Exception {
37 super.tearDown();
38 }
39
40 public void testDefaultConfigurationShouldBuildNotNullHandler() throws Exception {
41 assertNotNull(factory.build(monitor));
42 }
43
44 public void testStartScriptShouldBeIgnored() throws Exception {
45
46 SieveHandler handler = factory.build(monitor);
47
48
49 handler.startScript();
50
51
52 assertEquals("", monitor.toString());
53 }
54
55 public void testEndScriptShouldBeIgnored() throws Exception {
56
57 SieveHandler handler = factory.build(monitor);
58
59
60 handler.endScript();
61
62
63 assertEquals("", monitor.toString());
64 }
65
66 public void testStartBlockShouldOpenBracket() throws Exception {
67
68 SieveHandler handler = factory.build(monitor);
69
70
71 handler.startBlock();
72
73
74 assertEquals(" {", monitor.toString());
75 }
76
77 public void testEndBlockShouldCloseBracket() throws Exception {
78
79 SieveHandler handler = factory.build(monitor);
80
81
82 handler.endBlock();
83
84
85 assertEquals("}", monitor.toString());
86 }
87
88 public void testStartCommandsShouldBeIgnored() throws Exception {
89
90 SieveHandler handler = factory.build(monitor);
91
92
93 handler.startCommands();
94
95
96 assertEquals("", monitor.toString());
97 }
98
99 public void testEndCommandsShouldBeIgnored() throws Exception {
100
101 SieveHandler handler = factory.build(monitor);
102
103
104 handler.endCommands();
105
106
107 assertEquals("", monitor.toString());
108 }
109
110 public void testStartCommandShouldPrintIdentifier() throws Exception {
111
112 SieveHandler handler = factory.build(monitor);
113 String commandName = "SomeCommand";
114
115
116 handler.startCommand(commandName);
117
118
119 assertEquals("SomeCommand", monitor.toString());
120 }
121
122 public void testEndCommandShouldPrintColon() throws Exception {
123
124 SieveHandler handler = factory.build(monitor);
125 String commandName = "SomeCommand";
126
127
128 handler.endCommand(commandName);
129
130
131 assertEquals(";", monitor.toString());
132 }
133
134 public void testStartArgumentsShouldBeIgnored() throws Exception {
135
136 SieveHandler handler = factory.build(monitor);
137
138
139 handler.startArguments();
140
141
142 assertEquals("", monitor.toString());
143 }
144
145 public void testEndArgumentsShouldBeIgnored() throws Exception {
146
147 SieveHandler handler = factory.build(monitor);
148
149
150 handler.endArguments();
151
152
153 assertEquals("", monitor.toString());
154 }
155
156 public void testArgumentShouldPrintTag() throws Exception {
157
158 SieveHandler handler = factory.build(monitor);
159 String identifier = "AnIdentifier";
160
161
162 handler.argument(identifier);
163
164
165 assertEquals(" :" + identifier, monitor.toString());
166 }
167
168 public void testArgumentShouldPrintNumber() throws Exception {
169
170 SieveHandler handler = factory.build(monitor);
171 int number = 99;
172
173
174 handler.argument(number);
175
176
177 assertEquals(" " + Integer.toString(number), monitor.toString());
178 }
179
180 public void testStartStringListShouldOpenBracket() throws Exception {
181
182 SieveHandler handler = factory.build(monitor);
183
184
185 handler.startStringListArgument();
186
187
188 assertEquals(" [", monitor.toString());
189 }
190
191 public void testEndStringListShouldCloseBracket() throws Exception {
192
193 SieveHandler handler = factory.build(monitor);
194
195
196 handler.endStringListArgument();
197
198
199 assertEquals("]", monitor.toString());
200 }
201
202 public void testListMemberShouldQuoteString() throws Exception {
203
204 SieveHandler handler = factory.build(monitor);
205 String member = "A List Member";
206
207
208 handler.listMember(member);
209
210
211 assertEquals('"' + member + '"', monitor.toString());
212 }
213
214 public void testListMemberShouldEscapeDoubleQuote() throws Exception {
215
216 SieveHandler handler = factory.build(monitor);
217 String prefix = "A Prefix";
218 String suffix = "A Suffix";
219
220
221 handler.listMember(prefix + '"' + suffix);
222
223
224 assertEquals('"' + prefix + "\\\"" + suffix + '"', monitor.toString());
225 }
226
227 public void testListMemberShouldEscapeBackSlash() throws Exception {
228
229 SieveHandler handler = factory.build(monitor);
230 String prefix = "A Prefix";
231 String suffix = "A Suffix";
232
233
234 handler.listMember(prefix + '\\' + suffix);
235
236
237 assertEquals('"' + prefix + "\\\\" + suffix + '"', monitor.toString());
238 }
239
240 public void testListMemberShouldEscapeCR() throws Exception {
241
242 SieveHandler handler = factory.build(monitor);
243 String prefix = "A Prefix";
244 String suffix = "A Suffix";
245
246
247 handler.listMember(prefix + '\r' + suffix);
248
249
250 assertEquals('"' + prefix + "\\\r" + suffix + '"', monitor.toString());
251 }
252
253 public void testListMemberShouldEscapeLF() throws Exception {
254
255 SieveHandler handler = factory.build(monitor);
256 String prefix = "A Prefix";
257 String suffix = "A Suffix";
258
259
260 handler.listMember(prefix + '\f' + suffix);
261
262
263 assertEquals('"' + prefix + "\\\f" + suffix + '"', monitor.toString());
264 }
265
266 public void testStartTestListShouldOpenBracket() throws Exception {
267
268 SieveHandler handler = factory.build(monitor);
269
270
271 handler.startTestList();
272
273
274 assertEquals("(", monitor.toString());
275 }
276
277 public void testEndTestListShouldCloseBracket() throws Exception {
278
279 SieveHandler handler = factory.build(monitor);
280
281
282 handler.endTestList();
283
284
285 assertEquals(")", monitor.toString());
286 }
287
288 public void testStartTestShouldPrintIdentifier() throws Exception {
289
290 SieveHandler handler = factory.build(monitor);
291 String identifier = "AnIdentifier";
292
293
294 handler.startTest(identifier);
295
296
297 assertEquals(" " + identifier, monitor.toString());
298 }
299
300 public void testStartSecondTestShouldPrefixComma() throws Exception {
301
302 SieveHandler handler = factory.build(monitor);
303 String firstIdentifier = "FirstIdentifier";
304 String secondIdentifier = "SecondIdentifier";
305
306
307 handler.startTest(firstIdentifier);
308 handler.endTest(firstIdentifier);
309 handler.startTest(secondIdentifier);
310
311
312 assertEquals(" " +firstIdentifier + ", " + secondIdentifier, monitor.toString());
313 }
314
315 public void testAfterEndTestListShouldNotNextPrefixTestWithComma() throws Exception {
316
317 SieveHandler handler = factory.build(monitor);
318 String firstIdentifier = "FirstIdentifier";
319 String secondIdentifier = "SecondIdentifier";
320
321
322 handler.startTest(firstIdentifier);
323 handler.endTest(firstIdentifier);
324 handler.endTestList();
325 handler.startTestList();
326 handler.startTest(secondIdentifier);
327
328
329 assertEquals(" " + firstIdentifier + ")(" + secondIdentifier, monitor.toString());
330 }
331
332 public void testEndTestShouldBeIgnored() throws Exception {
333
334 SieveHandler handler = factory.build(monitor);
335 String identifier = "AnIdentifier";
336
337
338 handler.endTest(identifier);
339
340
341 assertEquals("", monitor.toString());
342 }
343
344 public void testEndCommandShouldNotPrintSemiColonAfterBlock() throws Exception {
345
346 SieveHandler handler = factory.build(monitor);
347 String commandName = "SomeCommand";
348
349
350 handler.endBlock();
351 handler.endCommand(commandName);
352
353
354 assertEquals("}", monitor.toString());
355 }
356
357 public void testAfterEndCommandNextShouldPrintSpace() throws Exception {
358
359 SieveHandler handler = factory.build(monitor);
360 String firstCommandName = "FirstCommand";
361 String secondCommandName = "SecondCommand";
362
363
364 handler.endCommand(firstCommandName);
365 handler.startCommand(secondCommandName);
366
367
368 assertEquals("; " + secondCommandName, monitor.toString());
369 }
370 }