camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r896358 - in /camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper: ./ GZIPHelperTest.java HttpProducerHelperTest.java LoadingByteArrayOutputStreamTest.java
Date Wed, 06 Jan 2010 09:35:46 GMT
Author: davsclaus
Date: Wed Jan  6 09:35:46 2010
New Revision: 896358

URL: http://svn.apache.org/viewvc?rev=896358&view=rev
Log:
CAMEL-1530: Added unit tests for gzip

Added:
    camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/
    camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
  (with props)
    camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
  (with props)
    camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
  (with props)

Added: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java?rev=896358&view=auto
==============================================================================
--- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
(added)
+++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
Wed Jan  6 09:35:46 2010
@@ -0,0 +1,79 @@
+/**
+ * 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.camel.component.http.helper;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.camel.Message;
+import org.apache.camel.converter.IOConverter;
+import org.apache.camel.impl.DefaultMessage;
+import org.junit.Test;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class GZIPHelperTest {
+
+    private static byte[] sampleBytes = new byte[]{1, 2, 3, 1, 2, 3};
+
+    @Test
+    public void toGZIPInputStreamShouldReturnTheSameInputStream() throws IOException {
+        InputStream inputStream = GZIPHelper.toGZIPInputStream("text", new ByteArrayInputStream(sampleBytes));
+        byte[] bytes = new byte[6];
+        inputStream.read(bytes);
+
+        assertEquals(-1, inputStream.read());
+        assertArrayEquals(sampleBytes, bytes);
+    }
+
+    @Test
+    public void toGZIPInputStreamShouldReturnAByteArrayInputStream() throws IOException {
+        InputStream inputStream = GZIPHelper.toGZIPInputStream("text", sampleBytes);
+
+        byte[] bytes = IOConverter.toBytes(inputStream);
+        assertArrayEquals(sampleBytes, bytes);
+    }
+
+    @Test
+    public void testIsGzipMessage() {
+        assertTrue(GZIPHelper.isGzip(createMessageWithContentEncodingHeader("gzip")));
+        assertTrue(GZIPHelper.isGzip(createMessageWithContentEncodingHeader("GZip")));
+
+        assertFalse(GZIPHelper.isGzip(createMessageWithContentEncodingHeader(null)));
+        assertFalse(GZIPHelper.isGzip(createMessageWithContentEncodingHeader("zip")));
+    }
+
+    @Test
+    public void isGzipString() {
+        assertTrue(GZIPHelper.isGzip("gzip"));
+        assertTrue(GZIPHelper.isGzip("GZip"));
+
+        assertFalse(GZIPHelper.isGzip((String) null));
+        assertFalse(GZIPHelper.isGzip("zip"));
+    }
+
+    private Message createMessageWithContentEncodingHeader(String contentEncoding) {
+        Message msg = new DefaultMessage();
+        msg.setHeader("Content-Encoding", contentEncoding);
+
+        return msg;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/GZIPHelperTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java?rev=896358&view=auto
==============================================================================
--- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
(added)
+++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
Wed Jan  6 09:35:46 2010
@@ -0,0 +1,167 @@
+/**
+ * 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.camel.component.http.helper;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.http.HttpEndpoint;
+import org.apache.camel.component.http.HttpMethods;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.DefaultExchange;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HttpProducerHelperTest {
+
+    @Test
+    public void createURLShouldReturnTheHeaderURIIfNotBridgeEndpoint() throws URISyntaxException
{
+        String url = HttpProducerHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", null),
+                createHttpEndpoint(false, "http://camel.apache.org"));
+
+        assertEquals("http://apache.org", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfBridgeEndpoint() throws URISyntaxException
{
+        String url = HttpProducerHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader("http://apache.org", null),
+                createHttpEndpoint(true, "http://camel.apache.org"));
+
+        assertEquals("http://camel.apache.org", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIIfNotBridgeEndpoint() throws URISyntaxException
{
+        String url = HttpProducerHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader(null, null),
+                createHttpEndpoint(false, "http://camel.apache.org"));
+
+        assertEquals("http://camel.apache.org", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIWithHeaderHttpPathAndAddOneSlash() throws
URISyntaxException {
+        String url = HttpProducerHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader(null, "search"),
+                createHttpEndpoint(true, "http://www.google.com"));
+
+        assertEquals("http://www.google.com/search", url);
+    }
+
+    @Test
+    public void createURLShouldReturnTheEndpointURIWithHeaderHttpPathAndRemoveOneSlash()
throws URISyntaxException {
+        String url = HttpProducerHelper.createURL(
+                createExchangeWithOptionalCamelHttpUriHeader(null, "/search"),
+                createHttpEndpoint(true, "http://www.google.com/"));
+
+        assertEquals("http://www.google.com/search", url);
+    }
+
+    @Test
+    public void createMethodAlwaysUseUserChoosenMethod() throws URISyntaxException {
+        HttpMethods method = HttpProducerHelper.createMethod(
+                createExchangeWithOptionalHttpQueryAndHttpMethodHeader("q=camel", HttpMethods.POST),
+                createHttpEndpoint(true, "http://www.google.com/search"),
+                false);
+
+        assertEquals(HttpMethods.POST, method);
+    }
+
+    @Test
+    public void createMethodUseGETIfQueryIsProvidedInHeader() throws URISyntaxException {
+        HttpMethods method = HttpProducerHelper.createMethod(
+                createExchangeWithOptionalHttpQueryAndHttpMethodHeader("q=camel", null),
+                createHttpEndpoint(true, "http://www.google.com/search"),
+                false);
+
+        assertEquals(HttpMethods.GET, method);
+    }
+
+    @Test
+    public void createMethodUseGETIfQueryIsProvidedInEndpointURI() throws URISyntaxException
{
+        HttpMethods method = HttpProducerHelper.createMethod(
+                createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null),
+                createHttpEndpoint(true, "http://www.google.com/search?q=test"),
+                false);
+
+        assertEquals(HttpMethods.GET, method);
+    }
+
+    @Test
+    public void createMethodUseGETIfNoneQueryOrPayloadIsProvided() throws URISyntaxException
{
+        HttpMethods method = HttpProducerHelper.createMethod(
+                createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null),
+                createHttpEndpoint(true, "http://www.google.com/search"),
+                false);
+
+        assertEquals(HttpMethods.GET, method);
+    }
+
+    @Test
+    public void createMethodUsePOSTIfNoneQueryButPayloadIsProvided() throws URISyntaxException
{
+        HttpMethods method = HttpProducerHelper.createMethod(
+                createExchangeWithOptionalHttpQueryAndHttpMethodHeader(null, null),
+                createHttpEndpoint(true, "http://www.google.com/search"),
+                true);
+
+        assertEquals(HttpMethods.POST, method);
+    }
+
+    private Exchange createExchangeWithOptionalHttpQueryAndHttpMethodHeader(String httpQuery,
HttpMethods httpMethod) {
+        CamelContext context = new DefaultCamelContext();
+        Exchange exchange = new DefaultExchange(context);
+        Message inMsg = exchange.getIn();
+        if (httpQuery != null) {
+            inMsg.setHeader("CamelHttpQuery", httpQuery);
+        }
+        if (httpMethod != null) {
+            inMsg.setHeader("CamelHttpMethod", httpMethod);
+        }
+
+        return exchange;
+    }
+
+    private Exchange createExchangeWithOptionalCamelHttpUriHeader(String endpointURI, String
httpPath) {
+        CamelContext context = new DefaultCamelContext();
+        Exchange exchange = new DefaultExchange(context);
+        Message inMsg = exchange.getIn();
+        if (endpointURI != null) {
+            inMsg.setHeader("CamelHttpUri", endpointURI);
+        }
+        if (httpPath != null) {
+            inMsg.setHeader("CamelHttpPath", httpPath);
+        }
+
+        return exchange;
+    }
+
+    private HttpEndpoint createHttpEndpoint(boolean bridgeEndpoint, String endpointURI) throws
URISyntaxException {
+        HttpEndpoint endpoint = new HttpEndpoint();
+        endpoint.setBridgeEndpoint(bridgeEndpoint);
+        if (endpointURI != null) {
+            endpoint.setHttpUri(new URI(endpointURI));
+        }
+
+        return endpoint;
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/HttpProducerHelperTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java?rev=896358&view=auto
==============================================================================
--- camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
(added)
+++ camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
Wed Jan  6 09:35:46 2010
@@ -0,0 +1,79 @@
+/**
+ * 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.camel.component.http.helper;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+public class LoadingByteArrayOutputStreamTest {
+
+    private LoadingByteArrayOutputStream out;
+    private byte[] bytes = new byte[]{1, 2, 3, 4, 5};
+
+    @Before
+    public void setUp() throws Exception {
+        out = new LoadingByteArrayOutputStream(4);
+        out.write(bytes);
+    }
+
+    @Test
+    public void defaultConstructor() {
+        out = new LoadingByteArrayOutputStream() {
+            public byte[] toByteArray() {
+                return buf;
+            }
+        };
+
+        assertEquals(1024, out.toByteArray().length);
+    }
+
+    @Test
+    public void toByteArrayShouldReturnTheSameArray() {
+        byte[] byteArray1 = out.toByteArray();
+        byte[] byteArray2 = out.toByteArray();
+
+        assertEquals(5, byteArray1.length);
+        assertSame(byteArray1, byteArray2);
+    }
+
+    @Test
+    public void toByteArrayShouldReturnANewArray() throws IOException {
+        byte[] byteArray1 = out.toByteArray();
+        out.write(bytes);
+        byteArray1 = out.toByteArray();
+
+        assertEquals(10, byteArray1.length);
+    }
+
+    @Test
+    public void createInputStream() {
+        ByteArrayInputStream in = out.createInputStream();
+
+        assertEquals(1, in.read());
+        assertEquals(2, in.read());
+        assertEquals(3, in.read());
+        assertEquals(4, in.read());
+        assertEquals(5, in.read());
+        assertEquals(-1, in.read());
+    }
+}
\ No newline at end of file

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-http/src/test/java/org/apache/camel/component/http/helper/LoadingByteArrayOutputStreamTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date



Mime
View raw message