james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1481814 - in /james/mime4j/trunk: core/src/test/java/org/apache/james/mime4j/ core/src/test/java/org/apache/james/mime4j/parser/ dom/src/test/java/org/apache/james/mime4j/dom/ dom/src/test/java/org/apache/james/mime4j/message/
Date Mon, 13 May 2013 12:13:09 GMT
Author: olegk
Date: Mon May 13 12:13:09 2013
New Revision: 1481814

URL: http://svn.apache.org/r1481814
Log:
Refactored sample message based test suites; fixed frivolous usage of file based resources

Added:
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
  (with props)
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
  (with props)
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
  (with props)
Modified:
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserExampleMessagesTest.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/EntityTest.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/ExampleMessagesRoundtripTest.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
    james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/StringInputStreamTest.java

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java?rev=1481814&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
Mon May 13 12:13:09 2013
@@ -0,0 +1,77 @@
+/****************************************************************
+ * 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;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.james.mime4j.stream.MimeConfig;
+
+import junit.framework.TestCase;
+
+public abstract class ExampleMessageTestCase extends TestCase {
+
+    private final File file;
+    private final URL resource;
+
+    protected ExampleMessageTestCase(final File file, final URL resource) {
+        super(file.getName());
+        this.file = file;
+        this.resource = resource;
+    }
+
+    public File getSourceFile() {
+        return file;
+    }
+
+    public URL getResource() {
+        return resource;
+    }
+
+    public String getResourceBase() {
+        String r = resource.toString();
+        String s = file.getName();
+        if (r.endsWith(s)) {
+            return r.substring(0, r.length() - s.length()) + getFilenameBase();
+        } else {
+            return r;
+        }
+    }
+
+    public String getFilenameBase() {
+        String s = file.getName();
+        int idx = s.indexOf('.');
+        if (idx != -1) {
+            return s.substring(0, idx);
+        } else {
+            return s;
+        }
+    }
+
+    public MimeConfig getConfig() {
+        MimeConfig config = new MimeConfig();
+        if (file.getName().startsWith("malformedHeaderStartsBody")) {
+            config.setMalformedHeaderStartsBody(true);
+        }
+        config.setMaxLineLen(-1);
+        return config;
+    }
+
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java?rev=1481814&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
Mon May 13 12:13:09 2013
@@ -0,0 +1,30 @@
+/****************************************************************
+ * 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+
+public interface ExampleMessageTestCaseFactory {
+
+    ExampleMessageTestCase create(File file, URL resource) throws IOException;
+
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestCaseFactory.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java?rev=1481814&view=auto
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
(added)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
Mon May 13 12:13:09 2013
@@ -0,0 +1,87 @@
+/****************************************************************
+ * 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+import junit.framework.TestSuite;
+
+/**
+ * Creates a TestSuite running the test for each sample .msg file in the test resource folder.
+ */
+public class ExampleMessageTestSuiteBuilder {
+
+    private final ExampleMessageTestCaseFactory testFactory;
+
+    public ExampleMessageTestSuiteBuilder(final ExampleMessageTestCaseFactory testFactory)
{
+        super();
+        this.testFactory = testFactory;
+    }
+
+    public TestSuite build() throws IOException {
+        TestSuite suite = new TestSuite();
+        addAllTests(suite);
+        return suite;
+    }
+
+    private void addAllTests(
+            final TestSuite testSuite) throws IOException {
+        addTests(testSuite, "/testmsgs");
+        addTests(testSuite, "/mimetools-testmsgs");
+    }
+
+    private void addTests(
+            final TestSuite testSuite,
+            final String testsFolder) throws IOException {
+        URL resource = ExampleMessageTestSuiteBuilder.class.getResource(testsFolder);
+        if (resource != null) {
+            if (resource.getProtocol().equalsIgnoreCase("file")) {
+                File dir = new File(resource.getFile());
+                File[] files = dir.listFiles();
+
+                for (File f : files) {
+                    if (f.getName().endsWith(".msg")) {
+                        testSuite.addTest(this.testFactory.create(
+                                f, f.toURL()));
+                    }
+                }
+            } else if (resource.getProtocol().equalsIgnoreCase("jar")) {
+                JarURLConnection conn = (JarURLConnection) resource.openConnection();
+                JarFile jar = conn.getJarFile();
+                for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();
) {
+                    JarEntry entry = it.nextElement();
+                    String s = "/" + entry.toString();
+                    File f = new File(s);
+                    if (s.startsWith(testsFolder) && s.endsWith(".msg")) {
+                        testSuite.addTest(testFactory.create(
+                                f, new URL("jar:file:" + jar.getName() + "!" + s)));
+                    }
+                }
+            }
+        }
+    }
+
+}

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMessageTestSuiteBuilder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserExampleMessagesTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserExampleMessagesTest.java?rev=1481814&r1=1481813&r2=1481814&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserExampleMessagesTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/MimeStreamParserExampleMessagesTest.java
Mon May 13 12:13:09 2013
@@ -19,108 +19,85 @@
 
 package org.apache.james.mime4j.parser;
 
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import org.apache.commons.io.IOUtils;
-import org.apache.james.mime4j.stream.MimeConfig;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.JarURLConnection;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.Enumeration;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 
-/**
- * Creates a TestSuite running the test for each .msg file in the test resouce folder.
- * Allow running of a single test from Unit testing GUIs
- */
-public class MimeStreamParserExampleMessagesTest extends TestCase {
-
-    private final URL url;
+import junit.framework.TestSuite;
 
-    public MimeStreamParserExampleMessagesTest(String name, URL url) {
-        super(name);
-        this.url = url;
-    }
+import org.apache.commons.io.IOUtils;
+import org.apache.james.mime4j.ExampleMessageTestCase;
+import org.apache.james.mime4j.ExampleMessageTestCaseFactory;
+import org.apache.james.mime4j.ExampleMessageTestSuiteBuilder;
+import org.apache.james.mime4j.stream.MimeConfig;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.junit.Assert;
+import org.junit.runner.RunWith;
+import org.junit.runners.AllTests;
 
-    @Override
-    protected void runTest() throws Throwable {
-        MimeStreamParser parser;
-        TestHandler handler;
-        MimeConfig config = new MimeConfig();
-        if (getName().startsWith("malformedHeaderStartsBody")) {
-            config.setMalformedHeaderStartsBody(true);
-        }
-        config.setMaxLineLen(-1);
-        parser = new MimeStreamParser(config);
-        handler = new TestHandler();
+/**
+ * Test parsing of all sample messages
+ */
+@RunWith(AllTests.class)
+public class MimeStreamParserExampleMessagesTest extends ExampleMessageTestCase {
 
-        parser.setContentHandler(handler);
-        parser.parse(url.openStream());
+    public static TestSuite suite() throws IOException {
+        ExampleMessageTestSuiteBuilder testSuiteBuilder = new ExampleMessageTestSuiteBuilder(
+                new ExampleMessageTestCaseFactory() {
 
-        String result = handler.sb.toString();
+                    public ExampleMessageTestCase create(final File file, final URL resource)
throws IOException {
+                        return new MimeStreamParserExampleMessagesTest(file, resource);
+                    }
 
-        String s = url.toString();
-        String prefix = s.substring(0, s.lastIndexOf('.'));
-        URL xmlFileUrl = new URL(prefix + ".xml");
-        try {
-            InputStream openStream = xmlFileUrl.openStream();
-            String expected = IOUtils.toString(openStream, "ISO8859-1");
-            assertEquals(expected, result);
-        } catch (FileNotFoundException e) {
-            IOUtils.write(result, new FileOutputStream(xmlFileUrl.getPath() + ".expected"));
-            fail("Expected file created.");
-        }
+                });
+        return testSuiteBuilder.build();
     }
 
-    public static Test suite() throws IOException, URISyntaxException {
-        return new MimeStreamParserExampleMessagesTestSuite();
+    public MimeStreamParserExampleMessagesTest(final File file, final URL resource) {
+        super(file, resource);
     }
 
-    static class MimeStreamParserExampleMessagesTestSuite extends TestSuite {
+    @Override
+    public void runTest() throws Exception {
+        MimeConfig config = getConfig();
+
+        TestHandler handler = new TestHandler();
+        InputStream msgstream = getResource().openStream();
+        try {
+            MimeStreamParser parser = new MimeStreamParser(config);
+            parser.setContentHandler(handler);
+            parser.parse(msgstream);
 
-        public MimeStreamParserExampleMessagesTestSuite() throws IOException, URISyntaxException
{
-            addTests("/testmsgs");
-            addTests("/mimetools-testmsgs");
+        } finally {
+            msgstream.close();
         }
 
-        private void addTests(String testsFolder) throws URISyntaxException,
-                MalformedURLException, IOException {
-            URL resource = MimeStreamParserExampleMessagesTestSuite.class.getResource(testsFolder);
-            if (resource != null) {
-                if (resource.getProtocol().equalsIgnoreCase("file")) {
-                    File dir = new File(resource.toURI());
-                    File[] files = dir.listFiles();
-
-                    for (File f : files) {
-                        if (f.getName().endsWith(".msg")) {
-                            addTest(new MimeStreamParserExampleMessagesTest(f.getName(),
-                                    f.toURI().toURL()));
-                        }
-                    }
-                } else if (resource.getProtocol().equalsIgnoreCase("jar")) {
-                    JarURLConnection conn = (JarURLConnection) resource.openConnection();
-                    JarFile jar = conn.getJarFile();
-                    for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();
) {
-                        JarEntry entry = it.nextElement();
-                        String s = "/" + entry.toString();
-                        File f = new File(s);
-                        if (s.startsWith(testsFolder) && s.endsWith(".msg")) {
-                            addTest(new MimeStreamParserExampleMessagesTest(f.getName(),
-                                    new URL("jar:file:" + jar.getName() + "!" + s)));
-                        }
-                    }
-                }
+        String result = handler.sb.toString();
+
+        URL xmlFileUrl = new URL(getResourceBase() + ".xml");
+        try {
+            String expected;
+            InputStream contentstream = xmlFileUrl.openStream();
+            try {
+                expected = IOUtils.toString(contentstream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                contentstream.close();
             }
+            Assert.assertEquals(expected, result);
+        } catch (FileNotFoundException ex) {
+            // Create expected content template to the current directory
+            File expectedFileTemplate = new File(getFilenameBase() + ".xml.expected");
+            FileOutputStream outstream = new FileOutputStream(expectedFileTemplate);
+            try {
+                IOUtils.write(result, outstream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                outstream.close();
+            }
+            Assert.fail("Expected file created.");
         }
-
     }
+
 }

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/EntityTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/EntityTest.java?rev=1481814&r1=1481813&r2=1481814&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/EntityTest.java (original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/EntityTest.java Mon May
13 12:13:09 2013
@@ -19,30 +19,29 @@
 
 package org.apache.james.mime4j.dom;
 
-import org.apache.james.mime4j.dom.Body;
-import org.apache.james.mime4j.dom.Entity;
-import org.apache.james.mime4j.dom.Header;
 import org.apache.james.mime4j.field.DefaultFieldParser;
 import org.apache.james.mime4j.message.BasicBodyFactory;
 import org.apache.james.mime4j.message.BodyPart;
 import org.apache.james.mime4j.message.HeaderImpl;
+import org.junit.Assert;
+import org.junit.Test;
 
-import junit.framework.TestCase;
-
-public class EntityTest extends TestCase {
+public class EntityTest {
 
+    @Test
     public void testSetBody() throws Exception {
         Entity entity = new BodyPart();
-        assertNull(entity.getBody());
+        Assert.assertNull(entity.getBody());
 
         Body body = new BasicBodyFactory().textBody("test");
-        assertNull(body.getParent());
+        Assert.assertNull(body.getParent());
 
         entity.setBody(body);
-        assertSame(body, entity.getBody());
-        assertSame(entity, body.getParent());
+        Assert.assertSame(body, entity.getBody());
+        Assert.assertSame(entity, body.getParent());
     }
 
+    @Test
     public void testSetBodyTwice() throws Exception {
         Entity entity = new BodyPart();
 
@@ -52,77 +51,83 @@ public class EntityTest extends TestCase
         entity.setBody(b1);
         try {
             entity.setBody(b2);
-            fail();
+            Assert.fail("IllegalStateException expected");
         } catch (IllegalStateException expected) {
         }
     }
 
+    @Test
     public void testRemoveBody() throws Exception {
         Entity entity = new BodyPart();
         Body body = new BasicBodyFactory().textBody("test");
         entity.setBody(body);
 
         Body removed = entity.removeBody();
-        assertSame(body, removed);
+        Assert.assertSame(body, removed);
 
-        assertNull(entity.getBody());
-        assertNull(removed.getParent());
+        Assert.assertNull(entity.getBody());
+        Assert.assertNull(removed.getParent());
     }
 
+    @Test
     public void testGetDispositionType() throws Exception {
         BodyPart entity = new BodyPart();
 
-        assertNull(entity.getDispositionType());
+        Assert.assertNull(entity.getDispositionType());
 
         Header header = new HeaderImpl();
         header.setField(DefaultFieldParser.parse("Content-Disposition: inline"));
         entity.setHeader(header);
 
-        assertEquals("inline", entity.getDispositionType());
+        Assert.assertEquals("inline", entity.getDispositionType());
     }
 
+    @Test
     public void testSetContentDispositionType() throws Exception {
         BodyPart entity = new BodyPart();
 
         entity.setContentDisposition("attachment");
 
-        assertEquals("attachment", entity.getHeader().getField(
+        Assert.assertEquals("attachment", entity.getHeader().getField(
                 "Content-Disposition").getBody());
     }
 
+    @Test
     public void testSetContentDispositionTypeFilename() throws Exception {
         BodyPart entity = new BodyPart();
 
         entity.setContentDisposition("attachment", "some file.dat");
 
-        assertEquals("attachment; filename=\"some file.dat\"", entity
+        Assert.assertEquals("attachment; filename=\"some file.dat\"", entity
                 .getHeader().getField("Content-Disposition").getBody());
     }
 
+    @Test
     public void testGetFilename() throws Exception {
         BodyPart entity = new BodyPart();
 
-        assertNull(entity.getFilename());
+        Assert.assertNull(entity.getFilename());
 
         Header header = new HeaderImpl();
         header.setField(DefaultFieldParser.parse("Content-Disposition: attachment; "
                 + "filename=\"some file.dat\""));
         entity.setHeader(header);
 
-        assertEquals("some file.dat", entity.getFilename());
+        Assert.assertEquals("some file.dat", entity.getFilename());
     }
 
+    @Test
     public void testSetFilename() throws Exception {
         BodyPart entity = new BodyPart();
 
         entity.setFilename("file name.ext");
 
-        assertEquals("attachment; filename=\"file name.ext\"", entity
+        Assert.assertEquals("attachment; filename=\"file name.ext\"", entity
                 .getHeader().getField("Content-Disposition").getBody());
 
         entity.setFilename(null);
 
-        assertEquals("attachment", entity.getHeader().getField(
+        Assert.assertEquals("attachment", entity.getHeader().getField(
                 "Content-Disposition").getBody());
     }
 

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/ExampleMessagesRoundtripTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/ExampleMessagesRoundtripTest.java?rev=1481814&r1=1481813&r2=1481814&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/ExampleMessagesRoundtripTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/ExampleMessagesRoundtripTest.java
Mon May 13 12:13:09 2013
@@ -1,5 +1,5 @@
 /****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one   *
+((TextBody) b).getInputStream()((TextBody) b).getInputStream()((TextBody) b).getInputStream()((TextBody)
b).getInputStream()((TextBody) b).getInputStream()((TextBody) b).getInputStream()((TextBody)
b).getInputStream() * 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   *
@@ -19,110 +19,92 @@
 
 package org.apache.james.mime4j.dom;
 
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import org.apache.james.mime4j.codec.CodecUtil;
-import org.apache.james.mime4j.message.DefaultMessageBuilder;
-import org.apache.james.mime4j.message.DefaultMessageWriter;
-import org.apache.james.mime4j.stream.MimeConfig;
-
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.net.JarURLConnection;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
+import java.io.InputStream;
 import java.net.URL;
-import java.util.Enumeration;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
+
+import junit.framework.TestSuite;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.james.mime4j.ExampleMessageTestCase;
+import org.apache.james.mime4j.ExampleMessageTestCaseFactory;
+import org.apache.james.mime4j.ExampleMessageTestSuiteBuilder;
+import org.apache.james.mime4j.message.DefaultMessageBuilder;
+import org.apache.james.mime4j.message.DefaultMessageWriter;
+import org.apache.james.mime4j.stream.MimeConfig;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.junit.Assert;
+import org.junit.runner.RunWith;
+import org.junit.runners.AllTests;
 
 /**
- * Creates a TestSuite running the test for each .msg file in the test resouce folder.
- * Allow running of a single test from Unit testing GUIs
+ * Test round-trip of all sample messages
  */
-public class ExampleMessagesRoundtripTest extends TestCase {
+@RunWith(AllTests.class)
+public class ExampleMessagesRoundtripTest extends ExampleMessageTestCase {
+
+    public static TestSuite suite() throws IOException {
+        ExampleMessageTestSuiteBuilder testSuiteBuilder = new ExampleMessageTestSuiteBuilder(
+                new ExampleMessageTestCaseFactory() {
+
+                    public ExampleMessageTestCase create(final File file, final URL resource)
throws IOException {
+                        return new ExampleMessagesRoundtripTest(file, resource);
+                    }
 
-    private final URL url;
+                });
+        return testSuiteBuilder.build();
+    }
 
-    public ExampleMessagesRoundtripTest(String name, URL url) {
-        super(name);
-        this.url = url;
+    public ExampleMessagesRoundtripTest(final File file, final URL resource) {
+        super(file, resource);
     }
 
     @Override
-    protected void runTest() throws Throwable {
-        MimeConfig config = new MimeConfig();
-        if (getName().startsWith("malformedHeaderStartsBody")) {
-            config.setMalformedHeaderStartsBody(true);
-        }
-        config.setMaxLineLen(-1);
-        DefaultMessageBuilder builder = new DefaultMessageBuilder();
-        DefaultMessageWriter writer = new DefaultMessageWriter();
-        builder.setMimeEntityConfig(config);
-        Message inputMessage = builder.parseMessage(url.openStream());
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        writer.writeMessage(inputMessage, out);
+    protected void runTest() throws Exception {
+        MimeConfig config = getConfig();
 
-        String s = url.toString();
-        URL msgout = new URL(s.substring(0, s.lastIndexOf('.')) + ".out");
+        Message inputMessage;
+        InputStream msgstream = getResource().openStream();
         try {
-            ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
-            CodecUtil.copy(msgout.openStream(), expectedstream);
-            assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()),
new String(out.toByteArray()));
-        } catch (FileNotFoundException e) {
-            FileOutputStream fos = new FileOutputStream(msgout.getPath() + ".expected");
-            writer.writeMessage(inputMessage, fos);
-            fos.close();
-            fail("Expected file created");
+            DefaultMessageBuilder msgbuilder = new DefaultMessageBuilder();
+            msgbuilder.setMimeEntityConfig(config);
+            inputMessage = msgbuilder.parseMessage(msgstream);
+        } finally {
+            msgstream.close();
         }
-    }
-
-    public static Test suite() throws IOException, URISyntaxException {
-        return new ExampleMessagesRountripTestSuite();
-    }
 
+        DefaultMessageWriter msgwriter = new DefaultMessageWriter();
 
-    static class ExampleMessagesRountripTestSuite extends TestSuite {
+        ByteArrayOutputStream outstream = new ByteArrayOutputStream();
+        msgwriter.writeMessage(inputMessage, outstream);
 
-        public ExampleMessagesRountripTestSuite() throws IOException, URISyntaxException
{
-            super();
-            addTests("/testmsgs");
-            addTests("/mimetools-testmsgs");
-        }
+        String result = new String(outstream.toByteArray(), CharsetUtil.ISO_8859_1.name());
 
-        private void addTests(String testsFolder) throws URISyntaxException,
-                MalformedURLException, IOException {
-            URL resource = ExampleMessagesRountripTestSuite.class.getResource(testsFolder);
-            if (resource != null) {
-                if (resource.getProtocol().equalsIgnoreCase("file")) {
-                    File dir = new File(resource.toURI());
-                    File[] files = dir.listFiles();
-
-                    for (File f : files) {
-                        if (f.getName().endsWith(".msg")) {
-                            addTest(new ExampleMessagesRoundtripTest(f.getName(),
-                                    f.toURI().toURL()));
-                        }
-                    }
-                } else if (resource.getProtocol().equalsIgnoreCase("jar")) {
-                    JarURLConnection conn = (JarURLConnection) resource.openConnection();
-                    JarFile jar = conn.getJarFile();
-                    for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();
) {
-                        JarEntry entry = it.nextElement();
-                        String s = "/" + entry.toString();
-                        File f = new File(s);
-                        if (s.startsWith(testsFolder) && s.endsWith(".msg")) {
-                            addTest(new ExampleMessagesRoundtripTest(f.getName(),
-                                    new URL("jar:file:" + jar.getName() + "!" + s)));
-                        }
-                    }
-                }
+        URL outFile = new URL(getResourceBase() + ".out");
+        try {
+            String expected;
+            InputStream contentstream = outFile.openStream();
+            try {
+                expected = IOUtils.toString(contentstream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                contentstream.close();
             }
+            assertEquals(expected, result);
+        } catch (FileNotFoundException e) {
+            // Create expected content template to the current directory
+            File expectedFileTemplate = new File(getFilenameBase() + ".out.expected");
+            FileOutputStream templatestream = new FileOutputStream(expectedFileTemplate);
+            try {
+                IOUtils.write(result, templatestream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                templatestream.close();
+            }
+            Assert.fail("Expected file created.");
         }
-
     }
+
 }

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java?rev=1481814&r1=1481813&r2=1481814&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
Mon May 13 12:13:09 2013
@@ -19,104 +19,89 @@
 
 package org.apache.james.mime4j.dom;
 
-import junit.framework.Test;
-import junit.framework.TestCase;
-import junit.framework.TestSuite;
-import org.apache.commons.io.IOUtils;
-import org.apache.james.mime4j.field.FieldsTest;
-import org.apache.james.mime4j.message.DefaultMessageBuilder;
-import org.apache.james.mime4j.message.MessageImpl;
-import org.apache.james.mime4j.stream.Field;
-import org.apache.james.mime4j.stream.MimeConfig;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.JarURLConnection;
-import java.net.MalformedURLException;
-import java.net.URISyntaxException;
 import java.net.URL;
-import java.util.Enumeration;
 import java.util.List;
-import java.util.jar.JarEntry;
-import java.util.jar.JarFile;
 
-public class MessageParserTest extends TestCase {
+import junit.framework.TestSuite;
 
-    private final URL url;
+import org.apache.commons.io.IOUtils;
+import org.apache.james.mime4j.ExampleMessageTestCase;
+import org.apache.james.mime4j.ExampleMessageTestCaseFactory;
+import org.apache.james.mime4j.ExampleMessageTestSuiteBuilder;
+import org.apache.james.mime4j.field.FieldsTest;
+import org.apache.james.mime4j.message.DefaultMessageBuilder;
+import org.apache.james.mime4j.message.MessageImpl;
+import org.apache.james.mime4j.stream.Field;
+import org.apache.james.mime4j.stream.MimeConfig;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.junit.Assert;
+import org.junit.runner.RunWith;
+import org.junit.runners.AllTests;
+
+/**
+ * Test parsing into DOM of all sample messages
+ */
+@RunWith(AllTests.class)
+public class MessageParserTest extends ExampleMessageTestCase {
+
+    public static TestSuite suite() throws IOException {
+        ExampleMessageTestSuiteBuilder testSuiteBuilder = new ExampleMessageTestSuiteBuilder(
+                new ExampleMessageTestCaseFactory() {
 
-    public MessageParserTest(String name, URL url) {
-        super(name);
-        this.url = url;
-    }
+                    public ExampleMessageTestCase create(final File file, final URL resource)
throws IOException {
+                        return new MessageParserTest(file, resource);
+                    }
 
-    public static Test suite() throws IOException, URISyntaxException {
-        return new MessageParserTestSuite();
+                });
+        return testSuiteBuilder.build();
     }
 
-    static class MessageParserTestSuite extends TestSuite {
-
-        public MessageParserTestSuite() throws IOException, URISyntaxException {
-            addTests("/testmsgs");
-            addTests("/mimetools-testmsgs");
-        }
-
-        private void addTests(String testsFolder) throws URISyntaxException,
-                MalformedURLException, IOException {
-            URL resource = MessageParserTestSuite.class.getResource(testsFolder);
-            if (resource != null) {
-                if (resource.getProtocol().equalsIgnoreCase("file")) {
-                    File dir = new File(resource.toURI());
-                    File[] files = dir.listFiles();
-
-                    for (File f : files) {
-                        if (f.getName().endsWith(".msg")) {
-                            addTest(new MessageParserTest(f.getName(),
-                                    f.toURI().toURL()));
-                        }
-                    }
-                } else if (resource.getProtocol().equalsIgnoreCase("jar")) {
-                    JarURLConnection conn = (JarURLConnection) resource.openConnection();
-                    JarFile jar = conn.getJarFile();
-                    for (Enumeration<JarEntry> it = jar.entries(); it.hasMoreElements();
) {
-                        JarEntry entry = it.nextElement();
-                        String s = "/" + entry.toString();
-                        File f = new File(s);
-                        if (s.startsWith(testsFolder) && s.endsWith(".msg")) {
-                            addTest(new MessageParserTest(f.getName(),
-                                    new URL("jar:file:" + jar.getName() + "!" + s)));
-                        }
-                    }
-                }
-            }
-        }
-
+    public MessageParserTest(final File file, final URL resource) {
+        super(file, resource);
     }
 
     @Override
     protected void runTest() throws IOException {
-        MimeConfig config = new MimeConfig();
-        if (getName().startsWith("malformedHeaderStartsBody")) {
-            config.setMalformedHeaderStartsBody(true);
-        }
-        config.setMaxLineLen(-1);
-        DefaultMessageBuilder builder = new DefaultMessageBuilder();
-        builder.setMimeEntityConfig(config);
-        Message m = builder.parseMessage(url.openStream());
-
-        String s = url.toString();
-        String prefix = s.substring(0, s.lastIndexOf('.'));
-        URL xmlFileUrl = new URL(prefix + "_decoded.xml");
+        MimeConfig config = getConfig();
 
-        String result = getStructure(m, prefix, "1");
+        Message inputMessage;
+        InputStream msgstream = getResource().openStream();
         try {
-            String expected = IOUtils.toString(xmlFileUrl.openStream(), "ISO8859-1");
-            assertEquals(expected, result);
+            DefaultMessageBuilder msgbuilder = new DefaultMessageBuilder();
+            msgbuilder.setMimeEntityConfig(config);
+            inputMessage = msgbuilder.parseMessage(msgstream);
+        } finally {
+            msgstream.close();
+        }
+
+        String resourceBase = getResourceBase();
+        URL decodedFile = new URL(resourceBase + "_decoded.xml");
+
+        String result = getStructure(inputMessage, resourceBase, "1");
+        try {
+            String expected;
+            InputStream contentstream = decodedFile.openStream();
+            try {
+                expected = IOUtils.toString(contentstream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                contentstream.close();
+            }
+            Assert.assertEquals(expected, result);
         } catch (FileNotFoundException ex) {
-            IOUtils.write(result, new FileOutputStream(xmlFileUrl.getPath() + ".expected"),
"ISO8859-1");
-            fail("Expected file created.");
+            // Create expected content template to the current directory
+            File expectedFileTemplate = new File(getFilenameBase() + "_decoded.xml.expected");
+            FileOutputStream templatestream = new FileOutputStream(expectedFileTemplate);
+            try {
+                IOUtils.write(result, templatestream, CharsetUtil.ISO_8859_1.name());
+            } finally {
+                templatestream.close();
+            }
+            Assert.fail("Expected file created.");
         }
     }
 
@@ -126,8 +111,8 @@ public class MessageParserTest extends T
         return s.replaceAll(">", "&gt;");
     }
 
-    private String getStructure(Entity e, String prefix, String id)
-            throws IOException {
+    private String getStructure(
+            final Entity e, final String resourceBase, final String id) throws IOException
{
 
         StringBuilder sb = new StringBuilder();
 
@@ -157,7 +142,7 @@ public class MessageParserTest extends T
 
             int i = 1;
             for (Entity bodyPart : parts) {
-                sb.append(getStructure(bodyPart, prefix, id + "_" + (i++)));
+                sb.append(getStructure(bodyPart, resourceBase, id + "_" + (i++)));
             }
 
             if (multipart.getEpilogue() != null) {
@@ -169,37 +154,62 @@ public class MessageParserTest extends T
             sb.append("</multipart>\r\n");
 
         } else if (e.getBody() instanceof MessageImpl) {
-            sb.append(getStructure((MessageImpl) e.getBody(), prefix, id + "_1"));
+            sb.append(getStructure((MessageImpl) e.getBody(), resourceBase, id + "_1"));
         } else {
             Body b = e.getBody();
-            String s = prefix + "_decoded_" + id
-                    + (b instanceof TextBody ? ".txt" : ".bin");
+            String suffix = "_decoded_" + id + (b instanceof TextBody ? ".txt" : ".bin");
+            String filename = getFilenameBase() + suffix;
+
             String tag = b instanceof TextBody ? "text-body" : "binary-body";
-            File f = new File(s);
-            sb.append("<").append(tag).append(" name=\"").append(f.getName()).append("\"/>\r\n");
-            URL expectedUrl = new URL(s);
+            sb.append("<").append(tag).append(" name=\"").append(filename).append("\"/>\r\n");
+            URL expectedUrl = new URL(getResourceBase() + suffix);
 
             if (b instanceof TextBody) {
                 String charset = e.getCharset();
                 if (charset == null) {
-                    charset = "ISO8859-1";
+                    charset = CharsetUtil.ISO_8859_1.name();
                 }
 
-                String s2 = IOUtils.toString(((TextBody) b).getReader());
+                String result = IOUtils.toString(((TextBody) b).getReader());
                 try {
-                    String s1 = IOUtils.toString(expectedUrl.openStream(), charset);
-                    assertEquals(f.getName(), s1, s2);
+                    String expected;
+                    InputStream contentstream = expectedUrl.openStream();
+                    try {
+                        expected = IOUtils.toString(contentstream, charset);
+                    } finally {
+                        contentstream.close();
+                    }
+                    Assert.assertEquals(filename, expected, result);
                 } catch (FileNotFoundException ex) {
-                    IOUtils.write(s2, new FileOutputStream(expectedUrl.getPath() + ".expected"));
-                    fail("Expected file created.");
+                    // Create expected content template to the current directory
+                    File expectedFileTemplate = new File(filename + ".expected");
+                    FileOutputStream templatestream = new FileOutputStream(expectedFileTemplate);
+                    try {
+                        IOUtils.copy(((TextBody) b).getInputStream(), templatestream);
+                    } finally {
+                        templatestream.close();
+                    }
+                    Assert.fail("Expected file created.");
                 }
             } else {
                 try {
-                    assertEqualsBinary(f.getName(), expectedUrl.openStream(),
-                            ((BinaryBody) b).getInputStream());
+                    InputStream contentstream = expectedUrl.openStream();
+                    try {
+                        assertEqualsBinary(filename, contentstream,
+                                ((BinaryBody) b).getInputStream());
+                    } finally {
+                        contentstream.close();
+                    }
                 } catch (FileNotFoundException ex) {
-                    IOUtils.copy(((BinaryBody) b).getInputStream(), new FileOutputStream(expectedUrl.getPath()
+ ".expected"));
-                    fail("Expected file created.");
+                    // Create expected content template to the current directory
+                    File expectedFileTemplate = new File(filename + ".expected");
+                    FileOutputStream templatestream = new FileOutputStream(expectedFileTemplate);
+                    try {
+                        IOUtils.copy(((BinaryBody) b).getInputStream(), templatestream);
+                    } finally {
+                        templatestream.close();
+                    }
+                    Assert.fail("Expected file created.");
                 }
             }
         }
@@ -220,7 +230,7 @@ public class MessageParserTest extends T
         while (true) {
             int b1 = a.read();
             int b2 = b.read();
-            assertEquals(msg + " (Position " + (++pos) + ")", b1, b2);
+            Assert.assertEquals(msg + " (Position " + (++pos) + ")", b1, b2);
 
             if (b1 == -1 || b2 == -1) {
                 break;

Modified: james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/StringInputStreamTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/StringInputStreamTest.java?rev=1481814&r1=1481813&r2=1481814&view=diff
==============================================================================
--- james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/StringInputStreamTest.java
(original)
+++ james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/message/StringInputStreamTest.java
Mon May 13 12:13:09 2013
@@ -53,6 +53,7 @@ public class StringInputStreamTest {
             assertTrue(read <= 255);
             assertEquals(b, (byte) read);
         }
+        in.close();
         assertEquals(-1, in.read());
     }
 
@@ -80,6 +81,7 @@ public class StringInputStreamTest {
                 }
             }
         }
+        in.close();
     }
 
     @Test
@@ -107,6 +109,7 @@ public class StringInputStreamTest {
         InputStream r = new StringInputStream("test", CharsetUtil.UTF_8);
         byte[] bytes = new byte[30];
         Assert.assertEquals(0, r.read(bytes, 0, 0));
+        r.close();
     }
 
     @Test
@@ -117,6 +120,7 @@ public class StringInputStreamTest {
         Assert.assertEquals('t', r.read());
         r.skip(100);
         Assert.assertEquals(-1, r.read());
+        r.close();
     }
 
     @Test
@@ -133,6 +137,7 @@ public class StringInputStreamTest {
         Assert.assertEquals(-1, r.read());
         r.reset();
         r.reset();
+        r.close();
     }
 
 }



Mime
View raw message