You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by mi...@apache.org on 2015/07/10 11:00:40 UTC

[8/9] olingo-odata4 git commit: [OLINGO-729] More tests and clean up

[OLINGO-729] More tests and clean up


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/b484f7f6
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/b484f7f6
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/b484f7f6

Branch: refs/heads/OLINGO-708_AsyncSupportTec
Commit: b484f7f64809fd62a0cfc7cf5cd98eb2f0f1abe6
Parents: 594ad4a
Author: Michael Bolz <mi...@sap.com>
Authored: Fri Jul 10 09:27:56 2015 +0200
Committer: Michael Bolz <mi...@sap.com>
Committed: Fri Jul 10 09:39:25 2015 +0200

----------------------------------------------------------------------
 .../deserializer/batch/BatchLineReader.java     | 100 +++---------------
 .../serializer/BatchResponseSerializer.java     |   3 +-
 .../deserializer/batch/BatchLineReaderTest.java | 103 ++-----------------
 .../serializer/BatchResponseSerializerTest.java |  60 ++---------
 4 files changed, 31 insertions(+), 235 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b484f7f6/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java
index 150d318..bd71d93 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReader.java
@@ -28,29 +28,19 @@ import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
 
-/**
- * Read batch content and split it into lines.
- * This class is not thread safe.
- */
 public class BatchLineReader {
-  private static final byte CR_BYTE = '\r';
-  private static final byte LF_BYTE = '\n';
+  private static final byte CR = '\r';
+  private static final byte LF = '\n';
   private static final int EOF = -1;
   private static final int BUFFER_SIZE = 8192;
   private static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
   private static final Charset CS_ISO_8859_1 = Charset.forName("iso-8859-1");
-  private static final String BOUNDARY = "boundary";
-  private static final String DOUBLE_DASH = "--";
-  private static final String CR = "\r";
-  private static final String LF = "\n";
-  private static final String CRLF = "\r\n";
-  // length of the Content-Type Header field including the ':'
-  // "Content-Type:" => 13
-  private static final int CONTENT_TYPE_LENGTH = 13;
-
-  private final ReadState readState = new ReadState();
+  public static final String BOUNDARY = "boundary";
+  public static final String DOUBLE_DASH = "--";
+  public static final String CRLF = "\r\n";
   private Charset currentCharset = DEFAULT_CHARSET;
   private String currentBoundary = null;
+  private ReadState readState = new ReadState();
   private InputStream reader;
   private byte[] buffer;
   private int offset = 0;
@@ -103,57 +93,11 @@ public class BatchLineReader {
     return result;
   }
 
-  int read(final byte[] byteBuffer, final int bufferOffset, final int length) throws IOException {
-    if ((bufferOffset + length) > byteBuffer.length) {
-      throw new IndexOutOfBoundsException("Buffer is too small");
-    }
-
-    if (length < 0 || bufferOffset < 0) {
-      throw new IndexOutOfBoundsException("Offset and length must be greater than zero");
-    }
-
-    // Check if buffer is filled. Return if EOF is reached
-    // Is buffer refill required
-    if (limit == offset || isEOF()) {
-      fillBuffer();
-
-      if (isEOF()) {
-        return EOF;
-      }
-    }
-
-    int bytesRead = 0;
-    int bytesToRead = length;
-    int currentOutputOffset = bufferOffset;
-
-    while (bytesToRead != 0) {
-      // Is buffer refill required?
-      if (limit == offset) {
-        fillBuffer();
-
-        if (isEOF()) {
-          bytesToRead = 0;
-        }
-      }
-
-      if (bytesToRead > 0) {
-        int readByte = Math.min(limit - offset, bytesToRead);
-        bytesRead += readByte;
-        bytesToRead -= readByte;
-
-        for (int i = 0; i < readByte; i++) {
-          byteBuffer[currentOutputOffset++] = buffer[offset++];
-        }
-      }
-    }
-
-    return bytesRead;
-  }
-
   private void updateCurrentCharset(String currentLine) {
     if(currentLine != null) {
       if(currentLine.startsWith(HttpHeader.CONTENT_TYPE)) {
-        ContentType ct = parseContentType(currentLine);
+        currentLine = currentLine.substring(13, currentLine.length() - 2).trim();
+        ContentType ct = ContentType.parse(currentLine);
         if (ct != null) {
           String charsetString = ct.getParameter(ContentType.PARAMETER_CHARSET);
           if (charsetString != null) {
@@ -167,7 +111,7 @@ public class BatchLineReader {
             currentBoundary = DOUBLE_DASH + boundary;
           }
         }
-      } else if(isLinebreak(currentLine)) {
+      } else if(CRLF.equals(currentLine)) {
         readState.foundLinebreak();
       } else if(isBoundary(currentLine)) {
         readState.foundBoundary();
@@ -175,18 +119,6 @@ public class BatchLineReader {
     }
   }
 
-  private ContentType parseContentType(String currentLine) {
-    currentLine = currentLine.substring(CONTENT_TYPE_LENGTH, currentLine.length()).trim();
-    return ContentType.parse(currentLine);
-  }
-
-  private boolean isLinebreak(String currentLine) {
-    if(currentLine.length() > 2) {
-      return false;
-    }
-    return CR.equals(currentLine) || LF.equals(currentLine) || CRLF.equals(currentLine);
-  }
-
   private boolean isBoundary(String currentLine) {
     if((currentBoundary + CRLF).equals(currentLine)) {
       return true;
@@ -222,20 +154,20 @@ public class BatchLineReader {
         }
         buffer.put(currentChar);
 
-        if (currentChar == LF_BYTE) {
+        if (currentChar == LF) {
           foundLineEnd = true;
-        } else if (currentChar == CR_BYTE) {
+        } else if (currentChar == CR) {
           foundLineEnd = true;
 
-          // Check next char. Consume \n if available
+          // Check next byte. Consume \n if available
           // Is buffer refill required?
           if (limit == offset) {
             fillBuffer();
           }
 
           // Check if there is at least one character
-          if (limit != EOF && this.buffer[offset] == LF_BYTE) {
-            buffer.put(LF_BYTE);
+          if (limit != EOF && this.buffer[offset] == LF) {
+            buffer.put(LF);
             offset++;
           }
         }
@@ -256,10 +188,6 @@ public class BatchLineReader {
     }
   }
 
-  private boolean isEOF() {
-    return limit == EOF;
-  }
-
   private int fillBuffer() throws IOException {
     limit = reader.read(buffer, 0, buffer.length);
     offset = 0;

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b484f7f6/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java
index 21474dd..377c5e1 100644
--- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java
+++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/BatchResponseSerializer.java
@@ -22,6 +22,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.ByteBuffer;
 import java.nio.channels.Channels;
 import java.nio.channels.ReadableByteChannel;
@@ -193,7 +194,7 @@ public class BatchResponseSerializer {
       }
       if(buffer.remaining() < b.length) {
         buffer.flip();
-        ByteBuffer tmp = ByteBuffer.allocate(b.length + BUFFER_SIZE);
+        ByteBuffer tmp = ByteBuffer.allocate(buffer.limit() *2);
         tmp.put(buffer);
         buffer = tmp;
       }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b484f7f6/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java
index afb51f0..9861b7f 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/deserializer/batch/BatchLineReaderTest.java
@@ -24,20 +24,8 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.nio.ByteBuffer;
-import java.nio.channels.Channels;
-import java.nio.channels.ReadableByteChannel;
-import java.nio.channels.WritableByteChannel;
-import java.nio.charset.Charset;
 import java.util.List;
 
-import org.apache.commons.io.IOUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.apache.olingo.commons.api.ODataRuntimeException;
-import org.apache.olingo.server.api.ODataResponse;
 import org.junit.Test;
 
 public class BatchLineReaderTest {
@@ -55,7 +43,6 @@ public class BatchLineReaderTest {
       "\n";
 
   private static final String TEXT_EMPTY = "";
-  private static final String LF = "\n";
 
   @Test
   public void testSimpleText() throws Exception {
@@ -216,22 +203,6 @@ public class BatchLineReaderTest {
   }
 
   @Test
-  public void testReadMoreBufferCapacityThanCharacterAvailable() throws Exception {
-    final String TEXT = "Foo";
-    byte[] buffer = new byte[20];
-
-    BatchLineReader reader = create(TEXT);
-    assertEquals(3, reader.read(buffer, 0, 20));
-    assertEquals(-1, reader.read(buffer, 0, 20));
-    reader.close();
-
-    BatchLineReader readerBufferOne = create(TEXT, 1);
-    assertEquals(3, readerBufferOne.read(buffer, 0, 20));
-    assertEquals(-1, readerBufferOne.read(buffer, 0, 20));
-    readerBufferOne.close();
-  }
-
-  @Test
   public void testLineEqualsAndHashCode() {
     Line l1 = new Line("The first line", 1);
     Line l2 = new Line("The first line", 1);
@@ -242,28 +213,20 @@ public class BatchLineReaderTest {
     assertTrue(l1.hashCode() != l3.hashCode());
   }
 
-  @Test
-  public void testToList() throws Exception {
-    BatchLineReader reader = create(TEXT_COMBINED);
-    List<String> stringList = reader.toList();
+  @Test(expected = IllegalArgumentException.class)
+  public void testFailBufferSizeZero() throws Exception {
+    BatchLineReader reader = create(TEXT_EMPTY, 0);
+    reader.close();
+  }
 
-    assertEquals(11, stringList.size());
-    assertEquals("Test\r", stringList.get(0));
-    assertEquals("Test2\r\n", stringList.get(1));
-    assertEquals("Test3\n", stringList.get(2));
-    assertEquals("Test4\r", stringList.get(3));
-    assertEquals("\r", stringList.get(4));
-    assertEquals("\r\n", stringList.get(5));
-    assertEquals("\r\n", stringList.get(6));
-    assertEquals("Test5\n", stringList.get(7));
-    assertEquals("Test6\r\n", stringList.get(8));
-    assertEquals("Test7\n", stringList.get(9));
-    assertEquals("\n", stringList.get(10));
+  @Test(expected = IllegalArgumentException.class)
+  public void testFailBufferSizeNegative() throws Exception {
+    BatchLineReader reader = create(TEXT_EMPTY, -1);
     reader.close();
   }
 
   @Test
-  public void testToLineList() throws Exception {
+  public void testToList() throws Exception {
     BatchLineReader reader = create(TEXT_COMBINED);
     List<Line> stringList = reader.toLineList();
 
@@ -282,53 +245,6 @@ public class BatchLineReaderTest {
     reader.close();
   }
 
-  @Test
-  public void testBatchContent() throws Exception {
-    String batchContent = getFileContent("/batchLarge.batch", "utf-8");
-    BatchLineReader reader = create(batchContent);
-
-    List<String> lines = reader.toList();
-    assertEquals(2422, lines.size());
-    assertEquals("--batch_8194-cf13-1f56\n", lines.get(0));
-    assertEquals("              <d:City m:type=\"RefScenario.c_City\">\n", lines.get(1402));
-    assertEquals("\n", lines.get(1903));
-    assertEquals("--batch_8194-cf13-1f56--", lines.get(2421));
-  }
-
-  private String getFileContent(String fileName, String charset) throws IOException {
-    byte[] content = getFileContent(fileName);
-    return new String(content, Charset.forName(charset));
-  }
-
-  private byte[] getFileContent(String fileName) throws IOException {
-    final InputStream input = ClassLoader.class.getResourceAsStream(fileName);
-    if (input == null) {
-      throw new IOException("Requested file '" + fileName + "' was not found.");
-    }
-      ByteArrayOutputStream output = new ByteArrayOutputStream();
-      ByteBuffer inBuffer = ByteBuffer.allocate(8192);
-      ReadableByteChannel ic = Channels.newChannel(input);
-      WritableByteChannel oc = Channels.newChannel(output);
-      while (ic.read(inBuffer) > 0) {
-        inBuffer.flip();
-        oc.write(inBuffer);
-        inBuffer.rewind();
-      }
-      return output.toByteArray();
-  }
-
-  @Test(expected = IllegalArgumentException.class)
-  public void testFailBufferSizeZero() throws Exception {
-    BatchLineReader reader = create(TEXT_EMPTY, 0);
-    reader.close();
-  }
-
-  @Test(expected = IllegalArgumentException.class)
-  public void testFailBufferSizeNegative() throws Exception {
-    BatchLineReader reader = create(TEXT_EMPTY, -1);
-    reader.close();
-  }
-
   private BatchLineReader create(final String inputString) throws Exception {
     return new BatchLineReader(new ByteArrayInputStream(inputString
         .getBytes("UTF-8")));
@@ -338,5 +254,4 @@ public class BatchLineReaderTest {
     return new BatchLineReader(new ByteArrayInputStream(inputString
         .getBytes("UTF-8")), bufferSize);
   }
-
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/b484f7f6/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java
----------------------------------------------------------------------
diff --git a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java
index 9570d35..f73479b 100644
--- a/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java
+++ b/lib/server-core/src/test/java/org/apache/olingo/server/core/serializer/BatchResponseSerializerTest.java
@@ -27,7 +27,6 @@ import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Random;
 import java.util.UUID;
 
 import org.apache.commons.io.IOUtils;
@@ -97,7 +96,7 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 
   @Test
@@ -153,7 +152,7 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 
   @Test
@@ -213,7 +212,7 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 
   @Test
@@ -309,7 +308,7 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 
   @Test
@@ -363,7 +362,7 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 
   @Test
@@ -398,54 +397,7 @@ public class BatchResponseSerializerTest {
     assertEquals("Content-Length: 13" + CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertEquals("Walter Winter" + CRLF, body.get(line++));
-    assertTrue(body.get(line).contains("--batch_"));
-  }
-
-  @Test
-  public void testResponseVeryLargeHeader() throws Exception {
-    List<ODataResponsePart> parts = new ArrayList<ODataResponsePart>();
-    ODataResponse response = new ODataResponse();
-    response.setStatusCode(HttpStatusCode.OK.getStatusCode());
-    response.setHeader(HttpHeader.CONTENT_TYPE, "application/json");
-    final String chValue = generateTestData(20000);
-    response.setHeader("Custom-Header", chValue);
-    response.setContent(IOUtils.toInputStream("Walter Winter"));
-
-    List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
-    responses.add(response);
-    parts.add(new ODataResponsePart(responses, false));
-
-    final BatchResponseSerializer serializer = new BatchResponseSerializer();
-    final InputStream content = serializer.serialize(parts, BOUNDARY);
-
-    assertNotNull(content);
-    final BatchLineReader reader =
-            new BatchLineReader(content);
-    final List<String> body = reader.toList();
-    reader.close();
-
-    int line = 0;
-    assertEquals(11, body.size());
     assertTrue(body.get(line++).contains("--batch_"));
-    assertEquals("Content-Type: application/http" + CRLF, body.get(line++));
-    assertEquals("Content-Transfer-Encoding: binary" + CRLF, body.get(line++));
-    assertEquals(CRLF, body.get(line++));
-    assertEquals("HTTP/1.1 200 OK" + CRLF, body.get(line++));
-    assertEquals("Custom-Header: " + chValue + CRLF, body.get(line++));
-    assertEquals("Content-Type: application/json" + CRLF, body.get(line++));
-    assertEquals("Content-Length: 13" + CRLF, body.get(line++));
-    assertEquals(CRLF, body.get(line++));
-    assertEquals("Walter Winter" + CRLF, body.get(line++));
-    assertTrue(body.get(line).contains("--batch_"));
-  }
-
-  private String generateTestData(int amount) {
-    StringBuilder sb = new StringBuilder();
-    Random r = new Random();
-    for (int j = 0; j < amount; j++) {
-      sb.append((char)(65 + r.nextInt(25)));
-    }
-    return sb.toString();
   }
 
   @Test
@@ -484,6 +436,6 @@ public class BatchResponseSerializerTest {
     assertEquals(CRLF, body.get(line++));
     assertEquals(CRLF, body.get(line++));
     assertTrue(body.get(line++).contains("--changeset_"));
-    assertTrue(body.get(line).contains("--batch_"));
+    assertTrue(body.get(line++).contains("--batch_"));
   }
 }