You are viewing a plain text version of this content. The canonical link for it is here.
Posted to mime4j-dev@james.apache.org by ol...@apache.org on 2010/02/05 17:42:38 UTC
svn commit: r906996 - in /james/mime4j/trunk:
core/src/test/java/org/apache/james/mime4j/parser/
dom/src/test/java/org/apache/james/mime4j/dom/
Author: olegk
Date: Fri Feb 5 16:42:38 2010
New Revision: 906996
URL: http://svn.apache.org/viewvc?rev=906996&view=rev
Log:
Fixed broken test cases
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/ExampleMessagesRoundtripTest.java
james/mime4j/trunk/dom/src/test/java/org/apache/james/mime4j/dom/MessageParserTest.java
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=906996&r1=906995&r2=906996&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 Fri Feb 5 16:42:38 2010
@@ -20,12 +20,13 @@
package org.apache.james.mime4j.parser;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -41,16 +42,11 @@
*/
public class MimeStreamParserExampleMessagesTest extends TestCase {
- private File file;
+ private URL url;
-
- public MimeStreamParserExampleMessagesTest(String testName) throws URISyntaxException {
- this(testName, MimeStreamParserExampleMessagesTestSuite.getFile(testName));
- }
-
- public MimeStreamParserExampleMessagesTest(String name, File testFile) {
+ public MimeStreamParserExampleMessagesTest(String name, URL url) {
super(name);
- this.file = testFile;
+ this.url = url;
}
@Override
@@ -65,53 +61,54 @@
parser = new MimeStreamParser(config);
handler = new TestHandler();
- System.out.println("Parsing " + file.getName());
parser.setContentHandler(handler);
- parser.parse(new FileInputStream(file));
+ parser.parse(url.openStream());
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!");
- }
+ String s = url.toString();
+ String prefix = s.substring(0, s.lastIndexOf('.'));
+ URL xmlFileUrl = new URL(prefix + ".xml");
+ String expected = IOUtils.toString(xmlFileUrl.openStream(), "ISO8859-1");
+ assertEquals(expected, result);
}
public static Test suite() throws IOException, URISyntaxException {
return new MimeStreamParserExampleMessagesTestSuite();
}
-
static class MimeStreamParserExampleMessagesTestSuite extends TestSuite {
private static final String TESTS_FOLDER = "/testmsgs";
public MimeStreamParserExampleMessagesTestSuite() throws IOException, URISyntaxException {
- super();
URL resource = MimeStreamParserExampleMessagesTestSuite.class.getResource(TESTS_FOLDER);
if (resource != null) {
- File dir = new File(resource.toURI());
- File[] files = dir.listFiles();
-
- for (File f : files) {
- if (f.getName().toLowerCase().endsWith(".msg")) {
- addTest(new MimeStreamParserExampleMessagesTest(f.getName().substring(0, f.getName().length()-4), f));
+ 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.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(TESTS_FOLDER) && s.endsWith(".msg")) {
+ addTest(new MimeStreamParserExampleMessagesTest(f.getName(),
+ new URL("jar:file:" + jar.getName() + "!" + s)));
+ }
}
}
}
}
-
- public static File getFile(String name) throws URISyntaxException {
- return new File(MimeStreamParserExampleMessagesTestSuite.class.getResource(TESTS_FOLDER+File.separator+name+".msg").toURI());
- }
}
}
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=906996&r1=906995&r2=906996&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 Fri Feb 5 16:42:38 2010
@@ -21,12 +21,13 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
import java.io.IOException;
+import java.net.JarURLConnection;
import java.net.URISyntaxException;
import java.net.URL;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -36,7 +37,6 @@
import org.apache.james.mime4j.dom.Message;
import org.apache.james.mime4j.message.MessageImpl;
import org.apache.james.mime4j.stream.MimeEntityConfig;
-import org.apache.log4j.BasicConfigurator;
/**
* Creates a TestSuite running the test for each .msg file in the test resouce folder.
@@ -44,49 +44,30 @@
*/
public class ExampleMessagesRoundtripTest extends TestCase {
- private File file;
+ private URL url;
-
- public ExampleMessagesRoundtripTest(String testName) throws URISyntaxException {
- this(testName, ExampleMessagesRountripTestSuite.getFile(testName));
- }
-
- public ExampleMessagesRoundtripTest(String name, File testFile) {
+ public ExampleMessagesRoundtripTest(String name, URL url) {
super(name);
- this.file = testFile;
+ this.url = url;
}
@Override
- public void setUp() {
- BasicConfigurator.resetConfiguration();
- BasicConfigurator.configure();
- }
-
- @Override
protected void runTest() throws Throwable {
MimeEntityConfig config = new MimeEntityConfig();
if (getName().startsWith("malformedHeaderStartsBody")) {
config.setMalformedHeaderStartsBody(true);
}
config.setMaxLineLen(-1);
- Message inputMessage = new MessageImpl(new FileInputStream(file), config);
+ Message inputMessage = new MessageImpl(url.openStream(), config);
ByteArrayOutputStream out = new ByteArrayOutputStream();
inputMessage.writeTo(out);
- String msgoutFile = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".out";
- String msgoutFileMime4j = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf('.')) + ".mime4j.out";
+ String s = url.toString();
+ URL msgout = new URL(s.substring(0, s.lastIndexOf('.')) + ".out");
- try {
- ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
- CodecUtil.copy(new FileInputStream(msgoutFile), expectedstream);
- assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()), new String(out.toByteArray()));
- } catch (FileNotFoundException e) {
- FileOutputStream fos = new FileOutputStream(msgoutFileMime4j);
- fos.write(out.toByteArray());
- fos.flush();
- fos.close();
- fail("Expected file not found: generated a file with the expected result!");
- }
+ ByteArrayOutputStream expectedstream = new ByteArrayOutputStream();
+ CodecUtil.copy(msgout.openStream(), expectedstream);
+ assertEquals("Wrong Expected result", new String(expectedstream.toByteArray()), new String(out.toByteArray()));
}
public static Test suite() throws IOException, URISyntaxException {
@@ -107,18 +88,26 @@
File[] files = dir.listFiles();
for (File f : files) {
- if (f.getName().toLowerCase().endsWith(".msg")) {
- addTest(new ExampleMessagesRoundtripTest(f.getName().substring(0, f.getName().length()-4), f));
+ if (f.getName().endsWith(".msg")) {
+ addTest(new ExampleMessagesRoundtripTest(f.getName(),
+ 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(TESTS_FOLDER) && s.endsWith(".msg")) {
+ addTest(new ExampleMessagesRoundtripTest(f.getName(),
+ new URL("jar:file:" + jar.getName() + "!" + s)));
+ }
+ }
}
}
}
- public static File getFile(String name) throws URISyntaxException {
- return new File(ExampleMessagesRountripTestSuite.class.getResource(TESTS_FOLDER+File.separator+name+".msg").toURI());
- }
-
}
}
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=906996&r1=906995&r2=906996&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 Fri Feb 5 16:42:38 2010
@@ -19,18 +19,16 @@
package org.apache.james.mime4j.dom;
-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.net.JarURLConnection;
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;
import junit.framework.Test;
import junit.framework.TestCase;
@@ -47,27 +45,17 @@
import org.apache.james.mime4j.message.MessageImpl;
import org.apache.james.mime4j.stream.MimeEntityConfig;
import org.apache.james.mime4j.util.CharsetUtil;
-import org.apache.log4j.BasicConfigurator;
public class MessageParserTest extends TestCase {
- private File file = null;
-
- public MessageParserTest(String name) throws URISyntaxException {
- this(name, MessageParserTestSuite.getFile(name));
- }
+
+ private URL url;
- public MessageParserTest(String name, File file) {
+ public MessageParserTest(String name, URL url) {
super(name);
- this.file = file;
+ this.url = url;
}
- @Override
- public void setUp() {
- BasicConfigurator.resetConfiguration();
- BasicConfigurator.configure();
- }
-
- public static Test suite() throws URISyntaxException {
+ public static Test suite() throws IOException, URISyntaxException {
return new MessageParserTestSuite();
}
@@ -75,7 +63,7 @@
private static final String TESTS_FOLDER = "/testmsgs";
- public MessageParserTestSuite() throws URISyntaxException {
+ public MessageParserTestSuite() throws IOException, URISyntaxException {
URL resource = MessageParserTestSuite.class.getResource(TESTS_FOLDER);
if (resource != null) {
if (resource.getProtocol().equalsIgnoreCase("file")) {
@@ -83,64 +71,47 @@
File[] files = dir.listFiles();
for (File f : files) {
- if (f.getName().toLowerCase().endsWith(".msg")) {
- addTest(new ExampleMessagesRoundtripTest(f.getName().substring(0, f.getName().length()-4), f));
+ if (f.getName().endsWith(".msg")) {
+ addTest(new MessageParserTest(f.getName(),
+ 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(TESTS_FOLDER) && s.endsWith(".msg")) {
+ addTest(new MessageParserTest(f.getName(),
+ new URL("jar:file:" + jar.getName() + "!" + s)));
+ }
+ }
}
}
}
- public static File getFile(String name) throws URISyntaxException {
- return new File(MessageParserTestSuite.class.getResource(TESTS_FOLDER+File.separator+name+".msg").toURI());
- }
}
@Override
protected void runTest() throws IOException {
- File f = file;
- String fileName = file.getAbsolutePath();
-
- System.out.println("Parsing " + f.getName());
-
MimeEntityConfig config = new MimeEntityConfig();
if (getName().startsWith("malformedHeaderStartsBody")) {
config.setMalformedHeaderStartsBody(true);
}
config.setMaxLineLen(-1);
- MessageImpl m = new MessageImpl(new FileInputStream(f), config);
+ MessageImpl m = new MessageImpl(url.openStream(), config);
- String prefix = f.getName().substring(0, f.getName().length() - 4);
- String xmlFileName = fileName.substring(0, fileName.length() - 4)
- + "_decoded.xml";
+ String s = url.toString();
+ String prefix = s.substring(0, s.lastIndexOf('.'));
+ URL xmlFileUrl = new URL(prefix + "_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;
- }
+ String expected = IOUtils.toString(xmlFileUrl.openStream(), "ISO8859-1");
+ assertEquals(expected, result);
}
- 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("<", "<");
@@ -191,41 +162,25 @@
sb.append(getStructure((MessageImpl) e.getBody(), prefix, id + "_1"));
} else {
Body b = e.getBody();
- String name = prefix + "_decoded_" + id
+ String s = 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(file.getParent(), name);
- File mime4jFile = new File(file.getParent(),
- name.substring(0, name.length() - 4) + ".mime4j"
- + (b instanceof TextBody ? ".txt" : ".bin"));
+ File f = new File(s);
+ sb.append("<" + tag + " name=\"" + f.getName() + "\"/>\r\n");
+ URL expectedUrl = new URL(s);
- 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);
- String s2 = IOUtils.toString(((TextBody) b).getReader());
- assertEquals(expectedFile.getName(), s1, s2);
- } else {
- assertEqualsBinary(expectedFile.getName(), expected,
- ((BinaryBody) b).getInputStream());
+ if (b instanceof TextBody) {
+ String charset = CharsetUtil.toJavaCharset(e.getCharset());
+ if (charset == null) {
+ charset = "ISO8859-1";
}
- } catch (AssertionError er) {
- writeToFile(b, mime4jFile);
- throw er;
+
+ String s1 = IOUtils.toString(expectedUrl.openStream(), charset);
+ String s2 = IOUtils.toString(((TextBody) b).getReader());
+ assertEquals(f.getName(), s1, s2);
+ } else {
+ assertEqualsBinary(f.getName(), expectedUrl.openStream(),
+ ((BinaryBody) b).getInputStream());
}
}
@@ -239,22 +194,6 @@
return sb.toString();
}
- private void writeToFile(Body b, File mime4jFile)
- throws FileNotFoundException, IOException {
- if (b instanceof TextBody) {
- String charset = CharsetUtil.toJavaCharset(b.getParent().getCharset());
- if (charset == null) {
- charset = "ISO8859-1";
- }
-
- OutputStream out = new FileOutputStream(mime4jFile);
- IOUtils.copy(((TextBody) b).getReader(), out, charset);
- } else {
- OutputStream out = new FileOutputStream(mime4jFile);
- IOUtils.copy(((BinaryBody) b).getInputStream(), out);
- }
- }
-
private void assertEqualsBinary(String msg, InputStream a, InputStream b)
throws IOException {