hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggreg...@apache.org
Subject svn commit: r1392511 - in /httpcomponents/httpcore/trunk/httpcore/src: main/java/org/apache/http/entity/ContentType.java test/java/org/apache/http/entity/TestContentType.java
Date Mon, 01 Oct 2012 18:43:32 GMT
Author: ggregory
Date: Mon Oct  1 18:43:32 2012
New Revision: 1392511

URL: http://svn.apache.org/viewvc?rev=1392511&view=rev
Log:
[HTTPCORE-314] Add org.apache.http.entity.ContentType.withCharset(Charset) and String APIs.

Modified:
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java?rev=1392511&r1=1392510&r2=1392511&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/ContentType.java
Mon Oct  1 18:43:32 2012
@@ -243,4 +243,26 @@ public final class ContentType implement
         return contentType != null ? contentType : DEFAULT_TEXT;
     }
 
+    /**
+     * Creates a new instance with this MIME type and the given Charset.
+     * 
+     * @param charset
+     * @return a new instance with this MIME type and the given Charset.
+     * @since 4.3
+     */
+    public ContentType withCharset(Charset charset) {
+        return create(this.getMimeType(), charset);
+    }
+
+    /**
+     * Creates a new instance with this MIME type and the given Charset name.
+     * 
+     * @param charset name
+     * @return a new instance with this MIME type and the given Charset name.
+     * @since 4.3
+     */
+    public ContentType withCharset(String charset) {
+        return create(this.getMimeType(), charset);
+    }
+
 }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java?rev=1392511&r1=1392510&r2=1392511&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestContentType.java
Mon Oct  1 18:43:32 2012
@@ -27,6 +27,8 @@
 
 package org.apache.http.entity;
 
+import java.nio.charset.Charset;
+
 import org.apache.http.Header;
 import org.apache.http.ParseException;
 import org.apache.http.entity.BasicHttpEntity;
@@ -49,6 +51,30 @@ public class TestContentType {
     }
 
     @Test
+    public void testWithCharset() throws Exception {
+        ContentType contentType = ContentType.create("text/plain", "US-ASCII");
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
+        contentType = contentType.withCharset(Charset.forName("UTF-8"));
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals("UTF-8", contentType.getCharset().name());
+        Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString());
+    }
+
+    @Test
+    public void testWithCharsetString() throws Exception {
+        ContentType contentType = ContentType.create("text/plain", "US-ASCII");
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals("US-ASCII", contentType.getCharset().name());
+        Assert.assertEquals("text/plain; charset=US-ASCII", contentType.toString());
+        contentType = contentType.withCharset("UTF-8");
+        Assert.assertEquals("text/plain", contentType.getMimeType());
+        Assert.assertEquals("UTF-8", contentType.getCharset().name());
+        Assert.assertEquals("text/plain; charset=UTF-8", contentType.toString());
+    }
+
+    @Test
     public void testLowCaseText() throws Exception {
         ContentType contentType = ContentType.create("Text/Plain", "ascii");
         Assert.assertEquals("text/plain", contentType.getMimeType());



Mime
View raw message