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.util.check;
21  
22  import java.util.Arrays;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jsieve.mail.Action;
27  import org.apache.jsieve.mail.ActionFileInto;
28  import org.apache.jsieve.mail.ActionKeep;
29  import org.apache.jsieve.mail.ActionRedirect;
30  import org.apache.jsieve.mail.ActionReject;
31  
32  public class ScriptCheckerTestActionsTest extends TestCase {
33  
34      private static final String REDIRECT_ADDRESS_TWO = "roadrunner@acme.example.org";
35  
36      private static final String REDIRECT_ADDRESS_ONE = "coyote@desert.example.org";
37  
38      private static final String REJECT_MESSAGE_TWO = "Oh well";
39  
40      private static final String REJECT_MESSAGE_ONE = "Better luck next time";
41  
42      private static final String DESTINATION_TWO = "org.apache.jakarta";
43  
44      private static final String DESTINATION_ONE = "org.apache.james";
45  
46      static final Action[] ACTIONS = { new ActionFileInto(DESTINATION_ONE),
47              new ActionKeep(), new ActionFileInto(DESTINATION_TWO),
48              new ActionReject(REJECT_MESSAGE_ONE),
49              new ActionRedirect(REDIRECT_ADDRESS_ONE),
50              new ActionRedirect(REDIRECT_ADDRESS_TWO),
51              new ActionReject(REJECT_MESSAGE_TWO), };
52  
53      ScriptChecker.Results result;
54  
55      protected void setUp() throws Exception {
56          super.setUp();
57          result = new ScriptChecker.Results(Arrays.asList(ACTIONS));
58      }
59  
60      public void testFileInto() throws Exception {
61          assertTrue("Check for file into action with right destination", result
62                  .isActionFileInto(DESTINATION_ONE, 0));
63          assertFalse("Check for file into action with right destination", result
64                  .isActionFileInto(DESTINATION_ONE, 1));
65          assertFalse("Check for file into action with right destination", result
66                  .isActionFileInto(DESTINATION_ONE, 2));
67          assertFalse("Check for file into action with right destination", result
68                  .isActionFileInto(DESTINATION_ONE, 3));
69          assertFalse("Check for file into action with right destination", result
70                  .isActionFileInto(DESTINATION_ONE, 4));
71          assertFalse("Check for file into action with right destination", result
72                  .isActionFileInto(DESTINATION_ONE, 5));
73          assertFalse("Check for file into action with right destination", result
74                  .isActionFileInto(DESTINATION_ONE, 6));
75          assertFalse("Check for file into action with right destination", result
76                  .isActionFileInto(DESTINATION_TWO, 0));
77          assertFalse("Check for file into action with right destination", result
78                  .isActionFileInto(DESTINATION_TWO, 1));
79          assertTrue("Check for file into action with right destination", result
80                  .isActionFileInto(DESTINATION_TWO, 2));
81          assertFalse("Check for file into action with right destination", result
82                  .isActionFileInto(DESTINATION_TWO, 3));
83          assertFalse("Check for file into action with right destination", result
84                  .isActionFileInto(DESTINATION_TWO, 4));
85          assertFalse("Check for file into action with right destination", result
86                  .isActionFileInto(DESTINATION_TWO, 5));
87          assertFalse("Check for file into action with right destination", result
88                  .isActionFileInto(DESTINATION_TWO, 6));
89      }
90  
91      public void testRedirect() throws Exception {
92          assertFalse("Check for redirect action with right message", result
93                  .isActionRedirect(REDIRECT_ADDRESS_ONE, 0));
94          assertFalse("Check for redirect action with right message", result
95                  .isActionRedirect(REDIRECT_ADDRESS_ONE, 1));
96          assertFalse("Check for redirect action with right message", result
97                  .isActionRedirect(REDIRECT_ADDRESS_ONE, 2));
98          assertFalse("Check for redirect action with right message", result
99                  .isActionRedirect(REDIRECT_ADDRESS_ONE, 3));
100         assertTrue("Check for redirect action with right message", result
101                 .isActionRedirect(REDIRECT_ADDRESS_ONE, 4));
102         assertFalse("Check for redirect action with right message", result
103                 .isActionRedirect(REDIRECT_ADDRESS_ONE, 5));
104         assertFalse("Check for redirect action with right message", result
105                 .isActionRedirect(REDIRECT_ADDRESS_ONE, 6));
106         assertFalse("Check for redirect action with right message", result
107                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 0));
108         assertFalse("Check for redirect action with right message", result
109                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 1));
110         assertFalse("Check for redirect action with right message", result
111                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 2));
112         assertFalse("Check for redirect action with right message", result
113                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 3));
114         assertFalse("Check for redirect action with right message", result
115                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 4));
116         assertTrue("Check for redirect action with right message", result
117                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 5));
118         assertFalse("Check for redirect action with right message", result
119                 .isActionRedirect(REDIRECT_ADDRESS_TWO, 6));
120     }
121 
122     public void testReject() throws Exception {
123         assertFalse("Check for reject action with right message", result
124                 .isActionReject(REJECT_MESSAGE_ONE, 0));
125         assertFalse("Check for reject action with right message", result
126                 .isActionReject(REJECT_MESSAGE_ONE, 1));
127         assertFalse("Check for reject action with right message", result
128                 .isActionReject(REJECT_MESSAGE_ONE, 2));
129         assertTrue("Check for reject action with right message", result
130                 .isActionReject(REJECT_MESSAGE_ONE, 3));
131         assertFalse("Check for reject action with right message", result
132                 .isActionReject(REJECT_MESSAGE_ONE, 4));
133         assertFalse("Check for reject action with right message", result
134                 .isActionReject(REJECT_MESSAGE_ONE, 5));
135         assertFalse("Check for reject action with right message", result
136                 .isActionReject(REJECT_MESSAGE_ONE, 6));
137         assertFalse("Check for reject action with right message", result
138                 .isActionReject(REJECT_MESSAGE_TWO, 0));
139         assertFalse("Check for reject action with right message", result
140                 .isActionReject(REJECT_MESSAGE_TWO, 1));
141         assertFalse("Check for reject action with right message", result
142                 .isActionReject(REJECT_MESSAGE_TWO, 2));
143         assertFalse("Check for reject action with right message", result
144                 .isActionReject(REJECT_MESSAGE_TWO, 3));
145         assertFalse("Check for reject action with right message", result
146                 .isActionReject(REJECT_MESSAGE_TWO, 4));
147         assertFalse("Check for reject action with right message", result
148                 .isActionReject(REJECT_MESSAGE_TWO, 5));
149         assertTrue("Check for reject action with right message", result
150                 .isActionReject(REJECT_MESSAGE_TWO, 6));
151     }
152 
153     public void testKeep() throws Exception {
154         assertFalse("Check for keep action ", result.isActionKeep(0));
155         assertTrue("Check for keep action ", result.isActionKeep(1));
156         assertFalse("Check for keep action ", result.isActionKeep(2));
157         assertFalse("Check for keep action ", result.isActionKeep(3));
158         assertFalse("Check for keep action ", result.isActionKeep(4));
159         assertFalse("Check for keep action ", result.isActionKeep(5));
160         assertFalse("Check for keep action ", result.isActionKeep(6));
161     }
162 }