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.mail.ActionFileInto;
27  import org.apache.jsieve.mail.MailAdapter;
28  import org.apache.jsieve.parser.generated.ParseException;
29  import org.apache.jsieve.utils.JUnitUtils;
30  
31  /**
32   * Class FileIntoTest
33   */
34  public class FileIntoTest extends TestCase {
35  
36      /**
37       * Test for Command 'fileinto'
38       */
39      public void testFileInto() {
40          boolean isTestPassed = false;
41          String script = "fileinto \"INBOX.test1\"; fileinto \"INBOX.test2\";";
42  
43          try {
44              MailAdapter mail = JUnitUtils.createMail();
45              JUnitUtils.interpret(mail, script);
46              assertTrue(mail.getActions().size() == 2);
47              assertTrue(mail.getActions().get(0) instanceof ActionFileInto);
48              assertTrue(((ActionFileInto) mail.getActions().get(0))
49                      .getDestination().equals("INBOX.test1"));
50              assertTrue(mail.getActions().get(1) instanceof ActionFileInto);
51              assertTrue(((ActionFileInto) mail.getActions().get(1))
52                      .getDestination().equals("INBOX.test2"));
53              isTestPassed = true;
54          } catch (ParseException e) {
55          } catch (SieveException e) {
56          }
57          assertTrue(isTestPassed);
58      }
59  
60      /**
61       * Test for Command 'fileinto' with duplicate destinations. Only one
62       * ActionFileInto should result.
63       */
64      public void testDuplicateFileInto() {
65          boolean isTestPassed = false;
66          String script = "fileinto \"INBOX.test1\"; fileinto \"INBOX.test1\";";
67  
68          try {
69              MailAdapter mail = JUnitUtils.createMail();
70              JUnitUtils.interpret(mail, script);
71              assertTrue(mail.getActions().size() == 1);
72              assertTrue(mail.getActions().get(0) instanceof ActionFileInto);
73              assertTrue(((ActionFileInto) mail.getActions().get(0))
74                      .getDestination().equals("INBOX.test1"));
75              isTestPassed = true;
76          } catch (ParseException e) {
77          } catch (SieveException e) {
78          }
79          assertTrue(isTestPassed);
80      }
81  
82      /**
83       * Test for Command 'fileinto' with an invalid argument type
84       */
85      public void testInvalidArgumentType() {
86          boolean isTestPassed = false;
87          String script = "fileinto 1 ;";
88  
89          try {
90              JUnitUtils.interpret(JUnitUtils.createMail(), script);
91          } catch (SyntaxException e) {
92              isTestPassed = true;
93          } catch (ParseException e) {
94          } catch (SieveException e) {
95          }
96          assertTrue(isTestPassed);
97      }
98  
99      /**
100      * Test for Command 'fileinto' with an invalid argument number
101      */
102     public void testInvalidArgumentNumber() {
103         boolean isTestPassed = false;
104         String script = "fileinto [\"INBOX.test\", \"elsewhere\"];";
105 
106         try {
107             JUnitUtils.interpret(JUnitUtils.createMail(), script);
108         } catch (SyntaxException e) {
109             isTestPassed = true;
110         } catch (ParseException e) {
111         } catch (SieveException e) {
112         }
113         assertTrue(isTestPassed);
114     }
115 
116     /**
117      * Test for Command 'fileinto' with an invalid block
118      */
119     public void testInvalidBlock() {
120         boolean isTestPassed = false;
121         String script = "fileinto 1 {throwTestException;}";
122 
123         try {
124             JUnitUtils.interpret(JUnitUtils.createMail(), script);
125         } catch (SyntaxException e) {
126             isTestPassed = true;
127         } catch (ParseException e) {
128         } catch (SieveException e) {
129         }
130         assertTrue(isTestPassed);
131     }
132 
133 }