camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r552453 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ test/java/org/apache/camel/component/file/
Date Mon, 02 Jul 2007 10:02:41 GMT
Author: jstrachan
Date: Mon Jul  2 03:02:40 2007
New Revision: 552453

URL: http://svn.apache.org/viewvc?view=rev&rev=552453
Log:
tidied up the test case for the file component as well as fixed a number of bugs; and made
the test case actually test things properly :)

Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileComponent.java
Mon Jul  2 03:02:40 2007
@@ -24,6 +24,7 @@
 
 import java.io.File;
 import java.util.Map;
+import java.net.URI;
 
 /**
  * The <a href="http://activemq.apache.org/camel/file.html">File Component</a>
for working with file systems
@@ -39,8 +40,10 @@
     }
 
     protected Endpoint<FileExchange> createEndpoint(String uri, String remaining, Map
parameters) throws Exception {
-        File file = new File(remaining);
-        FileEndpoint result = new FileEndpoint(file, remaining, this);
+        URI u = new URI(uri);
+        String schemeSpecificPart = u.getSchemeSpecificPart();
+        File file = new File(schemeSpecificPart);
+        FileEndpoint result = new FileEndpoint(file, uri, this);
         IntrospectionSupport.setProperties(result, parameters);
         return result;
     }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileConsumer.java
Mon Jul  2 03:02:40 2007
@@ -68,8 +68,13 @@
         if (file.exists() && file.lastModified() > lastPollTime) {
             if (isValidFile(file)) {
                 processFile(file);
+                deleteOrMoveFile(file);
             }
         }
+    }
+
+    protected void deleteOrMoveFile(File file) {
+        file.delete();
     }
 
     protected void processFile(File file) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileExchange.java
Mon Jul  2 03:02:40 2007
@@ -17,41 +17,37 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultExchange;
 
+import java.io.File;
+
 /**
  * A {@link Exchange} for File
- * 
+ *
  * @version $Revision: 520985 $
  */
 public class FileExchange extends DefaultExchange {
-
     private File file;
-    /**
-     * Constructor
-     * @param camelContext
-     * @param file
-     */
+
     public FileExchange(CamelContext camelContext, File file) {
         super(camelContext);
         setIn(new FileMessage(file));
         this.file = file;
     }
-    
+
     /**
      * @return the file
      */
-    public File getFile(){
+    public File getFile() {
         return this.file;
     }
-    
+
     /**
      * @param file the file to set
      */
-    public void setFile(File file){
-        this.file=file;
+    public void setFile(File file) {
+        this.file = file;
     }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileMessage.java
Mon Jul  2 03:02:40 2007
@@ -17,16 +17,20 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
 import org.apache.camel.Exchange;
+import org.apache.camel.RuntimeCamelException;
 import org.apache.camel.impl.DefaultMessage;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+
 /**
  * A {@link Exchange} for  File
- * 
+ *
  * @version $Revision: 520985 $
  */
-public class FileMessage extends DefaultMessage{
+public class FileMessage extends DefaultMessage {
     private File file;
 
     public FileMessage() {
@@ -34,7 +38,7 @@
     }
 
     public FileMessage(File file) {
-        this.file=file;
+        this.file = file;
     }
 
     @Override
@@ -47,20 +51,30 @@
         return (FileExchange) super.getExchange();
     }
 
-   
     public File getFile() {
         return file;
     }
 
     public void setFile(File file) {
-        this.file=file;
+        this.file = file;
     }
 
-    
-   
     @Override
     public FileMessage newInstance() {
         return new FileMessage();
     }
-   
+
+    @Override
+    protected Object createBody() {
+        if (file != null) {
+            // lets extract the body
+            try {
+                return new FileInputStream(file);
+            }
+            catch (FileNotFoundException e) {
+                throw new RuntimeCamelException("File has been deleted: " + file + ". Reason:
" + e, e);
+            }
+        }
+        return super.createBody();
+    }
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileProducer.java
Mon Jul  2 03:02:40 2007
@@ -46,34 +46,35 @@
         process(endpoint.toExchangeType(exchange));
     }
 
-    public void process(FileExchange exchange){
+    public void process(FileExchange exchange) {
         String fileName = exchange.getIn().getMessageId();
-        ByteBuffer payload=exchange.getIn().getBody(ByteBuffer.class);
+        ByteBuffer payload = exchange.getIn().getBody(ByteBuffer.class);
         payload.flip();
         File file = null;
-        if(endpoint.getFile()!=null&&endpoint.getFile().isDirectory()){
-            
-            file=new File(endpoint.getFile(),fileName);
-           
-        }else{
-            file=new File(fileName);
+        File endpointFile = endpoint.getFile();
+        if (endpointFile != null && endpointFile.isDirectory()) {
+            file = new File(endpointFile, fileName);
+        }
+        else {
+            file = new File(fileName);
         }
         buildDirectory(file);
-        try{
-            FileChannel fc=new RandomAccessFile(file,"rw").getChannel();
+        try {
+            FileChannel fc = new RandomAccessFile(file, "rw").getChannel();
             fc.position(fc.size());
             fc.write(payload);
             fc.close();
-        }catch(Throwable e){
-            log.error("Failed to write to File: "+file,e);
+        }
+        catch (Throwable e) {
+            log.error("Failed to write to File: " + file, e);
         }
     }
-    
+
     private void buildDirectory(File file) {
         String dirName = file.getAbsolutePath();
         int index = dirName.lastIndexOf(File.separatorChar);
         if (index > 0) {
-            dirName = dirName.substring(0,index);
+            dirName = dirName.substring(0, index);
             File dir = new File(dirName);
             dir.mkdirs();
         }

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java?view=diff&rev=552453&r1=552452&r2=552453
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileRouteTest.java
Mon Jul  2 03:02:40 2007
@@ -17,74 +17,31 @@
  */
 package org.apache.camel.component.file;
 
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import junit.framework.TestCase;
-import org.apache.camel.CamelContext;
-import org.apache.camel.Endpoint;
-import org.apache.camel.Message;
-import org.apache.camel.Processor;
-import org.apache.camel.Producer;
-import org.apache.camel.Exchange;
+import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.impl.DefaultCamelContext;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.camel.component.mock.MockEndpoint;
 
 /**
  * @version $Revision: 529902 $
  */
-public class FileRouteTest extends TestCase {
-    private static final transient Log log = LogFactory.getLog(FileRouteTest.class);
-    protected CamelContext container = new DefaultCamelContext();
-    protected CountDownLatch latch = new CountDownLatch(1);
-    protected Exchange receivedExchange;
-    protected String uri = "file://target/foo.txt";
-    protected Producer<FileExchange> producer;
+public class FileRouteTest extends ContextTestSupport {
+    protected Object expectedBody = "Hello there!";
+    protected String uri = "file:target/test-inbox";
 
-    
     public void testFileRoute() throws Exception {
+        MockEndpoint result = resolveMandatoryEndpoint("mock:result", MockEndpoint.class);
+        result.expectedBodiesReceived(expectedBody);
 
-        // now lets fire in a message
-        Endpoint<FileExchange> endpoint = container.getEndpoint(uri);
-        FileExchange exchange = endpoint.createExchange();
-        Message message = exchange.getIn();
-        message.setBody("Hello there!");
-        message.setHeader("cheese", 123);
+        template.sendBody(uri, expectedBody, "cheese", 123);
 
-        producer = endpoint.createProducer();
-        producer.process(exchange);
-
-        // now lets sleep for a while
-        boolean received = latch.await(5000, TimeUnit.SECONDS);
-        assertTrue("Did not receive the message!", received);
+        result.assertIsSatisfied();
     }
 
     @Override
-    protected void setUp() throws Exception {
-        container.addRoutes(createRouteBuilder());
-        container.start();
-    }
-
-
-    @Override
-    protected void tearDown() throws Exception {
-        if (producer != null) {
-            producer.stop();
-        }
-        container.stop();
-    }
-
     protected RouteBuilder createRouteBuilder() {
         return new RouteBuilder() {
             public void configure() {
-                from(uri).process(new Processor() {
-                    public void process(Exchange e) {
-                        log.debug("Received exchange: " + e.getIn());
-                        receivedExchange = e;
-                        latch.countDown();
-                    }
-                });
+                from(uri).convertBodyTo(String.class).to("mock:result");
             }
         };
     }



Mime
View raw message