1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }