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 junit.framework.TestCase;
23  
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.jsieve.BaseSieveContext;
26  import org.apache.jsieve.ConfigurationManager;
27  import org.apache.jsieve.utils.JUnitUtils;
28  import org.apache.jsieve.utils.SieveMailAdapter;
29  
30  public class AddressParseTest extends TestCase {
31  
32      private static final String MULTIPLE_ADDRESS_VALUES = "coyote@desert.example.org, bugs@example.org,  elmer@hunters.example.org";
33  
34      private static final String SOLO_ADDRESS_VALUES = "coyote@desert.example.org";
35  
36      BaseSieveContext context;
37  
38      SieveMailAdapter mail;
39  
40      OpenedAddress address;
41  
42      protected void setUp() throws Exception {
43          super.setUp();
44          ConfigurationManager configurationManager = new ConfigurationManager();
45          context = new BaseSieveContext(
46                  configurationManager.getCommandManager(), configurationManager
47                          .getComparatorManager(), configurationManager
48                          .getTestManager(), LogFactory
49                          .getLog(AddressParseTest.class));
50          mail = (SieveMailAdapter) JUnitUtils.createMail();
51          address = new OpenedAddress();
52      }
53  
54      public void testSingleAddress() throws Exception {
55          mail.getMessage().addHeader("From", SOLO_ADDRESS_VALUES);
56          assertTrue(address.match(mail, ":all", "i;ascii-casemap", ":is",
57                  "from", "coyote@desert.example.org", context));
58          assertFalse(address.match(mail, ":all", "i;ascii-casemap", ":is",
59                  "from", "elmer@hunters.example.org", context));
60          assertFalse(address.match(mail, ":all", "i;ascii-casemap", ":is",
61                  "from", "bugs@example.org", context));
62          assertFalse(address.match(mail, ":all", "i;ascii-casemap", ":is",
63                  "from", "roadrunner@example.org", context));
64      }
65  
66      public void testMultipleAddresses() throws Exception {
67          mail.getMessage().addHeader("From", MULTIPLE_ADDRESS_VALUES);
68          assertTrue(address.match(mail, ":all", "i;ascii-casemap", ":is",
69                  "from", "coyote@desert.example.org", context));
70          assertTrue(address.match(mail, ":all", "i;ascii-casemap", ":is",
71                  "from", "elmer@hunters.example.org", context));
72          assertTrue(address.match(mail, ":all", "i;ascii-casemap", ":is",
73                  "from", "bugs@example.org", context));
74          assertFalse(address.match(mail, ":all", "i;ascii-casemap", ":is",
75                  "from", "roadrunner@example.org", context));
76      }
77  }