1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
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          // Setup
46          SieveHandler handler = factory.build(monitor);
47          
48          // Exercise
49          handler.startScript();
50          
51          // Verify
52          assertEquals("", monitor.toString());
53      }
54      
55      public void testEndScriptShouldBeIgnored() throws Exception {
56          // Setup
57          SieveHandler handler = factory.build(monitor);
58          
59          // Exercise
60          handler.endScript();
61          
62          // Verify
63          assertEquals("", monitor.toString());
64      }
65      
66      public void testStartBlockShouldOpenBracket() throws Exception {
67          // Setup
68          SieveHandler handler = factory.build(monitor);
69          
70          // Exercise
71          handler.startBlock();
72          
73          // Verify
74          assertEquals(" {", monitor.toString());
75      }
76      
77      public void testEndBlockShouldCloseBracket() throws Exception {
78          // Setup
79          SieveHandler handler = factory.build(monitor);
80          
81          // Exercise
82          handler.endBlock();
83          
84          // Verify
85          assertEquals("}", monitor.toString());
86      }
87      
88      public void testStartCommandsShouldBeIgnored() throws Exception {
89          // Setup
90          SieveHandler handler = factory.build(monitor);
91          
92          // Exercise
93          handler.startCommands();
94          
95          // Verify
96          assertEquals("", monitor.toString());
97      }
98      
99      public void testEndCommandsShouldBeIgnored() throws Exception {
100         // Setup
101         SieveHandler handler = factory.build(monitor);
102         
103         // Exercise
104         handler.endCommands();
105         
106         // Verify
107         assertEquals("", monitor.toString());
108     }
109     
110     public void testStartCommandShouldPrintIdentifier() throws Exception {
111         // Setup
112         SieveHandler handler = factory.build(monitor);
113         String commandName = "SomeCommand";
114         
115         // Exercise
116         handler.startCommand(commandName);
117         
118         // Verify
119         assertEquals("SomeCommand", monitor.toString());
120     }
121     
122     public void testEndCommandShouldPrintColon() throws Exception {
123         // Setup
124         SieveHandler handler = factory.build(monitor);
125         String commandName = "SomeCommand";
126         
127         // Exercise
128         handler.endCommand(commandName);
129         
130         // Verify
131         assertEquals(";", monitor.toString());
132     }
133     
134     public void testStartArgumentsShouldBeIgnored() throws Exception {
135         // Setup
136         SieveHandler handler = factory.build(monitor);
137         
138         // Exercise
139         handler.startArguments();
140         
141         // Verify
142         assertEquals("", monitor.toString());
143     }
144     
145     public void testEndArgumentsShouldBeIgnored() throws Exception {
146         // Setup
147         SieveHandler handler = factory.build(monitor);
148         
149         // Exercise
150         handler.endArguments();
151         
152         // Verify
153         assertEquals("", monitor.toString());
154     }
155     
156     public void testArgumentShouldPrintTag() throws Exception {
157         // Setup
158         SieveHandler handler = factory.build(monitor);
159         String identifier = "AnIdentifier";
160         
161         // Exercise
162         handler.argument(identifier);
163         
164         // Verify
165         assertEquals(" :" + identifier, monitor.toString());
166     }
167     
168     public void testArgumentShouldPrintNumber() throws Exception {
169         // Setup
170         SieveHandler handler = factory.build(monitor);
171         int number = 99;
172         
173         // Exercise
174         handler.argument(number);
175         
176         // Verify
177         assertEquals(" " + Integer.toString(number), monitor.toString());
178     }
179     
180     public void testStartStringListShouldOpenBracket() throws Exception {
181         // Setup
182         SieveHandler handler = factory.build(monitor);
183         
184         // Exercise
185         handler.startStringListArgument();
186         
187         // Verify
188         assertEquals(" [", monitor.toString());
189     }
190     
191     public void testEndStringListShouldCloseBracket() throws Exception {
192         // Setup
193         SieveHandler handler = factory.build(monitor);
194         
195         // Exercise
196         handler.endStringListArgument();
197         
198         // Verify
199         assertEquals("]", monitor.toString());
200     }
201     
202     public void testListMemberShouldQuoteString() throws Exception {
203         // Setup
204         SieveHandler handler = factory.build(monitor);
205         String member = "A List Member";
206         
207         // Exercise
208         handler.listMember(member);
209         
210         // Verify
211         assertEquals('"' + member + '"', monitor.toString());
212     }
213     
214     public void testListMemberShouldEscapeDoubleQuote() throws Exception {
215         // Setup
216         SieveHandler handler = factory.build(monitor);
217         String prefix = "A Prefix";
218         String suffix = "A Suffix";
219         
220         // Exercise
221         handler.listMember(prefix + '"' + suffix);
222         
223         // Verify
224         assertEquals('"' + prefix + "\\\"" + suffix + '"', monitor.toString());
225     }
226     
227     public void testListMemberShouldEscapeBackSlash() throws Exception {
228         // Setup
229         SieveHandler handler = factory.build(monitor);
230         String prefix = "A Prefix";
231         String suffix = "A Suffix";
232         
233         // Exercise
234         handler.listMember(prefix + '\\' + suffix);
235         
236         // Verify
237         assertEquals('"' + prefix + "\\\\" + suffix + '"', monitor.toString());
238     }
239     
240     public void testListMemberShouldEscapeCR() throws Exception {
241         // Setup
242         SieveHandler handler = factory.build(monitor);
243         String prefix = "A Prefix";
244         String suffix = "A Suffix";
245         
246         // Exercise
247         handler.listMember(prefix + '\r' + suffix);
248         
249         // Verify
250         assertEquals('"' + prefix + "\\\r" + suffix + '"', monitor.toString());
251     }
252     
253     public void testListMemberShouldEscapeLF() throws Exception {
254         // Setup
255         SieveHandler handler = factory.build(monitor);
256         String prefix = "A Prefix";
257         String suffix = "A Suffix";
258         
259         // Exercise
260         handler.listMember(prefix + '\f' + suffix);
261         
262         // Verify
263         assertEquals('"' + prefix + "\\\f" + suffix + '"', monitor.toString());
264     }
265     
266     public void testStartTestListShouldOpenBracket() throws Exception {
267         // Setup
268         SieveHandler handler = factory.build(monitor);
269         
270         // Exercise
271         handler.startTestList();
272         
273         // Verify
274         assertEquals("(", monitor.toString());
275     }
276     
277     public void testEndTestListShouldCloseBracket() throws Exception {
278         // Setup
279         SieveHandler handler = factory.build(monitor);
280         
281         // Exercise
282         handler.endTestList();
283         
284         // Verify
285         assertEquals(")", monitor.toString());
286     }
287     
288     public void testStartTestShouldPrintIdentifier() throws Exception {
289         // Setup
290         SieveHandler handler = factory.build(monitor);
291         String identifier = "AnIdentifier";
292         
293         // Exercise
294         handler.startTest(identifier);
295         
296         // Verify
297         assertEquals(" " + identifier, monitor.toString());
298     }
299     
300     public void testStartSecondTestShouldPrefixComma() throws Exception {
301         // Setup
302         SieveHandler handler = factory.build(monitor);
303         String firstIdentifier = "FirstIdentifier";
304         String secondIdentifier = "SecondIdentifier";
305         
306         // Exercise
307         handler.startTest(firstIdentifier);
308         handler.endTest(firstIdentifier);
309         handler.startTest(secondIdentifier);
310         
311         // Verify
312         assertEquals(" " +firstIdentifier + ", " + secondIdentifier, monitor.toString());
313     }
314     
315     public void testAfterEndTestListShouldNotNextPrefixTestWithComma() throws Exception {
316         // Setup
317         SieveHandler handler = factory.build(monitor);
318         String firstIdentifier = "FirstIdentifier";
319         String secondIdentifier = "SecondIdentifier";
320         
321         // Exercise
322         handler.startTest(firstIdentifier);
323         handler.endTest(firstIdentifier);
324         handler.endTestList();
325         handler.startTestList();
326         handler.startTest(secondIdentifier);
327         
328         // Verify
329         assertEquals(" " + firstIdentifier + ")(" + secondIdentifier, monitor.toString());
330     }
331     
332     public void testEndTestShouldBeIgnored() throws Exception {
333         // Setup
334         SieveHandler handler = factory.build(monitor);
335         String identifier = "AnIdentifier";
336         
337         // Exercise
338         handler.endTest(identifier);
339         
340         // Verify
341         assertEquals("", monitor.toString());
342     }
343     
344     public void testEndCommandShouldNotPrintSemiColonAfterBlock() throws Exception {
345         // Setup
346         SieveHandler handler = factory.build(monitor);
347         String commandName = "SomeCommand";
348         
349         // Exercise
350         handler.endBlock();
351         handler.endCommand(commandName);
352         
353         // Verify
354         assertEquals("}", monitor.toString());
355     }
356     
357     public void testAfterEndCommandNextShouldPrintSpace() throws Exception {
358         // Setup
359         SieveHandler handler = factory.build(monitor);
360         String firstCommandName = "FirstCommand";
361         String secondCommandName = "SecondCommand";
362         
363         // Exercise
364         handler.endCommand(firstCommandName);
365         handler.startCommand(secondCommandName);
366         
367         // Verify
368         assertEquals("; " + secondCommandName, monitor.toString());
369     }
370 }