hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r322508 - in /jakarta/httpclient/trunk/http-common: ./ src/java/org/apache/http/entity/StringEntity.java src/test/org/apache/http/entity/TestHttpEntities.java
Date Sun, 16 Oct 2005 20:04:33 GMT
Author: olegk
Date: Sun Oct 16 13:04:24 2005
New Revision: 322508

URL: http://svn.apache.org/viewcvs?rev=322508&view=rev
Log:
* Fixed bug in StringEntity#getContentLength method
* Added test cases for StringEntity class

Modified:
    jakarta/httpclient/trunk/http-common/   (props changed)
    jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/StringEntity.java
    jakarta/httpclient/trunk/http-common/src/test/org/apache/http/entity/TestHttpEntities.java

Propchange: jakarta/httpclient/trunk/http-common/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sun Oct 16 13:04:24 2005
@@ -1,4 +1,6 @@
+
 .project
 .classpath
 bin
 lib
+.settings

Modified: jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/StringEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/StringEntity.java?rev=322508&r1=322507&r2=322508&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/StringEntity.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/java/org/apache/http/entity/StringEntity.java
Sun Oct 16 13:04:24 2005
@@ -33,6 +33,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
 
 import org.apache.http.HttpEntity;
 
@@ -47,25 +48,29 @@
  */
 public class StringEntity implements HttpEntity {
 
+    private final static String DEFAULT_CHARSET = "ISO-8859-1";
     private final static String DEFAULT_CONTENT_TYPE = "text/plain; charset=";
 
-    private final String content;
+    private final byte[] content;
     private String contentType = null;
     private String contentEncoding = null;
     private boolean chunked = false;
 
-    public StringEntity(final String s, final String charset) {
+    public StringEntity(final String s, String charset) 
+            throws UnsupportedEncodingException {
         super();
         if (s == null) {
             throw new IllegalArgumentException("Source string may not be null");
         }
-        this.content = s;
-        if (charset != null) {
-            this.contentType = DEFAULT_CONTENT_TYPE + charset;
+        if (charset == null) {
+            charset = DEFAULT_CHARSET;
         }
+        this.contentType = DEFAULT_CONTENT_TYPE + charset;
+        this.content = s.getBytes(charset);
     }
 
-    public StringEntity(final String s) {
+    public StringEntity(final String s) 
+            throws UnsupportedEncodingException {
         this(s, null);
     }
 
@@ -82,7 +87,7 @@
     }
 
     public long getContentLength() {
-        return this.content.length();
+        return this.content.length;
     }
     
     public String getContentType() {
@@ -102,17 +107,14 @@
     }
     
     public InputStream getContent() throws IOException {
-        String charset = EntityConsumer.getContentCharSet(this);
-        return new ByteArrayInputStream(this.content.getBytes(charset));
+        return new ByteArrayInputStream(this.content);
     }
     
     public boolean writeTo(final OutputStream outstream) throws IOException {
         if (outstream == null) {
             throw new IllegalArgumentException("Output stream may not be null");
         }
-        String charset = EntityConsumer.getContentCharSet(this);
-        byte[] content = this.content.getBytes(charset);
-        outstream.write(content);
+        outstream.write(this.content);
         outstream.flush();
         return true;
     }

Modified: jakarta/httpclient/trunk/http-common/src/test/org/apache/http/entity/TestHttpEntities.java
URL: http://svn.apache.org/viewcvs/jakarta/httpclient/trunk/http-common/src/test/org/apache/http/entity/TestHttpEntities.java?rev=322508&r1=322507&r2=322508&view=diff
==============================================================================
--- jakarta/httpclient/trunk/http-common/src/test/org/apache/http/entity/TestHttpEntities.java
(original)
+++ jakarta/httpclient/trunk/http-common/src/test/org/apache/http/entity/TestHttpEntities.java
Sun Oct 16 13:04:24 2005
@@ -157,13 +157,79 @@
     	for (int i = 0; i < bytes.length; i++) {
     		assertEquals(bytes[i], bytes2[i]);
     	}
-    	
+        
     	try {
     		httpentity.writeTo(null);
     		fail("IllegalArgumentException should have been thrown");
     	} catch (IllegalArgumentException ex) {
     		// expected
     	}
+    }
+    
+    public void testStringEntity() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s, "ISO-8859-1");
+        httpentity.setContentType("text/plain");
+        httpentity.setContentEncoding("identity");
+        httpentity.setChunked(false);
+        
+        byte[] bytes = s.getBytes("ISO-8859-1");
+        assertEquals(bytes.length, httpentity.getContentLength());
+        assertEquals("text/plain", httpentity.getContentType());
+        assertEquals("identity", httpentity.getContentEncoding());
+        assertNotNull(httpentity.getContent());
+        assertFalse(httpentity.isChunked());
+        assertTrue(httpentity.isRepeatable());
+    }
+
+    public void testStringEntityIllegalConstructor() throws Exception {
+        try {
+            new StringEntity(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testStringEntityDefaultContent() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s, "US-ASCII");
+        assertEquals("text/plain; charset=US-ASCII", 
+                httpentity.getContentType());
+        httpentity = new StringEntity(s);
+        assertEquals("text/plain; charset=ISO-8859-1", 
+                httpentity.getContentType());
+    }
+
+    public void testStringEntityWriteTo() throws Exception {
+        String s = "Message content";
+        byte[] bytes = s.getBytes("ISO-8859-1");
+        StringEntity httpentity = new StringEntity(s);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        assertTrue(httpentity.writeTo(out));
+        byte[] bytes2 = out.toByteArray();
+        assertNotNull(bytes2);
+        assertEquals(bytes.length, bytes2.length);
+        for (int i = 0; i < bytes.length; i++) {
+            assertEquals(bytes[i], bytes2[i]);
+        }
+
+        out = new ByteArrayOutputStream();
+        assertTrue(httpentity.writeTo(out));
+        bytes2 = out.toByteArray();
+        assertNotNull(bytes2);
+        assertEquals(bytes.length, bytes2.length);
+        for (int i = 0; i < bytes.length; i++) {
+            assertEquals(bytes[i], bytes2[i]);
+        }
+        
+        try {
+            httpentity.writeTo(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
     }
     
 }



Mime
View raw message