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.parser.generated.ParseException;
25  import org.apache.jsieve.utils.JUnitUtils;
26  
27  /**
28   * Class AddressTest
29   */
30  public class RequireMissingTest extends TestCase {
31  
32      /**
33       * Tests that unsupported requires are caught before script execution.
34       */
35      public void testUnsupportedRequireNoBrackets() throws Exception {
36          String script = "require \"whatever\"; if address :contains [\"To\", \"From\"] \"Fish!\"{ fileinto \"aFolder\"; }";
37          try {
38              JUnitUtils.parse(script);
39              fail("Expect exception to be throw during parse since command is unsupported");
40          } catch (ParseException e) {
41              // expected
42          }
43      }
44  
45      /**
46       * Tests that unsupported requires are caught before script execution.
47       */
48      public void testUnsupportedRequireMultiple() throws Exception {
49          String script = "require [\"fileinto\",\"whatever\"]; if address :contains [\"To\", \"From\"] \"Fish!\"{ fileinto \"aFolder\"; }";
50          try {
51              JUnitUtils.parse(script);
52              fail("Expect exception to be throw during parse since command is unsupported");
53          } catch (ParseException e) {
54              // expected
55          }
56      }
57  
58      /**
59       * Tests that unsupported requires are caught before script execution.
60       */
61      public void testUnsupportedRequire() throws Exception {
62          String script = "require [\"whatever\"]; if address :contains [\"To\", \"From\"] \"Fish!\"{ fileinto \"aFolder\"; }";
63          try {
64              JUnitUtils.parse(script);
65              fail("Expect exception to be throw during parse since command is unsupported");
66          } catch (ParseException e) {
67              // expected
68          }
69      }
70  
71      /**
72       * Tests 2.10.5 Extensions and Optional Features: If an extension is not
73       * enabled with "required" they must treat it as if they do not support it
74       * at all.
75       */
76      public void testMissingRequire() throws Exception {
77          String script = "if address :contains [\"To\", \"From\"] \"Fish!\"{ bogus \"aFolder\"; }";
78          try {
79              JUnitUtils.parse(script);
80              fail("Expect exception to be throw during parse since command is missing");
81          } catch (ParseException e) {
82              // expected
83          }
84      }
85  
86      /**
87       * Tests 3.2 Control Structure Require: Require MUST NOT be used after any
88       * other command.
89       */
90      public void testRequireAfterOtherCommand() throws Exception {
91          String script = "if address :contains [\"To\", \"From\"] \"Fish!\"{ fileinto \"aFolder\"; } require [\"whatever\"]; ";
92          try {
93              JUnitUtils.parse(script);
94              fail("Expect exception to be throw during parse");
95          } catch (ParseException e) {
96              // expected
97          }
98      }
99  }