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  package org.apache.jsieve.parser.address;
20  
21  import junit.framework.TestCase;
22  
23  import org.apache.jsieve.mail.MailAdapter.Address;
24  import org.apache.jsieve.parser.generated.address.ParseException;
25  
26  public class SieveAddressBuilderTest extends TestCase {
27  
28      public static final String DOMAIN = "example.org";
29  
30      public static final String COYOTE = "coyote";
31  
32      public static final String COYOTE_ADDRESS = COYOTE + "@" + DOMAIN;
33  
34      public static final String ROADRUNNER = "roadrunner";
35  
36      public static final String ROADRUNNER_ADDRESS = ROADRUNNER + "@" + DOMAIN;
37  
38      public static final String BUGS = "bugs";
39  
40      public static final String BUGS_ADDRESS = BUGS + "@" + DOMAIN;
41  
42      public static final String HEROS = ROADRUNNER_ADDRESS + " , "
43              + BUGS_ADDRESS;
44  
45      SieveAddressBuilder builder;
46  
47      protected void setUp() throws Exception {
48          super.setUp();
49          builder = new SieveAddressBuilder();
50      }
51  
52      public void testNotAddress() throws Exception {
53          try {
54              builder
55                      .addAddresses("What a load of rubbish - not an address in sight!");
56              fail("Parsing should fail when the input is not an address");
57          } catch (ParseException e) {
58              // expected
59          }
60      }
61  
62      public void testAddAddresses() throws Exception {
63          assertNotNull(builder.getAddresses());
64          builder.addAddresses(COYOTE_ADDRESS);
65          Address[] addresses = builder.getAddresses();
66          assertNotNull(addresses);
67          assertEquals(1, addresses.length);
68          assertEquals(COYOTE, addresses[0].getLocalPart());
69          assertEquals(DOMAIN, addresses[0].getDomain());
70          builder.addAddresses(HEROS);
71          addresses = builder.getAddresses();
72          assertNotNull(addresses);
73          assertEquals(3, addresses.length);
74          assertEquals(COYOTE, addresses[0].getLocalPart());
75          assertEquals(DOMAIN, addresses[0].getDomain());
76          assertEquals(ROADRUNNER, addresses[1].getLocalPart());
77          assertEquals(DOMAIN, addresses[1].getDomain());
78          assertEquals(BUGS, addresses[2].getLocalPart());
79          assertEquals(DOMAIN, addresses[2].getDomain());
80      }
81  
82      public void testReset() throws Exception {
83          assertNotNull(builder.getAddresses());
84          builder.addAddresses(COYOTE_ADDRESS);
85          Address[] addresses = builder.getAddresses();
86          assertNotNull(addresses);
87          assertEquals(1, addresses.length);
88          assertEquals(COYOTE, addresses[0].getLocalPart());
89          assertEquals(DOMAIN, addresses[0].getDomain());
90          addresses = builder.getAddresses();
91          assertNotNull(addresses);
92          assertEquals(1, addresses.length);
93          assertEquals(COYOTE, addresses[0].getLocalPart());
94          assertEquals(DOMAIN, addresses[0].getDomain());
95          builder.reset();
96          addresses = builder.getAddresses();
97          assertNotNull(addresses);
98          assertEquals(0, addresses.length);
99          builder.addAddresses(COYOTE_ADDRESS);
100         addresses = builder.getAddresses();
101         assertNotNull(addresses);
102         assertEquals(1, addresses.length);
103         assertEquals(COYOTE, addresses[0].getLocalPart());
104         assertEquals(DOMAIN, addresses[0].getDomain());
105     }
106 }