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.mailet;
21  
22  import javax.mail.internet.AddressException;
23  import javax.mail.internet.InternetAddress;
24  import junit.framework.TestCase;
25  
26  
27  /**
28   * Tests for Test method for {@link org.apache.mailet.MailAddress}.
29   */
30  public class MailAddressTest extends TestCase {
31      private static final String GOOD_LOCAL_PART = "\"quoted@local part\"";
32      private static final String GOOD_QUOTED_LOCAL_PART = "\"quoted@local part\"@james.apache.org";
33      private static final String BAD_LOCAL_PART_1 = "quoted local-part@james.apache.org";
34      private static final String BAD_LOCAL_PART_2 = "quoted@local-part@james.apache.org";
35      private static final String BAD_LOCAL_PART_3 = "local-part.@james.apache.org";
36      private static final String GOOD_ADDRESS = "server-dev@james.apache.org";
37      private static final String GOOD_DOMAIN = "james.apache.org";
38      
39      private static final String GOOD_DLIT = "server-dev@[127.0.0.1]";
40      private static final String BAD_DLIT_1 = "server-dev@[300.0.0.1]";
41      private static final String BAD_DLIT_2 = "server-dev@[127.0.1]";
42  
43      /**
44       * Test method for {@link org.apache.mailet.MailAddress#hashCode()}.
45       * @throws AddressException 
46       */
47      public void testHashCode() throws AddressException {
48  
49          MailAddress a = new MailAddress(GOOD_ADDRESS);
50          MailAddress b = new MailAddress(GOOD_ADDRESS);
51          assertTrue(a.hashCode()+" != "+ b.hashCode(),a.hashCode() == b.hashCode());
52      }
53  
54      /**
55       * Test method for {@link org.apache.mailet.MailAddress#MailAddress(java.lang.String)}.
56       * @throws AddressException 
57       */
58      public void testMailAddressString() throws AddressException {
59  
60          MailAddress a = new MailAddress(GOOD_ADDRESS);
61          assertTrue(GOOD_ADDRESS.equals(a.toString()));
62          try{
63              a = new MailAddress(GOOD_QUOTED_LOCAL_PART);
64          }catch (AddressException e){
65              assertTrue(e.getMessage(), false);
66          }
67          try{
68              a = new MailAddress(GOOD_DLIT);
69          }catch (AddressException e){
70              assertTrue(e.getMessage(), false);
71          }
72          try{
73              a = new MailAddress(BAD_LOCAL_PART_1);
74              assertFalse(BAD_LOCAL_PART_1,true);
75          }catch (AddressException e){
76              assertTrue(true);
77          }
78          
79          try{
80              a = new MailAddress(BAD_LOCAL_PART_2);
81              assertFalse(BAD_LOCAL_PART_2,true);
82          }catch (AddressException e){
83               assertTrue(true);
84          }
85          
86          try{
87              a = new MailAddress(BAD_LOCAL_PART_3);
88              assertFalse(BAD_LOCAL_PART_3,true);
89          }catch (AddressException e){
90              assertTrue(true);
91          }
92          try{
93              a = new MailAddress(BAD_DLIT_1);
94              assertFalse(BAD_DLIT_1,true);
95          }catch (AddressException e){
96              assertTrue(true);
97          }
98          try{
99              a = new MailAddress(BAD_DLIT_2);
100             assertFalse(BAD_DLIT_2,true);
101         }catch (AddressException e){
102             assertTrue(true);
103         }
104     }
105 
106     /**
107      * Test method for {@link org.apache.mailet.MailAddress#MailAddress(java.lang.String, java.lang.String)}.
108      */
109     public void testMailAddressStringString() {
110 
111         try{
112             MailAddress a = new MailAddress("local-part", "domain");
113         }catch (AddressException e){
114             assertTrue(e.getMessage(), false);
115         }
116         try{
117             MailAddress a = new MailAddress("local-part", "-domain");
118             assertFalse(a.toString(),true);
119         }catch (AddressException e){
120             assertTrue(true);
121         }
122     }
123 
124     /**
125      * Test method for {@link org.apache.mailet.MailAddress#MailAddress(javax.mail.internet.InternetAddress)}.
126      */
127     public void testMailAddressInternetAddress() {
128 
129         try{
130             MailAddress a = new MailAddress(new InternetAddress(GOOD_QUOTED_LOCAL_PART));
131         }catch (AddressException e){
132             System.out.println("AddressException"+e.getMessage());
133             assertTrue(e.getMessage(), false);
134         }
135     }
136 
137     /**
138      * Test method for {@link org.apache.mailet.MailAddress#getDomain()}.
139      */
140     public void testGetDomain() {
141 
142         try{
143             MailAddress a = new MailAddress(new InternetAddress(GOOD_ADDRESS));
144             assertTrue(a.getDomain()+" != "+GOOD_DOMAIN,a.getDomain().equals(GOOD_DOMAIN));
145         }catch (AddressException e){
146             System.out.println("AddressException"+e.getMessage());
147             assertTrue(e.getMessage(), false);
148         }
149     }
150 
151     /**
152      * Test method for {@link org.apache.mailet.MailAddress#getLocalPart()}.
153      */
154     public void testGetLocalPart() {
155 
156         try{
157             MailAddress a = new MailAddress(new InternetAddress(GOOD_QUOTED_LOCAL_PART));
158             assertTrue(GOOD_LOCAL_PART+" != "+a.getLocalPart(),a.getLocalPart().equals(GOOD_LOCAL_PART));
159         }catch (AddressException e){
160             System.out.println("AddressException"+e.getMessage());
161             assertTrue(e.getMessage(), false);
162         }
163     }
164 
165     /**
166      * Test method for {@link org.apache.mailet.MailAddress#toString()}.
167      */
168     public void testToString() {
169 
170         try{
171             MailAddress a = new MailAddress(new InternetAddress(GOOD_ADDRESS));
172             assertTrue(a.toString()+" != "+GOOD_ADDRESS,a.toString().equals(GOOD_ADDRESS));
173         }catch (AddressException e){
174             System.out.println("AddressException"+e.getMessage());
175             assertTrue(e.getMessage(), false);
176         }
177     }
178 
179     /**
180      * Test method for {@link org.apache.mailet.MailAddress#toInternetAddress()}.
181      */
182     public void testToInternetAddress() {
183 
184         try{
185             InternetAddress b = new InternetAddress(GOOD_ADDRESS);
186             MailAddress a = new MailAddress(b);
187             assertTrue(a.toInternetAddress().equals(b));
188             assertTrue(a.toString()+" != "+GOOD_ADDRESS,a.toString().equals(GOOD_ADDRESS));
189         }catch (AddressException e){
190             System.out.println("AddressException"+e.getMessage());
191             assertTrue(e.getMessage(), false);
192         }
193     }
194 
195     /**
196      * Test method for {@link org.apache.mailet.MailAddress#equals(java.lang.Object)}.
197      * @throws AddressException 
198      */
199     public void testEqualsObject() throws AddressException {
200 
201         MailAddress a = new MailAddress(GOOD_ADDRESS);
202         MailAddress b = new MailAddress(GOOD_ADDRESS);
203         
204         assertTrue(a.toString()+" != "+b.toString(),a.equals(b));
205     }
206 }