james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject svn commit: r678314 - in /james/mime4j/trunk/src/test/java/org/apache/james/mime4j: MimeStreamParserExampleMessagesTest.java MimeStreamParserTest.java TestHandler.java message/MessageParserTest.java
Date Sun, 20 Jul 2008 19:48:41 GMT
Author: bago
Date: Sun Jul 20 12:48:41 2008
New Revision: 678314

URL: http://svn.apache.org/viewvc?rev=678314&view=rev
Log:
Refactored TestSuites to be better GUI friendly (show message under testing, allow running
of single message test)

Added:
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserExampleMessagesTest.java
  (with props)
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/TestHandler.java   (with props)
Modified:
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserTest.java
    james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserExampleMessagesTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserExampleMessagesTest.java?rev=678314&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserExampleMessagesTest.java
(added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserExampleMessagesTest.java
Sun Jul 20 12:48:41 2008
@@ -0,0 +1,112 @@
+/****************************************************************
+ * 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 org.apache.commons.io.IOUtils;
+import org.apache.log4j.BasicConfigurator;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * 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 File file;
+
+
+    public MimeStreamParserExampleMessagesTest(String testName) {
+        this(testName, MimeStreamParserExampleMessagesTestSuite.getFile(testName));
+    }
+
+    public MimeStreamParserExampleMessagesTest(String name, File testFile) {
+        super(name);
+        this.file = testFile;
+    }
+
+    public void setUp() {
+        BasicConfigurator.resetConfiguration();
+        BasicConfigurator.configure();
+    }
+   
+    protected void runTest() throws Throwable {
+        MimeStreamParser parser = null;
+        TestHandler handler = null;
+        parser = new MimeStreamParser();
+        handler = new TestHandler();
+        
+        System.out.println("Parsing " + file.getName());
+        parser.setContentHandler(handler);
+        parser.parse(new FileInputStream(file));
+        
+        String result = handler.sb.toString();
+        String xmlFile = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.'))
+ ".xml";
+        String xmlFileMime4j = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.'))
+ ".mime4j.xml";
+        
+        try {
+            String expected = IOUtils.toString(new FileInputStream(xmlFile), "ISO8859-1");
+            assertEquals("Error parsing " + file.getName(), expected, result);
+        } catch (FileNotFoundException e) {
+            FileOutputStream fos = new FileOutputStream(xmlFileMime4j);
+            fos.write(result.getBytes());
+            fos.flush();
+            fos.close();
+            fail("XML file not found: generated a file with the expected result!");
+        }
+    }
+
+    public static Test suite() throws IOException {
+        return new MimeStreamParserExampleMessagesTestSuite();
+    }
+
+    
+    static class MimeStreamParserExampleMessagesTestSuite extends TestSuite {
+
+        private static final File TESTS_FOLDER = new File("src/test/resources/testmsgs");
+
+        public MimeStreamParserExampleMessagesTestSuite() throws IOException {
+            super();
+            File dir = TESTS_FOLDER;
+            File[] files = dir.listFiles();
+            
+            for (int i = 0; i < files.length; i++) {
+                File f = files[i];
+                
+                if (f.getName().toLowerCase().endsWith(".msg")) {
+                    addTest(new MimeStreamParserExampleMessagesTest(f.getName().substring(0,
f.getName().length()-4), f));
+                }
+            }
+        }
+        
+        public static File getFile(String name) {
+            return new File(TESTS_FOLDER.getAbsolutePath()+File.separator+name+".msg");
+        }
+
+    }
+}

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

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

Modified: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserTest.java?rev=678314&r1=678313&r2=678314&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserTest.java (original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/MimeStreamParserTest.java Sun
Jul 20 12:48:41 2008
@@ -19,22 +19,15 @@
 
 package org.apache.james.mime4j;
 
-import org.apache.commons.io.IOUtils;
 import org.apache.log4j.BasicConfigurator;
 
 import java.io.ByteArrayInputStream;
-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.util.LinkedList;
 
 import junit.framework.TestCase;
 
-
-
 /**
  * 
  *
@@ -415,41 +408,6 @@
         assertEquals(0, expected.size());
     }
     
-    public void testParse() throws Exception {
-        File dir = new File("src/test/resources/testmsgs");
-        File[] files = dir.listFiles();
-        
-        for (int i = 0; i < files.length; i++) {
-            File f = files[i];
-            
-            if (f.getName().toLowerCase().endsWith(".msg")) {
-                MimeStreamParser parser = null;
-                TestHandler handler = null;
-                parser = new MimeStreamParser();
-                handler = new TestHandler();
-                
-                System.out.println("Parsing " + f.getName());
-                parser.setContentHandler(handler);
-                parser.parse(new FileInputStream(f));
-                
-                String result = handler.sb.toString();
-                String xmlFile = f.getAbsolutePath().substring(0, f.getAbsolutePath().lastIndexOf('.'))
+ ".xml";
-                String xmlFileMime4j = f.getAbsolutePath().substring(0, f.getAbsolutePath().lastIndexOf('.'))
+ ".mime4j.xml";
-                
-                try {
-                    String expected = IOUtils.toString(new FileInputStream(xmlFile), "ISO8859-1");
-                    assertEquals("Error parsing " + f.getName(), expected, result);
-                } catch (FileNotFoundException e) {
-                    FileOutputStream fos = new FileOutputStream(xmlFileMime4j);
-                    fos.write(result.getBytes());
-                    fos.flush();
-                    fos.close();
-                    fail("XML file not found: generated a file with the expected result!");
-                }
-            }
-        }
-    }
-    
     public void testAutomaticContentDecoding() throws Exception {
         MimeStreamParser parser = new MimeStreamParser();
         parser.setContentDecoding(true);
@@ -488,83 +446,4 @@
         assertEquals(expected, result);
     }
     
-    private static class TestHandler implements ContentHandler {
-        private StringBuffer sb = new StringBuffer();
-
-        private String escape(char c) {
-            if (c == '&') {
-                return "&amp;";
-            }
-            if (c == '>') {
-                return "&gt;";
-            }
-            if (c == '<') {
-                return "&lt;";
-            }
-            return "" + c;
-        }
-        
-        private String escape(String s) {
-            s = s.replaceAll("&", "&amp;");
-            s = s.replaceAll(">", "&gt;");
-            s = s.replaceAll("<", "&lt;");
-            return s;
-        }
-        
-        public void epilogue(InputStream is) throws IOException {
-            sb.append("<epilogue>\r\n");
-            int b = 0;
-            while ((b = is.read()) != -1) {
-                sb.append(escape((char) b));
-            }
-            sb.append("</epilogue>\r\n");
-        }
-        public void preamble(InputStream is) throws IOException {
-            sb.append("<preamble>\r\n");
-            int b = 0;
-            while ((b = is.read()) != -1) {
-                sb.append(escape((char) b));
-            }
-            sb.append("</preamble>\r\n");
-        }
-        public void startMultipart(BodyDescriptor bd) {
-            sb.append("<multipart>\r\n");
-        }
-        public void body(BodyDescriptor bd, InputStream is) throws IOException {
-            sb.append("<body>\r\n");
-            int b = 0;
-            while ((b = is.read()) != -1) {
-                sb.append(escape((char) b));
-            }
-            sb.append("</body>\r\n");
-        }
-        public void endMultipart() {
-            sb.append("</multipart>\r\n");
-        }
-        public void startBodyPart() {
-            sb.append("<body-part>\r\n");
-        }
-        public void endBodyPart() {
-            sb.append("</body-part>\r\n");
-        }
-        public void startHeader() {
-            sb.append("<header>\r\n");
-        }
-        public void field(String fieldData) {
-            sb.append("<field>\r\n" + escape(fieldData) + "</field>\r\n");
-        }
-        public void endHeader() {
-            sb.append("</header>\r\n");
-        }
-        public void startMessage() {
-            sb.append("<message>\r\n");
-        }
-        public void endMessage() {
-            sb.append("</message>\r\n");
-        }
-
-        public void raw(InputStream is) throws IOException {
-            fail("raw should never be called");
-        }
-    }
 }

Added: james/mime4j/trunk/src/test/java/org/apache/james/mime4j/TestHandler.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/src/test/java/org/apache/james/mime4j/TestHandler.java?rev=678314&view=auto
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/TestHandler.java (added)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/TestHandler.java Sun Jul 20 12:48:41
2008
@@ -0,0 +1,106 @@
+/****************************************************************
+ * 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.IOException;
+import java.io.InputStream;
+
+/**
+ * Helper class to run comparison of parsed results
+ */
+class TestHandler implements ContentHandler {
+    StringBuffer sb = new StringBuffer();
+
+    private String escape(char c) {
+        if (c == '&') {
+            return "&amp;";
+        }
+        if (c == '>') {
+            return "&gt;";
+        }
+        if (c == '<') {
+            return "&lt;";
+        }
+        return "" + c;
+    }
+    
+    private String escape(String s) {
+        s = s.replaceAll("&", "&amp;");
+        s = s.replaceAll(">", "&gt;");
+        s = s.replaceAll("<", "&lt;");
+        return s;
+    }
+    
+    public void epilogue(InputStream is) throws IOException {
+        sb.append("<epilogue>\r\n");
+        int b = 0;
+        while ((b = is.read()) != -1) {
+            sb.append(escape((char) b));
+        }
+        sb.append("</epilogue>\r\n");
+    }
+    public void preamble(InputStream is) throws IOException {
+        sb.append("<preamble>\r\n");
+        int b = 0;
+        while ((b = is.read()) != -1) {
+            sb.append(escape((char) b));
+        }
+        sb.append("</preamble>\r\n");
+    }
+    public void startMultipart(BodyDescriptor bd) {
+        sb.append("<multipart>\r\n");
+    }
+    public void body(BodyDescriptor bd, InputStream is) throws IOException {
+        sb.append("<body>\r\n");
+        int b = 0;
+        while ((b = is.read()) != -1) {
+            sb.append(escape((char) b));
+        }
+        sb.append("</body>\r\n");
+    }
+    public void endMultipart() {
+        sb.append("</multipart>\r\n");
+    }
+    public void startBodyPart() {
+        sb.append("<body-part>\r\n");
+    }
+    public void endBodyPart() {
+        sb.append("</body-part>\r\n");
+    }
+    public void startHeader() {
+        sb.append("<header>\r\n");
+    }
+    public void field(String fieldData) {
+        sb.append("<field>\r\n" + escape(fieldData) + "</field>\r\n");
+    }
+    public void endHeader() {
+        sb.append("</header>\r\n");
+    }
+    public void startMessage() {
+        sb.append("<message>\r\n");
+    }
+    public void endMessage() {
+        sb.append("</message>\r\n");
+    }
+
+    public void raw(InputStream is) throws IOException {
+        MimeStreamParserExampleMessagesTest.fail("raw should never be called");
+    }
+}
\ No newline at end of file

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

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

Modified: 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?rev=678314&r1=678313&r2=678314&view=diff
==============================================================================
--- james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java
(original)
+++ james/mime4j/trunk/src/test/java/org/apache/james/mime4j/message/MessageParserTest.java
Sun Jul 20 12:48:41 2008
@@ -48,12 +48,15 @@
  * @version $Id: MessageParserTest.java,v 1.4 2004/10/25 07:26:47 ntherning Exp $
  */
 public class MessageParserTest extends TestCase {
-    private String fileName = null;
+    private File file = null;
 
-    public MessageParserTest(String name, String fileName) {
+    public MessageParserTest(String name) {
+        this(name, MessageParserTestSuite.getFile(name));
+    }
+
+    public MessageParserTest(String name, File file) {
         super(name);
-        
-        this.fileName = fileName;
+        this.file = file;
     }
 
     public void setUp() {
@@ -62,24 +65,33 @@
     }
         
     public static Test suite() {
-        TestSuite suite = new TestSuite();
-        
-        File dir = new File("src/test/resources/testmsgs");
-        File[] files = dir.listFiles();
+        return new MessageParserTestSuite();
+    }
+    
+    static class MessageParserTestSuite extends TestSuite {
         
-        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()));
+        private static final File TESTS_FOLDER = new File("src/test/resources/testmsgs");
+
+        public MessageParserTestSuite() {
+            File dir = TESTS_FOLDER;
+            File[] files = dir.listFiles();
+            
+            for (int i = 0; i < files.length && i < 5000; i++) {
+                File f = files[i];
+                if (f.getName().toLowerCase().endsWith(".msg")) {
+                    addTest(new MessageParserTest(f.getName().substring(0, f.getName().length()-4),
f));
+                }
             }
         }
         
-        return suite;
+        public static File getFile(String name) {
+            return new File(TESTS_FOLDER.getAbsolutePath()+File.separator+name+".msg");
+        }
     }
     
     protected void runTest() throws IOException {
-        File f = new File(fileName);
+        File f = file;
+        String fileName = file.getAbsolutePath();
         
         System.out.println("Parsing " + f.getName());
         
@@ -169,8 +181,8 @@
             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(), 
+            File expectedFile = new File(file.getParent(), name);
+            File mime4jFile = new File(file.getParent(), 
                               name.substring(0, name.length() - 4) + ".mime4j"
                                + (b instanceof TextBody ? ".txt" : ".bin"));
                 



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message