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.mail.SieveMailException;
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 SizeTest
35   */
36  public class SizeTest extends TestCase {
37  
38      /**
39       * Test for Test 'size'
40       */
41      public void testSizeIsOverTrue() {
42          boolean isTestPassed = false;
43          SieveMailAdapter mail = null;
44          int size = 0;
45          try {
46              mail = (SieveMailAdapter) JUnitUtils.createMail();
47              mail.getMessage().setText("Hi!");
48              mail.getMessage().saveChanges();
49              // Need to copy the mail to get JavaMail to report the message size
50              // correctly (saveChanges() only saves the headers!)
51              mail = (SieveMailAdapter) JUnitUtils.copyMail(mail);
52              size = mail.getSize();
53          } catch (SieveMailException e) {
54          } catch (MessagingException e) {
55          }
56  
57          String script = "if size :over " + new Integer(size - 1).toString()
58                  + " {throwTestException;}";
59          try {
60  
61              JUnitUtils.interpret(mail, script);
62          }
63  
64          catch (ThrowTestException.TestException e) {
65              isTestPassed = true;
66          } catch (ParseException e) {
67          } catch (SieveException e) {
68          }
69          assertTrue(isTestPassed);
70      }
71  
72      /**
73       * Test for Test 'size'
74       */
75      public void testSizeIsOverFalse() {
76          boolean isTestPassed = false;
77          SieveMailAdapter mail = null;
78          int size = 0;
79          try {
80              mail = (SieveMailAdapter) JUnitUtils.createMail();
81              mail.getMessage().setText("Hi!");
82              mail.getMessage().saveChanges();
83              // Need to copy the mail to get JavaMail to report the message size
84              // correctly (saveChanges() only saves the headers!)
85              mail = (SieveMailAdapter) JUnitUtils.copyMail(mail);
86              size = mail.getSize();
87          } catch (SieveMailException e) {
88          } catch (MessagingException e) {
89          }
90  
91          String script = "if size :over " + new Integer(size).toString()
92                  + " {throwTestException;}";
93          try {
94  
95              JUnitUtils.interpret(mail, script);
96              isTestPassed = true;
97          }
98  
99          catch (ThrowTestException.TestException e) {
100         } catch (ParseException e) {
101         } catch (SieveException e) {
102         }
103         assertTrue(isTestPassed);
104     }
105 
106     /**
107      * Test for Test 'size'
108      */
109     public void testSizeIsUnderTrue() {
110         boolean isTestPassed = false;
111         SieveMailAdapter mail = null;
112         int size = 0;
113         try {
114             mail = (SieveMailAdapter) JUnitUtils.createMail();
115             mail.getMessage().setText("Hi!");
116             mail.getMessage().saveChanges();
117             // Need to copy the mail to get JavaMail to report the message size
118             // correctly (saveChanges() only saves the headers!)
119             mail = (SieveMailAdapter) JUnitUtils.copyMail(mail);
120             size = mail.getSize();
121         } catch (SieveMailException e) {
122         } catch (MessagingException e) {
123         }
124 
125         String script = "if size :under " + new Integer(size + 1).toString()
126                 + " {throwTestException;}";
127         try {
128 
129             JUnitUtils.interpret(mail, script);
130         }
131 
132         catch (ThrowTestException.TestException e) {
133             isTestPassed = true;
134         } catch (ParseException e) {
135         } catch (SieveException e) {
136         }
137         assertTrue(isTestPassed);
138     }
139 
140     /**
141      * Test for Test 'size'
142      */
143     public void testSizeIsUnderFalse() {
144         boolean isTestPassed = false;
145         SieveMailAdapter mail = null;
146         int size = 0;
147         try {
148             mail = (SieveMailAdapter) JUnitUtils.createMail();
149             mail.getMessage().setText("Hi!");
150             mail.getMessage().saveChanges();
151             // Need to copy the mail to get JavaMail to report the message size
152             // correctly (saveChanges() only saves the headers!)
153             mail = (SieveMailAdapter) JUnitUtils.copyMail(mail);
154             size = mail.getSize();
155         } catch (SieveMailException e) {
156         } catch (MessagingException e) {
157         }
158 
159         String script = "if size :over " + new Integer(size).toString()
160                 + " {throwTestException;}";
161         try {
162 
163             JUnitUtils.interpret(mail, script);
164             isTestPassed = true;
165         }
166 
167         catch (ThrowTestException.TestException e) {
168         } catch (ParseException e) {
169         } catch (SieveException e) {
170         }
171         assertTrue(isTestPassed);
172     }
173     
174     /**
175      * Test for Test 'size' with quantifier
176      */
177     public void testSizeIsWithQuantifier() throws Exception {
178         boolean isTestPassed = false;
179         SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
180         mail.getMessage().setText("Hi!");
181         mail.getMessage().saveChanges();
182         // Need to copy the mail to get JavaMail to report the message size
183         // correctly (saveChanges() only saves the headers!)
184         mail = (SieveMailAdapter) JUnitUtils.copyMail(mail);
185 
186         String script = "if size :over 1G {throwTestException;}";
187         try {
188 
189             JUnitUtils.interpret(mail, script);
190             isTestPassed = true;
191         }
192 
193         catch (ThrowTestException.TestException e) {
194         } catch (ParseException e) {
195         } catch (SieveException e) {
196         }
197         assertTrue(isTestPassed);
198     }
199 
200 }