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.CommandException;
25  import org.apache.jsieve.exception.SieveException;
26  import org.apache.jsieve.exception.SyntaxException;
27  import org.apache.jsieve.mail.ActionReject;
28  import org.apache.jsieve.mail.MailAdapter;
29  import org.apache.jsieve.parser.generated.ParseException;
30  import org.apache.jsieve.utils.JUnitUtils;
31  
32  /**
33   * Class RejectTest
34   */
35  public class RejectTest extends TestCase {
36  
37      /**
38       * Test for Command 'reject' with invalid arguments
39       */
40      public void testInvalidArguments() {
41          boolean isTestPassed = false;
42          String script = "reject 1 ;";
43  
44          try {
45              JUnitUtils.interpret(JUnitUtils.createMail(), script);
46          } catch (SyntaxException e) {
47              isTestPassed = true;
48          } catch (ParseException e) {
49          } catch (SieveException e) {
50          }
51          assertTrue(isTestPassed);
52      }
53  
54      /**
55       * Test for Command 'reject' with an invalid block
56       */
57      public void testInvalidBlock() {
58          boolean isTestPassed = false;
59          String script = "reject \"Spam not consumed here!\" {throwTestException;}";
60  
61          try {
62              JUnitUtils.interpret(JUnitUtils.createMail(), script);
63          } catch (SyntaxException e) {
64              isTestPassed = true;
65          } catch (ParseException e) {
66          } catch (SieveException e) {
67          }
68          assertTrue(isTestPassed);
69      }
70  
71      /**
72       * Test for Command 'reject'
73       */
74      public void testReject() {
75          boolean isTestPassed = false;
76          String script = "reject \"Spam not consumed here!\";";
77  
78          try {
79              MailAdapter mail = JUnitUtils.createMail();
80              JUnitUtils.interpret(mail, script);
81              assertTrue(mail.getActions().size() == 1);
82              assertTrue(mail.getActions().get(0) instanceof ActionReject);
83              isTestPassed = true;
84          } catch (ParseException e) {
85          } catch (SieveException e) {
86          }
87          assertTrue(isTestPassed);
88      }
89  
90      /**
91       * Tests that the message is correctly passed
92       */
93      public void testRejectMessage() throws Exception {
94          String message = "Spam not consumed here!";
95          String script = "reject \"" + message + "\";";
96          ActionReject rejection = runRejectScript(script);        
97          assertEquals(message, rejection.getMessage());
98      }
99  
100     private ActionReject runRejectScript(String script) throws SieveException, ParseException {
101         MailAdapter mail = JUnitUtils.createMail();
102         JUnitUtils.interpret(mail, script);
103         assertTrue(mail.getActions().size() == 1);
104         Object action = mail.getActions().get(0);
105         assertTrue(action instanceof ActionReject);
106         ActionReject rejection = (ActionReject) action;
107         return rejection;
108     }
109     
110     /**
111      * Test for Command 'reject'
112      */
113     public void testRejectMissingMessage() {
114         boolean isTestPassed = false;
115         String script = "reject;";
116 
117         try {
118             MailAdapter mail = JUnitUtils.createMail();
119             JUnitUtils.interpret(mail, script);
120             assertTrue(mail.getActions().size() == 1);
121             assertTrue(mail.getActions().get(0) instanceof ActionReject);
122         } catch (ParseException e) {
123         } catch (SieveException e) {
124             isTestPassed = true;
125         }
126         assertTrue(isTestPassed);
127     }
128 
129     /**
130      * Test for duplicate Command 'reject'
131      */
132     public void testDuplicateReject() {
133         boolean isTestPassed = false;
134         String script = "reject \"Spam not consumed here!\"; reject \"Spam not consumed here!\";";
135 
136         try {
137             MailAdapter mail = JUnitUtils.createMail();
138             JUnitUtils.interpret(mail, script);
139         } catch (CommandException e) {
140             isTestPassed = true;
141         } catch (ParseException e) {
142         } catch (SieveException e) {
143         }
144         assertTrue(isTestPassed);
145     }
146 
147     /**
148      * Test for Command 'reject' preceded by another command
149      */
150     public void testRejectAndAPrecedingCommand() {
151         boolean isTestPassed = false;
152         String script = "keep; reject \"Spam not consumed here!\";";
153 
154         try {
155             MailAdapter mail = JUnitUtils.createMail();
156             JUnitUtils.interpret(mail, script);
157         } catch (CommandException e) {
158             isTestPassed = true;
159         } catch (ParseException e) {
160         } catch (SieveException e) {
161         }
162         assertTrue(isTestPassed);
163     }
164 
165     /**
166      * Test for Command 'reject' followed by another command
167      */
168     public void testRejectAndAFollowingCommand() {
169         boolean isTestPassed = false;
170         String script = "reject \"Spam not consumed here!\"; keep;";
171 
172         try {
173             MailAdapter mail = JUnitUtils.createMail();
174             JUnitUtils.interpret(mail, script);
175         } catch (CommandException e) {
176             isTestPassed = true;
177         } catch (ParseException e) {
178         } catch (SieveException e) {
179         }
180         assertTrue(isTestPassed);
181     }
182 
183 }