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 java.io.ByteArrayInputStream;
23  import java.io.InputStream;
24  import java.util.List;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.jsieve.util.check.ScriptChecker;
29  
30  public class MultipleToTest extends TestCase {
31  
32      private static final String SOLO_TO_EMAIL = "Date: Sun, 1 Apr 2007 1100:00:00 +0100 (BST)\r\f"
33              + "From: roadrunner@acme.example.com\r\f"
34              + "To: coyote@desert.example.org\r\f"
35              + "Subject: Who's The Fool?\r\f" + "\r\f" + "Beep-Beep\r\f";
36  
37      private static final String MULTIPLE_TO_EMAIL = "Date: Sun, 1 Apr 2007 1100:00:00 +0100 (BST)\r\f"
38              + "From: roadrunner@acme.example.com\r\f"
39              + "To: coyote@desert.example.org, bugs@example.org, "
40              + "    elmer@hunters.example.org,\r\f"
41              + "Subject: Who's The Fool?\r\f" + "\r\f" + "Beep-Beep\r\f";
42  
43      private static final String FILTER_SCRIPT = "require \"fileinto\";\r\f"
44              + "if address :is :all \"to\" \"coyote@desert.example.org\" {\r\f"
45              + "  fileinto \"coyote\";\r\f}\r\f"
46              + "if address :is :all \"to\" \"bugs@example.org\" {\r\f"
47              + "  fileinto \"bugs\";\r\f}\r\f"
48              + "if address :is :all \"to\" \"roadrunneracme.@example.org\" {\r\f"
49              + "  fileinto \"rr\";\r\f}\r\f"
50              + "if address :is :all \"to\" \"elmer@hunters.example.org\" {\r\f"
51              + "  fileinto \"elmer\";\r\f}\r\f";
52  
53      public void testSingleTo() throws Exception {
54          ScriptChecker checker = new ScriptChecker();
55          ScriptChecker.Results results = checker.check(toStream(SOLO_TO_EMAIL),
56                  toStream(FILTER_SCRIPT));
57          if (results.getException() != null) {
58              fail(results.getException().toString());
59          }
60          final List actionsExecuted = results.getActionsExecuted();
61          assertEquals(1, actionsExecuted.size());
62          assertTrue(results.isActionFileInto("coyote", 0));
63      }
64  
65      public void testMultipleTo() throws Exception {
66          ScriptChecker checker = new ScriptChecker();
67          ScriptChecker.Results results = checker.check(
68                  toStream(MULTIPLE_TO_EMAIL), toStream(FILTER_SCRIPT));
69          if (results.getException() != null) {
70              fail(results.getException().toString());
71          }
72          final List actionsExecuted = results.getActionsExecuted();
73          assertEquals(3, actionsExecuted.size());
74          assertTrue(results.isActionFileInto("coyote", 0));
75          assertTrue(results.isActionFileInto("bugs", 1));
76          assertTrue(results.isActionFileInto("elmer", 2));
77      }
78  
79      private InputStream toStream(String in) throws Exception {
80          byte[] bytes = in.getBytes("US-ASCII");
81          ByteArrayInputStream result = new ByteArrayInputStream(bytes);
82          return result;
83      }
84  }