http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseParserTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseParserTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseParserTest.java index f7e4602..aa9143a 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseParserTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseParserTest.java @@ -31,6 +31,7 @@ import org.apache.olingo.odata2.api.client.batch.BatchSingleResponse; import org.apache.olingo.odata2.api.commons.HttpContentType; import org.apache.olingo.odata2.api.commons.HttpHeaders; import org.apache.olingo.odata2.api.ep.EntityProvider; +import org.apache.olingo.odata2.core.batch.v2.BatchParser; import org.apache.olingo.odata2.testutil.helper.StringHelper; import org.junit.Test; @@ -39,7 +40,6 @@ public class BatchResponseParserTest { private static final String CRLF = "\r\n"; private static final String LF = "\n"; - @Test public void testSimpleBatchResponse() throws BatchException { String getResponse = "--batch_123" + CRLF @@ -56,8 +56,8 @@ public class BatchResponseParserTest { + "--batch_123--"; InputStream in = new ByteArrayInputStream(getResponse.getBytes()); - BatchResponseParser parser = new BatchResponseParser("multipart/mixed;boundary=batch_123"); - List<BatchSingleResponse> responses = parser.parse(in); + BatchParser parser = new BatchParser("multipart/mixed;boundary=batch_123", true); + List<BatchSingleResponse> responses = parser.parseBatchResponse(in); for (BatchSingleResponse response : responses) { assertEquals("200", response.getStatusCode()); assertEquals("OK", response.getStatusInfo()); @@ -74,8 +74,9 @@ public class BatchResponseParserTest { if (in == null) { throw new IOException("Requested file '" + fileName + "' was not found."); } - BatchResponseParser parser = new BatchResponseParser("multipart/mixed;boundary=batch_123"); - List<BatchSingleResponse> responses = parser.parse(StringHelper.toStream(in).asStreamWithLineSeparation("\r\n")); + BatchParser parser = new BatchParser("multipart/mixed;boundary=batch_123", true); + List<BatchSingleResponse> responses = + parser.parseBatchResponse(StringHelper.toStream(in).asStreamWithLineSeparation("\r\n")); for (BatchSingleResponse response : responses) { if ("1".equals(response.getContentId())) { assertEquals("204", response.getStatusCode()); @@ -106,8 +107,8 @@ public class BatchResponseParserTest { + "--batch_123--"; InputStream in = new ByteArrayInputStream(putResponse.getBytes()); - BatchResponseParser parser = new BatchResponseParser("multipart/mixed;boundary=batch_123"); - List<BatchSingleResponse> responses = parser.parse(in); + BatchParser parser = new BatchParser("multipart/mixed;boundary=batch_123", true); + List<BatchSingleResponse> responses = parser.parseBatchResponse(in); for (BatchSingleResponse response : responses) { assertEquals("204", response.getStatusCode()); assertEquals("No Content", response.getStatusInfo()); @@ -289,9 +290,9 @@ public class BatchResponseParserTest { public void parseWithAdditionalLineEndingAtTheEnd() throws Exception { String fileString = readFile("BatchResponseWithAdditionalLineEnding.batch"); assertTrue(fileString.contains("\r\n--batch_123--")); - InputStream stream =new ByteArrayInputStream(fileString.getBytes()); + InputStream stream = new ByteArrayInputStream(fileString.getBytes()); BatchSingleResponse response = - EntityProvider.parseBatchResponse(stream , "multipart/mixed;boundary=batch_123").get(0); + EntityProvider.parseBatchResponse(stream, "multipart/mixed;boundary=batch_123").get(0); assertEquals("This is the body we need to parse. The trailing line ending is part of the body." + CRLF, response .getBody()); @@ -308,25 +309,24 @@ public class BatchResponseParserTest { assertEquals(body, response.getBody()); } - + @Test public void parseWithUnixLineEndingsInBody() throws Exception { String body = "This is the body we need to parse. The line spaces in the body " + LF + LF + LF + "are " + LF + LF - + "part of the body and must not be ignored or filtered."; + + "part of the body and must not be ignored or filtered."; String responseString = "--batch_123" + CRLF + "Content-Type: application/http" + CRLF + "Content-Length: 234" + CRLF + "content-transfer-encoding: binary" + CRLF - + CRLF + + CRLF + "HTTP/1.1 500 Internal Server Error" + CRLF + "Content-Type: application/xml;charset=utf-8" + CRLF + "Content-Length: 125" + CRLF + CRLF + body + CRLF - + "--batch_123--" - ; + + "--batch_123--"; InputStream stream = new ByteArrayInputStream(responseString.getBytes()); BatchSingleResponse response = EntityProvider.parseBatchResponse(stream, "multipart/mixed;boundary=batch_123").get(0); @@ -347,7 +347,7 @@ public class BatchResponseParserTest { return b.toString(); } - + private InputStream getFileAsStream(final String filename) throws IOException { InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename); if (in == null) { @@ -358,7 +358,7 @@ public class BatchResponseParserTest { private void parseInvalidBatchResponseBody(final String putResponse) throws BatchException { InputStream in = new ByteArrayInputStream(putResponse.getBytes()); - BatchResponseParser parser = new BatchResponseParser("multipart/mixed;boundary=batch_123"); - parser.parse(in); + BatchParser parser = new BatchParser("multipart/mixed;boundary=batch_123", true); + parser.parseBatchResponse(in); } }
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseTest.java index f5f05ff..2f8b7f8 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseTest.java @@ -33,6 +33,7 @@ import org.apache.olingo.odata2.api.batch.BatchResponsePart; import org.apache.olingo.odata2.api.client.batch.BatchSingleResponse; import org.apache.olingo.odata2.api.commons.HttpStatusCodes; import org.apache.olingo.odata2.api.processor.ODataResponse; +import org.apache.olingo.odata2.core.batch.v2.BatchParser; import org.apache.olingo.odata2.testutil.helper.StringHelper; import org.junit.Test; @@ -75,8 +76,8 @@ public class BatchResponseTest { assertTrue(body.contains("HTTP/1.1 204 No Content")); String contentHeader = batchResponse.getContentHeader(); - BatchResponseParser parser = new BatchResponseParser(contentHeader); - List<BatchSingleResponse> result = parser.parse(new ByteArrayInputStream(body.getBytes())); + BatchParser parser = new BatchParser(contentHeader, true); + List<BatchSingleResponse> result = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes())); assertEquals(2, result.size()); } @@ -104,8 +105,8 @@ public class BatchResponseTest { assertTrue(body.contains("Content-Type: multipart/mixed; boundary=changeset")); String contentHeader = batchResponse.getContentHeader(); - BatchResponseParser parser = new BatchResponseParser(contentHeader); - List<BatchSingleResponse> result = parser.parse(new ByteArrayInputStream(body.getBytes())); + BatchParser parser = new BatchParser(contentHeader, true); + List<BatchSingleResponse> result = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes())); assertEquals(1, result.size()); } @@ -135,9 +136,9 @@ public class BatchResponseTest { assertTrue(body.contains("Content-Type: multipart/mixed; boundary=changeset")); String contentHeader = batchResponse.getContentHeader(); - BatchResponseParser parser = new BatchResponseParser(contentHeader); + BatchParser parser = new BatchParser(contentHeader, true); StringHelper.Stream content = StringHelper.toStream(body); - List<BatchSingleResponse> result = parser.parse(content.asStream()); + List<BatchSingleResponse> result = parser.parseBatchResponse(content.asStream()); assertEquals(2, result.size()); assertEquals("Failing content:\n" + content.asString(), 20, content.linesCount()); } http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseWriterTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseWriterTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseWriterTest.java index ea7d2bb..cc58159 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseWriterTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchResponseWriterTest.java @@ -57,7 +57,7 @@ public class BatchResponseWriterTest { assertEquals(202, batchResponse.getStatus().getStatusCode()); assertNotNull(batchResponse.getEntity()); String body = (String) batchResponse.getEntity(); - + assertTrue(body.contains("--batch")); assertTrue(body.contains("--changeset")); assertTrue(body.contains("HTTP/1.1 200 OK")); http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchTransformatorCommonTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchTransformatorCommonTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchTransformatorCommonTest.java new file mode 100644 index 0000000..3e18304 --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BatchTransformatorCommonTest.java @@ -0,0 +1,123 @@ +/******************************************************************************* + * 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.odata2.core.batch; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.odata2.api.batch.BatchException; +import org.apache.olingo.odata2.api.commons.HttpContentType; +import org.apache.olingo.odata2.api.commons.HttpHeaders; +import org.apache.olingo.odata2.core.batch.v2.BatchTransformatorCommon; +import org.apache.olingo.odata2.core.batch.v2.Header; +import org.junit.Test; + +public class BatchTransformatorCommonTest { + + private static final String BASE64_ENCODING = "BASE64"; + + @Test + public void testValidateContentTypeApplicationHTTP() throws BatchException { + List<String> contentTypeValues = Arrays.asList(new String[] { HttpContentType.APPLICATION_HTTP }); + final Header headers = makeHeaders(HttpHeaders.CONTENT_TYPE, contentTypeValues); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test + public void testValidateContentTypeMultipartMixed() throws BatchException { + List<String> contentTypeValues = + Arrays.asList(new String[] { HttpContentType.MULTIPART_MIXED + "; boundary=batch_32332_32323_fdsf" }); + final Header headers = makeHeaders(HttpHeaders.CONTENT_TYPE, contentTypeValues); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test + public void testValidateContentTypeMultipartMixedCaseInsensitiv() throws BatchException { + List<String> contentTypeValues = + Arrays.asList(new String[] { "mulTiPart/MiXed; boundary=batch_32332_32323_fdsf" }); + final Header headers = makeHeaders(HttpHeaders.CONTENT_TYPE, contentTypeValues); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test(expected = BatchException.class) + public void testValidateContentTypeNoValue() throws BatchException { + List<String> contentTypeValues = Arrays.asList(new String[] {}); + final Header headers = makeHeaders(HttpHeaders.CONTENT_TYPE, contentTypeValues); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test(expected = BatchException.class) + public void testValidateContentTypeMissingHeader() throws BatchException { + final Header headers = new Header(1); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test(expected = BatchException.class) + public void testValidateContentTypeMultipleValues() throws BatchException { + List<String> contentTypeValues = + Arrays.asList(new String[] { HttpContentType.APPLICATION_HTTP, HttpContentType.MULTIPART_MIXED }); + final Header headers = makeHeaders(HttpHeaders.CONTENT_TYPE, contentTypeValues); + + BatchTransformatorCommon.validateContentType(headers); + } + + @Test + public void testValidateContentTransferEncoding() throws BatchException { + List<String> contentTransferEncoding = Arrays.asList(new String[] { BatchHelper.BINARY_ENCODING }); + final Header headers = makeHeaders(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING, contentTransferEncoding); + + BatchTransformatorCommon.validateContentTransferEncoding(headers, false); + } + + @Test(expected = BatchException.class) + public void testValidateContentTransferEncodingMultipleValues() throws BatchException { + List<String> contentTransferEncoding = Arrays.asList(new String[] { BatchHelper.BINARY_ENCODING, BASE64_ENCODING }); + final Header headers = makeHeaders(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING, contentTransferEncoding); + + BatchTransformatorCommon.validateContentTransferEncoding(headers, false); + } + + @Test(expected = BatchException.class) + public void testValidateContentTransferEncodingMissingHeader() throws BatchException { + final Header headers = new Header(1); + + BatchTransformatorCommon.validateContentTransferEncoding(headers, true); + } + + @Test(expected = BatchException.class) + public void testValidateContentTransferEncodingMissingValue() throws BatchException { + List<String> contentTransferEncoding = Arrays.asList(new String[] {}); + final Header headers = makeHeaders(BatchHelper.HTTP_CONTENT_TRANSFER_ENCODING, contentTransferEncoding); + + BatchTransformatorCommon.validateContentTransferEncoding(headers, true); + } + + private Header makeHeaders(final String headerName, final List<String> values) { + final Header headers = new Header(1); + headers.addHeader(headerName, values, 1); + + return headers; + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BufferedReaderIncludingLineEndingsTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BufferedReaderIncludingLineEndingsTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BufferedReaderIncludingLineEndingsTest.java new file mode 100644 index 0000000..69b546f --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/BufferedReaderIncludingLineEndingsTest.java @@ -0,0 +1,482 @@ +/******************************************************************************* + * 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.odata2.core.batch; + +import static org.junit.Assert.*; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.UnsupportedEncodingException; +import java.util.List; + +import org.apache.olingo.odata2.core.batch.v2.BufferedReaderIncludingLineEndings; +import org.apache.olingo.odata2.core.batch.v2.BufferedReaderIncludingLineEndings.Line; +import org.junit.Test; + +public class BufferedReaderIncludingLineEndingsTest { + + private static final String TEXT_COMBINED = "Test\r" + + "Test2\r\n" + + "Test3\n" + + "Test4\r" + + "\r" + + "\r\n" + + "\r\n" + + "Test5\n" + + "Test6\r\n" + + "Test7\n" + + "\n"; + + private static final String TEXT_SMALL = "Test\r" + + "123"; + private static final String TEXT_EMPTY = ""; + + @Test + public void testSimpleText() throws IOException { + final String TEXT = "Test"; + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals(TEXT, reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testNoText() throws IOException { + final String TEXT = ""; + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testNoBytes() throws IOException { + BufferedReaderIncludingLineEndings reader = + new BufferedReaderIncludingLineEndings(new InputStreamReader(new ByteArrayInputStream(new byte[0]))); + + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testCRLF() throws IOException { + final String TEXT = "Test\r\n" + + "Test2"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals("Test\r\n", reader.readLine()); + assertEquals("Test2", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testLF() throws IOException { + final String TEXT = "Test\n" + + "Test2"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals("Test\n", reader.readLine()); + assertEquals("Test2", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testCR() throws IOException { + final String TEXT = "Test\r" + + "Test2"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals("Test\r", reader.readLine()); + assertEquals("Test2", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testCombined() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED); + + assertEquals("Test\r", reader.readLine()); + assertEquals("Test2\r\n", reader.readLine()); + assertEquals("Test3\n", reader.readLine()); + assertEquals("Test4\r", reader.readLine()); + assertEquals("\r", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("Test5\n", reader.readLine()); + assertEquals("Test6\r\n", reader.readLine()); + assertEquals("Test7\n", reader.readLine()); + assertEquals("\n", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testCombinedBufferSizeTwo() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED, 2); + + assertEquals("Test\r", reader.readLine()); + assertEquals("Test2\r\n", reader.readLine()); + assertEquals("Test3\n", reader.readLine()); + assertEquals("Test4\r", reader.readLine()); + assertEquals("\r", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("Test5\n", reader.readLine()); + assertEquals("Test6\r\n", reader.readLine()); + assertEquals("Test7\n", reader.readLine()); + assertEquals("\n", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testCombinedBufferSizeOne() throws IOException { + final String TEXT = "Test\r" + + "Test2\r\n" + + "Test3\n" + + "Test4\r" + + "\r" + + "\r\n" + + "\r\n" + + "Test5\n" + + "Test6\r\n" + + "Test7\n" + + "\r\n"; + + BufferedReaderIncludingLineEndings reader = create(TEXT, 1); + + assertEquals("Test\r", reader.readLine()); + assertEquals("Test2\r\n", reader.readLine()); + assertEquals("Test3\n", reader.readLine()); + assertEquals("Test4\r", reader.readLine()); + assertEquals("\r", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertEquals("Test5\n", reader.readLine()); + assertEquals("Test6\r\n", reader.readLine()); + assertEquals("Test7\n", reader.readLine()); + assertEquals("\r\n", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + + reader.close(); + } + + @Test + public void testDoubleLF() throws IOException { + final String TEXT = "Test\r" + + "\r"; + + BufferedReaderIncludingLineEndings reader = create(TEXT, 1); + + assertEquals("Test\r", reader.readLine()); + assertEquals("\r", reader.readLine()); + reader.close(); + } + + @Test + public void testSkipSimple() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL); + + assertEquals(5, reader.skip(5)); // Test\r + assertEquals("123", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testSkipBufferOne() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL, 1); + + assertEquals(5, reader.skip(5)); // Test\r + assertEquals("123", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testReadThanSkip() throws IOException { + final String TEXT = "Test\r" + + "\r" + + "123"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals("Test\r", reader.readLine()); + assertEquals(1, reader.skip(1)); // Test\r + assertEquals("123", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testReadMoreBufferCapacityThanCharacterAvailable() throws IOException { + final String TEXT = "Foo"; + char[] buffer = new char[20]; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + assertEquals(3, reader.read(buffer, 0, 20)); + assertEquals(-1, reader.read(buffer, 0, 20)); + reader.close(); + + BufferedReaderIncludingLineEndings readerBufferOne = create(TEXT, 1); + assertEquals(3, readerBufferOne.read(buffer, 0, 20)); + assertEquals(-1, readerBufferOne.read(buffer, 0, 20)); + readerBufferOne.close(); + } + + @Test + public void testSkipZero() throws IOException { + final String TEXT = "Test\r" + + "123\r\n"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals(0, reader.skip(0)); // Test\r + assertEquals("Test\r", reader.readLine()); + assertEquals("123\r\n", reader.readLine()); + assertNull(reader.readLine()); + assertNull(reader.readLine()); + reader.close(); + } + + @Test + public void testSkipToMuch() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL); + + assertEquals(8, reader.skip(10)); // Test\r + assertEquals(null, reader.readLine()); + reader.close(); + } + + @Test + public void testReadBufferOne() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL, 1); + + assertEquals('T', reader.read()); + assertEquals('e', reader.read()); + assertEquals('s', reader.read()); + assertEquals('t', reader.read()); + assertEquals('\r', reader.read()); + assertEquals('1', reader.read()); + assertEquals('2', reader.read()); + assertEquals('3', reader.read()); + assertEquals(-1, reader.read()); + assertEquals(-1, reader.read()); + } + + @Test + public void testReadZeroBytes() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL, 1); + + char[] buffer = new char[3]; + assertEquals(0, reader.read(buffer, 0, 0)); + assertEquals('T', reader.read()); + assertEquals(0, reader.read(buffer, 0, 0)); + assertEquals("est\r", reader.readLine()); + assertEquals("123", reader.readLine()); + + reader.close(); + } + + @Test + public void testRead() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_SMALL); + + assertEquals('T', reader.read()); + assertEquals('e', reader.read()); + assertEquals('s', reader.read()); + assertEquals('t', reader.read()); + assertEquals('\r', reader.read()); + assertEquals('1', reader.read()); + assertEquals('2', reader.read()); + assertEquals('3', reader.read()); + assertEquals(-1, reader.read()); + assertEquals(-1, reader.read()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testFailReadBufferAndOffsetBiggerThanBuffer() throws IOException { + BufferedReaderIncludingLineEndings reader = create(""); + + final char[] buffer = new char[3]; + reader.read(buffer, 1, 3); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testFailLengthNegative() throws IOException { + final char[] buffer = new char[3]; + BufferedReaderIncludingLineEndings reader = create("123"); + + reader.read(buffer, 1, -2); + reader.close(); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testFailOffsetNegative() throws IOException { + final char[] buffer = new char[3]; + BufferedReaderIncludingLineEndings reader = create("123"); + + reader.read(buffer, -1, 2); + reader.close(); + } + + @Test + public void testReadAndReadLine() throws IOException { + final String TEXT = "Test\r" + + "bar\n" + + "123\r\n" + + "foo"; + + BufferedReaderIncludingLineEndings reader = create(TEXT); + + assertEquals('T', reader.read()); + assertEquals('e', reader.read()); + assertEquals('s', reader.read()); + assertEquals('t', reader.read()); + assertEquals("\r", reader.readLine()); + assertEquals("bar\n", reader.readLine()); + assertEquals('1', reader.read()); + assertEquals('2', reader.read()); + assertEquals("3\r\n", reader.readLine()); + assertEquals("foo", reader.readLine()); + assertEquals(null, reader.readLine()); + assertEquals(-1, reader.read()); + } + + @Test + public void testLineEqualsAndHashCode() { + Line l1 = new Line("The first line", 1); + Line l2 = new Line("The first line", 1); + Line l3 = new Line("The second line", 2); + + assertEquals(l1, l2); + assertFalse(l1.equals(l3)); + assertTrue(l1.hashCode() != l3.hashCode()); + } + + @Test(expected = IllegalArgumentException.class) + public void testSkipNegative() throws IOException { + BufferedReaderIncludingLineEndings reader = create("123"); + reader.skip(-1); + } + + @Test(expected = IllegalArgumentException.class) + public void testFailBufferSizeZero() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY, 0); + reader.close(); + } + + @Test(expected = NullPointerException.class) + public void testInputStreamIsNull() throws IOException { + // Same behaviour like BufferedReader + BufferedReaderIncludingLineEndings reader = new BufferedReaderIncludingLineEndings(null); + reader.close(); + } + + @Test(expected = IllegalArgumentException.class) + public void testFailBufferSizeNegative() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY, -1); + reader.close(); + } + + @Test + public void testMarkSupoorted() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_EMPTY); + + assertEquals(false, reader.markSupported()); + reader.close(); + } + + @Test(expected = IOException.class) + public void testFailMark() throws IOException { + BufferedReaderIncludingLineEndings reader = create("123"); + + reader.mark(1); + } + + @Test(expected = IOException.class) + public void testFailReset() throws IOException { + BufferedReaderIncludingLineEndings reader = create("123"); + + reader.reset(); + } + + @Test + public void testReady() throws IOException { + BufferedReaderIncludingLineEndings reader = create("123\r123"); + assertEquals(false, reader.ready()); + assertEquals("123\r", reader.readLine()); + assertEquals(true, reader.ready()); + assertEquals("123", reader.readLine()); + assertEquals(false, reader.ready()); + + reader.close(); + } + + @Test + public void testToList() throws IOException { + BufferedReaderIncludingLineEndings reader = create(TEXT_COMBINED); + List<Line> stringList = reader.toList(); + + assertEquals(11, stringList.size()); + assertEquals("Test\r", stringList.get(0).toString()); + assertEquals("Test2\r\n", stringList.get(1).toString()); + assertEquals("Test3\n", stringList.get(2).toString()); + assertEquals("Test4\r", stringList.get(3).toString()); + assertEquals("\r", stringList.get(4).toString()); + assertEquals("\r\n", stringList.get(5).toString()); + assertEquals("\r\n", stringList.get(6).toString()); + assertEquals("Test5\n", stringList.get(7).toString()); + assertEquals("Test6\r\n", stringList.get(8).toString()); + assertEquals("Test7\n", stringList.get(9).toString()); + assertEquals("\n", stringList.get(10).toString()); + reader.close(); + } + + private BufferedReaderIncludingLineEndings create(final String inputString) throws UnsupportedEncodingException { + return new BufferedReaderIncludingLineEndings(new InputStreamReader(new ByteArrayInputStream(inputString + .getBytes("UTF-8")))); + } + + private BufferedReaderIncludingLineEndings create(final String inputString, int bufferSize) + throws UnsupportedEncodingException { + return new BufferedReaderIncludingLineEndings(new InputStreamReader(new ByteArrayInputStream(inputString + .getBytes("UTF-8"))), bufferSize); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java new file mode 100644 index 0000000..9561567 --- /dev/null +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/batch/HeaderTest.java @@ -0,0 +1,179 @@ +/******************************************************************************* + * 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.odata2.core.batch; + +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.odata2.api.commons.HttpContentType; +import org.apache.olingo.odata2.api.commons.HttpHeaders; +import org.apache.olingo.odata2.core.batch.v2.BatchParserCommon; +import org.apache.olingo.odata2.core.batch.v2.Header; +import org.junit.Test; + +public class HeaderTest { + + @Test + public void test() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(1, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(0)); + } + + @Test + public void testNotAvailable() { + Header header = new Header(1); + + assertNull(header.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(0, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + assertEquals("", header.getHeaderNotNull(HttpHeaders.CONTENT_TYPE)); + } + + @Test + public void testCaseInsensitive() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeader("cOnTenT-TyPE")); + assertEquals(1, header.getHeaders("cOnTenT-TyPE").size()); + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeaders("cOnTenT-TyPE").get(0)); + } + + @Test + public void testDuplicatedAdd() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 2); + + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(1, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(0)); + } + + @Test + public void testMatcher() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + ";boundary=123", 1); + + assertTrue(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_BOUNDARY)); + } + + @Test + public void testFieldName() { + Header header = new Header(0); + header.addHeader("MyFieldNamE", "myValue", 1); + + assertEquals("MyFieldNamE", header.getHeaderField("myfieldname").getFieldName()); + assertEquals("MyFieldNamE", header.toSingleMap().keySet().toArray(new String[0])[0]); + assertEquals("MyFieldNamE", header.toMultiMap().keySet().toArray(new String[0])[0]); + + assertEquals("myValue", header.toMultiMap().get("MyFieldNamE").get(0)); + assertEquals("myValue", header.toSingleMap().get("MyFieldNamE")); + } + + @Test + public void testDeepCopy() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + ";boundary=123", 1); + + Header copy = header.clone(); + assertEquals(header.getHeaders(HttpHeaders.CONTENT_TYPE), copy.getHeaders(HttpHeaders.CONTENT_TYPE)); + assertEquals(header.getHeader(HttpHeaders.CONTENT_TYPE), copy.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(header.getHeaderField(HttpHeaders.CONTENT_TYPE), copy.getHeaderField(HttpHeaders.CONTENT_TYPE)); + + assertTrue(header.getHeaders(HttpHeaders.CONTENT_TYPE) != copy.getHeaders(HttpHeaders.CONTENT_TYPE)); + assertTrue(header.getHeaderField(HttpHeaders.CONTENT_TYPE) != copy.getHeaderField(HttpHeaders.CONTENT_TYPE)); + } + + @Test + public void testMatcherNoHeader() { + Header header = new Header(1); + + assertFalse(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_MULTIPART_BOUNDARY)); + } + + @Test + public void testMatcherFail() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED + ";boundary=123", 1); + + assertFalse(header.isHeaderMatching(HttpHeaders.CONTENT_TYPE, BatchParserCommon.PATTERN_HEADER_LINE)); + } + + @Test + public void testDuplicatedAddList() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + header.addHeader(HttpHeaders.CONTENT_TYPE, Arrays.asList(new String[] { HttpContentType.MULTIPART_MIXED, + HttpContentType.APPLICATION_ATOM_SVC }), 2); + + assertEquals(HttpContentType.MULTIPART_MIXED + ", " + HttpContentType.APPLICATION_ATOM_SVC, header + .getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(2, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(0)); + assertEquals(HttpContentType.APPLICATION_ATOM_SVC, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(1)); + } + + @Test + public void testRemove() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + header.removeHeader(HttpHeaders.CONTENT_TYPE); + + assertNull(header.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(0, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + } + + @Test + public void testMultipleValues() { + Header header = new Header(1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.MULTIPART_MIXED, 1); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.APPLICATION_ATOM_SVC, 2); + header.addHeader(HttpHeaders.CONTENT_TYPE, HttpContentType.APPLICATION_ATOM_XML, 3); + + final String fullHeaderString = + HttpContentType.MULTIPART_MIXED + ", " + HttpContentType.APPLICATION_ATOM_SVC + ", " + + HttpContentType.APPLICATION_ATOM_XML; + + assertEquals(fullHeaderString, header.getHeader(HttpHeaders.CONTENT_TYPE)); + assertEquals(3, header.getHeaders(HttpHeaders.CONTENT_TYPE).size()); + assertEquals(HttpContentType.MULTIPART_MIXED, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(0)); + assertEquals(HttpContentType.APPLICATION_ATOM_SVC, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(1)); + assertEquals(HttpContentType.APPLICATION_ATOM_XML, header.getHeaders(HttpHeaders.CONTENT_TYPE).get(2)); + } + + @Test + public void testSplitValues() { + final String values = "abc, def,123,77, 99, ysd"; + List<String> splittedValues = Header.splitValuesByComma(values); + + assertEquals(6, splittedValues.size()); + assertEquals("abc", splittedValues.get(0)); + assertEquals("def", splittedValues.get(1)); + assertEquals("123", splittedValues.get(2)); + assertEquals("77", splittedValues.get(3)); + assertEquals("99", splittedValues.get(4)); + assertEquals("ysd", splittedValues.get(5)); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/a6e2fbe5/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java index 2a6786e..05912c4 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/edm/provider/EdmServiceMetadataImplProvTest.java @@ -28,7 +28,6 @@ import static org.mockito.Mockito.when; import java.net.URI; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -76,7 +75,7 @@ public class EdmServiceMetadataImplProvTest extends BaseTest { List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos(); assertNotNull(infos); - assertEquals(Collections.emptyList(), infos); + assertTrue(infos.isEmpty()); } @Test @@ -90,7 +89,27 @@ public class EdmServiceMetadataImplProvTest extends BaseTest { List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos(); assertNotNull(infos); - assertEquals(Collections.emptyList(), infos); + assertTrue(infos.isEmpty()); + } + + /** + * Check that no NPE can occur with a new created Schema + * + * @throws Exception + */ + @Test + public void getEntitySetInfosForNewEdmProviderSchemas() throws Exception { + List<Schema> schemas = new ArrayList<Schema>(); + schemas.add(new Schema()); + + EdmProvider edmProvider = mock(EdmProvider.class); + when(edmProvider.getSchemas()).thenReturn(schemas); + + EdmServiceMetadata serviceMetadata = new EdmServiceMetadataImplProv(edmProvider); + + List<EdmEntitySetInfo> infos = serviceMetadata.getEntitySetInfos(); + assertNotNull(infos); + assertTrue(infos.isEmpty()); } @Test
