james-mime4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1093972 - in /james/mime4j/trunk/core/src: main/java/org/apache/james/mime4j/util/CharsetUtil.java test/java/org/apache/james/mime4j/util/CharsetUtilTest.java
Date Sat, 16 Apr 2011 11:48:33 GMT
Author: olegk
Date: Sat Apr 16 11:48:33 2011
New Revision: 1093972

URL: http://svn.apache.org/viewvc?rev=1093972&view=rev
Log:
CharsetUtil#lookup to catch IllegalCharsetNameException; added javadocs and test cases

Modified:
    james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java
    james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/util/CharsetUtilTest.java

Modified: james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java?rev=1093972&r1=1093971&r2=1093972&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java (original)
+++ james/mime4j/trunk/core/src/main/java/org/apache/james/mime4j/util/CharsetUtil.java Sat
Apr 16 11:48:33 2011
@@ -20,6 +20,7 @@
 package org.apache.james.mime4j.util;
 
 import java.nio.charset.Charset;
+import java.nio.charset.IllegalCharsetNameException;
 import java.nio.charset.UnsupportedCharsetException;
 
 /**
@@ -119,13 +120,24 @@ public class CharsetUtil {
         }
         return true;
     }
-    
+
+    /**
+     * Returns a {@link Charset} instance if character set with the given name 
+     * is recognized and supported by Java runtime. Returns <code>null</code>

+     * otherwise.
+     * <p/>
+     * This method is a wrapper around {@link Charset#forName(String)} method 
+     * that catches {@link IllegalCharsetNameException} and 
+     *  {@link UnsupportedCharsetException} and returns <code>null</code>.
+     */
     public static Charset lookup(final String name) {
         if (name == null) {
             return null;
         }
         try {
             return Charset.forName(name);
+        } catch (IllegalCharsetNameException ex) {
+            return null;
         } catch (UnsupportedCharsetException ex) {
             return null;
         }

Modified: james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/util/CharsetUtilTest.java
URL: http://svn.apache.org/viewvc/james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/util/CharsetUtilTest.java?rev=1093972&r1=1093971&r2=1093972&view=diff
==============================================================================
--- james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/util/CharsetUtilTest.java
(original)
+++ james/mime4j/trunk/core/src/test/java/org/apache/james/mime4j/util/CharsetUtilTest.java
Sat Apr 16 11:48:33 2011
@@ -19,6 +19,8 @@
 
 package org.apache.james.mime4j.util;
 
+import java.nio.charset.Charset;
+
 import junit.framework.TestCase;
 
 public class CharsetUtilTest extends TestCase {
@@ -36,4 +38,21 @@ public class CharsetUtilTest extends Tes
         assertFalse(CharsetUtil.isASCII(RUSSIAN_HELLO));
     }
 
+    public void testCharsetLookup() {
+        Charset c1 = CharsetUtil.lookup("us-ascii");
+        Charset c2 = CharsetUtil.lookup("ascii");
+        assertEquals(CharsetUtil.US_ASCII, c1);
+        assertEquals(CharsetUtil.US_ASCII, c2);
+    }
+
+    public void testCharsetLookupNullInput() {
+        Charset c1 = CharsetUtil.lookup(null);
+        assertNull(c1);
+    }
+
+    public void testCharsetLookupFailure() {
+        Charset c1 = CharsetUtil.lookup("whatever");
+        assertNull(c1);
+    }
+
 }



Mime
View raw message