source: contrib/MailArchiver/sources/vendor/mime4j/apache-mime4j-0.7-SNAPSHOT-20110327.010440-17/dom/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java @ 6785

Revision 6785, 18.8 KB checked in by rafaelraymundo, 12 years ago (diff)

Ticket #2946 - Liberado codigo do MailArchiver?. Documentação na subpasta DOCS.

Line 
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
20package org.apache.james.mime4j.field.address;
21
22import org.apache.james.mime4j.MimeException;
23import org.apache.james.mime4j.dom.address.Address;
24import org.apache.james.mime4j.dom.address.AddressList;
25import org.apache.james.mime4j.dom.address.DomainList;
26import org.apache.james.mime4j.dom.address.Group;
27import org.apache.james.mime4j.dom.address.Mailbox;
28import org.apache.james.mime4j.dom.address.MailboxList;
29import org.apache.james.mime4j.field.address.ParseException;
30
31import java.util.ArrayList;
32import java.util.Arrays;
33import java.util.List;
34
35import junit.framework.TestCase;
36
37public class AddressTest extends TestCase {
38
39    public void testExceptionTree() {
40        // make sure that our ParseException extends MimeException.
41        assertTrue(MimeException.class.isAssignableFrom(ParseException.class));
42    }
43
44    public void testParse1() throws ParseException {
45        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList(
46                "John Doe <jdoe@machine(comment).  example>");
47        assertEquals(1, addrList.size());
48        Mailbox mailbox = (Mailbox)addrList.get(0);
49        assertEquals("John Doe", mailbox.getName());
50        assertEquals("jdoe", mailbox.getLocalPart());
51        assertEquals("machine.example", mailbox.getDomain());
52    }
53
54    public void testParse2() throws ParseException {
55        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList(
56                "Mary Smith \t    \t\t  <mary@example.net>");
57        assertEquals(1, addrList.size());
58        Mailbox mailbox = (Mailbox)addrList.get(0);
59        assertEquals("Mary Smith", mailbox.getName());
60        assertEquals("mary", mailbox.getLocalPart());
61        assertEquals("example.net", mailbox.getDomain());
62    }
63
64    public void testEmptyGroup() throws ParseException {
65        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("undisclosed-recipients:;");
66        assertEquals(1, addrList.size());
67        Group group = (Group)addrList.get(0);
68        assertEquals(0, group.getMailboxes().size());
69        assertEquals("undisclosed-recipients", group.getName());
70    }
71
72    public void testMessyGroupAndMailbox() throws ParseException {
73        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList(
74                "Marketing  folks :  Jane Smith < jane @ example . net >," +
75                " \" Jack \\\"Jackie\\\" Jones \" < jjones@example.com > (comment(comment)); ,, (comment)  ," +
76                " <@example . net,@example(ignore\\)).com:(ignore)john@(ignore)example.net>");
77        assertEquals(2, addrList.size());
78
79        Group group = (Group)addrList.get(0);
80        assertEquals("Marketing  folks", group.getName());
81        assertEquals(2, group.getMailboxes().size());
82
83        Mailbox mailbox1 = group.getMailboxes().get(0);
84        Mailbox mailbox2 = group.getMailboxes().get(1);
85
86        assertEquals("Jane Smith", mailbox1.getName());
87        assertEquals("jane", mailbox1.getLocalPart());
88        assertEquals("example.net", mailbox1.getDomain());
89
90        assertEquals(" Jack \"Jackie\" Jones ", mailbox2.getName());
91        assertEquals("jjones", mailbox2.getLocalPart());
92        assertEquals("example.com", mailbox2.getDomain());
93
94        Mailbox mailbox = (Mailbox)addrList.get(1);
95        assertEquals("john", mailbox.getLocalPart());
96        assertEquals("example.net", mailbox.getDomain());
97        assertEquals(2, mailbox.getRoute().size());
98        assertEquals("example.net", mailbox.getRoute().get(0));
99        assertEquals("example.com", mailbox.getRoute().get(1));
100    }
101
102    public void testEmptyAddressList() throws ParseException {
103        assertEquals(0, AddressBuilder.DEFAULT.parseAddressList("  \t   \t ").size());
104        assertEquals(0, AddressBuilder.DEFAULT.parseAddressList("  \t  ,  , , ,,, , \t ").size());
105    }
106
107    public void testSimpleForm() throws ParseException {
108        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("\"a b c d e f g\" (comment) @example.net");
109        assertEquals(1, addrList.size());
110        Mailbox mailbox = (Mailbox)addrList.get(0);
111        assertEquals("a b c d e f g", mailbox.getLocalPart());
112        assertEquals("example.net", mailbox.getDomain());
113    }
114
115    public void testFlatten() throws ParseException {
116        AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("dev : one@example.com, two@example.com; , ,,, marketing:three@example.com ,four@example.com;, five@example.com");
117        assertEquals(3, addrList.size());
118        assertEquals(5, addrList.flatten().size());
119    }
120
121    public void testTortureTest() throws ParseException {
122
123        // Source: http://mailformat.dan.info/headers/from.html
124        // (Commented out pending confirmation of legality--I think the local-part is illegal.)
125        // AddressList.parse("\"Guy Macon\" <guymacon+\" http://www.guymacon.com/ \"00@spamcop.net>");
126
127        // Taken mostly from RFC822.
128
129        // Just make sure these are recognized as legal address lists;
130        // there shouldn't be any aspect of the RFC that is tested here
131        // but not in the other unit tests.
132
133        AddressBuilder.DEFAULT.parseAddressList("Alfred Neuman <Neuman@BBN-TENEXA>");
134        AddressBuilder.DEFAULT.parseAddressList("Neuman@BBN-TENEXA");
135        AddressBuilder.DEFAULT.parseAddressList("\"George, Ted\" <Shared@Group.Arpanet>");
136        AddressBuilder.DEFAULT.parseAddressList("Wilt . (the Stilt) Chamberlain@NBA.US");
137
138        // NOTE: In RFC822 8.1.5, the following example did not have "Galloping Gourmet"
139        // in double-quotes.  I can only assume this was a typo, since 6.2.4 specifically
140        // disallows spaces in unquoted local-part.
141        AddressBuilder.DEFAULT.parseAddressList("     Gourmets:  Pompous Person <WhoZiWhatZit@Cordon-Bleu>," +
142                "                Childs@WGBH.Boston, \"Galloping Gourmet\"@" +
143                "                ANT.Down-Under (Australian National Television)," +
144                "                Cheapie@Discount-Liquors;," +
145                "       Cruisers:  Port@Portugal, Jones@SEA;," +
146                "         Another@Somewhere.SomeOrg");
147
148        // NOTE: In RFC822 8.3.3, the following example ended with a lone ">" after
149        // Tops-20-Host.  I can only assume this was a typo, since 6.1 clearly shows
150        // ">" requires a matching "<".
151        AddressBuilder.DEFAULT.parseAddressList("Important folk:" +
152                "                   Tom Softwood <Balsa@Tree.Root>," +
153                "                   \"Sam Irving\"@Other-Host;," +
154                "                 Standard Distribution:" +
155                "                   /main/davis/people/standard@Other-Host," +
156                "                   \"<Jones>standard.dist.3\"@Tops-20-Host;");
157
158        // The following are from a Usenet post by Dan J. Bernstein:
159        // http://groups.google.com/groups?selm=1996Aug1418.21.01.28081%40koobera.math.uic.edu
160        AddressBuilder.DEFAULT.parseAddressList("\":sysmail\"@  Some-Group.\t         Some-Org, Muhammed.(I am  the greatest) Ali @(the)Vegas.WBA");
161        AddressBuilder.DEFAULT.parseAddressList("me@home.com (comment (nested (deeply\\))))");
162        AddressBuilder.DEFAULT.parseAddressList("mailing list: me@home.com, route two <you@work.com>, them@play.com ;");
163
164    }
165
166    public void testLexicalError() {
167        // ensure that TokenMgrError doesn't get thrown
168        try {
169            AddressBuilder.DEFAULT.parseAddressList(")");
170            fail("Expected parsing error");
171        }
172        catch (ParseException e) {
173
174        }
175    }
176   
177    public void testNullConstructorAndBadUsage() {
178        AddressList al = new AddressList(null, false);
179        assertEquals(0, al.size());
180       
181        try {
182            al.get(-1);
183            fail("Expected index out of bound exception!");
184        } catch (IndexOutOfBoundsException e) {
185        }
186       
187        try {
188            al.get(0);
189            fail("Expected index out of bound exception!");
190        } catch (IndexOutOfBoundsException e) {
191        }
192    }
193
194   
195    public void testAddressList() throws ParseException {
196        AddressList addlist = AddressBuilder.DEFAULT.parseAddressList("foo@example.com, bar@example.com, third@example.com");
197        List<Address> al = new ArrayList<Address>();
198        al.add(addlist.get(0));
199
200        // shared arraylist
201        AddressList dl = new AddressList(al, true);
202        assertEquals(1, dl.size());
203        al.add(addlist.get(1));
204        assertEquals(2, dl.size());
205       
206        // cloned arraylist
207        AddressList dlcopy = new AddressList(al, false);
208        assertEquals(2, dlcopy.size());
209        al.add(addlist.get(2));
210        assertEquals(2, dlcopy.size());
211       
212        // check route string
213        assertEquals(2, dlcopy.flatten().size());
214    }
215
216    public void testEmptyDomainList() {
217        DomainList dl = new DomainList(null, false);
218        assertEquals(0, dl.size());
219       
220        try {
221            dl.get(-1);
222            fail("Expected index out of bound exception!");
223        } catch (IndexOutOfBoundsException e) {
224        }
225       
226        try {
227            dl.get(0);
228            fail("Expected index out of bound exception!");
229        } catch (IndexOutOfBoundsException e) {
230        }
231    }
232   
233    public void testDomainList() {
234        List<String> al = new ArrayList<String>();
235        al.add("example.com");
236
237        // shared arraylist
238        DomainList dl = new DomainList(al, true);
239        assertEquals(1, dl.size());
240        al.add("foo.example.com");
241        assertEquals(2, dl.size());
242       
243        // cloned arraylist
244        DomainList dlcopy = new DomainList(al, false);
245        assertEquals(2, dlcopy.size());
246        al.add("bar.example.com");
247        assertEquals(2, dlcopy.size());
248       
249        // check route string
250        assertEquals("@example.com,@foo.example.com", dlcopy.toRouteString());
251    }
252   
253
254    public void testEmptyMailboxList() {
255        MailboxList ml = new MailboxList(null, false);
256        assertEquals(0, ml.size());
257       
258        try {
259            ml.get(-1);
260            fail("Expected index out of bound exception!");
261        } catch (IndexOutOfBoundsException e) {
262        }
263       
264        try {
265            ml.get(0);
266            fail("Expected index out of bound exception!");
267        } catch (IndexOutOfBoundsException e) {
268        }
269    }
270
271    public void testMailboxList() {
272        List<Mailbox> al = new ArrayList<Mailbox>();
273        al.add(new Mailbox("local","example.com"));
274
275        // shared arraylist
276        MailboxList ml = new MailboxList(al, true);
277        assertEquals(1, ml.size());
278        al.add(new Mailbox("local2", "foo.example.com"));
279        assertEquals(2, ml.size());
280       
281        // cloned arraylist
282        MailboxList mlcopy = new MailboxList(al, false);
283        assertEquals(2, mlcopy.size());
284        al.add(new Mailbox("local3", "bar.example.com"));
285        assertEquals(2, mlcopy.size());
286    }
287   
288    public void testGroupSerialization() {
289        List<Mailbox> al = new ArrayList<Mailbox>();
290        al.add(new Mailbox("test", "example.com"));
291        al.add(new Mailbox("Foo!", "foo", "example.com"));
292        DomainList dl = new DomainList(new ArrayList<String>(
293                Arrays.asList(new String[] {"foo.example.com"})), true);
294        Mailbox mailbox = new Mailbox("Foo Bar", dl, "foo2", "example.com");
295        assertSame(dl, mailbox.getRoute());
296        al.add(mailbox);
297        Group g = new Group("group", new MailboxList(al, false));
298        String s = AddressFormatter.DEFAULT.format(g, false);
299        assertEquals("group: test@example.com, Foo! <foo@example.com>, Foo Bar <foo2@example.com>;", s);
300    }
301   
302    public void testEmptyQuotedStringBeforeDotAtomInLocalPart() throws Exception {
303        /*
304         * This used to give a StringIndexOutOfBoundsException instead of the expected
305         * ParseException
306         */
307        try {
308            AddressBuilder.DEFAULT.parseAddressList("\"\"bar@bar.com");
309            fail("ParseException expected");
310        } catch (ParseException pe) {
311        }
312    }
313   
314    public void testMailboxGetEncodedString() throws Exception {
315        Mailbox m1 = new Mailbox("john.doe", "acme.org");
316        assertEquals("john.doe@acme.org", AddressFormatter.DEFAULT.encode(m1));
317        Mailbox m2 = new Mailbox("john doe", "acme.org");
318        assertEquals("\"john doe\"@acme.org", AddressFormatter.DEFAULT.encode(m2));
319        Mailbox m3 = new Mailbox("John Doe", "john.doe", "acme.org");
320        assertEquals("John Doe <john.doe@acme.org>", AddressFormatter.DEFAULT.encode(m3));
321        Mailbox m4 = new Mailbox("John Doe @Home", "john.doe", "acme.org");
322        assertEquals("\"John Doe @Home\" <john.doe@acme.org>", AddressFormatter.DEFAULT.encode(m4));
323        Mailbox m5 = new Mailbox("Hans M\374ller", "hans.mueller", "acme.org");
324        assertEquals("=?ISO-8859-1?Q?Hans_M=FCller?= <hans.mueller@acme.org>",
325                AddressFormatter.DEFAULT.encode(m5));
326    }
327
328    public void testGroupGetEncodedString() throws Exception {
329        List<Mailbox> al = new ArrayList<Mailbox>();
330        al.add(new Mailbox("test", "example.com"));
331        al.add(new Mailbox("Foo!", "foo", "example.com"));
332        al.add(new Mailbox("Hans M\374ller", "hans.mueller", "acme.org"));
333        Group g = new Group("group @work", new MailboxList(al, false));
334        assertEquals("\"group @work\": test@example.com, "
335                + "Foo! <foo@example.com>, =?ISO-8859-1?Q?Hans_M=FCller?="
336                + " <hans.mueller@acme.org>;", AddressFormatter.DEFAULT.encode(g));
337    }
338
339    public void testEmptyGroupGetEncodedString() throws Exception {
340        MailboxList emptyMailboxes = new MailboxList(null, true);
341        Group g = new Group("Undisclosed recipients", emptyMailboxes);
342        assertEquals("Undisclosed recipients:;", AddressFormatter.DEFAULT.encode(g));
343    }
344
345    public void testParseAddress() throws Exception {
346        Address address = AddressBuilder.DEFAULT.parseAddress("Mary Smith <mary@example.net>");
347        assertTrue(address instanceof Mailbox);
348        assertEquals("Mary Smith", ((Mailbox) address).getName());
349        assertEquals("mary@example.net", ((Mailbox) address).getAddress());
350
351        address = AddressBuilder.DEFAULT.parseAddress("group: Mary Smith <mary@example.net>;");
352        assertTrue(address instanceof Group);
353        assertEquals("group", ((Group) address).getName());
354        assertEquals("Mary Smith", ((Group) address).getMailboxes().get(0)
355                .getName());
356        assertEquals("mary@example.net", ((Group) address).getMailboxes()
357                .get(0).getAddress());
358
359        try {
360            AddressBuilder.DEFAULT.parseGroup("john.doe@acme.org, jane.doe@acme.org");
361            fail();
362        } catch (ParseException expected) {
363        }
364    }
365   
366    public void testParseGroup() throws Exception {
367        Group group = AddressBuilder.DEFAULT.parseGroup(
368                "group: john.doe@acme.org, Mary Smith <mary@example.net>;");
369        assertEquals("group", group.getName());
370
371        MailboxList mailboxes = group.getMailboxes();
372        assertEquals(2, mailboxes.size());
373
374        Mailbox mailbox1 = mailboxes.get(0);
375        assertNull(mailbox1.getName());
376        assertEquals("john.doe@acme.org", mailbox1.getAddress());
377
378        Mailbox mailbox2 = mailboxes.get(1);
379        assertEquals("Mary Smith", mailbox2.getName());
380        assertEquals("mary@example.net", mailbox2.getAddress());
381
382        try {
383            AddressBuilder.DEFAULT.parseGroup("john.doe@acme.org");
384            fail();
385        } catch (ParseException expected) {
386        }
387
388        try {
389            AddressBuilder.DEFAULT.parseGroup("g1: john.doe@acme.org;, g2: mary@example.net;");
390            fail();
391        } catch (ParseException expected) {
392        }
393    }
394
395    public void testParseMailbox() throws Exception {
396        Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@acme.org");
397        assertNull(mailbox1.getName());
398        assertEquals("john.doe@acme.org", mailbox1.getAddress());
399
400        Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith <mary@example.net>");
401        assertEquals("Mary Smith", mailbox2.getName());
402        assertEquals("mary@example.net", mailbox2.getAddress());
403
404        // non-ascii should be allowed in quoted strings
405        Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox(
406                "\"Hans M\374ller\" <hans.mueller@acme.org>");
407        assertEquals("Hans M\374ller", mailbox3.getName());
408        assertEquals("hans.mueller@acme.org", mailbox3.getAddress());
409
410        try {
411            AddressBuilder.DEFAULT.parseMailbox("g: Mary Smith <mary@example.net>;");
412            fail();
413        } catch (ParseException expected) {
414        }
415
416        try {
417            AddressBuilder.DEFAULT.parseMailbox(
418                    "Mary Smith <mary@example.net>, hans.mueller@acme.org");
419            fail();
420        } catch (ParseException expected) {
421        }
422    }
423
424    public void testMailboxEquals() throws Exception {
425        Mailbox m1 = new Mailbox("john.doe", "acme.org");
426        Mailbox m2 = new Mailbox("john doe", "acme.org");
427        Mailbox m3 = new Mailbox("john.doe", "Acme.Org");
428        Mailbox m4 = new Mailbox("john.doe", null);
429        assertTrue(m1.equals(m1));
430        assertFalse(m1.equals(m2));
431        assertTrue(m1.equals(m3));
432        assertFalse(m1.equals(m4));
433        assertFalse(m1.equals(null));
434    }
435   
436    public void testMailboxHashCode() throws Exception {
437        Mailbox m1 = new Mailbox("john.doe", "acme.org");
438        Mailbox m2 = new Mailbox("john doe", "acme.org");
439        Mailbox m3 = new Mailbox("john.doe", "Acme.Org");
440        Mailbox m4 = new Mailbox("john.doe", null);
441        assertTrue(m1.hashCode() == m1.hashCode());
442        assertFalse(m1.hashCode() == m2.hashCode());
443        assertTrue(m1.hashCode() == m3.hashCode());
444        assertFalse(m1.hashCode() == m4.hashCode());
445    }
446
447}
Note: See TracBrowser for help on using the repository browser.