Return-Path: Delivered-To: apmail-jakarta-httpclient-commits-archive@www.apache.org Received: (qmail 48564 invoked from network); 16 Oct 2005 20:05:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 16 Oct 2005 20:05:07 -0000 Received: (qmail 15958 invoked by uid 500); 16 Oct 2005 20:05:07 -0000 Mailing-List: contact httpclient-commits-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: httpclient-dev@jakarta.apache.org Delivered-To: mailing list httpclient-commits@jakarta.apache.org Received: (qmail 15947 invoked by uid 500); 16 Oct 2005 20:05:07 -0000 Delivered-To: apmail-jakarta-httpclient-cvs@jakarta.apache.org Received: (qmail 15942 invoked by uid 99); 16 Oct 2005 20:05:07 -0000 X-ASF-Spam-Status: No, hits=-9.4 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.29) with SMTP; Sun, 16 Oct 2005 13:05:06 -0700 Received: (qmail 47637 invoked by uid 65534); 16 Oct 2005 20:04:34 -0000 Message-ID: <20051016200434.47636.qmail@minotaur.apache.org> Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: httpclient-cvs@jakarta.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.0.5 X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 + } } }