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 java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import junit.framework.TestCase;
27  
28  import org.apache.jsieve.ConfigurationManager;
29  import org.apache.jsieve.SieveConfigurationException;
30  
31  /**
32   * Class ConfigurationManagerTest
33   */
34  public class ConfigurationManagerTest extends TestCase {
35  
36      /**
37       * Test the CommandMap maps 'MUST' and 'SHOULD' be supported commands to the
38       * correct classes.
39       */
40      public void testCommandMap() {
41          Map<String, String> map = new HashMap<String, String>();
42          // Condition Commands
43          // RFC3082 - Implementations MUST support these:
44          map.put("if", "org.apache.jsieve.commands.If");
45          map.put("else", "org.apache.jsieve.commands.Else");
46          map.put("elsif", "org.apache.jsieve.commands.Elsif");
47          map.put("require", "org.apache.jsieve.commands.Require");
48          map.put("stop", "org.apache.jsieve.commands.Stop");
49  
50          // Action Commands
51          // RFC3082 - Implementations MUST support these:
52          map.put("keep", "org.apache.jsieve.commands.Keep");
53          map.put("discard", "org.apache.jsieve.commands.Discard");
54          map.put("redirect", "org.apache.jsieve.commands.Redirect");
55          // RFC3082 - Implementations SHOULD support these:
56          map.put("reject", "org.apache.jsieve.commands.optional.Reject");
57          map.put("fileinto", "org.apache.jsieve.commands.optional.FileInto");
58  
59          boolean isTestPassed = false;
60          try {
61              Map commandMap = new ConfigurationManager().getCommandMap();
62  
63              Iterator mapIter = map.entrySet().iterator();
64              while (mapIter.hasNext()) {
65                  Map.Entry entry = (Map.Entry) mapIter.next();
66                  assertTrue("Key: " + entry.getKey(), commandMap
67                          .containsKey(entry.getKey()));
68                  assertTrue("Value: " + entry.getValue(), commandMap.get(
69                          entry.getKey()).equals(entry.getValue()));
70              }
71              isTestPassed = true;
72          } catch (SieveConfigurationException e) {
73          }
74          assertTrue(isTestPassed);
75      }
76  
77      /**
78       * Test the TestMap maps 'MUST' and 'SHOULD' be supported tests to the
79       * correct classes.
80       */
81      public void testTestMap() {
82          Map<String, String> map = new HashMap<String, String>();
83  
84          // RFC3082 - Implementations MUST support these tests:
85          map.put("address", "org.apache.jsieve.tests.Address");
86          map.put("allof", "org.apache.jsieve.tests.AllOf");
87          map.put("anyof", "org.apache.jsieve.tests.AnyOf");
88          map.put("exists", "org.apache.jsieve.tests.Exists");
89          map.put("false", "org.apache.jsieve.tests.False");
90          map.put("header", "org.apache.jsieve.tests.Header");
91          map.put("not", "org.apache.jsieve.tests.Not");
92          map.put("size", "org.apache.jsieve.tests.Size");
93          map.put("true", "org.apache.jsieve.tests.True");
94  
95          // RFC3082 - Implementations SHOULD support the "envelope" test.
96          map.put("envelope", "org.apache.jsieve.tests.optional.Envelope");
97  
98          boolean isTestPassed = false;
99          try {
100             Map testMap = new ConfigurationManager().getTestMap();
101 
102             Iterator mapIter = map.entrySet().iterator();
103             while (mapIter.hasNext()) {
104                 Map.Entry entry = (Map.Entry) mapIter.next();
105                 assertTrue("Key: " + entry.getKey(), testMap.containsKey(entry
106                         .getKey()));
107                 assertTrue("Value: " + entry.getValue(), testMap.get(
108                         entry.getKey()).equals(entry.getValue()));
109             }
110             isTestPassed = true;
111         } catch (SieveConfigurationException e) {
112         }
113         assertTrue(isTestPassed);
114     }
115 
116     /**
117      * Test the CommparatorMap maps 'MUST' and 'SHOULD' be supported comparators
118      * to the correct classes.
119      */
120     public void testComparatorMap() {
121         Map<String, String> map = new HashMap<String, String>();
122 
123         // RFC3082 - Required Comparators
124         map.put("i;octet", "org.apache.jsieve.comparators.Octet");
125         map
126                 .put("i;ascii-casemap",
127                         "org.apache.jsieve.comparators.AsciiCasemap");
128 
129         boolean isTestPassed = false;
130         try {
131             Map comparatorMap = new ConfigurationManager().getComparatorMap();
132 
133             Iterator mapIter = map.entrySet().iterator();
134             while (mapIter.hasNext()) {
135                 Map.Entry entry = (Map.Entry) mapIter.next();
136                 assertTrue("Key: " + entry.getKey(), comparatorMap
137                         .containsKey(entry.getKey()));
138                 assertTrue("Value: " + entry.getValue(), comparatorMap.get(
139                         entry.getKey()).equals(entry.getValue()));
140             }
141             isTestPassed = true;
142         } catch (SieveConfigurationException e) {
143         }
144         assertTrue(isTestPassed);
145     }
146 
147 }