james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1481557 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/util/ test/java/org/apache/james/mime4j/ test/java/org/apache/james/mime4j/parser/
Date Sun, 12 May 2013 13:58:51 GMT
Author: olegk
Date: Sun May 12 13:58:51 2013
New Revision: 1481557

URL: http://svn.apache.org/r1481557
Log:
Cleanups in core unit tests; removed unnecessary test utils

Removed:
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/EncodeUtils.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/TestUtil.java
Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMail.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java?rev=1481557&r1=1481556&r2=1481557&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/ContentUtil.java Sun
May 12 13:58:51 2013
@@ -163,52 +163,70 @@ public class ContentUtil {
                 .toString();
     }
 
-    public static byte[] toAsciiByteArray(final String s) {
+    public static byte[] toByteArray(final String s, final Charset charset) {
         if (s == null) {
             return null;
         }
         try {
-            return s.getBytes(CharsetUtil.US_ASCII.name());
+            return s.getBytes((charset != null ? charset : CharsetUtil.DEFAULT_CHARSET).name());
         } catch (UnsupportedEncodingException ex) {
             // Should never happen
             throw new Error(ex);
         }
     }
 
-    public static String toAsciiString(final byte[] b) {
+    public static byte[] toAsciiByteArray(final String s) {
+        return toByteArray(s, CharsetUtil.US_ASCII);
+    }
+
+    public static String toString(final byte[] b, final Charset charset) {
         if (b == null) {
             return null;
         }
         try {
-            return new String(b, CharsetUtil.US_ASCII.name());
+            return new String(b, (charset != null ? charset : CharsetUtil.DEFAULT_CHARSET).name());
         } catch (UnsupportedEncodingException ex) {
             // Should never happen
             throw new Error(ex);
         }
     }
 
-    public static String toAsciiString(final byte[] b, int off, int len) {
+    public static String toAsciiString(final byte[] b) {
+        return toString(b, CharsetUtil.US_ASCII);
+    }
+
+    public static String toString(final byte[] b, int off, int len, final Charset charset)
{
         if (b == null) {
             return null;
         }
         try {
-            return new String(b, off, len, CharsetUtil.US_ASCII.name());
+            return new String(b, off, len,
+                    (charset != null ? charset : CharsetUtil.DEFAULT_CHARSET).name());
         } catch (UnsupportedEncodingException ex) {
             // Should never happen
             throw new Error(ex);
         }
     }
 
-    public static String toAsciiString(final ByteArrayBuffer b) {
+    public static String toAsciiString(final byte[] b, int off, int len) {
+        return toString(b, off, len, CharsetUtil.US_ASCII);
+    }
+
+    public static String toString(final ByteArrayBuffer b, final Charset charset) {
         if (b == null) {
             return null;
         }
         try {
-            return new String(b.buffer(), 0, b.length(), CharsetUtil.US_ASCII.name());
+            return new String(b.buffer(), 0, b.length(),
+                    (charset != null ? charset : CharsetUtil.DEFAULT_CHARSET).name());
         } catch (UnsupportedEncodingException ex) {
             // Should never happen
             throw new Error(ex);
         }
     }
 
+    public static String toAsciiString(final ByteArrayBuffer b) {
+        return toString(b, CharsetUtil.US_ASCII);
+    }
+
 }

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMail.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMail.java?rev=1481557&r1=1481556&r2=1481557&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMail.java (original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/ExampleMail.java Sun May
12 13:58:51 2013
@@ -19,11 +19,12 @@
 
 package org.apache.james.mime4j;
 
-import org.apache.james.mime4j.util.CharsetUtil;
-
-import java.nio.charset.Charset;
 import java.util.Locale;
 
+import org.apache.james.mime4j.codec.EncoderUtil;
+import org.apache.james.mime4j.util.CharsetUtil;
+import org.apache.james.mime4j.util.ContentUtil;
+
 public class ExampleMail {
 
     public static final String MIME_MULTIPART_EMBEDDED_MESSAGES_INNER_MULTIPART_MIXED = "--4.66920160910299\r\n"
+
@@ -119,8 +120,6 @@ public class ExampleMail {
     public static final String MD5_CONTENT = "Q2hlY2sgSW50ZWdyaXR5IQ==";
     public static final String CONTENT_DESCRIPTION = "Blah blah blah";
     public static final String CONTENT_ID = "<f470f68e0803061002n22bc4124he14015a4b6d6327f@mail.gmail.com>";
-    public static final Charset US_ASCII = CharsetUtil.US_ASCII;
-    public static final Charset LATIN1 = CharsetUtil.ISO_8859_1;
 
     public static final String MIME_MULTIPART_EMBEDDED_MESSAGES =
             "From: Timothy Tayler <timothy@example.org>\r\n" +
@@ -353,7 +352,8 @@ public class ExampleMail {
             "  You still shall live,--such virtue hath my pen,--\r\n" +
             "  Where breath most breathes, even in the mouths of men.\r\n";
 
-    private static final byte[] ONE_PART_MIME_BASE64_LATIN1_ENCODED = EncodeUtils.toBase64(latin1(ONE_PART_MIME_BASE64_LATIN1_BODY));
+    private static final byte[] ONE_PART_MIME_BASE64_LATIN1_ENCODED = ascii(EncoderUtil.encodeB(
+            latin1(ONE_PART_MIME_BASE64_LATIN1_BODY)));
 
     public static final String ONE_PART_MIME_BASE64_ASCII_BODY = "Hello, World!\r\n";
 
@@ -372,7 +372,8 @@ public class ExampleMail {
                     "\r\n" +
                     ONE_PART_MIME_ASCII_BODY;
 
-    private static final byte[] ONE_PART_MIME_BASE64_ASCII_ENCODED = EncodeUtils.toBase64(ascii(ONE_PART_MIME_BASE64_ASCII_BODY));
+    private static final byte[] ONE_PART_MIME_BASE64_ASCII_ENCODED = ascii(EncoderUtil.encodeB(
+            ascii(ONE_PART_MIME_BASE64_ASCII_BODY)));
 
     public static final String ONE_PART_MIME_ASCII = "Received: by 10.114.126.16 with HTTP;
Thu, 6 Mar 2008 10:02:03 -0800 (PST)\r\n" +
             "Message-ID: <f470f68e0803061002n22bc4124he14015a4b6d6327f@mail.gmail.com>\r\n"
+
@@ -628,7 +629,7 @@ public class ExampleMail {
             ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_TWO),
             ascii(breakLines(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_QUOTED_PRINTABLE.replaceAll("\r\n",
"=0D=0A"))),
             ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_THREE),
-            EncodeUtils.toBase64(ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_BASE64)),
+            ascii(EncoderUtil.encodeB(ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_BASE64))),
             ascii(MIME_MIXED_MULTIPART_VARIOUS_ENCODINGS_END),
     };
 
@@ -641,25 +642,31 @@ public class ExampleMail {
     public static final byte[] ONE_PART_MIME_BASE64_LATIN1_UPPERCASE_BYTES = join(ascii(ONE_PART_MIME_BASE64_LATIN1_HEADERS.toUpperCase(Locale.UK)),
ONE_PART_MIME_BASE64_LATIN1_ENCODED);
     public static final byte[] ONE_PART_MIME_BASE64_LATIN1_BYTES = join(ascii(ONE_PART_MIME_BASE64_LATIN1_HEADERS),
ONE_PART_MIME_BASE64_LATIN1_ENCODED);
     public static final byte[] ONE_PART_MIME_BASE64_ASCII_BYTES = join(ascii(ONE_PART_MIME_BASE64_ASCII_HEADERS),
ONE_PART_MIME_BASE64_ASCII_ENCODED);
-    public static final byte[] RFC822_SIMPLE_BYTES = US_ASCII.encode(RFC_SIMPLE).array();
-    public static final byte[] ONE_PART_MIME_ASCII_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII).array();
-    public static final byte[] ONE_PART_MIME_8859_BYTES = LATIN1.encode(ONE_PART_MIME_8859).array();
-    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES = US_ASCII.encode(MULTIPART_WITH_BINARY_ATTACHMENTS).array();
-    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES = US_ASCII.encode(MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE).array();
-    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES
= US_ASCII.encode(MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE).array();
-    public static final byte[] ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION).array();
-    public static final byte[] ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES = US_ASCII.encode(ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES).array();
+    public static final byte[] RFC822_SIMPLE_BYTES = ascii(RFC_SIMPLE);
+    public static final byte[] ONE_PART_MIME_ASCII_BYTES = ascii(ONE_PART_MIME_ASCII);
+    public static final byte[] ONE_PART_MIME_8859_BYTES = latin1(ONE_PART_MIME_8859);
+    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_BYTES = ascii(MULTIPART_WITH_BINARY_ATTACHMENTS);
+    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE_BYTES = ascii(MULTIPART_WITH_BINARY_ATTACHMENTS_NOPREAMBLE);
+    public static final byte[] MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE_BYTES
= ascii(MULTIPART_WITH_BINARY_ATTACHMENTS_PREAMBLE_EPILOGUE);
+    public static final byte[] ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION_BYTES = ascii(ONE_PART_MIME_ASCII_COMMENT_IN_MIME_VERSION);
+    public static final byte[] ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES_BYTES = ascii(ONE_PART_MIME_ASCII_MIME_VERSION_SPANS_TWO_LINES);
     public static final byte[] MAIL_WITH_RFC822_PART_BYTES = ascii(MAIL_WITH_RFC822_PART);
     public static final byte[] MIME_MULTIPART_EMBEDDED_MESSAGES_BYTES = ascii(MIME_MULTIPART_EMBEDDED_MESSAGES);
 
-    public static byte[] ascii(String text) {
+    public static byte[] ascii(final String text) {
+        return  ContentUtil.toAsciiByteArray(text);
+    }
 
-        return US_ASCII.encode(text).array();
+    public static byte[] latin1(final String text) {
+        return ContentUtil.toByteArray(text, CharsetUtil.ISO_8859_1);
     }
 
-    public static byte[] latin1(String text) {
+    public static String ascii(final byte[] b) {
+        return  ContentUtil.toAsciiString(b);
+    }
 
-        return LATIN1.encode(text).array();
+    public static String latin1(final byte[] b) {
+        return ContentUtil.toString(b, CharsetUtil.ISO_8859_1);
     }
 
     public static byte[] join(byte[] one, byte[] two) {

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java?rev=1481557&r1=1481556&r2=1481557&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/parser/TestHandler.java
Sun May 12 13:58:51 2013
@@ -26,6 +26,7 @@ import org.apache.james.mime4j.parser.Co
 import org.apache.james.mime4j.stream.BodyDescriptor;
 import org.apache.james.mime4j.stream.Field;
 import org.apache.james.mime4j.util.ContentUtil;
+import org.junit.Assert;
 
 /**
  * Helper class to run comparison of parsed results
@@ -106,6 +107,6 @@ class TestHandler implements ContentHand
     }
 
     public void raw(InputStream is) throws IOException {
-        MimeStreamParserExampleMessagesTest.fail("raw should never be called");
+        Assert.fail("raw should never be called");
     }
 }



Mime
View raw message