hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r385308 - in /jakarta/httpcomponents/trunk/http-core/src: java/org/apache/http/entity/ test/org/apache/http/entity/
Date Sun, 12 Mar 2006 16:25:59 GMT
Author: olegk
Date: Sun Mar 12 08:25:59 2006
New Revision: 385308

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

Added:
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBufferedHttpEntity.java
  (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestFileEntity.java
  (with props)
    jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntityWrapper.java
  (with props)
Modified:
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BufferedHttpEntity.java
    jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.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/BufferedHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BufferedHttpEntity.java?rev=385308&r1=385307&r2=385308&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BufferedHttpEntity.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/BufferedHttpEntity.java
Sun Mar 12 08:25:59 2006
@@ -56,7 +56,7 @@
       
     public BufferedHttpEntity(final HttpEntity entity) throws IOException {
         super(entity);
-        if (entity.isChunked() || !entity.isRepeatable() ) {
+        if (!entity.isRepeatable() || entity.getContentLength() < 0) {
             this.buffer = EntityUtils.toByteArray(entity);
         } else {
             this.buffer = null;
@@ -85,7 +85,7 @@
      * @return  <code>false</code>
      */
     public boolean isChunked() {
-        return false;
+        return (buffer == null) && wrappedEntity.isChunked();
     }
     
     /**

Modified: jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java?rev=385308&r1=385307&r2=385308&view=diff
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
(original)
+++ jakarta/httpcomponents/trunk/http-core/src/java/org/apache/http/entity/FileEntity.java
Sun Mar 12 08:25:59 2006
@@ -49,7 +49,8 @@
     private final File file; 
 
     public FileEntity(final File file, final String contentType) {
-        super();        if (file == null) {
+        super();
+        if (file == null) {
             throw new IllegalArgumentException("File may not be null");
         }
         this.file = file;

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=385308&r1=385307&r2=385308&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 08:25:59 2006
@@ -42,7 +42,10 @@
         suite.addTest(TestStringEntity.suite());
         suite.addTest(TestByteArrayEntity.suite());
         suite.addTest(TestInputStreamEntity.suite());
+        suite.addTest(TestFileEntity.suite());
         suite.addTest(TestBasicHttpEntity.suite());
+        suite.addTest(TestHttpEntityWrapper.suite());
+        suite.addTest(TestBufferedHttpEntity.suite());
         return suite;
     }
 

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBufferedHttpEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBufferedHttpEntity.java?rev=385308&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBufferedHttpEntity.java
(added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestBufferedHttpEntity.java
Sun Mar 12 08:25:59 2006
@@ -0,0 +1,161 @@
+/*
+ * $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 junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.protocol.HTTP;
+
+/**
+ * Unit tests for {@link WrappedEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestBufferedHttpEntity extends TestCase {
+
+    public TestBufferedHttpEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestBufferedHttpEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestBufferedHttpEntity.class);
+    }
+
+    public void testBufferingEntity() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes),
-1);
+        BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
+        assertEquals(bytes.length, bufentity.getContentLength());
+        assertTrue(bufentity.isRepeatable());
+        assertFalse(bufentity.isChunked());
+        assertFalse(bufentity.isStreaming());
+
+        // test if we can obtain contain multiple times
+        assertNotNull(bufentity.getContent ());
+        assertNotNull(bufentity.getContent ());
+    }
+
+    public void testWrappingEntity() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
+        httpentity.setChunked(true);
+        BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
+        assertEquals(bytes.length, bufentity.getContentLength());
+        assertTrue(bufentity.isRepeatable());
+        assertTrue(bufentity.isChunked());
+        assertFalse(bufentity.isStreaming());
+
+        // test if we can obtain contain multiple times
+        assertNotNull(bufentity.getContent ());
+        assertNotNull(bufentity.getContent ());
+    }
+
+    public void testIllegalConstructor() throws Exception {
+        try {
+            new BufferedHttpEntity(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testWriteToBuffered() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes),
-1);
+        BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bufentity.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();
+        bufentity.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 {
+            bufentity.writeTo(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+    
+    public void testWriteToWrapped() throws Exception {
+        byte[] bytes = "Message content".getBytes(HTTP.US_ASCII);
+        ByteArrayEntity httpentity = new ByteArrayEntity(bytes);
+        BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        bufentity.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();
+        bufentity.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 {
+            bufentity.writeTo(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+}

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

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

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

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestFileEntity.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestFileEntity.java?rev=385308&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestFileEntity.java
(added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestFileEntity.java
Sun Mar 12 08:25:59 2006
@@ -0,0 +1,114 @@
+/*
+ * $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 java.io.File;
+import java.io.FileOutputStream;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.http.protocol.HTTP;
+
+/**
+ * Unit tests for {@link FileEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestFileEntity extends TestCase {
+
+    public TestFileEntity(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestFileEntity.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestFileEntity.class);
+    }
+
+    public void testBasics() throws Exception {
+        File tmpfile = File.createTempFile("testfile", ".txt");
+        tmpfile.deleteOnExit();
+        FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1);
+        
+        assertEquals(tmpfile.length(), httpentity.getContentLength());
+        assertNotNull(httpentity.getContent());
+        assertTrue(httpentity.isRepeatable());
+        assertFalse(httpentity.isStreaming());        
+        tmpfile.delete();
+    }
+
+    public void testIllegalConstructor() throws Exception {
+        try {
+            new FileEntity(null, null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testWriteTo() throws Exception {
+        File tmpfile = File.createTempFile("testfile", ".txt");
+        tmpfile.deleteOnExit();
+        
+        FileOutputStream outstream = new FileOutputStream(tmpfile);
+        outstream.write(0);
+        outstream.write(1);
+        outstream.write(2);
+        outstream.write(3);
+        outstream.close();
+        
+        FileEntity httpentity = new FileEntity(tmpfile, HTTP.ISO_8859_1);
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        httpentity.writeTo(out);
+        byte[] bytes = out.toByteArray();
+        assertNotNull(bytes);
+        assertEquals(tmpfile.length(), bytes.length);
+        for (int i = 0; i < 4; i++) {
+            assertEquals(i, bytes[i]);
+        }
+        tmpfile.delete();
+
+        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/TestFileEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

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

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

Added: jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntityWrapper.java
URL: http://svn.apache.org/viewcvs/jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntityWrapper.java?rev=385308&view=auto
==============================================================================
--- jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntityWrapper.java
(added)
+++ jakarta/httpcomponents/trunk/http-core/src/test/org/apache/http/entity/TestHttpEntityWrapper.java
Sun Mar 12 08:25:59 2006
@@ -0,0 +1,125 @@
+/*
+ * $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 WrappedEntity}.
+ *
+ * @author <a href="mailto:oleg at ural.ru">Oleg Kalnichevski</a>
+ */
+public class TestHttpEntityWrapper extends TestCase {
+
+    public TestHttpEntityWrapper(String testName) {
+        super(testName);
+    }
+
+    public static void main(String args[]) {
+        String[] testCaseName = { TestHttpEntityWrapper.class.getName() };
+        junit.textui.TestRunner.main(testCaseName);
+    }
+
+    public static Test suite() {
+        return new TestSuite(TestHttpEntityWrapper.class);
+    }
+
+    public void testBasics() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
+        httpentity.setContentType(HTTP.PLAIN_TEXT_TYPE);
+        httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
+        HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity); 
+        
+        assertEquals(httpentity.getContentLength(), wrapped.getContentLength());
+        assertEquals(httpentity.getContentType(), wrapped.getContentType());
+        assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding());
+        assertEquals(httpentity.isChunked(), wrapped.isChunked());
+        assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
+        assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
+        assertNotNull(wrapped.getContent());
+    }
+
+    public void testIllegalConstructor() throws Exception {
+        try {
+            new HttpEntityWrapper(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testWriteTo() throws Exception {
+        String s = "Message content";
+        byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
+        StringEntity httpentity = new StringEntity(s);
+        HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity); 
+        
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        wrapped.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();
+        wrapped.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 {
+            wrapped.writeTo(null);
+            fail("IllegalArgumentException should have been thrown");
+        } catch (IllegalArgumentException ex) {
+            // expected
+        }
+    }
+
+    public void testConsumeContent() throws Exception {
+        String s = "Message content";
+        StringEntity httpentity = new StringEntity(s);
+        HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);
+        wrapped.consumeContent();
+        wrapped.consumeContent();
+    }
+    
+}

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

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

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



Mime
View raw message