hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1742947 - in /httpcomponents/httpcore/trunk: httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/
Date Mon, 09 May 2016 13:31:13 GMT
Author: olegk
Date: Mon May  9 13:31:13 2016
New Revision: 1742947

URL: http://svn.apache.org/viewvc?rev=1742947&view=rev
Log:
Improved mock writable / readable byte channels

Added:
    httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/
    httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ReadableByteChannelMock.java
      - copied, changed from r1741400, httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
    httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
  (with props)
Modified:
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
    httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/WritableByteChannelMock.java

Copied: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ReadableByteChannelMock.java
(from r1741400, httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java)
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ReadableByteChannelMock.java?p2=httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ReadableByteChannelMock.java&p1=httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java&r1=1741400&r2=1742947&rev=1742947&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/ReadableByteChannelMock.java
Mon May  9 13:31:13 2016
@@ -25,18 +25,16 @@
  *
  */
 
-package org.apache.hc.core5.http.impl.nio;
+package org.apache.hc.core5.http2.impl.nio;
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.ReadableByteChannel;
-import java.nio.charset.Charset;
 
 public class ReadableByteChannelMock implements ReadableByteChannel {
 
-    private final String[] chunks;
-    private final Charset charset;
+    private final byte[][] chunks;
 
     private int chunkCount = 0;
 
@@ -44,18 +42,17 @@ public class ReadableByteChannelMock imp
     private boolean eof = false;
     private boolean closed = false;
 
-    public ReadableByteChannelMock(final String[] chunks, final Charset charset) {
+    public ReadableByteChannelMock(final byte[]... chunks) {
         super();
         this.chunks = chunks;
-        this.charset = charset;
     }
 
     private void prepareChunk() {
         if (this.currentChunk == null || !this.currentChunk.hasRemaining()) {
             if (this.chunkCount < this.chunks.length) {
-                final String s = this.chunks[this.chunkCount];
+                final byte[] bytes = this.chunks[this.chunkCount];
                 this.chunkCount++;
-                this.currentChunk = ByteBuffer.wrap(s.getBytes(this.charset));
+                this.currentChunk = ByteBuffer.wrap(bytes);
             } else {
                 this.eof = true;
             }

Added: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java?rev=1742947&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
(added)
+++ httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
Mon May  9 13:31:13 2016
@@ -0,0 +1,115 @@
+/*
+ * ====================================================================
+ * 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.hc.core5.http2.impl.nio;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.WritableByteChannel;
+
+public class WritableByteChannelMock implements WritableByteChannel {
+
+    private final int capacityLimit;
+
+    private int capacityUsed;
+    private ByteBuffer buf;
+    private boolean closed;
+
+    public WritableByteChannelMock(final int initialSize, final int capacityLimit) {
+        this.buf = ByteBuffer.allocate(initialSize);
+        this.capacityLimit = capacityLimit;
+    }
+
+    public WritableByteChannelMock(final int initialSize) {
+        this(initialSize, 0);
+    }
+
+    private void expandCapacity(final int capacity) {
+        final ByteBuffer oldbuffer = this.buf;
+        this.buf = ByteBuffer.allocate(capacity);
+        oldbuffer.flip();
+        this.buf.put(oldbuffer);
+    }
+
+    private void ensureCapacity(final int requiredCapacity) {
+        if (requiredCapacity > this.buf.capacity()) {
+            expandCapacity(requiredCapacity);
+        }
+    }
+
+    @Override
+    public int write(final ByteBuffer src) throws IOException {
+        if (this.closed) {
+            throw new ClosedChannelException();
+        }
+        final int len = src.remaining();
+        ensureCapacity(this.buf.position() + len);
+        if (this.capacityLimit > 0) {
+            final int chunk = Math.min(this.capacityLimit - this.capacityUsed, len);
+            if (chunk > 0) {
+                final int limit = src.limit();
+                src.limit(src.position() + chunk);
+                this.buf.put(src);
+                src.limit(limit);
+                this.capacityUsed += chunk;
+                return chunk;
+            } else {
+                return 0;
+            }
+        } else {
+            this.buf.put(src);
+            return len;
+        }
+    }
+
+    @Override
+    public boolean isOpen() {
+        return !this.closed;
+    }
+
+    @Override
+    public void close() throws IOException {
+        this.closed = true;
+    }
+
+    public void flush() {
+        this.capacityUsed = 0;
+    }
+
+    public void reset() {
+        this.capacityUsed = 0;
+        this.buf.clear();
+    }
+
+    public ByteBuffer asReadOnly() {
+        final ByteBuffer dup = this.buf.duplicate();
+        dup.reset();
+        return dup.asReadOnlyBuffer();
+    }
+
+}
\ No newline at end of file

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: httpcomponents/httpcore/trunk/httpcore5-h2/src/test/java/org/apache/hc/core5/http2/impl/nio/WritableByteChannelMock.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java?rev=1742947&r1=1742946&r2=1742947&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/ReadableByteChannelMock.java
Mon May  9 13:31:13 2016
@@ -35,8 +35,7 @@ import java.nio.charset.Charset;
 
 public class ReadableByteChannelMock implements ReadableByteChannel {
 
-    private final String[] chunks;
-    private final Charset charset;
+    private final byte[][] chunks;
 
     private int chunkCount = 0;
 
@@ -44,18 +43,25 @@ public class ReadableByteChannelMock imp
     private boolean eof = false;
     private boolean closed = false;
 
-    public ReadableByteChannelMock(final String[] chunks, final Charset charset) {
+    public ReadableByteChannelMock(final String[] strings, final Charset charset) {
+        super();
+        this.chunks = new byte[strings.length][];
+        for (int i = 0; i < strings.length; i++) {
+            this.chunks[i] = strings[i].getBytes(charset);
+        }
+    }
+
+    public ReadableByteChannelMock(final byte[]... chunks) {
         super();
         this.chunks = chunks;
-        this.charset = charset;
     }
 
     private void prepareChunk() {
         if (this.currentChunk == null || !this.currentChunk.hasRemaining()) {
             if (this.chunkCount < this.chunks.length) {
-                final String s = this.chunks[this.chunkCount];
+                final byte[] bytes = this.chunks[this.chunkCount];
                 this.chunkCount++;
-                this.currentChunk = ByteBuffer.wrap(s.getBytes(this.charset));
+                this.currentChunk = ByteBuffer.wrap(bytes);
             } else {
                 this.eof = true;
             }

Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/WritableByteChannelMock.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/WritableByteChannelMock.java?rev=1742947&r1=1742946&r2=1742947&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/WritableByteChannelMock.java
(original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/impl/nio/WritableByteChannelMock.java
Mon May  9 13:31:13 2016
@@ -29,96 +29,40 @@ package org.apache.hc.core5.http.impl.ni
 
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
 import java.nio.channels.ClosedChannelException;
 import java.nio.channels.WritableByteChannel;
+import java.nio.charset.CharacterCodingException;
 import java.nio.charset.Charset;
 
-import org.apache.hc.core5.util.HeapByteBufferAllocator;
-
 public class WritableByteChannelMock implements WritableByteChannel {
 
-    static class InternalBuffer extends ExpandableBuffer {
-
-        private final int capacityLimit;
-        private int curCapacity;
-
-        public InternalBuffer(final int buffersize, final int capacityLimit) {
-            super(buffersize, HeapByteBufferAllocator.INSTANCE);
-            this.capacityLimit = capacityLimit;
-            this.curCapacity = capacityLimit;
-        }
-
-        public int write(final ByteBuffer src) {
-            if (src == null) {
-                return 0;
-            }
-            setInputMode();
-            if (this.capacityLimit > 0) {
-                if (this.curCapacity > 0) {
-                    final int requiredCapacity = buffer().position() + this.curCapacity;
-                    ensureCapacity(requiredCapacity);
-                    int count = 0;
-                    while (src.hasRemaining() && this.curCapacity > 0) {
-                        buffer().put(src.get());
-                        count++;
-                        this.curCapacity--;
-                    }
-                    return count;
-                } else {
-                    return 0;
-                }
-            } else {
-                final int chunk = src.remaining();
-                final int requiredCapacity = buffer().position() + src.remaining();
-                ensureCapacity(requiredCapacity);
-                buffer().put(src);
-                return chunk;
-            }
-        }
-
-        @Override
-        protected void clear() {
-            super.clear();
-        }
-
-        public void resetCapacity() {
-            this.curCapacity = this.capacityLimit;
-        }
+    private final int capacityLimit;
 
-        private static String toString(
-            final byte[] b, final int off, final int len, final Charset charset) {
-            return new String(b, off, len, charset);
-        }
-
-        public String dump(final Charset charset) {
-            setOutputMode();
-            if (buffer().hasArray()) {
-                return toString(buffer().array(), buffer().position(), buffer().limit(),
-                    charset);
-            } else {
-                final ByteBuffer dup = buffer().duplicate();
-                final byte[] b = new byte[dup.remaining()];
-                int i = 0;
-                while (dup.hasRemaining()) {
-                    b[i] = dup.get();
-                    i++;
-                }
-                return toString(b, 0, b.length, charset);
-            }
-        }
+    private int capacityUsed;
+    private ByteBuffer buf;
+    private boolean closed;
 
+    public WritableByteChannelMock(final int initialSize, final int capacityLimit) {
+        this.buf = ByteBuffer.allocate(initialSize);
+        this.capacityLimit = capacityLimit;
     }
 
-    private final InternalBuffer buf;
-    private boolean closed;
+    public WritableByteChannelMock(final int initialSize) {
+        this(initialSize, 0);
+    }
 
-    public WritableByteChannelMock(final int size, final int capacityLimit) {
-        super();
-        this.buf = new InternalBuffer(size, capacityLimit);
+    private void expandCapacity(final int capacity) {
+        final ByteBuffer oldbuffer = this.buf;
+        this.buf = ByteBuffer.allocate(capacity);
+        oldbuffer.flip();
+        this.buf.put(oldbuffer);
     }
 
-    public WritableByteChannelMock(final int size) {
-        this(size, 0);
+    private void ensureCapacity(final int requiredCapacity) {
+        if (requiredCapacity > this.buf.capacity()) {
+            expandCapacity(requiredCapacity);
+        }
     }
 
     @Override
@@ -126,7 +70,24 @@ public class WritableByteChannelMock imp
         if (this.closed) {
             throw new ClosedChannelException();
         }
-        return this.buf.write(src);
+        final int len = src.remaining();
+        ensureCapacity(this.buf.position() + len);
+        if (this.capacityLimit > 0) {
+            final int chunk = Math.min(this.capacityLimit - this.capacityUsed, len);
+            if (chunk > 0) {
+                final int limit = src.limit();
+                src.limit(src.position() + chunk);
+                this.buf.put(src);
+                src.limit(limit);
+                this.capacityUsed += chunk;
+                return chunk;
+            } else {
+                return 0;
+            }
+        } else {
+            this.buf.put(src);
+            return len;
+        }
     }
 
     @Override
@@ -140,16 +101,19 @@ public class WritableByteChannelMock imp
     }
 
     public void flush() {
-        this.buf.resetCapacity();
+        this.capacityUsed = 0;
     }
 
     public void reset() {
-        this.buf.resetCapacity();
+        this.capacityUsed = 0;
         this.buf.clear();
     }
 
-    public String dump(final Charset charset){
-        return this.buf.dump(charset);
+    public String dump(final Charset charset) throws CharacterCodingException {
+        this.buf.flip();
+        final CharBuffer charBuffer = charset.newDecoder().decode(this.buf);
+        this.buf.flip();
+        return charBuffer.toString();
     }
 
 }



Mime
View raw message