You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/03/12 15:04:38 UTC

svn commit: r385289 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/entity/ test/org/apache/http/entity/ test/org/apache/http/mockup/

Author: olegk
Date: Sun Mar 12 06:04:36 2006
New Revision: 385289

URL: http://svn.apache.org/viewcvs?rev=385289&view=rev
Log:
* Minor bug fixes in BasicHttpEntity and InputStreamEntity classes
* More test coverage

Added:
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java   (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java   (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java   (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java   (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java   (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java   (with props)
Removed:
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntities.java
Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/InputStreamEntity.java
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAllEntity.java

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java?rev=385289&r1=385288&r2=385289&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BasicHttpEntity.java Sun Mar 12 06:04:36 2006
@@ -55,7 +55,7 @@
      */
     public BasicHttpEntity() {
         super();
-        length = -1;
+        this.length = -1;
     }
 
     // non-javadoc, see interface HttpEntity
@@ -75,13 +75,13 @@
      */
     public InputStream getContent()
         throws IllegalStateException {
-
-        if (this.content == null)
-            throw new IllegalStateException("content has not been provided");
-        if (contentObtained)
-            throw new IllegalStateException("content is not repeatable");
-
-        contentObtained = true;
+        if (this.content == null) {
+            throw new IllegalStateException("Content has not been provided");
+        }
+        if (this.contentObtained) {
+            throw new IllegalStateException("Content has been consumed");
+        }
+        this.contentObtained = true;
         return this.content;
 
     } // getContent
@@ -113,8 +113,7 @@
      */
     public void setContent(final InputStream instream) {
         this.content = instream;
-        if (instream != null)
-            contentObtained = false;
+        this.contentObtained = false; 
     }
 
     // non-javadoc, see interface HttpEntity
@@ -122,10 +121,7 @@
         if (outstream == null) {
             throw new IllegalArgumentException("Output stream may not be null");
         }
-        if (this.content == null) {
-            return;
-        }
-        InputStream instream = this.content;
+        InputStream instream = getContent();
         int l;
         byte[] tmp = new byte[2048];
         while ((l = instream.read(tmp)) != -1) {
@@ -135,14 +131,13 @@
 
     // non-javadoc, see interface HttpEntity
     public boolean isStreaming() {
-        return (content != null);
+        return !this.contentObtained && this.content != null;
     }
 
     // non-javadoc, see interface HttpEntity
     public void consumeContent() throws IOException {
         if (content != null) {
             content.close(); // reads to the end of the entity
-            content = null;
         }
     }
     

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/InputStreamEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/InputStreamEntity.java?rev=385289&r1=385288&r2=385289&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/InputStreamEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/InputStreamEntity.java Sun Mar 12 06:04:36 2006
@@ -76,28 +76,38 @@
         }
         InputStream instream = this.content;
         byte[] buffer = new byte[BUFFER_SIZE];
-        long remaining = this.length;
-    	while (remaining > 0) {
-    	    int l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
-    	    if (l == -1) {
-    	    	break;
-    	    }
-            outstream.write(buffer, 0, l);
-    	    remaining -= l;
-    	}
+        int l;
+        if (this.length < 0) {
+            // consume until EOF
+            while ((l = instream.read(buffer)) != -1) {
+                outstream.write(buffer, 0, l);
+            }
+        } else {
+            // consume no more than length
+            long remaining = this.length;
+            while (remaining > 0) {
+                l = instream.read(buffer, 0, (int)Math.min(BUFFER_SIZE, remaining));
+                if (l == -1) {
+                    break;
+                }
+                outstream.write(buffer, 0, l);
+                remaining -= l;
+            }
+        }
+        this.consumed = true;
     }
 
     // non-javadoc, see interface HttpEntity
     public boolean isStreaming() {
-        return !consumed;
+        return !this.consumed;
     }
 
     // non-javadoc, see interface HttpEntity
     public void consumeContent() throws IOException {
-        consumed = true;
+        this.consumed = true;
         // If the input stream is from a connection, closing it will read to
         // the end of the content. Otherwise, we don't care what it does.
-        content.close();
+        this.content.close();
     }
     
 } // class InputStreamEntity

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,111 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import org.apache.http.Header;
+import org.apache.http.mockup.HttpEntityMockup;
+import org.apache.http.protocol.HTTP;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link AbstractHttpEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestAbstractHttpEntity extends TestCase {
+
+    public TestAbstractHttpEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestAbstractHttpEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestAbstractHttpEntity.class);
+    }
+
+    public void testContentType() throws Exception {
+    	HttpEntityMockup httpentity = new HttpEntityMockup();
+        httpentity.setContentType(new Header(HTTP.CONTENT_TYPE, HTTP.PLAIN_TEXT_TYPE));
+        assertEquals(HTTP.CONTENT_TYPE, httpentity.getContentType().getName());
+        assertEquals(HTTP.PLAIN_TEXT_TYPE, httpentity.getContentType().getValue());
+
+        httpentity.setContentType(HTTP.PLAIN_TEXT_TYPE);
+        assertEquals(HTTP.CONTENT_TYPE, httpentity.getContentType().getName());
+        assertEquals(HTTP.PLAIN_TEXT_TYPE, httpentity.getContentType().getValue());
+
+        httpentity.setContentType((Header)null);
+        assertNull(httpentity.getContentType());
+        httpentity.setContentType((String)null);
+        assertNull(httpentity.getContentType());
+    }
+
+    public void testContentEncoding() throws Exception {
+        HttpEntityMockup httpentity = new HttpEntityMockup();
+        httpentity.setContentEncoding(new Header(HTTP.CONTENT_ENCODING, "gzip"));
+        assertEquals(HTTP.CONTENT_ENCODING, httpentity.getContentEncoding().getName());
+        assertEquals("gzip", httpentity.getContentEncoding().getValue());
+
+        httpentity.setContentEncoding("gzip");
+        assertEquals(HTTP.CONTENT_ENCODING, httpentity.getContentEncoding().getName());
+        assertEquals("gzip", httpentity.getContentEncoding().getValue());
+
+        httpentity.setContentEncoding((Header)null);
+        assertNull(httpentity.getContentEncoding());
+        httpentity.setContentEncoding((String)null);
+        assertNull(httpentity.getContentEncoding());
+    }
+
+    public void testChunkingFlag() throws Exception {
+        HttpEntityMockup httpentity = new HttpEntityMockup();
+        assertFalse(httpentity.isChunked());
+        httpentity.setChunked(true);
+        assertTrue(httpentity.isChunked());
+    }
+    
+    public void testConsumeContent() throws Exception {
+        HttpEntityMockup httpentity = new HttpEntityMockup();
+        httpentity.setStreaming(true);
+        try {
+            httpentity.consumeContent();
+        } catch (UnsupportedOperationException ex) {
+            // expected
+        }
+        httpentity.setStreaming(false);
+        httpentity.consumeContent();
+    }
+    
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAbstractHttpEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAllEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAllEntity.java?rev=385289&r1=385288&r2=385289&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAllEntity.java (original)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestAllEntity.java Sun Mar 12 06:04:36 2006
@@ -38,7 +38,11 @@
 
     public static Test suite() {
         TestSuite suite = new TestSuite();
-        suite.addTest(TestHttpEntities.suite());
+        suite.addTest(TestAbstractHttpEntity.suite());
+        suite.addTest(TestStringEntity.suite());
+        suite.addTest(TestByteArrayEntity.suite());
+        suite.addTest(TestInputStreamEntity.suite());
+        suite.addTest(TestBasicHttpEntity.suite());
         return suite;
     }
 

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,154 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import org.apache.http.protocol.HTTP;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link BasicHttpEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestBasicHttpEntity extends TestCase {
+
+    public TestBasicHttpEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestBasicHttpEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestBasicHttpEntity.class);
+    }
+
+    public void testBasics() throws Exception {
+    	
+    	byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+    	InputStream content = new ByteArrayInputStream(bytes);
+    	BasicHttpEntity httpentity = new BasicHttpEntity();
+    	httpentity.setContent(content);
+    	httpentity.setContentLength(bytes.length);
+    	
+    	assertEquals(bytes.length, httpentity.getContentLength());
+    	assertFalse(httpentity.isRepeatable());
+        assertTrue(httpentity.isStreaming());
+    }
+    
+    public void testContent() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        InputStream content = new ByteArrayInputStream(bytes);
+        BasicHttpEntity httpentity = new BasicHttpEntity();
+        try {
+            httpentity.getContent();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException ex) {
+            // expected
+        }
+
+        httpentity.setContent(null);
+        try {
+            httpentity.getContent();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException ex) {
+            // expected
+        }
+
+        httpentity.setContent(content);
+        httpentity.getContent();
+        try {
+            httpentity.getContent();
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException ex) {
+            // expected
+        }
+
+    }
+    
+    public void testWriteTo() throws Exception {
+    	byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+    	InputStream content = new ByteArrayInputStream(bytes);
+    	BasicHttpEntity httpentity = new BasicHttpEntity();
+    	httpentity.setContent(content);
+    	
+    	ByteArrayOutputStream out = new ByteArrayOutputStream();
+    	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();
+        try {
+            httpentity.writeTo(out);
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException ex) {
+            // expected
+        }
+
+    	httpentity.setContent(null);
+        out = new ByteArrayOutputStream();
+        try {
+            httpentity.writeTo(out);
+            fail("IllegalStateException should have been thrown");
+        } catch (IllegalStateException ex) {
+            // expected
+        }
+
+    	try {
+    		httpentity.writeTo(null);
+    		fail("IllegalArgumentException should have been thrown");
+    	} catch (IllegalArgumentException ex) {
+    		// expected
+    	}
+    }
+
+    public void testConsumeContent() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        InputStream content = new ByteArrayInputStream(bytes);
+        BasicHttpEntity httpentity = new BasicHttpEntity();
+        httpentity.setContent(null);
+        httpentity.consumeContent();
+        httpentity.setContent(content);
+        httpentity.consumeContent();
+    }
+    
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBasicHttpEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,109 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import java.io.ByteArrayOutputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.protocol.HTTP;
+
+/**
+ * Unit tests for {@link ByteArrayEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestByteArrayEntity extends TestCase {
+
+    public TestByteArrayEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestByteArrayEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestByteArrayEntity.class);
+    }
+
+    public void testBasics() throws Exception {
+    	byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+    	ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
+    	
+    	assertEquals(bytes.length, httpentity.getContentLength());
+    	assertNotNull(httpentity.getContent());
+    	assertTrue(httpentity.isRepeatable());
+        assertFalse(httpentity.isStreaming());        
+    }
+        
+    public void testIllegalConstructor() throws Exception {
+    	try {
+    		new ByteArrayEntity(null);
+    		fail("IllegalArgumentException should have been thrown");
+    	} catch (IllegalArgumentException ex) {
+    		// expected
+    	}
+    }
+
+    public void testWriteTo() throws Exception {
+    	byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+    	ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
+    	
+    	ByteArrayOutputStream out = new ByteArrayOutputStream();
+    	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();
+    	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
+    	}
+    }
+            
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestByteArrayEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,133 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+
+import org.apache.http.protocol.HTTP;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+/**
+ * Unit tests for {@link InputStreamEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestInputStreamEntity extends TestCase {
+
+    public TestInputStreamEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestInputStreamEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestInputStreamEntity.class);
+    }
+
+    public void testBasics() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
+        InputStream instream = new ByteArrayInputStream(bytes);
+        InputStreamEntity httpentity = new InputStreamEntity(instream, bytes.length);
+        
+        assertEquals(bytes.length, httpentity.getContentLength());
+        assertEquals(instream, httpentity.getContent());
+        assertNotNull(httpentity.getContent());
+        assertFalse(httpentity.isRepeatable());
+        assertTrue(httpentity.isStreaming());
+    }
+
+    public void testIllegalConstructor() throws Exception {
+        try {
+            new InputStreamEntity(null, 0);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testWriteTo() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
+        InputStream instream = new ByteArrayInputStream(bytes);
+        InputStreamEntity httpentity = new InputStreamEntity(instream, 7);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        httpentity.writeTo(out);
+        byte[] bytes2 = out.toByteArray();
+        assertNotNull(bytes2);
+        assertEquals(7, bytes2.length);
+        String s = new String(bytes2, HTTP.ISO_8859_1);
+        assertEquals("Message", s);
+
+        instream = new ByteArrayInputStream(bytes);
+        httpentity = new InputStreamEntity(instream, 20);
+        out = new ByteArrayOutputStream();
+        httpentity.writeTo(out);
+        bytes2 = out.toByteArray();
+        assertNotNull(bytes2);
+        assertEquals(bytes.length, bytes2.length);
+        assertFalse(httpentity.isStreaming());
+
+        instream = new ByteArrayInputStream(bytes);
+        httpentity = new InputStreamEntity(instream, -1);
+        out = new ByteArrayOutputStream();
+        httpentity.writeTo(out);
+        bytes2 = out.toByteArray();
+        assertNotNull(bytes2);
+        assertEquals(bytes.length, bytes2.length);
+        assertFalse(httpentity.isStreaming());
+        
+        try {
+            httpentity.writeTo(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
+    public void testConsume() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.ISO_8859_1);
+        InputStream instream = new ByteArrayInputStream(bytes);
+        InputStreamEntity httpentity = new InputStreamEntity(instream, -1);
+        assertTrue(httpentity.isStreaming());
+        httpentity.consumeContent();
+        assertFalse(httpentity.isStreaming());
+        httpentity.consumeContent();
+        assertFalse(httpentity.isStreaming());
+    }
+        
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestInputStreamEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,121 @@
+/*
+ * $HeadURL: $
+ * $Revision: $
+ * $Date: $
+ * 
+ * ====================================================================
+ *
+ *  Copyright 1999-2006 The Apache Software Foundation
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.entity;
+
+import java.io.ByteArrayOutputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.protocol.HTTP;
+
+/**
+ * Unit tests for {@link StringEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestStringEntity extends TestCase {
+
+    public TestStringEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestStringEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestStringEntity.class);
+    }
+
+    public void testBasics() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
+        
+        byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
+        assertEquals(bytes.length, httpentity.getContentLength());
+        assertNotNull(httpentity.getContent());
+        assertTrue(httpentity.isRepeatable());
+        assertFalse(httpentity.isStreaming());        
+    }
+
+    public void testIllegalConstructor() throws Exception {
+        try {
+            new StringEntity(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testDefaultContent() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s, HTTP.US_ASCII);
+        assertEquals("text/plain; charset=US-ASCII", 
+                httpentity.getContentType().getValue());
+        httpentity = new StringEntity(s);
+        assertEquals("text/plain; charset=ISO-8859-1", 
+                httpentity.getContentType().getValue());
+    }
+
+    public void testWriteTo() throws Exception {
+        String s = "Message content";
+        byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
+        StringEntity httpentity = new StringEntity(s);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        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();
+        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
+        }
+    }
+        
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestStringEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java?rev=385289&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java (added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java Sun Mar 12 06:04:36 2006
@@ -0,0 +1,41 @@
+package org.apache.http.mockup;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.http.entity.AbstractHttpEntity;
+
+/**
+ * {@link AbstractHttpEntity} mockup implementation.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class HttpEntityMockup extends AbstractHttpEntity {
+
+    private boolean stream;
+    
+    public InputStream getContent() throws IOException, IllegalStateException {
+        return null;
+    }
+
+    public long getContentLength() {
+        return 0;
+    }
+
+    public boolean isRepeatable() {
+        return false;
+    }
+
+    public void setStreaming(final boolean b) {
+        this.stream = b;
+    }
+
+    public boolean isStreaming() {
+        return this.stream;
+    }
+
+    public void writeTo(OutputStream outstream) throws IOException {
+    }    
+    
+}

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/mockup/HttpEntityMockup.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain