Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/ContentTypeFieldTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/ContentTypeFieldTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/ContentTypeFieldTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/ContentTypeFieldTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,116 @@ +/**************************************************************** + * 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; + +import org.apache.james.mime4j.field.ContentTypeField; +import org.apache.james.mime4j.field.Field; +import org.apache.log4j.BasicConfigurator; + +import junit.framework.TestCase; + +/** + * + * + * + * @version $Id: ContentTypeFieldTest.java,v 1.4 2004/11/09 12:57:41 ntherning Exp $ + */ +public class ContentTypeFieldTest extends TestCase { + + public void setUp() { + BasicConfigurator.resetConfiguration(); + BasicConfigurator.configure(); + } + + public void testMimeTypeWithSemiColonNoParams() { + ContentTypeField f = null; + + f = (ContentTypeField) Field.parse("Content-Type: text/html;"); + assertEquals("text/html", f.getMimeType()); + } + + public void testGetMimeType() { + ContentTypeField f = null; + + f = (ContentTypeField) Field.parse("Content-Type: text/PLAIN"); + assertEquals("text/plain", f.getMimeType()); + + f = (ContentTypeField) Field.parse("content-type: TeXt / html "); + assertEquals("text/html", f.getMimeType()); + + f = (ContentTypeField) Field.parse("CONTENT-TYPE: x-app/yada ;" + + " param = yada"); + assertEquals("x-app/yada", f.getMimeType()); + + f = (ContentTypeField) Field.parse("CONTENT-TYPE: yada"); + assertEquals("", f.getMimeType()); + } + + public void testGetMimeTypeStatic() { + ContentTypeField child = null; + ContentTypeField parent = null; + + child = (ContentTypeField) Field.parse("Content-Type: child/type"); + parent = (ContentTypeField) Field.parse("Content-Type: parent/type"); + assertEquals("child/type", ContentTypeField.getMimeType(child, parent)); + + child = null; + parent = (ContentTypeField) Field.parse("Content-Type: parent/type"); + assertEquals("text/plain", ContentTypeField.getMimeType(child, parent)); + parent = (ContentTypeField) Field.parse("Content-Type: multipart/digest"); + assertEquals("message/rfc822", ContentTypeField.getMimeType(child, parent)); + + child = (ContentTypeField) Field.parse("Content-Type:"); + parent = (ContentTypeField) Field.parse("Content-Type: parent/type"); + assertEquals("text/plain", ContentTypeField.getMimeType(child, parent)); + parent = (ContentTypeField) Field.parse("Content-Type: multipart/digest"); + assertEquals("message/rfc822", ContentTypeField.getMimeType(child, parent)); + } + + public void testGetCharsetStatic() { + ContentTypeField f = null; + + f = (ContentTypeField) Field.parse("Content-Type: some/type; charset=iso8859-1"); + assertEquals("iso8859-1", ContentTypeField.getCharset(f)); + + f = (ContentTypeField) Field.parse("Content-Type: some/type;"); + assertEquals("us-ascii", ContentTypeField.getCharset(f)); + } + + public void testGetParameter() { + ContentTypeField f = null; + + f = (ContentTypeField) Field.parse("CONTENT-TYPE: text / html ;" + + " boundary=yada yada"); + assertEquals("yada", f.getParameter("boundary")); + + f = (ContentTypeField) Field.parse("Content-Type: x-app/yada;" + + " boUNdarY= \"ya:\\\"*da\"; " + + "\tcharset\t = us-ascii"); + assertEquals("ya:\"*da", f.getParameter("boundary")); + assertEquals("us-ascii", f.getParameter("charset")); + + f = (ContentTypeField) Field.parse("Content-Type: x-app/yada; " + + "boUNdarY= \"ya \\\"\\\"\tda \\\"\"; " + + "\tcharset\t = \"\\\"hepp\\\" =us\t-ascii\""); + assertEquals("ya \"\"\tda \"", f.getParameter("boundary")); + assertEquals("\"hepp\" =us\t-ascii", f.getParameter("charset")); + } + +}
Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/FieldTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,68 @@ +/**************************************************************** + * 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; + +import org.apache.james.mime4j.field.ContentTransferEncodingField; +import org.apache.james.mime4j.field.ContentTypeField; +import org.apache.james.mime4j.field.Field; +import org.apache.james.mime4j.field.UnstructuredField; + +import junit.framework.TestCase; + +/** + * + * + * + * @version $Id: FieldTest.java,v 1.3 2004/10/25 07:26:47 ntherning Exp $ + */ +public class FieldTest extends TestCase { + + public void testGetName() { + Field f = null; + + f = Field.parse("Subject: Yada yada yada"); + assertEquals("Testing simple field", "Subject", f.getName()); + + f = Field.parse("X-yada-yada: Yada yada yada"); + assertEquals("Testing an X- field", "X-yada-yada", f.getName()); + + try { + f = Field.parse("Yada yada yada"); + fail("IllegalArgumentException not thrown when using an invalid " + + "field"); + } catch (IllegalArgumentException e) { + } + } + + public void testParse() { + Field f = null; + + f = Field.parse("Subject: Yada yada yada"); + assertTrue("Field should be UnstructuredField", + f instanceof UnstructuredField); + f = Field.parse("Content-Type: text/plain"); + assertTrue("Field should be ContentTypeField", + f instanceof ContentTypeField); + f = Field.parse("Content-Transfer-Encoding: 7bit"); + assertTrue("Field should be ContentTransferEncodingField", + f instanceof ContentTransferEncodingField); + } + +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/UnstructuredFieldTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/UnstructuredFieldTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/UnstructuredFieldTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/UnstructuredFieldTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,48 @@ +/**************************************************************** + * 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; + +import org.apache.james.mime4j.field.Field; +import org.apache.james.mime4j.field.UnstructuredField; + +import junit.framework.TestCase; + +/** + * + * + * + * @version $Id: UnstructuredFieldTest.java,v 1.2 2004/10/02 12:41:11 ntherning Exp $ + */ +public class UnstructuredFieldTest extends TestCase { + + public void testGetBody() { + UnstructuredField f = null; + + f = (UnstructuredField) Field.parse("Subject: Yada\r\n yada yada\r\n"); + assertEquals("Testing folding value 1", "Yada yada yada", f.getValue()); + + f = (UnstructuredField) Field.parse("Subject: \r\n\tyada"); + assertEquals("Testing folding value 2", " \tyada", f.getValue()); + + f = (UnstructuredField) Field.parse("Subject:yada"); + assertEquals("Testing value without a leading ' '", "yada", f.getValue()); + } + +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/address/AddressTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,159 @@ +/**************************************************************** + * 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 junit.framework.TestCase; + +import org.apache.james.mime4j.field.address.AddressList; +import org.apache.james.mime4j.field.address.Group; +import org.apache.james.mime4j.field.address.Mailbox; +import org.apache.james.mime4j.field.address.NamedMailbox; +import org.apache.james.mime4j.field.address.parser.ParseException; + +public class AddressTest extends TestCase { + + public void testParse1() throws ParseException { + AddressList addrList = AddressList.parse("John Doe <[EMAIL PROTECTED](comment). example>"); + assertEquals(1, addrList.size()); + NamedMailbox mailbox = (NamedMailbox)addrList.get(0); + assertEquals("John Doe", mailbox.getName()); + assertEquals("jdoe", mailbox.getLocalPart()); + assertEquals("machine.example", mailbox.getDomain()); + } + + public void testParse2() throws ParseException { + AddressList addrList = AddressList.parse("Mary Smith \t \t\t <[EMAIL PROTECTED]>"); + assertEquals(1, addrList.size()); + NamedMailbox mailbox = (NamedMailbox)addrList.get(0); + assertEquals("Mary Smith", mailbox.getName()); + assertEquals("mary", mailbox.getLocalPart()); + assertEquals("example.net", mailbox.getDomain()); + } + + public void testEmptyGroup() throws ParseException { + AddressList addrList = AddressList.parse("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 = AddressList.parse("Marketing folks : Jane Smith < jane @ example . net >, \" Jack \\\"Jackie\\\" Jones \" < [EMAIL PROTECTED] > (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()); + + NamedMailbox namedMailbox1 = (NamedMailbox)group.getMailboxes().get(0); + NamedMailbox namedMailbox2 = (NamedMailbox)group.getMailboxes().get(1); + + assertEquals("Jane Smith", namedMailbox1.getName()); + assertEquals("jane", namedMailbox1.getLocalPart()); + assertEquals("example.net", namedMailbox1.getDomain()); + + assertEquals(" Jack \"Jackie\" Jones ", namedMailbox2.getName()); + assertEquals("jjones", namedMailbox2.getLocalPart()); + assertEquals("example.com", namedMailbox2.getDomain()); + + Mailbox mailbox = (Mailbox)addrList.get(1); + assertFalse(mailbox instanceof NamedMailbox); + 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, AddressList.parse(" \t \t ").size()); + assertEquals(0, AddressList.parse(" \t , , , ,,, , \t ").size()); + } + + public void testSimpleForm() throws ParseException { + AddressList addrList = AddressList.parse("\"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 = AddressList.parse("dev : [EMAIL PROTECTED], [EMAIL PROTECTED]; , ,,, marketing:[EMAIL PROTECTED] ,[EMAIL PROTECTED];, [EMAIL PROTECTED]"); + 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\" <guymacon+\" http://www.guymacon.com/ \"[EMAIL PROTECTED]>"); + + // 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. + + AddressList.parse("Alfred Neuman <[EMAIL PROTECTED]>"); + AddressList.parse("[EMAIL PROTECTED]"); + AddressList.parse("\"George, Ted\" <[EMAIL PROTECTED]>"); + AddressList.parse("Wilt . (the Stilt) [EMAIL PROTECTED]"); + + // 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. + AddressList.parse(" Gourmets: Pompous Person <[EMAIL PROTECTED]>," + + " [EMAIL PROTECTED], \"Galloping Gourmet\"@" + + " ANT.Down-Under (Australian National Television)," + + " [EMAIL PROTECTED];," + + " Cruisers: [EMAIL PROTECTED], [EMAIL PROTECTED];," + + " [EMAIL PROTECTED]"); + + // 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 "<". + AddressList.parse("Important folk:" + + " Tom Softwood <[EMAIL PROTECTED]>," + + " \"Sam Irving\"@Other-Host;," + + " Standard Distribution:" + + " /main/davis/people/[EMAIL PROTECTED]," + + " \"<Jones>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 + AddressList.parse("\":sysmail\"@ Some-Group.\t Some-Org, Muhammed.(I am the greatest) Ali @(the)Vegas.WBA"); + AddressList.parse("[EMAIL PROTECTED] (comment (nested (deeply\\))))"); + AddressList.parse("mailing list: [EMAIL PROTECTED], route two <[EMAIL PROTECTED]>, [EMAIL PROTECTED] ;"); + + } + + public void testLexicalError() { + // ensure that TokenMgrError doesn't get thrown + try { + AddressList.parse(")"); + } + catch (ParseException e) { + + } + } +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/contenttype/ContentTypeTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/contenttype/ContentTypeTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/contenttype/ContentTypeTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/contenttype/ContentTypeTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,51 @@ +/**************************************************************** + * 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.contenttype; + +import org.apache.james.mime4j.field.contenttype.parser.ContentTypeParser; +import org.apache.james.mime4j.field.contenttype.parser.ParseException; + +import java.io.StringReader; + +import junit.framework.TestCase; + +public class ContentTypeTest extends TestCase { + + public void testContentType() throws ParseException { + test("one/two; three = four", "one", "two"); + test("one/(foo)two; three = \"four\"", "one", "two"); + test("one(foo)/two; three = (foo) four", "one", "two"); + test("one/two; three = four", "one", "two"); + + // TODO: add more tests + } + + private void test(String val, String expectedType, String expectedSubtype) throws ParseException { + ContentTypeParser parser = new ContentTypeParser(new StringReader(val)); + parser.parseAll(); + + String type = parser.getType(); + String subtype = parser.getSubType(); + + assertEquals(expectedType, type); + assertEquals(expectedSubtype, subtype); + } + +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/datetime/DateTimeTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/datetime/DateTimeTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/datetime/DateTimeTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/field/datetime/DateTimeTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,102 @@ +/**************************************************************** + * 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.datetime; + +import junit.framework.TestCase; +import org.apache.james.mime4j.field.datetime.parser.DateTimeParser; +import org.apache.james.mime4j.field.datetime.parser.ParseException; + +import java.io.StringReader; + +public class DateTimeTest extends TestCase { + + public void testNormalDate() throws ParseException { + new DateTimeParser(new StringReader("Fri, 21 Nov 1997 09:55:06 -0600")).parseAll(); + new DateTimeParser(new StringReader("21 Nov 97 09:55:06 GMT")).parseAll(); + + + ensureAllEqual(new String[] { + "Fri, 21 Nov 1997 09:55:06 -0600", // baseline + "Fri, 21 Nov 97 09:55:06 -0600", // 2-digit year + "Fri, 21 Nov 097 09:55:06 -0600", // 3-digit year + "Fri, 21 Nov 1997 10:55:06 -0500", // shift time zone + "Fri, 21 Nov 1997 19:25:06 +0330", // shift time zone + "21 Nov 1997 09:55:06 -0600" // omit day of week + }); + + ensureAllEqual(new String[] { + "Thu, 16 Sep 2019 14:37:22 +0000", // baseline + "Thu, 16 Sep 19 14:37:22 +0000", // 2-digit year + "Thu, 16 Sep 119 14:37:22 +0000", // 3-digit year + "Thu, 16 Sep 2019 14:37:22 -0000", // minus-zero zone + "Thu, 16 Sep 2019 14:37:22 GMT", // alternate zone + "Thu, 16 Sep 2019 14:37:22 UT" // alternate zone + }); + + ensureAllEqual(new String[] { + "Fri, 21 Nov 1997 12:00:00 GMT", + "Fri, 21 Nov 1997 07:00:00 EST", + "Fri, 21 Nov 1997 08:00:00 EDT", + "Fri, 21 Nov 1997 06:00:00 CST", + "Fri, 21 Nov 1997 07:00:00 CDT", + "Fri, 21 Nov 1997 05:00:00 MST", + "Fri, 21 Nov 1997 06:00:00 MDT", + "Fri, 21 Nov 1997 04:00:00 PST", + "Fri, 21 Nov 1997 05:00:00 PDT", + + // make sure military zones are ignored, per RFC2822 instructions + "Fri, 21 Nov 1997 12:00:00 A", + "Fri, 21 Nov 1997 12:00:00 B", + "Fri, 21 Nov 1997 12:00:00 C", + "Fri, 21 Nov 1997 12:00:00 D", + "Fri, 21 Nov 1997 12:00:00 E", + "Fri, 21 Nov 1997 12:00:00 F", + "Fri, 21 Nov 1997 12:00:00 G", + "Fri, 21 Nov 1997 12:00:00 H", + "Fri, 21 Nov 1997 12:00:00 I", + "Fri, 21 Nov 1997 12:00:00 K", + "Fri, 21 Nov 1997 12:00:00 L", + "Fri, 21 Nov 1997 12:00:00 M", + "Fri, 21 Nov 1997 12:00:00 N", + "Fri, 21 Nov 1997 12:00:00 O", + "Fri, 21 Nov 1997 12:00:00 P", + "Fri, 21 Nov 1997 12:00:00 Q", + "Fri, 21 Nov 1997 12:00:00 R", + "Fri, 21 Nov 1997 12:00:00 S", + "Fri, 21 Nov 1997 12:00:00 T", + "Fri, 21 Nov 1997 12:00:00 U", + "Fri, 21 Nov 1997 12:00:00 V", + "Fri, 21 Nov 1997 12:00:00 W", + "Fri, 21 Nov 1997 12:00:00 X", + "Fri, 21 Nov 1997 12:00:00 Y", + "Fri, 21 Nov 1997 12:00:00 Z", + }); + } + + private void ensureAllEqual(String[] dateStrings) throws ParseException { + for (int i = 0; i < dateStrings.length - 1; i++) { + assertEquals( + new DateTimeParser(new StringReader(dateStrings[i])).parseAll().getDate().getTime(), + new DateTimeParser(new StringReader(dateStrings[i + 1])).parseAll().getDate().getTime() + ); + } + } + +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/HeaderTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/HeaderTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/HeaderTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/HeaderTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,44 @@ +/**************************************************************** + * 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.message; + +import org.apache.james.mime4j.field.Field; +import org.apache.james.mime4j.message.Header; + +import junit.framework.TestCase; + +public class HeaderTest extends TestCase { + + public static final String SUBJECT = "Subject: test"; + + public static final String TO = "To: anyuser <[EMAIL PROTECTED]>"; + + public void testHeader() { + Header header = new Header(); + header.addField(Field.parse(SUBJECT)); + header.addField(Field.parse(TO)); + + assertNotNull("Subject found", header.getField("Subject")); + assertNotNull("To found", header.getField("To")); + + assertEquals("Headers equals", SUBJECT + "\r\n" + TO + "\r\n", header + .toString()); + } +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,266 @@ +/**************************************************************** + * 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.message; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.EOLConvertingInputStream; +import org.apache.james.mime4j.field.Field; +import org.apache.james.mime4j.message.BinaryBody; +import org.apache.james.mime4j.message.Body; +import org.apache.james.mime4j.message.Entity; +import org.apache.james.mime4j.message.Message; +import org.apache.james.mime4j.message.Multipart; +import org.apache.james.mime4j.message.TextBody; +import org.apache.james.mime4j.util.CharsetUtil; +import org.apache.log4j.BasicConfigurator; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.util.Iterator; +import java.util.List; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * + * + * + * + * @version $Id: MessageParserTest.java,v 1.4 2004/10/25 07:26:47 ntherning Exp $ + */ +public class MessageParserTest extends TestCase { + private String fileName = null; + + public MessageParserTest(String name, String fileName) { + super(name); + + this.fileName = fileName; + } + + public void setUp() { + BasicConfigurator.resetConfiguration(); + BasicConfigurator.configure(); + } + + public static Test suite() { + TestSuite suite = new TestSuite(); + + File dir = new File("src/test/resources/testmsgs"); + File[] files = dir.listFiles(); + + for (int i = 0; i < files.length && i < 5000; i++) { + File f = files[i]; + if (f.getName().toLowerCase().endsWith(".msg")) { + suite.addTest(new MessageParserTest(f.getName(), + f.getAbsolutePath())); + } + } + + return suite; + } + + protected void runTest() throws IOException { + File f = new File(fileName); + + System.out.println("Parsing " + f.getName()); + + InputStream in = new EOLConvertingInputStream(new BufferedInputStream( + new FileInputStream(f))); //, + + Message m = new Message(in); + + String prefix = f.getName().substring(0, f.getName().length() - 4); + String xmlFileName = fileName.substring(0, fileName.length() - 4) + + "_decoded.xml"; + + String result = getStructure(m, prefix, "1"); + String mime4jFileName = fileName.substring(0, fileName.length() - 4) + + "_decoded.mime4j.xml"; + String expected = null; + try { + expected = IOUtils.toString( + new FileInputStream(xmlFileName), "ISO8859-1"); + } catch (FileNotFoundException ex) { + writeToFile(result, mime4jFileName); + fail("Test file not found. Generated the expected result with mime4j prefix: "+ex.getMessage()); + } + try { + assertEquals(expected, result); + } catch (AssertionError ae) { + writeToFile(result, mime4jFileName); + throw ae; + } + } + + private void writeToFile(String result, String mime4jFileName) + throws FileNotFoundException, IOException, + UnsupportedEncodingException { + FileOutputStream out = new FileOutputStream(mime4jFileName); + out.write(result.getBytes("ISO8859-1")); + out.close(); + } + + private String escape(String s) { + s = s.replaceAll("&", "&"); + s = s.replaceAll("<", "<"); + return s.replaceAll(">", ">"); + } + + private String getStructure(Entity e, String prefix, String id) + throws IOException { + + StringBuffer sb = new StringBuffer(); + + if (e instanceof Message) { + sb.append("<message>\r\n"); + } else { + sb.append("<body-part>\r\n"); + } + + sb.append("<header>\r\n"); + for (Iterator it = e.getHeader().getFields().iterator(); it.hasNext();) { + sb.append("<field>\r\n" + escape(((Field) it.next()).getRaw()) + + "</field>\r\n"); + } + sb.append("</header>\r\n"); + + if (e.getBody() instanceof Multipart) { + sb.append("<multipart>\r\n"); + + Multipart multipart =(Multipart) e.getBody(); + List parts =multipart.getBodyParts(); + + sb.append("<preamble>\r\n"); + sb.append(escape(multipart.getPreamble())); + sb.append("</preamble>\r\n"); + + int i = 1; + for (Iterator it = parts.iterator(); it.hasNext();) { + sb.append(getStructure((Entity) it.next(), prefix, id + "_" + (i++))); + } + + sb.append("<epilogue>\r\n"); + sb.append(escape(multipart.getEpilogue())); + sb.append("</epilogue>\r\n"); + + sb.append("</multipart>\r\n"); + + } else if (e.getBody() instanceof Message) { + sb.append(getStructure((Message) e.getBody(), prefix, id + "_1")); + } else { + Body b = e.getBody(); + String name = prefix + "_decoded_" + id + + (b instanceof TextBody ? ".txt" : ".bin"); + String tag = b instanceof TextBody ? "text-body" : "binary-body"; + sb.append("<" + tag + " name=\"" + name + "\"/>\r\n"); + + File expectedFile = new File(new File(fileName).getParent(), name); + File mime4jFile = new File(new File(fileName).getParent(), + name.substring(0, name.length() - 4) + ".mime4j" + + (b instanceof TextBody ? ".txt" : ".bin")); + + InputStream expected = null; + try { + expected = new BufferedInputStream(new FileInputStream(expectedFile)); + } catch (FileNotFoundException ex) { + writeToFile(b, mime4jFile); + fail("Test file not found. Generated the expected result with mime4j prefix: "+ex.getMessage()); + } + + try { + if (b instanceof TextBody) { + String charset = CharsetUtil.toJavaCharset(e.getCharset()); + if (charset == null) { + charset = "ISO8859-1"; + } + + String s1 = IOUtils.toString(expected, charset).replaceAll( + "\r", ""); + String s2 = IOUtils.toString(((TextBody) b).getReader()) + .replaceAll("\r", ""); + assertEquals(expectedFile.getName(), s1, s2); + } else { + assertEqualsBinary(expectedFile.getName(), expected, + ((BinaryBody) b).getInputStream()); + } + } catch (AssertionError er) { + writeToFile(b, mime4jFile); + throw er; + } + } + + + if (e instanceof Message) { + sb.append("</message>\r\n"); + } else { + sb.append("</body-part>\r\n"); + } + + return sb.toString(); + } + + private void writeToFile(Body b, File mime4jFile) + throws FileNotFoundException, IOException { + if (b instanceof TextBody) { + OutputStream out = new FileOutputStream(mime4jFile); + IOUtils.copy(((TextBody) b).getReader(), out); + } else { + OutputStream out = new FileOutputStream(mime4jFile); + IOUtils.copy(((BinaryBody) b).getInputStream(), out); + } + } + + private void assertEqualsBinary(String msg, InputStream a, InputStream b) + throws IOException { + + int pos = 0; + while (true) { + int b1 = a.read(); + int b2 = b.read(); + assertEquals(msg + " (Position " + (++pos) + ")", b1, b2); + + if (b1 == -1 || b2 == -1) { + break; + } + } + } + + public static void main(String[] args) throws IOException { + File dir = new File("/home/niklas/Projects/upskido/tmp"); + File[] files = dir.listFiles(); + + for (int i = 0; i < files.length; i++) { + File f = files[i]; + if (f.getName().toLowerCase().endsWith(".msg")) { + Message m = new Message(new BufferedInputStream(new FileInputStream(f))); + } + } + + } +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,187 @@ +/**************************************************************** + * 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.message; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.List; + +import org.apache.commons.io.IOUtils; +import org.apache.james.mime4j.field.Field; +import org.apache.james.mime4j.message.Header; +import org.apache.james.mime4j.message.Message; + +import junit.framework.TestCase; + +/** + * + * + * + * @version $Id: MessageTest.java,v 1.4 2004/10/02 12:41:11 ntherning Exp $ + */ +public class MessageTest extends TestCase { + private Header headerTextPlain = null; + private Header headerMessageRFC822 = null; + private Header headerEmpty = null; + private Header headerMultipartMixed = null; + private Header headerMultipartDigest = null; + + public void setUp() { + headerTextPlain = new Header(); + headerMessageRFC822 = new Header(); + headerEmpty = new Header(); + headerMultipartMixed = new Header(); + headerMultipartDigest = new Header(); + + headerTextPlain.addField( + Field.parse("Content-Type: text/plain")); + headerMessageRFC822.addField( + Field.parse("Content-Type: message/RFC822")); + headerMultipartMixed.addField( + Field.parse("Content-Type: multipart/mixed; boundary=foo")); + headerMultipartDigest.addField( + Field.parse("Content-Type: multipart/digest; boundary=foo")); + } + + public void testGetParts() { + } + + public void testGetMimeType() { + Message parent = null; + Message child = null; + + parent = new Message(); + child = new Message(); + child.setParent(parent); + parent.setHeader(headerMultipartDigest); + child.setHeader(headerEmpty); + assertEquals("multipart/digest, empty", "message/rfc822", + child.getMimeType()); + child.setHeader(headerTextPlain); + assertEquals("multipart/digest, text/plain", "text/plain", + child.getMimeType()); + child.setHeader(headerMessageRFC822); + assertEquals("multipart/digest, message/rfc822", "message/rfc822", + child.getMimeType()); + + parent = new Message(); + child = new Message(); + child.setParent(parent); + parent.setHeader(headerMultipartMixed); + child.setHeader(headerEmpty); + assertEquals("multipart/mixed, empty", "text/plain", + child.getMimeType()); + child.setHeader(headerTextPlain); + assertEquals("multipart/mixed, text/plain", "text/plain", + child.getMimeType()); + child.setHeader(headerMessageRFC822); + assertEquals("multipart/mixed, message/rfc822", "message/rfc822", + child.getMimeType()); + + child = new Message(); + child.setHeader(headerEmpty); + assertEquals("null, empty", "text/plain", child.getMimeType()); + child.setHeader(headerTextPlain); + assertEquals("null, text/plain", "text/plain", child.getMimeType()); + child.setHeader(headerMessageRFC822); + assertEquals("null, message/rfc822", "message/rfc822", + child.getMimeType()); + } + + public void testIsMultipart() { + Message m = new Message(); + + m.setHeader(headerEmpty); + assertTrue("empty", !m.isMultipart()); + + m.setHeader(headerTextPlain); + assertTrue("text/plain", !m.isMultipart()); + + m.setHeader(headerMultipartDigest); + assertTrue("multipart/digest", m.isMultipart()); + + m.setHeader(headerMultipartMixed); + assertTrue("multipart/mixed", m.isMultipart()); + } + + public void testWriteTo() throws IOException { + byte[] inputByte = getRawMessageAsByteArray(); + + Message m = new Message(new ByteArrayInputStream(inputByte)); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + m.writeTo(out); + + InputStream output = new ByteArrayInputStream(out.toByteArray()); + + int b = -1; + int i = 0; + while ((b = output.read()) != -1) { + assertEquals("same byte", b, inputByte[i]); + i++; + } + } + + public void testAddHeaderWriteTo() throws IOException { + String headerName = "testheader"; + String headerValue = "testvalue"; + String testheader = headerName + ": " + headerValue; + + byte[] inputByte = getRawMessageAsByteArray(); + + Message m = new Message(new ByteArrayInputStream(inputByte)); + m.getHeader().addField(Field.parse(testheader)); + + assertEquals("header added", m.getHeader().getField(headerName) + .getBody(), headerValue); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + m.writeTo(out); + List lines = IOUtils.readLines((new BufferedReader( + new InputStreamReader(new ByteArrayInputStream(out + .toByteArray()))))); + + assertTrue("header added", lines.contains(testheader)); + } + + private byte[] getRawMessageAsByteArray() { + StringBuffer header = new StringBuffer(); + StringBuffer body = new StringBuffer(); + StringBuffer complete = new StringBuffer(); + + header.append("Date: Wed, 21 Feb 2007 11:09:27 +0100\r\n"); + header.append("From: Test <[EMAIL PROTECTED]>\r\n"); + header.append("To: Norman Maurer <[EMAIL PROTECTED]>\r\n"); + header.append("Subject: Testmail\r\n"); + header + .append("Content-Type: text/plain; charset=ISO-8859-15; format=flowed\r\n"); + header.append("Content-Transfer-Encoding: 8bit\r\n\r\n"); + body.append("testbody\r\n"); + complete.append(header); + complete.append(body); + + return complete.toString().getBytes(); + } + +} Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/SimpleTempStorageTest.java URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/SimpleTempStorageTest.java?view=auto&rev=532898 ============================================================================== --- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/SimpleTempStorageTest.java (added) +++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/util/SimpleTempStorageTest.java Thu Apr 26 15:56:11 2007 @@ -0,0 +1,119 @@ +/**************************************************************** + * 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.util; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.apache.james.mime4j.util.SimpleTempStorage; +import org.apache.james.mime4j.util.TempFile; +import org.apache.james.mime4j.util.TempPath; + +import junit.framework.TestCase; + +/** + * + * + * + * @version $Id: SimpleTempStorageTest.java,v 1.2 2004/10/02 12:41:12 ntherning Exp $ + */ +public class SimpleTempStorageTest extends TestCase { + + public void testGetRootTempPath() { + SimpleTempStorage man = new SimpleTempStorage(); + TempPath path = man.getRootTempPath(); + File tmpdir = new File(System.getProperty("java.io.tmpdir")); + assertEquals(tmpdir.getAbsolutePath(), path.getAbsolutePath()); + } + + public void testCreateTempPath() throws IOException { + SimpleTempStorage man = new SimpleTempStorage(); + TempPath path = man.getRootTempPath().createTempPath(); + File tmpdir = new File(System.getProperty("java.io.tmpdir")); + assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath())); + + String fileName = path.getAbsolutePath().substring( + path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1); + assertTrue("Unexpected chars in file name " + fileName, + fileName.matches("^[0-9]+$")); + assertTrue("Temp dir doesn't exist", + new File(path.getAbsolutePath()).exists()); + } + + public void testCreateTempPathString() throws IOException { + SimpleTempStorage man = new SimpleTempStorage(); + TempPath path = man.getRootTempPath().createTempPath("test_prefix"); + File tmpdir = new File(System.getProperty("java.io.tmpdir"), + "test_prefix"); + assertTrue(path.getAbsolutePath().startsWith(tmpdir.getAbsolutePath())); + + String fileName = path.getAbsolutePath().substring( + path.getAbsolutePath().lastIndexOf(File.separatorChar) + 1); + assertTrue("Unexpected chars in file name " + fileName, + fileName.matches("^test_prefix[0-9]+$")); + assertTrue("Temp dir doesn't exist", + new File(path.getAbsolutePath()).exists()); + } + + public void testCreateTempFile() throws IOException { + SimpleTempStorage man = new SimpleTempStorage(); + TempPath path = man.getRootTempPath().createTempPath(); + TempFile file = path.createTempFile(); + assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath())); + + String fileName = file.getAbsolutePath().substring( + file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1); + assertTrue("Unexpected chars in file name " + fileName, + fileName.matches("^[0-9]+\\.tmp$")); + assertTrue("Temp file doesn't exist", + new File(file.getAbsolutePath()).exists()); + + String s = "A short string"; + OutputStream out = file.getOutputStream(); + out.write(s.getBytes()); + out.close(); + + byte[] buffer = new byte[s.length() * 2]; + InputStream in = file.getInputStream(); + int i = 0; + for (int data; (data = in.read()) != -1; i++) { + buffer[i] = (byte) data; + } + in.close(); + assertTrue(s.length() == i); + assertEquals(s, new String(buffer, 0, i)); + } + + public void testCreateTempFileStringString() throws IOException { + SimpleTempStorage man = new SimpleTempStorage(); + TempPath path = man.getRootTempPath().createTempPath(); + TempFile file = path.createTempFile("test_prefix", ".suffix"); + assertTrue(file.getAbsolutePath().startsWith(path.getAbsolutePath())); + + String fileName = file.getAbsolutePath().substring( + file.getAbsolutePath().lastIndexOf(File.separatorChar) + 1); + assertTrue("Unexpected chars in file name " + fileName, + fileName.matches("^test_prefix[0-9]+\\.suffix$")); + assertTrue("Temp file doesn't exist", + new File(file.getAbsolutePath()).exists()); + } +} --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
