[OLINGO-659] Add CSB tests
This commit is contained in:
parent
d414dfb037
commit
2a718c172b
|
@ -47,8 +47,8 @@ public class CircleStreamBuffer {
|
||||||
private Queue<ByteBuffer> bufferQueue = new LinkedBlockingQueue<ByteBuffer>();
|
private Queue<ByteBuffer> bufferQueue = new LinkedBlockingQueue<ByteBuffer>();
|
||||||
private ByteBuffer currentWriteBuffer;
|
private ByteBuffer currentWriteBuffer;
|
||||||
|
|
||||||
private InternalInputStream inStream;
|
private final InternalInputStream inStream;
|
||||||
private InternalOutputStream outStream;
|
private final InternalOutputStream outStream;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link CircleStreamBuffer} with default buffer size.
|
* Creates a {@link CircleStreamBuffer} with default buffer size.
|
||||||
|
@ -58,12 +58,12 @@ public class CircleStreamBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a {@link CircleStreamBuffer} with given buffer size in bytes.
|
* Create a {@link CircleStreamBuffer} with given initial buffer size.
|
||||||
*
|
*
|
||||||
* @param bufferSize
|
* @param initialCapacity initial capacity of internal buffer
|
||||||
*/
|
*/
|
||||||
public CircleStreamBuffer(final int bufferSize) {
|
public CircleStreamBuffer(final int initialCapacity) {
|
||||||
currentAllocateCapacity = bufferSize;
|
currentAllocateCapacity = initialCapacity;
|
||||||
createNewWriteBuffer();
|
createNewWriteBuffer();
|
||||||
inStream = new InternalInputStream(this);
|
inStream = new InternalInputStream(this);
|
||||||
outStream = new InternalOutputStream(this);
|
outStream = new InternalOutputStream(this);
|
||||||
|
@ -244,21 +244,27 @@ public class CircleStreamBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Allocate a new buffer with requested capacity
|
||||||
*
|
*
|
||||||
* @param requestedCapacity
|
* @param requestedCapacity minimal capacity of new buffer
|
||||||
* @return the buffer
|
* @return the buffer
|
||||||
*/
|
*/
|
||||||
private ByteBuffer allocateBuffer(final int requestedCapacity) {
|
private ByteBuffer allocateBuffer(final int requestedCapacity) {
|
||||||
int allocateCapacity = requestedCapacity;
|
if (requestedCapacity > MAX_CAPACITY) {
|
||||||
if (allocateCapacity < currentAllocateCapacity) {
|
currentAllocateCapacity = MAX_CAPACITY;
|
||||||
allocateCapacity = currentAllocateCapacity * NEW_BUFFER_RESIZE_FACTOR;
|
return ByteBuffer.allocate(requestedCapacity);
|
||||||
}
|
}
|
||||||
if (allocateCapacity > MAX_CAPACITY) {
|
|
||||||
allocateCapacity = MAX_CAPACITY;
|
if (requestedCapacity <= currentAllocateCapacity) {
|
||||||
|
currentAllocateCapacity *= NEW_BUFFER_RESIZE_FACTOR;
|
||||||
|
if (currentAllocateCapacity > MAX_CAPACITY) {
|
||||||
|
currentAllocateCapacity = MAX_CAPACITY;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentAllocateCapacity = requestedCapacity;
|
||||||
}
|
}
|
||||||
// update current
|
|
||||||
currentAllocateCapacity = allocateCapacity;
|
return ByteBuffer.allocate(currentAllocateCapacity);
|
||||||
return ByteBuffer.allocate(allocateCapacity);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// #############################################
|
// #############################################
|
||||||
|
@ -289,8 +295,8 @@ public class CircleStreamBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int read(final byte[] b, final int off, final int len) throws IOException {
|
public int read(final byte[] buffer, final int off, final int len) throws IOException {
|
||||||
return inBuffer.read(b, off, len);
|
return inBuffer.read(buffer, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -315,8 +321,8 @@ public class CircleStreamBuffer {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(final byte[] b, final int off, final int len) throws IOException {
|
public void write(final byte[] buffer, final int off, final int len) throws IOException {
|
||||||
outBuffer.write(b, off, len);
|
outBuffer.write(buffer, off, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -0,0 +1,310 @@
|
||||||
|
/*******************************************************************************
|
||||||
|
* 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.
|
||||||
|
******************************************************************************/
|
||||||
|
package org.apache.olingo.server.core.serializer.utils;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class CircleStreamBufferTest {
|
||||||
|
|
||||||
|
private static final Charset DEFAULT_CHARSET = Charset.forName("utf-8");
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadSignBySign() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream write = csb.getOutputStream();
|
||||||
|
byte[] writeData = "Test".getBytes(DEFAULT_CHARSET);
|
||||||
|
for (byte element : writeData) {
|
||||||
|
write.write(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
byte[] buffer = new byte[4];
|
||||||
|
for (int i = 0; i < buffer.length; i++) {
|
||||||
|
buffer[i] = (byte) inStream.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = new String(buffer, DEFAULT_CHARSET);
|
||||||
|
|
||||||
|
assertEquals("Test", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadSignBySignMoreThenBufferSize() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(128);
|
||||||
|
|
||||||
|
OutputStream write = csb.getOutputStream();
|
||||||
|
int signs = 1024;
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
byte[] writeData = testData.getBytes(DEFAULT_CHARSET);
|
||||||
|
for (byte element : writeData) {
|
||||||
|
write.write(element);
|
||||||
|
}
|
||||||
|
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
byte[] buffer = new byte[signs];
|
||||||
|
for (int i = 0; i < buffer.length; i++) {
|
||||||
|
buffer[i] = (byte) inStream.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = new String(buffer, DEFAULT_CHARSET);
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadOnce() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream write = csb.getOutputStream();
|
||||||
|
write.write("Test".getBytes(DEFAULT_CHARSET), 0, 4);
|
||||||
|
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
|
||||||
|
String result = readFrom(inStream);
|
||||||
|
assertEquals(4, result.length());
|
||||||
|
assertEquals("Test", result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadTwice() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
|
||||||
|
// first writeInternal/read cycle
|
||||||
|
outStream.write("Test_1".getBytes(DEFAULT_CHARSET));
|
||||||
|
String firstResult = readFrom(inStream);
|
||||||
|
assertEquals("Test_1", firstResult);
|
||||||
|
|
||||||
|
// second writeInternal/read cycle
|
||||||
|
outStream.write("Test_2".getBytes(DEFAULT_CHARSET));
|
||||||
|
String secondResult = readFrom(inStream);
|
||||||
|
assertEquals("Test_2", secondResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadOnce8k() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = 8192;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteExactOneCharacterMoreThenBufferSize() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = bufferSize + 1;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream, bufferSize * 2);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteReadOneCharacterMoreThenBufferSize() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
|
||||||
|
int signs = (1 + bufferSize) * 3;
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteMoreThenDefaultBufferSize() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = 70110;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteMoreThenBufferSize() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = bufferSize * 10;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream, bufferSize * 2);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteMoreThenBufferSizeAndUmlauts() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = bufferSize * 10;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
testData = "äüöÄÜÖ" + testData + "äüöÄÜÖ";
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream);
|
||||||
|
|
||||||
|
assertEquals(testData.length(), result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteMoreThenBufferSizeAndUmlautsIso() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = bufferSize * 10;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
testData = "äüöÄÜÖ" + testData + "äüöÄÜÖ";
|
||||||
|
outStream.write(testData.getBytes("iso-8859-1"));
|
||||||
|
String result = readFrom(inStream, "iso-8859-1");
|
||||||
|
|
||||||
|
assertEquals(testData.length(), result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleWriteALotMoreThenBufferSize() throws Exception {
|
||||||
|
int bufferSize = 4096;
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer(bufferSize);
|
||||||
|
|
||||||
|
OutputStream outStream = csb.getOutputStream();
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
final int signs = bufferSize * 100;
|
||||||
|
|
||||||
|
String testData = createTestString(signs);
|
||||||
|
outStream.write(testData.getBytes(DEFAULT_CHARSET));
|
||||||
|
String result = readFrom(inStream, bufferSize * 2);
|
||||||
|
|
||||||
|
assertEquals(signs, result.length());
|
||||||
|
assertEquals(testData, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IOException.class)
|
||||||
|
public void testCloseInputStream() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream write = csb.getOutputStream();
|
||||||
|
write.write("Test".getBytes(DEFAULT_CHARSET), 0, 4);
|
||||||
|
|
||||||
|
InputStream inStream = csb.getInputStream();
|
||||||
|
inStream.close();
|
||||||
|
byte[] buffer = new byte[4];
|
||||||
|
int count = inStream.read(buffer);
|
||||||
|
assertEquals(4, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IOException.class)
|
||||||
|
public void testCloseOutputStream() throws Exception {
|
||||||
|
CircleStreamBuffer csb = new CircleStreamBuffer();
|
||||||
|
|
||||||
|
OutputStream write = csb.getOutputStream();
|
||||||
|
write.close();
|
||||||
|
write.write("Test".getBytes(DEFAULT_CHARSET), 0, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ###################################################
|
||||||
|
// #
|
||||||
|
// # Below here are test helper methods
|
||||||
|
// #
|
||||||
|
// ###################################################
|
||||||
|
|
||||||
|
private String readFrom(final InputStream stream) throws IOException {
|
||||||
|
return readFrom(stream, DEFAULT_CHARSET, 128);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFrom(final InputStream stream, final String charset) throws IOException {
|
||||||
|
return readFrom(stream, Charset.forName(charset), 128);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFrom(final InputStream stream, final int bufferSize) throws IOException {
|
||||||
|
return readFrom(stream, DEFAULT_CHARSET, bufferSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String readFrom(final InputStream stream, final Charset charset, final int bufferSize) throws IOException {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
int count;
|
||||||
|
byte[] buffer = new byte[bufferSize];
|
||||||
|
while ((count = stream.read(buffer)) >= 0) {
|
||||||
|
b.append(new String(buffer, 0, count, charset));
|
||||||
|
}
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private String createTestString(final int signs) {
|
||||||
|
StringBuilder b = new StringBuilder();
|
||||||
|
|
||||||
|
for (int i = 0; i < signs; i++) {
|
||||||
|
int sign = (int) (32 + (Math.random() * 90));
|
||||||
|
b.append((char) sign);
|
||||||
|
}
|
||||||
|
|
||||||
|
return b.toString();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue