/**************************************************************** * Licensed to the Apache Software Foundation (ASF) under one * * or more contributor license agreements. See the NOTICE file * * distributed with this work for additional information * * regarding copyright ownership. The ASF licenses this file * * to you under the Apache License, Version 2.0 (the * * "License"); you may not use this file except in compliance * * with the License. You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/ package org.apache.james.mime4j.field.address; import org.apache.james.mime4j.MimeException; import org.apache.james.mime4j.dom.address.Address; import org.apache.james.mime4j.dom.address.AddressList; import org.apache.james.mime4j.dom.address.DomainList; import org.apache.james.mime4j.dom.address.Group; import org.apache.james.mime4j.dom.address.Mailbox; import org.apache.james.mime4j.dom.address.MailboxList; import org.apache.james.mime4j.field.address.ParseException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import junit.framework.TestCase; public class AddressTest extends TestCase { public void testExceptionTree() { // make sure that our ParseException extends MimeException. assertTrue(MimeException.class.isAssignableFrom(ParseException.class)); } public void testParse1() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList( "John Doe "); assertEquals(1, addrList.size()); Mailbox mailbox = (Mailbox)addrList.get(0); assertEquals("John Doe", mailbox.getName()); assertEquals("jdoe", mailbox.getLocalPart()); assertEquals("machine.example", mailbox.getDomain()); } public void testParse2() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList( "Mary Smith \t \t\t "); assertEquals(1, addrList.size()); Mailbox mailbox = (Mailbox)addrList.get(0); assertEquals("Mary Smith", mailbox.getName()); assertEquals("mary", mailbox.getLocalPart()); assertEquals("example.net", mailbox.getDomain()); } public void testEmptyGroup() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("undisclosed-recipients:;"); assertEquals(1, addrList.size()); Group group = (Group)addrList.get(0); assertEquals(0, group.getMailboxes().size()); assertEquals("undisclosed-recipients", group.getName()); } public void testMessyGroupAndMailbox() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList( "Marketing folks : Jane Smith < jane @ example . net >," + " \" Jack \\\"Jackie\\\" Jones \" < jjones@example.com > (comment(comment)); ,, (comment) ," + " <@example . net,@example(ignore\\)).com:(ignore)john@(ignore)example.net>"); assertEquals(2, addrList.size()); Group group = (Group)addrList.get(0); assertEquals("Marketing folks", group.getName()); assertEquals(2, group.getMailboxes().size()); Mailbox mailbox1 = group.getMailboxes().get(0); Mailbox mailbox2 = group.getMailboxes().get(1); assertEquals("Jane Smith", mailbox1.getName()); assertEquals("jane", mailbox1.getLocalPart()); assertEquals("example.net", mailbox1.getDomain()); assertEquals(" Jack \"Jackie\" Jones ", mailbox2.getName()); assertEquals("jjones", mailbox2.getLocalPart()); assertEquals("example.com", mailbox2.getDomain()); Mailbox mailbox = (Mailbox)addrList.get(1); assertEquals("john", mailbox.getLocalPart()); assertEquals("example.net", mailbox.getDomain()); assertEquals(2, mailbox.getRoute().size()); assertEquals("example.net", mailbox.getRoute().get(0)); assertEquals("example.com", mailbox.getRoute().get(1)); } public void testEmptyAddressList() throws ParseException { assertEquals(0, AddressBuilder.DEFAULT.parseAddressList(" \t \t ").size()); assertEquals(0, AddressBuilder.DEFAULT.parseAddressList(" \t , , , ,,, , \t ").size()); } public void testSimpleForm() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("\"a b c d e f g\" (comment) @example.net"); assertEquals(1, addrList.size()); Mailbox mailbox = (Mailbox)addrList.get(0); assertEquals("a b c d e f g", mailbox.getLocalPart()); assertEquals("example.net", mailbox.getDomain()); } public void testFlatten() throws ParseException { AddressList addrList = AddressBuilder.DEFAULT.parseAddressList("dev : one@example.com, two@example.com; , ,,, marketing:three@example.com ,four@example.com;, five@example.com"); assertEquals(3, addrList.size()); assertEquals(5, addrList.flatten().size()); } public void testTortureTest() throws ParseException { // Source: http://mailformat.dan.info/headers/from.html // (Commented out pending confirmation of legality--I think the local-part is illegal.) // AddressList.parse("\"Guy Macon\" "); // Taken mostly from RFC822. // Just make sure these are recognized as legal address lists; // there shouldn't be any aspect of the RFC that is tested here // but not in the other unit tests. AddressBuilder.DEFAULT.parseAddressList("Alfred Neuman "); AddressBuilder.DEFAULT.parseAddressList("Neuman@BBN-TENEXA"); AddressBuilder.DEFAULT.parseAddressList("\"George, Ted\" "); AddressBuilder.DEFAULT.parseAddressList("Wilt . (the Stilt) Chamberlain@NBA.US"); // NOTE: In RFC822 8.1.5, the following example did not have "Galloping Gourmet" // in double-quotes. I can only assume this was a typo, since 6.2.4 specifically // disallows spaces in unquoted local-part. AddressBuilder.DEFAULT.parseAddressList(" Gourmets: Pompous Person ," + " Childs@WGBH.Boston, \"Galloping Gourmet\"@" + " ANT.Down-Under (Australian National Television)," + " Cheapie@Discount-Liquors;," + " Cruisers: Port@Portugal, Jones@SEA;," + " Another@Somewhere.SomeOrg"); // NOTE: In RFC822 8.3.3, the following example ended with a lone ">" after // Tops-20-Host. I can only assume this was a typo, since 6.1 clearly shows // ">" requires a matching "<". AddressBuilder.DEFAULT.parseAddressList("Important folk:" + " Tom Softwood ," + " \"Sam Irving\"@Other-Host;," + " Standard Distribution:" + " /main/davis/people/standard@Other-Host," + " \"standard.dist.3\"@Tops-20-Host;"); // The following are from a Usenet post by Dan J. Bernstein: // http://groups.google.com/groups?selm=1996Aug1418.21.01.28081%40koobera.math.uic.edu AddressBuilder.DEFAULT.parseAddressList("\":sysmail\"@ Some-Group.\t Some-Org, Muhammed.(I am the greatest) Ali @(the)Vegas.WBA"); AddressBuilder.DEFAULT.parseAddressList("me@home.com (comment (nested (deeply\\))))"); AddressBuilder.DEFAULT.parseAddressList("mailing list: me@home.com, route two , them@play.com ;"); } public void testLexicalError() { // ensure that TokenMgrError doesn't get thrown try { AddressBuilder.DEFAULT.parseAddressList(")"); fail("Expected parsing error"); } catch (ParseException e) { } } public void testNullConstructorAndBadUsage() { AddressList al = new AddressList(null, false); assertEquals(0, al.size()); try { al.get(-1); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } try { al.get(0); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } } public void testAddressList() throws ParseException { AddressList addlist = AddressBuilder.DEFAULT.parseAddressList("foo@example.com, bar@example.com, third@example.com"); List
al = new ArrayList
(); al.add(addlist.get(0)); // shared arraylist AddressList dl = new AddressList(al, true); assertEquals(1, dl.size()); al.add(addlist.get(1)); assertEquals(2, dl.size()); // cloned arraylist AddressList dlcopy = new AddressList(al, false); assertEquals(2, dlcopy.size()); al.add(addlist.get(2)); assertEquals(2, dlcopy.size()); // check route string assertEquals(2, dlcopy.flatten().size()); } public void testEmptyDomainList() { DomainList dl = new DomainList(null, false); assertEquals(0, dl.size()); try { dl.get(-1); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } try { dl.get(0); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } } public void testDomainList() { List al = new ArrayList(); al.add("example.com"); // shared arraylist DomainList dl = new DomainList(al, true); assertEquals(1, dl.size()); al.add("foo.example.com"); assertEquals(2, dl.size()); // cloned arraylist DomainList dlcopy = new DomainList(al, false); assertEquals(2, dlcopy.size()); al.add("bar.example.com"); assertEquals(2, dlcopy.size()); // check route string assertEquals("@example.com,@foo.example.com", dlcopy.toRouteString()); } public void testEmptyMailboxList() { MailboxList ml = new MailboxList(null, false); assertEquals(0, ml.size()); try { ml.get(-1); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } try { ml.get(0); fail("Expected index out of bound exception!"); } catch (IndexOutOfBoundsException e) { } } public void testMailboxList() { List al = new ArrayList(); al.add(new Mailbox("local","example.com")); // shared arraylist MailboxList ml = new MailboxList(al, true); assertEquals(1, ml.size()); al.add(new Mailbox("local2", "foo.example.com")); assertEquals(2, ml.size()); // cloned arraylist MailboxList mlcopy = new MailboxList(al, false); assertEquals(2, mlcopy.size()); al.add(new Mailbox("local3", "bar.example.com")); assertEquals(2, mlcopy.size()); } public void testGroupSerialization() { List al = new ArrayList(); al.add(new Mailbox("test", "example.com")); al.add(new Mailbox("Foo!", "foo", "example.com")); DomainList dl = new DomainList(new ArrayList( Arrays.asList(new String[] {"foo.example.com"})), true); Mailbox mailbox = new Mailbox("Foo Bar", dl, "foo2", "example.com"); assertSame(dl, mailbox.getRoute()); al.add(mailbox); Group g = new Group("group", new MailboxList(al, false)); String s = AddressFormatter.DEFAULT.format(g, false); assertEquals("group: test@example.com, Foo! , Foo Bar ;", s); } public void testEmptyQuotedStringBeforeDotAtomInLocalPart() throws Exception { /* * This used to give a StringIndexOutOfBoundsException instead of the expected * ParseException */ try { AddressBuilder.DEFAULT.parseAddressList("\"\"bar@bar.com"); fail("ParseException expected"); } catch (ParseException pe) { } } public void testMailboxGetEncodedString() throws Exception { Mailbox m1 = new Mailbox("john.doe", "acme.org"); assertEquals("john.doe@acme.org", AddressFormatter.DEFAULT.encode(m1)); Mailbox m2 = new Mailbox("john doe", "acme.org"); assertEquals("\"john doe\"@acme.org", AddressFormatter.DEFAULT.encode(m2)); Mailbox m3 = new Mailbox("John Doe", "john.doe", "acme.org"); assertEquals("John Doe ", AddressFormatter.DEFAULT.encode(m3)); Mailbox m4 = new Mailbox("John Doe @Home", "john.doe", "acme.org"); assertEquals("\"John Doe @Home\" ", AddressFormatter.DEFAULT.encode(m4)); Mailbox m5 = new Mailbox("Hans M\374ller", "hans.mueller", "acme.org"); assertEquals("=?ISO-8859-1?Q?Hans_M=FCller?= ", AddressFormatter.DEFAULT.encode(m5)); } public void testGroupGetEncodedString() throws Exception { List al = new ArrayList(); al.add(new Mailbox("test", "example.com")); al.add(new Mailbox("Foo!", "foo", "example.com")); al.add(new Mailbox("Hans M\374ller", "hans.mueller", "acme.org")); Group g = new Group("group @work", new MailboxList(al, false)); assertEquals("\"group @work\": test@example.com, " + "Foo! , =?ISO-8859-1?Q?Hans_M=FCller?=" + " ;", AddressFormatter.DEFAULT.encode(g)); } public void testEmptyGroupGetEncodedString() throws Exception { MailboxList emptyMailboxes = new MailboxList(null, true); Group g = new Group("Undisclosed recipients", emptyMailboxes); assertEquals("Undisclosed recipients:;", AddressFormatter.DEFAULT.encode(g)); } public void testParseAddress() throws Exception { Address address = AddressBuilder.DEFAULT.parseAddress("Mary Smith "); assertTrue(address instanceof Mailbox); assertEquals("Mary Smith", ((Mailbox) address).getName()); assertEquals("mary@example.net", ((Mailbox) address).getAddress()); address = AddressBuilder.DEFAULT.parseAddress("group: Mary Smith ;"); assertTrue(address instanceof Group); assertEquals("group", ((Group) address).getName()); assertEquals("Mary Smith", ((Group) address).getMailboxes().get(0) .getName()); assertEquals("mary@example.net", ((Group) address).getMailboxes() .get(0).getAddress()); try { AddressBuilder.DEFAULT.parseGroup("john.doe@acme.org, jane.doe@acme.org"); fail(); } catch (ParseException expected) { } } public void testParseGroup() throws Exception { Group group = AddressBuilder.DEFAULT.parseGroup( "group: john.doe@acme.org, Mary Smith ;"); assertEquals("group", group.getName()); MailboxList mailboxes = group.getMailboxes(); assertEquals(2, mailboxes.size()); Mailbox mailbox1 = mailboxes.get(0); assertNull(mailbox1.getName()); assertEquals("john.doe@acme.org", mailbox1.getAddress()); Mailbox mailbox2 = mailboxes.get(1); assertEquals("Mary Smith", mailbox2.getName()); assertEquals("mary@example.net", mailbox2.getAddress()); try { AddressBuilder.DEFAULT.parseGroup("john.doe@acme.org"); fail(); } catch (ParseException expected) { } try { AddressBuilder.DEFAULT.parseGroup("g1: john.doe@acme.org;, g2: mary@example.net;"); fail(); } catch (ParseException expected) { } } public void testParseMailbox() throws Exception { Mailbox mailbox1 = AddressBuilder.DEFAULT.parseMailbox("john.doe@acme.org"); assertNull(mailbox1.getName()); assertEquals("john.doe@acme.org", mailbox1.getAddress()); Mailbox mailbox2 = AddressBuilder.DEFAULT.parseMailbox("Mary Smith "); assertEquals("Mary Smith", mailbox2.getName()); assertEquals("mary@example.net", mailbox2.getAddress()); // non-ascii should be allowed in quoted strings Mailbox mailbox3 = AddressBuilder.DEFAULT.parseMailbox( "\"Hans M\374ller\" "); assertEquals("Hans M\374ller", mailbox3.getName()); assertEquals("hans.mueller@acme.org", mailbox3.getAddress()); try { AddressBuilder.DEFAULT.parseMailbox("g: Mary Smith ;"); fail(); } catch (ParseException expected) { } try { AddressBuilder.DEFAULT.parseMailbox( "Mary Smith , hans.mueller@acme.org"); fail(); } catch (ParseException expected) { } } public void testMailboxEquals() throws Exception { Mailbox m1 = new Mailbox("john.doe", "acme.org"); Mailbox m2 = new Mailbox("john doe", "acme.org"); Mailbox m3 = new Mailbox("john.doe", "Acme.Org"); Mailbox m4 = new Mailbox("john.doe", null); assertTrue(m1.equals(m1)); assertFalse(m1.equals(m2)); assertTrue(m1.equals(m3)); assertFalse(m1.equals(m4)); assertFalse(m1.equals(null)); } public void testMailboxHashCode() throws Exception { Mailbox m1 = new Mailbox("john.doe", "acme.org"); Mailbox m2 = new Mailbox("john doe", "acme.org"); Mailbox m3 = new Mailbox("john.doe", "Acme.Org"); Mailbox m4 = new Mailbox("john.doe", null); assertTrue(m1.hashCode() == m1.hashCode()); assertFalse(m1.hashCode() == m2.hashCode()); assertTrue(m1.hashCode() == m3.hashCode()); assertFalse(m1.hashCode() == m4.hashCode()); } }