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 javax.mail.MessagingException;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jsieve.commands.ThrowTestException;
27  import org.apache.jsieve.exception.SieveException;
28  import org.apache.jsieve.exception.SyntaxException;
29  import org.apache.jsieve.parser.generated.ParseException;
30  import org.apache.jsieve.utils.JUnitUtils;
31  import org.apache.jsieve.utils.SieveMailAdapter;
32  
33  /**
34   * Class ExistsTest
35   */
36  public class ExistsTest extends TestCase {
37  
38      /**
39       * Test for Test 'exists'
40       */
41      public void testExistsTrue() {
42          boolean isTestPassed = false;
43          String script = "if exists \"From\" {throwTestException;}";
44  
45          try {
46              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
47              mail.getMessage().addHeader("From", "tweety@pie");
48              JUnitUtils.interpret(mail, script);
49          } catch (MessagingException e) {
50          } catch (ThrowTestException.TestException e) {
51              isTestPassed = true;
52          } catch (ParseException e) {
53          } catch (SieveException e) {
54          }
55          assertTrue(isTestPassed);
56      }
57  
58      /**
59       * Test for Test 'exists'
60       */
61      public void testCaseInsensitivity() {
62          boolean isTestPassed = false;
63          String script = "if exists \"From\" {throwTestException;}";
64  
65          try {
66              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
67              mail.getMessage().addHeader("from", "tweety@pie");
68              JUnitUtils.interpret(mail, script);
69          } catch (MessagingException e) {
70          } catch (ThrowTestException.TestException e) {
71              isTestPassed = true;
72          } catch (ParseException e) {
73          } catch (SieveException e) {
74          }
75          assertTrue(isTestPassed);
76      }
77  
78      /**
79       * Test for Test 'exists'
80       */
81      public void testExistsTrueTrue() {
82          boolean isTestPassed = false;
83          String script = "if exists [\"From\", \"X-Files\"] {throwTestException;}";
84  
85          try {
86              SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
87              mail.getMessage().addHeader("From", "tweety@pie");
88              mail.getMessage().addHeader("X-Files", "spooks@everywhere");
89              JUnitUtils.interpret(mail, script);
90          } catch (MessagingException e) {
91          } catch (ThrowTestException.TestException e) {
92              isTestPassed = true;
93          } catch (ParseException e) {
94          } catch (SieveException e) {
95          }
96          assertTrue(isTestPassed);
97      }
98  
99      /**
100      * Test for Test 'exists'
101      */
102     public void testExistsTrueFalse() {
103         boolean isTestPassed = false;
104         String script = "if exists [\"From\", \"X-Files\"] {stop;} throwTestException;";
105         try {
106             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
107             mail.getMessage().addHeader("From", "tweety@pie");
108             JUnitUtils.interpret(mail, script);
109         } catch (MessagingException e) {
110         } catch (ThrowTestException.TestException e) {
111             isTestPassed = true;
112         } catch (ParseException e) {
113         } catch (SieveException e) {
114         }
115         assertTrue(isTestPassed);
116     }
117 
118     /**
119      * Test for Test 'exists'
120      */
121     public void testExistsFalse() {
122         boolean isTestPassed = false;
123         String script = "if exists \"From\" {stop;} throwTestException;";
124 
125         try {
126             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
127             JUnitUtils.interpret(mail, script);
128         } catch (ThrowTestException.TestException e) {
129             isTestPassed = true;
130         } catch (ParseException e) {
131         } catch (SieveException e) {
132         }
133         assertTrue(isTestPassed);
134     }
135 
136     /**
137      * Test for Test 'exists'
138      */
139     public void testExistsFalseFalse() {
140         boolean isTestPassed = false;
141         String script = "if exists [\"From\", \"X-Files\"] {stop;} throwTestException;";
142 
143         try {
144             SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
145             JUnitUtils.interpret(mail, script);
146         } catch (ThrowTestException.TestException e) {
147             isTestPassed = true;
148         } catch (ParseException e) {
149         } catch (SieveException e) {
150         }
151         assertTrue(isTestPassed);
152     }
153 
154     /**
155      * Test for Test 'exists' with invalid numeric argument
156      */
157     public void testInvalidNumericArgument() {
158         boolean isTestPassed = false;
159         String script = "if exists 1 {throwTestException;}";
160 
161         try {
162             JUnitUtils.interpret(JUnitUtils.createMail(), script);
163         } catch (SyntaxException e) {
164             isTestPassed = true;
165         } catch (ParseException e) {
166         } catch (SieveException e) {
167         }
168         assertTrue(isTestPassed);
169     }
170 
171     /**
172      * Test for Test 'exists' with invalid test argument
173      */
174     public void testInvalidTestArgument() {
175         boolean isTestPassed = false;
176         String script = "if exists not {throwTestException;}";
177 
178         try {
179             JUnitUtils.interpret(JUnitUtils.createMail(), script);
180         } catch (SyntaxException e) {
181             isTestPassed = true;
182         } catch (ParseException e) {
183         } catch (SieveException e) {
184         }
185         assertTrue(isTestPassed);
186     }
187 
188 }