JSIEVE-108 Discard instruction should emmit a ActionDiscard
[james-jsieve.git] / core / src / test / java / org / apache / jsieve / DiscardTest.java
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 static org.assertj.core.api.Assertions.assertThat;
23
24 import org.apache.jsieve.exception.SyntaxException;
25 import org.apache.jsieve.mail.Action;
26 import org.apache.jsieve.mail.ActionDiscard;
27 import org.apache.jsieve.mail.MailAdapter;
28 import org.apache.jsieve.utils.JUnitUtils;
29 import org.assertj.core.api.Condition;
30 import org.junit.Test;
31
32 public class DiscardTest {
33
34 private static final Condition<Action> INSTANCE_OF_ACTION_DISCARDED = new Condition<Action>() {
35 @Override
36 public boolean matches(Action action) {
37 return action instanceof ActionDiscard;
38 }
39 };
40
41 @Test(expected = SyntaxException.class)
42 public void discardShouldThrowOnInvalidArguments() throws Exception {
43 String script = "discard 1 ;";
44
45 JUnitUtils.interpret(JUnitUtils.createMail(), script);
46 }
47
48 @Test(expected = SyntaxException.class)
49 public void discardShouldThrowOnInvalidFollowingBlock() throws Exception {
50 String script = "discard {throwTestException;}";
51
52 JUnitUtils.interpret(JUnitUtils.createMail(), script);
53 }
54
55 @Test
56 public void discardShouldAddOnlyActionDiscard() throws Exception {
57 String script = "discard;";
58
59 MailAdapter mail = JUnitUtils.createMail();
60 JUnitUtils.interpret(mail, script);
61
62 assertThat(mail.getActions())
63 .hasSize(1)
64 .are(INSTANCE_OF_ACTION_DISCARDED);
65 }
66
67 }