camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r669391 - in /activemq/camel/trunk/components/camel-stream/src: main/java/org/apache/camel/component/stream/ test/java/org/apache/camel/component/stream/
Date Thu, 19 Jun 2008 05:12:55 GMT
Author: davsclaus
Date: Wed Jun 18 22:12:54 2008
New Revision: 669391

URL: http://svn.apache.org/viewvc?rev=669391&view=rev
Log:
CAMEL-421: Fixing stream component and adding unit tests. Work in progress.

Added:
    activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java
  (with props)
    activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
Modified:
    activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
    activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamMessage.java
    activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java

Modified: activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java?rev=669391&r1=669390&r2=669391&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
(original)
+++ activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamConsumer.java
Wed Jun 18 22:12:54 2008
@@ -29,7 +29,9 @@
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
+import org.apache.camel.Message;
 import org.apache.camel.impl.DefaultConsumer;
+import org.apache.camel.impl.DefaultMessage;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -39,7 +41,7 @@
 public class StreamConsumer extends DefaultConsumer<Exchange> {
 
     private static final transient Log LOG = LogFactory.getLog(StreamConsumer.class);
-    private static final String TYPES = "in";
+    private static final String TYPES = "in,file,url";
     private static final String INVALID_URI = "Invalid uri, valid form: 'stream:{" + TYPES
+ "}'";
     private static final List<String> TYPES_LIST = Arrays.asList(TYPES.split(","));
     private InputStream inputStream = System.in;
@@ -65,15 +67,7 @@
             inputStream = resolveStreamFromUrl();
         }
 
-        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
-        String line;
-        try {
-            while ((line = br.readLine()) != null) {
-                consume(line);
-            }
-        } finally {
-            br.close();
-        }
+        readFromStream();
     }
 
     @Override
@@ -84,9 +78,25 @@
         super.doStop();
     }
 
-    public void consume(Object o) throws Exception {
+    private void readFromStream() throws Exception {
+        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
+        String line;
+        try {
+            while ((line = br.readLine()) != null) {
+                consumeLine(line);
+            }
+        } finally {
+            br.close();
+        }
+    }
+
+    private void consumeLine(Object line) throws Exception {
         Exchange exchange = endpoint.createExchange();
-        exchange.setIn(new StreamMessage(o));
+
+        Message msg = new DefaultMessage();
+        msg.setBody(line);
+        exchange.setIn(msg);
+
         getProcessor().process(exchange);
     }
 

Modified: activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamMessage.java?rev=669391&r1=669390&r2=669391&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamMessage.java
(original)
+++ activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamMessage.java
Wed Jun 18 22:12:54 2008
@@ -18,6 +18,9 @@
 
 import org.apache.camel.impl.DefaultMessage;
 
+/**
+ * @deprecated Camel Stream uses a DefaultMessage to contain the body. Will be removed in
Camel 2.0
+ */
 public class StreamMessage extends DefaultMessage {
     private Object o;
 

Modified: activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java?rev=669391&r1=669390&r2=669391&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
(original)
+++ activemq/camel/trunk/components/camel-stream/src/main/java/org/apache/camel/component/stream/StreamProducer.java
Wed Jun 18 22:12:54 2008
@@ -36,7 +36,7 @@
 public class StreamProducer extends DefaultProducer<Exchange> {
 
     private static final transient Log LOG = LogFactory.getLog(StreamProducer.class);
-    private static final String TYPES = "in,out,err,file,url,header";
+    private static final String TYPES = "out,err,file,header,url";
     private static final String INVALID_URI = "Invalid uri, valid form: 'stream:{" + TYPES
+ "}'";
     private static final List<String> TYPES_LIST = Arrays.asList(TYPES.split(","));
     private OutputStream outputStream = System.out;
@@ -118,10 +118,13 @@
         if (body instanceof String) {
             LOG.debug("in text buffered mode");
             BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream));
-            bw.write((String)body);
-            bw.write("\n");
-            bw.flush();
-            bw.close();
+            try {
+                bw.write((String)body);
+                bw.write("\n");
+                bw.flush();
+            } finally {
+                bw.close();
+            }
         } else {
             LOG.debug("in binary stream mode");
             outputStream.write((byte[])body);

Added: activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java?rev=669391&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java
(added)
+++ activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamHeaderTest.java
Wed Jun 18 22:12:54 2008
@@ -0,0 +1,64 @@
+/**
+ * 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.OutputStream;
+import java.io.IOException;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test when using custom output stream.
+ */
+public class StreamHeaderTest extends ContextTestSupport {
+
+    // START SNIPPET: e1
+    private OutputStream mystream = new MyOutputStream();
+    private StringBuffer sb = new StringBuffer();
+
+    public void testStringContent() {
+        template.sendBody("direct:in", "Hello");
+        // StreamProducer appends \n in text mode
+        assertEquals("Hello\n", sb.toString());
+    }
+
+    public void testBinaryContent() {
+        template.sendBody("direct:in", "Hello".getBytes());
+        // StreamProducer is in binary mode so no \n is appended
+        assertEquals("Hello", sb.toString());
+    }
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:in").setHeader("stream", constant(mystream)).
+                    to("stream:header");
+            }
+        };
+    }
+
+    private class MyOutputStream extends OutputStream {
+
+        public void write(int b) throws IOException {
+            char c = (char)b;
+            sb.append((char)b);
+        }
+    }
+    // END SNIPPET: e1
+
+}

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

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

Added: activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java?rev=669391&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
(added)
+++ activemq/camel/trunk/components/camel-stream/src/test/java/org/apache/camel/component/stream/StreamSystemOutTest.java
Wed Jun 18 22:12:54 2008
@@ -0,0 +1,58 @@
+/**
+ * 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.OutputStream;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Exchange;
+import org.apache.camel.Producer;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * Unit test for System.out
+ */
+public class StreamSystemOutTest extends ContextTestSupport {
+
+    public void testStringContent() throws Exception {
+        Endpoint endpoint = context.getEndpoint("direct:in");
+        Exchange exchange = endpoint.createExchange(ExchangePattern.InOnly);
+        exchange.getIn().setBody("Hello World\n");
+        Producer producer = endpoint.createProducer();
+        producer.start();
+        producer.process(exchange);
+        producer.stop();
+
+        //template.sendBody("direct:in", "Hello");
+        System.out.println("End of test");
+    }
+
+/*    public void testBinaryContent() {
+        template.sendBody("direct:in", "World".getBytes());
+    }*/
+
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:in").to("stream:out");
+            }
+        };
+    }
+    
+}



Mime
View raw message