hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1161023 - in /httpcomponents/httpcore/trunk: ./ httpcore/src/main/java/org/apache/http/impl/io/ httpcore/src/test/java/org/apache/http/impl/ httpcore/src/test/java/org/apache/http/impl/io/
Date Wed, 24 Aug 2011 09:04:55 GMT
Author: olegk
Date: Wed Aug 24 09:04:55 2011
New Revision: 1161023

URL: http://svn.apache.org/viewvc?rev=1161023&view=rev
Log:
HTTPCORE-270: Fixed IllegalStateException in AbstractSessionOutputBuffer and AbstractSessionInputBuffer
Contributed by William R. Speirs <bill.speirs at gmail.com>

Added:
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java
    httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java
    httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestSessionBuffers.java

Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1161023&r1=1161022&r2=1161023&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Wed Aug 24 09:04:55 2011
@@ -1,5 +1,9 @@
 Changes since 4.2-ALPHA1  
 
+* [HTTPCORE-270] Fixed IllegalStateException in AbstractSessionOutputBuffer and 
+  AbstractSessionInputBuffer.
+  Contributed by William R. Speirs <bill.speirs at gmail.com>
+
 * [HTTPCORE-269] Connection pools incorrectly handle lease requests when the max limit for
the given 
   route has been exceeded and all connections in the route pool are stateful.
   Contributed by Oleg Kalnichevski <olegk at apache.org>

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java?rev=1161023&r1=1161022&r2=1161023&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionInputBuffer.java
Wed Aug 24 09:04:55 2011
@@ -347,6 +347,9 @@ public abstract class AbstractSessionInp
 
     private int appendDecoded(
             final CharArrayBuffer charbuffer, final ByteBuffer bbuf) throws IOException {
+        if (!bbuf.hasRemaining()) {
+            return 0;
+        }
         if (this.decoder == null) {
             this.decoder = this.charset.newDecoder();
         }

Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java?rev=1161023&r1=1161022&r2=1161023&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/impl/io/AbstractSessionOutputBuffer.java
Wed Aug 24 09:04:55 2011
@@ -250,6 +250,9 @@ public abstract class AbstractSessionOut
     }
 
     private void writeEncoded(final CharBuffer cbuf) throws IOException {
+        if (!cbuf.hasRemaining()) {
+            return;
+        }
         if (this.encoder == null) {
             this.encoder = this.charset.newEncoder();
         }

Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestSessionBuffers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestSessionBuffers.java?rev=1161023&r1=1161022&r2=1161023&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestSessionBuffers.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/TestSessionBuffers.java
Wed Aug 24 09:04:55 2011
@@ -504,9 +504,11 @@ public class TestSessionBuffers {
         for (int i = 0; i < 5; i++) {
             outbuffer.writeLine(s1);
         }
+        chbuffer.clear();
+        outbuffer.writeLine(chbuffer);
         outbuffer.flush();
         long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
-        long expected = ((s1.toString().getBytes(HTTP.ISO_8859_1).length + 2)) * 10;
+        long expected = ((s1.toString().getBytes(HTTP.ISO_8859_1).length + 2)) * 10 + 2;
         Assert.assertEquals(expected, bytesWritten);
 
         SessionInputBufferMock inbuffer = new SessionInputBufferMock(
@@ -514,12 +516,15 @@ public class TestSessionBuffers {
                 params);
         HttpProtocolParams.setHttpElementCharset(params, HTTP.ISO_8859_1);
 
+        CharArrayBuffer buf = new CharArrayBuffer(64);
         for (int i = 0; i < 10; i++) {
-            CharArrayBuffer buf = new CharArrayBuffer(64);
+            buf.clear();
             int len = inbuffer.readLine(buf);
             Assert.assertEquals(len, SWISS_GERMAN_HELLO.length);
             Assert.assertEquals(s1, buf.toString());
         }
+        buf.clear();
+        Assert.assertEquals("", inbuffer.readLine());
         Assert.assertNull(inbuffer.readLine());
         Assert.assertNull(inbuffer.readLine());
         long bytesRead = inbuffer.getMetrics().getBytesTransferred();

Added: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java?rev=1161023&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
Wed Aug 24 09:04:55 2011
@@ -0,0 +1,197 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.impl.io;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.io.OutputStream;
+import java.net.Socket;
+
+import org.apache.http.params.CoreConnectionPNames;
+import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpParams;
+import org.apache.http.util.CharArrayBuffer;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+public class TestSocketOutputBuffer {
+    
+    private SocketOutputBuffer sob;
+    
+    @Mock private Socket socket;
+    @Mock private OutputStream os;
+    @Mock private HttpParams params;
+    private byte[] b;
+    private CharArrayBuffer cb;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        
+        when(socket.getOutputStream()).thenReturn(os);
+    }
+
+    private void create(int buffSize, int arraySize, int minChunkLimit) throws Exception
{
+        b = new byte[arraySize];
+        cb = new CharArrayBuffer(arraySize);
+        
+        when(params.getIntParameter(CoreConnectionPNames.MIN_CHUNK_LIMIT, 512)).thenReturn(minChunkLimit);
+        
+        sob = new SocketOutputBuffer(socket, buffSize, params);
+    }
+    
+    @Test
+    public void testWriteByteArrayOffLenDirectToStream1() throws Exception {
+        create(2048, 2048, 1024);
+
+        sob.write(b, 0, b.length);
+        verify(os, times(1)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteByteArrayOffLenDirectToStream2() throws Exception {
+        create(1024, 2048, 2048);
+
+        sob.write(b, 0, b.length);
+        verify(os, times(1)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteByteArrayOffLenToBuffer() throws Exception {
+        create(2048, 2048, 2048);
+
+        sob.write(b, 0, b.length);
+        verify(os, times(0)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteByteArrayDirectToStream1() throws Exception {
+        create(2048, 2048, 1024);
+
+        sob.write(b);
+        verify(os, times(1)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteByteArrayDirectToStream2() throws Exception {
+        create(1024, 2048, 2048);
+
+        sob.write(b);
+        verify(os, times(1)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteByteArrayToBuffer() throws Exception {
+        create(2048, 2048, 2048);
+
+        sob.write(b);
+        verify(os, times(0)).write(any(byte[].class), eq(0), eq(b.length));
+    }
+
+    @Test
+    public void testWriteLineString() throws Exception {
+        create(2048, 2048, 2048);
+        
+        sob.writeLine("test");
+    }
+
+    @Test
+    public void testWriteLineStringEncode() throws Exception {
+        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
+        create(2048, 2048, 2048);
+        
+        sob.writeLine("test");
+    }
+
+    @Test
+    public void testWriteLineEmptyString() throws Exception {
+        create(2048, 2048, 2048);
+        
+        sob.writeLine("");
+    }
+
+    @Test
+    public void testWriteLineEmptyStringEncode() throws Exception {
+        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
+        create(2048, 2048, 2048);
+        
+        sob.writeLine("");
+    }
+
+    @Test
+    public void testWriteLineNullString() throws Exception {
+        create(2048, 2048, 2048);
+        
+        sob.writeLine((String)null);
+    }
+
+    @Test
+    public void testWriteLineNullStringEncode() throws Exception {
+        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
+        create(2048, 2048, 2048);
+        
+        sob.writeLine((String)null);
+    }
+
+    @Test
+    public void testWriteLineCharArrayBuffer() throws Exception {
+        create(2048, 2048, 2048);
+        
+        sob.writeLine(cb);
+    }
+
+    @Test
+    public void testWriteLineCharArrayBufferEncode() throws Exception {
+        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
+        create(2048, 2048, 2048);
+        
+        sob.writeLine(cb);
+    }
+
+    @Test
+    public void testWriteLineEmptyCharArrayBuffer() throws Exception {
+        create(2048, 0, 2048);
+        
+        sob.writeLine(cb);
+    }
+
+    @Test
+    public void testWriteLineEmptyCharArrayBufferEncode() throws Exception {
+        when(params.getParameter(CoreProtocolPNames.HTTP_ELEMENT_CHARSET)).thenReturn("UTF-8");
+        create(2048, 0, 2048);
+        
+        sob.writeLine(cb);
+    }
+
+}

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/impl/io/TestSocketOutputBuffer.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message