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.exception.SieveException;
25  import org.apache.jsieve.exception.SyntaxException;
26  import org.apache.jsieve.parser.generated.ParseException;
27  import org.apache.jsieve.utils.JUnitUtils;
28  
29  /**
30   * Class RequireTest
31   */
32  public class RequireTest extends TestCase {
33  
34      /**
35       * Test for Command 'require' with a single command that is present
36       */
37      public void testSingleCommandSatisfied() {
38          boolean isTestPassed = false;
39          String script = "require \"if\";";
40  
41          try {
42              JUnitUtils.interpret(JUnitUtils.createMail(), script);
43              isTestPassed = true;
44          } catch (ParseException e) {
45          } catch (SieveException e) {
46          }
47          assertTrue(isTestPassed);
48      }
49  
50      /**
51       * Test for Command 'require' with a single test that is present
52       */
53      public void testSingleTestSatisfied() throws Exception {
54          String script = "require \"true\";";
55          JUnitUtils.interpret(JUnitUtils.createMail(), script);
56      }
57  
58      /**
59       * Test for Command 'require' with multiple commands that are present
60       */
61      public void testMultipleCommandSatisfied() throws Exception {
62          String script = "require [\"if\", \"elsif\", \"else\"];";
63          JUnitUtils.interpret(JUnitUtils.createMail(), script);
64      }
65  
66      /**
67       * Test for Command 'require' with multiple tests that are present
68       */
69      public void testMultipleTestSatisfied() throws Exception {
70          String script = "require [\"true\", \"false\", \"not\"];";
71          JUnitUtils.interpret(JUnitUtils.createMail(), script);
72      }
73  
74      /**
75       * Test for Command 'require' with a single command that is absent
76       */
77      public void testSingleCommandUnsatisfied() throws Exception {
78          boolean isTestPassed = false;
79          String script = "require \"absent\";";
80  
81          try {
82              JUnitUtils.interpret(JUnitUtils.createMail(), script);
83          } catch (ParseException e) {
84              isTestPassed = true;
85          }
86          assertTrue(isTestPassed);
87      }
88  
89      /**
90       * Test for Command 'require' with a single test that is absent
91       */
92      public void testSingleTestUnsatisfied() throws Exception {
93          boolean isTestPassed = false;
94          String script = "require \"absent\";";
95  
96          try {
97              JUnitUtils.interpret(JUnitUtils.createMail(), script);
98          } catch (ParseException e) {
99              isTestPassed = true;
100         }
101         assertTrue(isTestPassed);
102     }
103 
104     /**
105      * Test for Command 'require' for missing argument
106      */
107     public void testMissingArgument() throws Exception {
108         boolean isTestPassed = false;
109         String script = "require;";
110 
111         try {
112             JUnitUtils.interpret(JUnitUtils.createMail(), script);
113         } catch (SyntaxException e) {
114             isTestPassed = true;
115         }
116         assertTrue(isTestPassed);
117     }
118 
119     /**
120      * Test for Command 'require' for extra argument
121      */
122     public void testExtraArgument() throws Exception {
123         boolean isTestPassed = false;
124         String script = "require \"if\" 1;";
125 
126         try {
127             JUnitUtils.interpret(JUnitUtils.createMail(), script);
128         } catch (SyntaxException e) {
129             isTestPassed = true;
130         }
131         assertTrue(isTestPassed);
132     }
133 
134     /**
135      * Test for Command 'require' rejecting Blocks
136      */
137     public void testRejectBlock() throws Exception {
138         boolean isTestPassed = false;
139         String script = "require \"if\" {stop;}";
140 
141         try {
142             JUnitUtils.interpret(JUnitUtils.createMail(), script);
143         } catch (SyntaxException e) {
144             isTestPassed = true;
145         }
146         assertTrue(isTestPassed);
147     }
148 
149     /**
150      * Test for Command 'require' after a Command
151      */
152     public void testInterveningCommand() throws Exception {
153         boolean isTestPassed = false;
154         String script = "fileinto \"someplace\"; require \"fileinto\";";
155 
156         try {
157             JUnitUtils.interpret(JUnitUtils.createMail(), script);
158         } catch (ParseException e) {
159             isTestPassed = true;
160         }
161         assertTrue(isTestPassed);
162     }
163 
164     /**
165      * Test for Command 'require' rejecting invalid arguments
166      */
167     public void testRejectInvalidArgument() throws Exception {
168         boolean isTestPassed = false;
169         String script = "require 1 ;";
170 
171         try {
172             JUnitUtils.interpret(JUnitUtils.createMail(), script);
173         } catch (SyntaxException e) {
174             isTestPassed = true;
175         }
176         assertTrue(isTestPassed);
177     }
178 
179     /**
180      * Test for Command 'require' with a multiple commands of which one is
181      * absent
182      */
183     public void testMultipleCommandsUnsatisfied() throws Exception {
184         boolean isTestPassed = false;
185         String script = "require [\"if\", \"elsif\", \"absent\"];";
186 
187         try {
188             JUnitUtils.interpret(JUnitUtils.createMail(), script);
189         } catch (ParseException e) {
190             isTestPassed = true;
191         }
192         assertTrue(isTestPassed);
193     }
194 
195     /**
196      * Test for Command 'require' with a multiple tests of which one is absent
197      */
198     public void testMultipleTestsUnsatisfied() throws Exception {
199         boolean isTestPassed = false;
200         String script = "require [\"true\", \"false\", \"absent\"];";
201 
202         try {
203             JUnitUtils.interpret(JUnitUtils.createMail(), script);
204         } catch (ParseException e) {
205             isTestPassed = true;
206         }
207         assertTrue(isTestPassed);
208     }
209 
210 }