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  
20  package org.apache.jsieve;
21  
22  import junit.framework.TestCase;
23  
24  import org.apache.jsieve.commands.ThrowTestException;
25  import org.apache.jsieve.exception.CommandException;
26  import org.apache.jsieve.exception.SieveException;
27  import org.apache.jsieve.exception.SyntaxException;
28  import org.apache.jsieve.parser.generated.ParseException;
29  import org.apache.jsieve.utils.JUnitUtils;
30  
31  /**
32   * Class <code>ConditionTest</code> tests the conditional commands if, elsif
33   * and else.
34   */
35  public class ConditionTest extends TestCase {
36  
37      /**
38       * Test for Command 'if' with an argument of 'true'
39       */
40      public void testIfTrue() {
41          boolean isTestPassed = false;
42          String script = "if true {throwTestException;}";
43  
44          try {
45              JUnitUtils.interpret(JUnitUtils.createMail(), script);
46          } catch (ThrowTestException.TestException e) {
47              isTestPassed = true;
48          } catch (ParseException e) {
49          } catch (SieveException e) {
50          }
51          assertTrue(isTestPassed);
52      }
53  
54      /**
55       * Test for Command 'if' with an argument of 'false'
56       */
57      public void testIfFalse() {
58          boolean isTestPassed = false;
59          String script = "if false {stop;} throwTestException;";
60  
61          try {
62              JUnitUtils.interpret(JUnitUtils.createMail(), script);
63          } catch (ThrowTestException.TestException e) {
64              isTestPassed = true;
65          } catch (ParseException e) {
66          } catch (SieveException e) {
67          }
68          assertTrue(isTestPassed);
69      }
70  
71      /**
72       * Test for Command 'elsif' with an argument of 'true'
73       */
74      public void testElsifTrue() {
75          boolean isTestPassed = false;
76          String script = "if false {stop;} elsif true {throwTestException;}";
77  
78          try {
79              JUnitUtils.interpret(JUnitUtils.createMail(), script);
80          } catch (ThrowTestException.TestException e) {
81              isTestPassed = true;
82          } catch (ParseException e) {
83          } catch (SieveException e) {
84          }
85          assertTrue(isTestPassed);
86      }
87  
88      /**
89       * Test for Command 'elsif' with an argument of 'false'
90       */
91      public void testElsifFalse() {
92          boolean isTestPassed = false;
93          String script = "if false {stop;} elsif false {stop;} throwTestException;";
94  
95          try {
96              JUnitUtils.interpret(JUnitUtils.createMail(), script);
97          } catch (ThrowTestException.TestException e) {
98              isTestPassed = true;
99          } catch (ParseException e) {
100         } catch (SieveException e) {
101         }
102         assertTrue(isTestPassed);
103     }
104 
105     /**
106      * Test for nested Command 'elsif' with an argument of 'true'
107      */
108     public void testElsifFalseElsifTrue() {
109         boolean isTestPassed = false;
110         String script = "if false {stop;} elsif false {stop;} elsif true {throwTestException;}";
111 
112         try {
113             JUnitUtils.interpret(JUnitUtils.createMail(), script);
114         } catch (ThrowTestException.TestException e) {
115             isTestPassed = true;
116         } catch (ParseException e) {
117         } catch (SieveException e) {
118         }
119         assertTrue(isTestPassed);
120     }
121 
122     /**
123      * Test for Command 'else' after 'elseif'
124      */
125     public void testElsifFalseElse() {
126         boolean isTestPassed = false;
127         String script = "if false {stop;} elsif false {stop;} else {throwTestException;}";
128 
129         try {
130             JUnitUtils.interpret(JUnitUtils.createMail(), script);
131         } catch (ThrowTestException.TestException e) {
132             isTestPassed = true;
133         } catch (ParseException e) {
134         } catch (SieveException e) {
135         }
136         assertTrue(isTestPassed);
137     }
138 
139     /**
140      * Test for Command 'else'
141      */
142     public void testElse() {
143         boolean isTestPassed = false;
144         String script = "if false {stop;} else {throwTestException;}";
145 
146         try {
147             JUnitUtils.interpret(JUnitUtils.createMail(), script);
148         } catch (ThrowTestException.TestException e) {
149             isTestPassed = true;
150         } catch (ParseException e) {
151         } catch (SieveException e) {
152         }
153         assertTrue(isTestPassed);
154     }
155 
156     /**
157      * Test for Command 'else' out of sequence
158      */
159     public void testOutOfSequenceElse() {
160         boolean isTestPassed = false;
161         String script = "else {stop;}";
162 
163         try {
164             JUnitUtils.interpret(JUnitUtils.createMail(), script);
165         } catch (CommandException e) {
166             isTestPassed = true;
167         } catch (ParseException e) {
168         } catch (SieveException e) {
169         }
170         assertTrue(isTestPassed);
171     }
172 
173     /**
174      * Test for Command 'elsif' out of sequence
175      */
176     public void testOutOfSequenceElsif() {
177         boolean isTestPassed = false;
178         String script = "elsif {stop;}";
179 
180         try {
181             JUnitUtils.interpret(JUnitUtils.createMail(), script);
182         } catch (SyntaxException e) {
183             isTestPassed = true;
184         } catch (ParseException e) {
185         } catch (SieveException e) {
186         }
187         assertTrue(isTestPassed);
188     }
189 
190     /**
191      * Test for Command 'if' without a corresponding Block
192      */
193     public void testIfMissingBlock() {
194         boolean isTestPassed = false;
195         String script = "if true stop;";
196 
197         try {
198             JUnitUtils.interpret(JUnitUtils.createMail(), script);
199         } catch (SyntaxException e) {
200             isTestPassed = true;
201         } catch (ParseException e) {
202         } catch (SieveException e) {
203         }
204         assertTrue(isTestPassed);
205     }
206 
207     /**
208      * Test for Command 'if' without a test
209      */
210     public void testIfMissingTest() {
211         boolean isTestPassed = false;
212         String script = "if {stop;}";
213 
214         try {
215             JUnitUtils.interpret(JUnitUtils.createMail(), script);
216         } catch (SyntaxException e) {
217             isTestPassed = true;
218         } catch (ParseException e) {
219         } catch (SieveException e) {
220         }
221         assertTrue(isTestPassed);
222     }
223 
224     /**
225      * Test for Command 'if' without a test
226      */
227     public void testElsifMissingTest() {
228         boolean isTestPassed = false;
229         String script = "if false {stop;} elsif {stop;}";
230 
231         try {
232             JUnitUtils.interpret(JUnitUtils.createMail(), script);
233         } catch (SyntaxException e) {
234             isTestPassed = true;
235         } catch (ParseException e) {
236         } catch (SieveException e) {
237         }
238         assertTrue(isTestPassed);
239     }
240 
241     /**
242      * Test for Command 'elsif' without a corresponding Block
243      */
244     public void testElsifMissingBlock() {
245         boolean isTestPassed = false;
246         String script = "if false {stop;} elsif true stop;";
247 
248         try {
249             JUnitUtils.interpret(JUnitUtils.createMail(), script);
250         } catch (SyntaxException e) {
251             isTestPassed = true;
252         } catch (ParseException e) {
253         } catch (SieveException e) {
254         }
255         assertTrue(isTestPassed);
256     }
257 
258     /**
259      * Test for Command 'else' without a corresponding Block
260      */
261     public void testElseMissingBlock() {
262         boolean isTestPassed = false;
263         String script = "if false {stop;} else stop;";
264 
265         try {
266             JUnitUtils.interpret(JUnitUtils.createMail(), script);
267         } catch (SyntaxException e) {
268             isTestPassed = true;
269         } catch (ParseException e) {
270         } catch (SieveException e) {
271         }
272         assertTrue(isTestPassed);
273     }
274 
275     /**
276      * Test for Command 'if' nested in a block
277      */
278     public void testNestedIf() {
279         boolean isTestPassed = false;
280         String script = "if true {if true {throwTestException;}}";
281 
282         try {
283             JUnitUtils.interpret(JUnitUtils.createMail(), script);
284         } catch (ThrowTestException.TestException e) {
285             isTestPassed = true;
286         } catch (ParseException e) {
287         } catch (SieveException e) {
288         }
289         assertTrue(isTestPassed);
290     }
291 
292     /**
293      * Test for Command 'else' out of sequence nested in a block
294      */
295     public void testNestedOutOfSequenceElse() {
296         boolean isTestPassed = false;
297         String script = "if true {else {stop;}}";
298 
299         try {
300             JUnitUtils.interpret(JUnitUtils.createMail(), script);
301         } catch (CommandException e) {
302             isTestPassed = true;
303         } catch (ParseException e) {
304         } catch (SieveException e) {
305         }
306         assertTrue(isTestPassed);
307     }
308 
309     /**
310      * Test for Command 'elsif' out of sequence nested in a block
311      */
312     public void testNestedOutOfSequenceElsif() {
313         boolean isTestPassed = false;
314         String script = "if true {elsif true {stop;}}";
315 
316         try {
317             JUnitUtils.interpret(JUnitUtils.createMail(), script);
318         } catch (CommandException e) {
319             isTestPassed = true;
320         } catch (ParseException e) {
321         } catch (SieveException e) {
322         }
323         assertTrue(isTestPassed);
324     }
325 
326 }