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 javax.mail.MessagingException;
23
24 import junit.framework.TestCase;
25
26 import org.apache.jsieve.commands.ThrowTestException;
27 import org.apache.jsieve.exception.SieveException;
28 import org.apache.jsieve.exception.SyntaxException;
29 import org.apache.jsieve.parser.generated.ParseException;
30 import org.apache.jsieve.utils.JUnitUtils;
31 import org.apache.jsieve.utils.SieveMailAdapter;
32
33
34
35
36 public class ExistsTest extends TestCase {
37
38
39
40
41 public void testExistsTrue() {
42 boolean isTestPassed = false;
43 String script = "if exists \"From\" {throwTestException;}";
44
45 try {
46 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
47 mail.getMessage().addHeader("From", "tweety@pie");
48 JUnitUtils.interpret(mail, script);
49 } catch (MessagingException e) {
50 } catch (ThrowTestException.TestException e) {
51 isTestPassed = true;
52 } catch (ParseException e) {
53 } catch (SieveException e) {
54 }
55 assertTrue(isTestPassed);
56 }
57
58
59
60
61 public void testCaseInsensitivity() {
62 boolean isTestPassed = false;
63 String script = "if exists \"From\" {throwTestException;}";
64
65 try {
66 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
67 mail.getMessage().addHeader("from", "tweety@pie");
68 JUnitUtils.interpret(mail, script);
69 } catch (MessagingException e) {
70 } catch (ThrowTestException.TestException e) {
71 isTestPassed = true;
72 } catch (ParseException e) {
73 } catch (SieveException e) {
74 }
75 assertTrue(isTestPassed);
76 }
77
78
79
80
81 public void testExistsTrueTrue() {
82 boolean isTestPassed = false;
83 String script = "if exists [\"From\", \"X-Files\"] {throwTestException;}";
84
85 try {
86 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
87 mail.getMessage().addHeader("From", "tweety@pie");
88 mail.getMessage().addHeader("X-Files", "spooks@everywhere");
89 JUnitUtils.interpret(mail, script);
90 } catch (MessagingException e) {
91 } catch (ThrowTestException.TestException e) {
92 isTestPassed = true;
93 } catch (ParseException e) {
94 } catch (SieveException e) {
95 }
96 assertTrue(isTestPassed);
97 }
98
99
100
101
102 public void testExistsTrueFalse() {
103 boolean isTestPassed = false;
104 String script = "if exists [\"From\", \"X-Files\"] {stop;} throwTestException;";
105 try {
106 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
107 mail.getMessage().addHeader("From", "tweety@pie");
108 JUnitUtils.interpret(mail, script);
109 } catch (MessagingException e) {
110 } catch (ThrowTestException.TestException e) {
111 isTestPassed = true;
112 } catch (ParseException e) {
113 } catch (SieveException e) {
114 }
115 assertTrue(isTestPassed);
116 }
117
118
119
120
121 public void testExistsFalse() {
122 boolean isTestPassed = false;
123 String script = "if exists \"From\" {stop;} throwTestException;";
124
125 try {
126 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
127 JUnitUtils.interpret(mail, script);
128 } catch (ThrowTestException.TestException e) {
129 isTestPassed = true;
130 } catch (ParseException e) {
131 } catch (SieveException e) {
132 }
133 assertTrue(isTestPassed);
134 }
135
136
137
138
139 public void testExistsFalseFalse() {
140 boolean isTestPassed = false;
141 String script = "if exists [\"From\", \"X-Files\"] {stop;} throwTestException;";
142
143 try {
144 SieveMailAdapter mail = (SieveMailAdapter) JUnitUtils.createMail();
145 JUnitUtils.interpret(mail, script);
146 } catch (ThrowTestException.TestException e) {
147 isTestPassed = true;
148 } catch (ParseException e) {
149 } catch (SieveException e) {
150 }
151 assertTrue(isTestPassed);
152 }
153
154
155
156
157 public void testInvalidNumericArgument() {
158 boolean isTestPassed = false;
159 String script = "if exists 1 {throwTestException;}";
160
161 try {
162 JUnitUtils.interpret(JUnitUtils.createMail(), script);
163 } catch (SyntaxException e) {
164 isTestPassed = true;
165 } catch (ParseException e) {
166 } catch (SieveException e) {
167 }
168 assertTrue(isTestPassed);
169 }
170
171
172
173
174 public void testInvalidTestArgument() {
175 boolean isTestPassed = false;
176 String script = "if exists not {throwTestException;}";
177
178 try {
179 JUnitUtils.interpret(JUnitUtils.createMail(), script);
180 } catch (SyntaxException e) {
181 isTestPassed = true;
182 } catch (ParseException e) {
183 } catch (SieveException e) {
184 }
185 assertTrue(isTestPassed);
186 }
187
188 }