Author: davsclaus
Date: Wed Jun  6 09:33:14 2012
New Revision: 1346797

URL: http://svn.apache.org/viewvc?rev=1346797&view=rev
Log:
CAMEL-5336: Improved camel-stream unit test. Thanks to Henryk for the patch.

Added:
    
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
   (with props)
    
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/
    
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
   (with props)
    
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
   (with props)
Modified:
    
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java

Modified: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java?rev=1346797&r1=1346796&r2=1346797&view=diff
==============================================================================
--- 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
 (original)
+++ 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
 Wed Jun  6 09:33:14 2012
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.component.stream;
 
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.junit.Test;
@@ -25,15 +28,43 @@ import org.junit.Test;
  */
 public class StreamSystemOutTest extends CamelTestSupport {
 
+    String message = "Hello World";
+
+    PrintStream stdOut = System.out;
+
+    ByteArrayOutputStream mockOut = new ByteArrayOutputStream();
+
     // START SNIPPET: e1
     @Test
     public void testStringContent() throws Exception {
-        template.sendBody("direct:in", "Hello Text World\n");
+        try {
+            // Given
+            System.setOut(new PrintStream(mockOut));
+
+            // When
+            template.sendBody("direct:in", message);
+
+            // Then
+            assertEquals(message + "\n", new String(mockOut.toByteArray()));
+        } finally {
+            System.setOut(stdOut);
+        }
     }
 
     @Test
     public void testBinaryContent() {
-        template.sendBody("direct:in", "Hello Bytes World\n".getBytes());
+        try {
+            // Given
+            System.setOut(new PrintStream(mockOut));
+
+            // When
+            template.sendBody("direct:in", message.getBytes());
+
+            // Then
+            assertEquals(message, new String(mockOut.toByteArray()));
+        } finally {
+            System.setOut(stdOut);
+        }
     }
 
     protected RouteBuilder createRouteBuilder() {

Added: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java?rev=1346797&view=auto
==============================================================================
--- 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
 (added)
+++ 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
 Wed Jun  6 09:33:14 2012
@@ -0,0 +1,69 @@
+/**
+ * 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.stream;
+
+import java.io.ByteArrayOutputStream;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.stream.mock.MockURLConnection;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Test;
+
+/**
+ * Unit test for producer writing to URL.
+ */
+public class StreamToUrlTest extends CamelTestSupport {
+
+    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+
+    String message = "message";
+
+    String existingHandlers = System.getProperty("java.protocol.handler.pkgs");
+
+    @Override
+    protected void doPreSetup() throws Exception {
+        System.setProperty("java.protocol.handler.pkgs", 
getClass().getPackage().getName());
+        MockURLConnection.setOutputStream(buffer);
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        if (existingHandlers != null) {
+            System.setProperty("java.protocol.handler.pkgs", existingHandlers);
+        }
+        super.tearDown();
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:start").to("stream:url?url=mock:");
+            }
+        };
+    }
+
+    @Test
+    public void shouldSendToUrlOutputStream() throws Exception {
+        // When
+        template.sendBody("direct:start", message);
+
+        // Then
+        String messageReceived = new String(buffer.toByteArray()).trim();
+        assertEquals(message, messageReceived);
+    }
+
+}

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamToUrlTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java?rev=1346797&view=auto
==============================================================================
--- 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
 (added)
+++ 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
 Wed Jun  6 09:33:14 2012
@@ -0,0 +1,34 @@
+/**
+ * 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.stream.mock;
+
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * Mock URL handler for testing URL connections.
+ */
+public class Handler extends URLStreamHandler {
+
+    @Override
+    protected URLConnection openConnection(URL u) throws IOException {
+        return new MockURLConnection(u);
+    }
+
+}

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/Handler.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java?rev=1346797&view=auto
==============================================================================
--- 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
 (added)
+++ 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
 Wed Jun  6 09:33:14 2012
@@ -0,0 +1,45 @@
+/**
+ * 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.stream.mock;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+
+public class MockURLConnection extends URLConnection {
+
+    private static final ThreadLocal<OutputStream> THREAD_OUTPUT_STREAM = new 
ThreadLocal<OutputStream>();
+
+    public MockURLConnection(URL url) {
+        super(url);
+    }
+
+    public static void setOutputStream(OutputStream outputStream) {
+        THREAD_OUTPUT_STREAM.set(outputStream);
+    }
+
+    @Override
+    public void connect() throws IOException {
+    }
+
+    @Override
+    public OutputStream getOutputStream() throws IOException {
+        return THREAD_OUTPUT_STREAM.get();
+    }
+
+}

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/mock/MockURLConnection.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date


Reply via email to