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.ListIterator;
23  
24  import junit.framework.TestCase;
25  
26  import org.apache.jsieve.mail.Action;
27  
28  public class ScriptCheckMailAdapterActionsTest extends TestCase {
29  
30      ScriptCheckMailAdapter adapter;
31  
32      Action action;
33  
34      Action anotherAction;
35  
36      protected void setUp() throws Exception {
37          super.setUp();
38          adapter = new ScriptCheckMailAdapter();
39          action = new MockAction();
40          anotherAction = new MockAction();
41      }
42  
43      public void testAddAction() {
44          adapter.addAction(action);
45          assertEquals("Running total updated", 1, adapter.getActions().size());
46          assertEquals("Running total updated", action, adapter.getActions().get(
47                  0));
48          adapter.addAction(anotherAction);
49          assertEquals("Order preserved", 2, adapter.getActions().size());
50          assertEquals("Order preserved", anotherAction, adapter.getActions()
51                  .get(1));
52      }
53  
54      public void testExecuteActions() throws Exception {
55          assertNotNull(adapter.getExecutedActions());
56          assertEquals("No actions executed", 0, adapter.getExecutedActions()
57                  .size());
58          adapter.addAction(action);
59          assertNotNull(adapter.getExecutedActions());
60          assertEquals("No actions executed", 0, adapter.getExecutedActions()
61                  .size());
62          adapter.executeActions();
63          assertNotNull(adapter.getExecutedActions());
64          assertEquals("One action executed", 1, adapter.getExecutedActions()
65                  .size());
66      }
67  
68      public void testGetActions() {
69          assertNotNull(adapter.getActions());
70          try {
71              adapter.getActions().add(new Action() {});
72              fail("Should not be able to modify collection");
73          } catch (UnsupportedOperationException e) {
74              // expected
75          }
76          adapter.addAction(action);
77          assertNotNull(adapter.getActions());
78          assertEquals("Running total updated", 1, adapter.getActions().size());
79          assertEquals("Running total updated", action, adapter.getActions().get(
80                  0));
81          adapter.addAction(anotherAction);
82          assertNotNull(adapter.getActions());
83          assertEquals("Order preserved", 2, adapter.getActions().size());
84          assertEquals("Order preserved", anotherAction, adapter.getActions()
85                  .get(1));
86      }
87  
88      public void testGetExecutedActions() throws Exception {
89          assertNotNull(adapter.getExecutedActions());
90          assertEquals("No actions executed", 0, adapter.getExecutedActions()
91                  .size());
92          adapter.addAction(action);
93          assertNotNull(adapter.getExecutedActions());
94          assertEquals("No actions executed", 0, adapter.getExecutedActions()
95                  .size());
96          adapter.executeActions();
97          assertEquals("One action executed", 1, adapter.getExecutedActions()
98                  .size());
99          assertEquals("One action executed", action, adapter
100                 .getExecutedActions().get(0));
101         adapter.addAction(anotherAction);
102         assertEquals("One action executed", 1, adapter.getExecutedActions()
103                 .size());
104         assertEquals("One action executed", action, adapter
105                 .getExecutedActions().get(0));
106         adapter.executeActions();
107         assertEquals("Two actions executed", 2, adapter.getExecutedActions()
108                 .size());
109         assertEquals("Two actions executed", action, adapter
110                 .getExecutedActions().get(0));
111         assertEquals("Two actions executed", anotherAction, adapter
112                 .getExecutedActions().get(1));
113         adapter.getExecutedActions().add(new Action(){});
114         assertEquals("Two actions executed", 2, adapter.getExecutedActions()
115                 .size());
116         assertEquals("Two actions executed", action, adapter
117                 .getExecutedActions().get(0));
118         assertEquals("Two actions executed", anotherAction, adapter
119                 .getExecutedActions().get(1));
120         adapter.executeActions();
121         assertEquals("Two actions executed", 2, adapter.getExecutedActions()
122                 .size());
123         assertEquals("Two actions executed", action, adapter
124                 .getExecutedActions().get(0));
125         assertEquals("Two actions executed", anotherAction, adapter
126                 .getExecutedActions().get(1));
127     }
128 
129     public void testReset() throws Exception {
130         adapter.addAction(action);
131         adapter.addAction(anotherAction);
132         adapter.executeActions();
133         assertEquals("Two actions executed", 2, adapter.getExecutedActions()
134                 .size());
135         assertEquals("Two actions", 2, adapter.getActions().size());
136         adapter.reset();
137         assertEquals("Two actions executed", 0, adapter.getExecutedActions()
138                 .size());
139         assertEquals("Two actions", 0, adapter.getActions().size());
140     }
141 }