camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r721798 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/file/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/language/simple/ test/java/org/apache/camel/component/file/ test/java/org/apache/camel...
Date Sun, 30 Nov 2008 11:36:05 GMT
Author: davsclaus
Date: Sun Nov 30 03:36:04 2008
New Revision: 721798

URL: http://svn.apache.org/viewvc?rev=721798&view=rev
Log:
CAMEL-1112: Added file language expression based sorting incl. grouping (quite fancy)

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/DefaultFileSorter.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
  (contents, props changed)
      - copied, changed from r721782, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByNestedExpressionTest.java
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/FileEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/DefaultFileSorter.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/DefaultFileSorter.java?rev=721798&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/DefaultFileSorter.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/DefaultFileSorter.java
Sun Nov 30 03:36:04 2008
@@ -0,0 +1,113 @@
+/**
+ * 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.file;
+
+import java.io.File;
+import java.util.Comparator;
+
+import org.apache.camel.Expression;
+import org.apache.camel.language.simple.FileLanguage;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * Default file sorter.
+ *
+ * @version $Revision$
+ */
+public final class DefaultFileSorter {
+
+    private DefaultFileSorter() {
+    }
+
+    /**
+     * Returns a new sory by name
+     */
+    public static Comparator<File> sortByName() {
+        return new Comparator<File>() {
+            public int compare(File o1, File o2) {
+                return o1.getName().compareTo(o2.getName());
+            }
+        };
+    }
+
+    /**
+     * Returns a new sory by path name
+     */
+    public static Comparator<File> sortByPathName() {
+        return new Comparator<File>() {
+            public int compare(File o1, File o2) {
+                return o1.getPath().compareTo(o2.getPath());
+            }
+        };
+    }
+
+    /**
+     * Returns a new sory by last modified (newest first)
+     */
+    public static Comparator<File> sortByLastModified() {
+        return new Comparator<File>() {
+            public int compare(File o1, File o2) {
+                long delta = o1.lastModified() - o2.lastModified();
+                if (delta == 0) {
+                    return 0;
+                }
+                return delta > 0 ? 1 : -1;
+            }
+        };
+    }
+
+    /**
+     * Returns a new sory by file size (smallest first)
+     */
+    public static Comparator<File> sortBySize() {
+        return new Comparator<File>() {
+            public int compare(File o1, File o2) {
+                long delta = o1.length() - o2.length();
+                if (delta == 0) {
+                    return 0;
+                } 
+                return delta > 0 ? 1 : -1;
+            }
+        };
+    }
+
+    public static Comparator<FileExchange> sortByFileLanguage(final String expression,
final boolean reverse) {
+        return sortByFileLanguage(expression, reverse, null);
+    }
+
+    public static Comparator<FileExchange> sortByFileLanguage(final String expression,
final boolean reverse,
+                                                              final Comparator<FileExchange>
nested) {
+        return new Comparator<FileExchange>() {
+            public int compare(FileExchange o1, FileExchange o2) {
+                final Expression exp = FileLanguage.file(expression);
+                Object result1 = exp.evaluate(o1);
+                Object result2 = exp.evaluate(o2);
+                int answer = ObjectHelper.compare(result1, result2);
+                // if equal then sub sort by nested comparator
+                if (answer == 0 && nested != null) {
+                    answer = nested.compare(o1, o2);
+                }
+                return reverse ? -1 * answer : answer;
+            }
+
+            public String toString() {
+                return expression + (nested != null ? ";" + nested.toString() : "");
+            }
+        };
+    }
+
+}

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?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- 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
Sun Nov 30 03:36:04 2008
@@ -19,12 +19,14 @@
 import java.io.File;
 import java.io.FileFilter;
 import java.util.Comparator;
+import java.util.Iterator;
 import java.util.Map;
 
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.processor.idempotent.MessageIdRepository;
+import org.apache.camel.util.ObjectHelper;
 
 /**
  * The <a href="http://activemq.apache.org/camel/file.html">File Component</a>
@@ -80,15 +82,39 @@
             result.setFilter(filter);
         }
 
+        // lookup sorter in registry if provided
         ref = getAndRemoveParameter(parameters, "sorterRef", String.class);
         if (ref != null) {
             Comparator<File> sorter = mandatoryLookup(ref, Comparator.class);
-            result.setSorter(sorter);
+            result.setFileSorter(sorter);
         }
 
-        // TODO: CAMEL-1112 having out-of-box sorters for by name, by filestamp, etc., maybe
even a reverse order
+        // sort by using file language 
+        String sortBy = getAndRemoveParameter(parameters, "sortBy", String.class);
+        if (sortBy != null) {
+            // we support nested sort groups so they should be chained
+            String[] groups = sortBy.split(";");
+            Iterator<String> it = ObjectHelper.createIterator(groups);
+            Comparator<FileExchange> comparator = createSortByComparator(it);
+            result.setExchangeSorter(comparator);
+        }
 
         setProperties(result, parameters);
         return result;
     }
+
+    private Comparator<FileExchange> createSortByComparator(Iterator<String>
it) {
+        if (!it.hasNext()) {
+            return null;
+        }
+
+        String group = it.next();
+        boolean reverse = group.startsWith("reverse:");
+        String reminder = reverse ? ifStartsWithReturnRemainder("reverse:", group) : group;
+        ObjectHelper.notNull(reminder, "sortBy option does not contain the expression");
+
+        // recursive add nested sorters
+        return DefaultFileSorter.sortByFileLanguage(reminder, reverse, createSortByComparator(it));
+    }
+
 }

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?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- 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
Sun Nov 30 03:36:04 2008
@@ -56,16 +56,32 @@
         List<File> files = new ArrayList<File>();
         scanFilesToPoll(endpoint.getFile(), true, files);
 
-        // resort files if provided
-        if (endpoint.getSorter() != null) {
-            Collections.sort(files, endpoint.getSorter());
+        // sort files using file comparator if provided
+        if (endpoint.getFileSorter() != null) {
+            Collections.sort(files, endpoint.getFileSorter());
+        }
+
+        // sort using build in sorters that is expression based
+        // first we need to convert to FileExchange objects so we can sort using expressions
+        List<FileExchange> exchanges = new ArrayList<FileExchange>(files.size());
+        for (File file : files) {
+            FileExchange exchange = endpoint.createExchange(file);
+            endpoint.configureMessage(file, exchange.getIn());
+            exchanges.add(exchange);
+        }
+        // sort files using exchange comparator if provided
+        if (endpoint.getExchangeSorter() != null) {
+            Collections.sort(exchanges, endpoint.getExchangeSorter());
         }
 
         // consume files one by one
-        int total = files.size();
-        for (int index = 0; index < files.size(); index++) {
-            File file = files.get(index);
-            processFile(file, total, index);
+        int total = exchanges.size();
+        for (int index = 0; index < total; index++) {
+            FileExchange exchange = exchanges.get(index);
+            // add current index and total as headers
+            exchange.getIn().setHeader(FileComponent.HEADER_FILE_BATCH_INDEX, index);
+            exchange.getIn().setHeader(FileComponent.HEADER_FILE_BATCH_TOTAL, total);
+            processExchange(exchange);
         }
     }
 
@@ -104,22 +120,18 @@
     /**
      * Processes the given file
      *
-     * @param file  the file
-     * @param total  total number of files in this batch
-     * @param index  current index out of total in this batch                           
          
+     * @param exchange  the file exchange
      */
-    protected void processFile(final File file, int total, int index) {
+    protected void processExchange(final FileExchange exchange) {
+        final File file = exchange.getFile();
+
         if (LOG.isTraceEnabled()) {
             LOG.trace("Processing file: " + file);
         }
 
-        final FileProcessStrategy processStrategy = endpoint.getFileStrategy();
-        final FileExchange exchange = endpoint.createExchange(file);
-        exchange.getIn().setHeader(FileComponent.HEADER_FILE_BATCH_TOTAL, total);
-        exchange.getIn().setHeader(FileComponent.HEADER_FILE_BATCH_INDEX, index);
-
-        endpoint.configureMessage(file, exchange.getIn());
         try {
+            final FileProcessStrategy processStrategy = endpoint.getFileStrategy();
+
             // is we use excluse read then acquire the exclusive read (waiting until we got
it)
             if (exclusiveReadLock) {
                 acquireExclusiveReadLock(file);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/file/FileEndpoint.java
Sun Nov 30 03:36:04 2008
@@ -73,7 +73,8 @@
     private boolean idempotent;
     private MessageIdRepository idempotentRepository;
     private FileFilter filter;
-    private Comparator<File> sorter;
+    private Comparator<File> fileSorter;
+    private Comparator<FileExchange> exchangeSorter;
 
     protected FileEndpoint(File file, String endpointUri, FileComponent component) {
         super(endpointUri, component);
@@ -346,12 +347,28 @@
         this.filter = filter;
     }
 
-    public Comparator<File> getSorter() {
-        return sorter;
+    public Comparator<File> getFileSorter() {
+        return fileSorter;
     }
 
-    public void setSorter(Comparator<File> sorter) {
-        this.sorter = sorter;
+    public void setFileSorter(Comparator<File> fileSorter) {
+        this.fileSorter = fileSorter;
+    }
+
+    public Comparator<FileExchange> getExchangeSorter() {
+        return exchangeSorter;
+    }
+
+    public void setExchangeSorter(Comparator<FileExchange> exchangeSorter) {
+        this.exchangeSorter = exchangeSorter;
+    }
+
+    public void setExchangeSorter(String expression) {
+        setExchangeSorter(expression, false);
+    }
+
+    public void setExchangeSorter(String expression, boolean reverse) {
+        setExchangeSorter(DefaultFileSorter.sortByFileLanguage(expression, reverse));
     }
 
     /**

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultComponent.java
Sun Nov 30 03:36:04 2008
@@ -25,7 +25,6 @@
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.Endpoint;
-import org.apache.camel.Exchange;
 import org.apache.camel.ResolveEndpointFailedException;
 import org.apache.camel.spi.Injector;
 import org.apache.camel.spi.Registry;
@@ -288,4 +287,23 @@
         return convertTo(type, value);
     }
 
+    /**
+     * Returns the reminder of the text if it starts with the prefix.
+     * <p/>
+     * Is useable for string parameters that contains commands.
+     * 
+     * @param prefix  the prefix
+     * @param text  the text
+     * @return the reminder, or null if no reminder
+     */
+    protected String ifStartsWithReturnRemainder(String prefix, String text) {
+        if (text.startsWith(prefix)) {
+            String remainder = text.substring(prefix.length());
+            if (remainder.length() > 0) {
+                return remainder;
+            }
+        }
+        return null;
+    }
+    
 }

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileExpressionBuilder.java
Sun Nov 30 03:36:04 2008
@@ -71,6 +71,24 @@
         };
     }
 
+    public static Expression fileNameExtensionExpression() {
+        return new Expression() {
+            public Object evaluate(Exchange exchange) {
+                String name = exchange.getIn().getHeader("CamelFileName", String.class);
+                if (name != null) {
+                    return name.substring(name.lastIndexOf('.') + 1);
+                } else {
+                    return null;
+                }
+            }
+
+            @Override
+            public String toString() {
+                return "file:name.ext";
+            }
+        };
+    }
+
     public static Expression fileParentExpression() {
         return new Expression() {
             public Object evaluate(Exchange exchange) {
@@ -136,6 +154,20 @@
         };
     }
 
+    public static Expression fileLastModifiedExpression() {
+        return new Expression() {
+            public Object evaluate(Exchange exchange) {
+                return exchange.getIn().getHeader("CamelFileLastModified", Date.class);
+            }
+
+            @Override
+            public String toString() {
+                return "file:modified";
+            }
+        };
+    }
+
+
     public static Expression dateExpression(final String command, final String pattern) {
         return new Expression() {
             public Object evaluate(Exchange exchange) {

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/language/simple/FileLanguage.java
Sun Nov 30 03:36:04 2008
@@ -17,7 +17,6 @@
 package org.apache.camel.language.simple;
 
 import org.apache.camel.Expression;
-import org.apache.camel.component.file.FileExchange;
 import org.apache.camel.language.IllegalSyntaxException;
 import org.apache.camel.util.ObjectHelper;
 
@@ -28,11 +27,13 @@
  * <ul>
  * <li><tt>file:name</tt> to access the file name</li>
  * <li><tt>file:name.noext</tt> to access the file name with no extension</li>
+ * <li><tt>file:name.ext</tt> to access the file name extension</li>
  * <li><tt>file:parent</tt> to access the parent file name</li>
  * <li><tt>file:path</tt> to access the file path name</li>
  * <li><tt>file:absolute.path</tt> to access the absolute file path name</li>
  * <li><tt>file:canonical.path</tt> to access the canonical path name</li>
  * <li><tt>file:length</tt> to access the file length as a Long type</li>
+ * <li><tt>file:modified</tt> to access the file last modified as a Date
type</li>
  * <li><tt>date:&lt;command&gt;:&lt;pattern&gt;</tt> for
date formatting using the {@link java.text.SimpleDateFormat} patterns.
  *     Additional Supported commands are: <tt>file</tt> for the last modified
timestamp of the file.
  *     All the commands from {@link SimpleLanguage} is also avaiable.
@@ -57,6 +58,8 @@
         if (remainder != null) {
             if (ObjectHelper.equal(remainder, "name")) {
                 return FileExpressionBuilder.fileNameExpression();
+            } else if (ObjectHelper.equal(remainder, "name.ext")) {
+                return FileExpressionBuilder.fileNameExtensionExpression();
             } else if (ObjectHelper.equal(remainder, "name.noext")) {
                 return FileExpressionBuilder.fileNameNoExtensionExpression();
             } else if (ObjectHelper.equal(remainder, "parent")) {
@@ -69,6 +72,8 @@
                 return FileExpressionBuilder.fileCanoicalPathExpression();
             } else if (ObjectHelper.equal(remainder, "length")) {
                 return FileExpressionBuilder.fileSizeExpression();
+            } else if (ObjectHelper.equal(remainder, "modified")) {
+                return FileExpressionBuilder.fileLastModifiedExpression();
             }
         }
 

Copied: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
(from r721782, activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java)
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java?p2=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java&p1=activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java&r1=721782&r2=721798&rev=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
Sun Nov 30 03:36:04 2008
@@ -16,27 +16,16 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.File;
-import java.util.Comparator;
-
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
-import org.apache.camel.impl.JndiRegistry;
 
 /**
- * Unit test for  the file sorter ref option
+ * Unit test for  the file sort by expression
  */
-public class FileSorterRefTest extends ContextTestSupport {
+public class FileSortByExpressionTest extends ContextTestSupport {
 
-    private String fileUrl = "file://target/filesorter/?sorterRef=mySorter";
-
-    @Override
-    protected JndiRegistry createRegistry() throws Exception {
-        JndiRegistry jndi = super.createRegistry();
-        jndi.bind("mySorter", new MyFileSorter());
-        return jndi;
-    }
+    private String fileUrl = "file://target/filesorter/?noop=true";
 
     @Override
     protected void setUp() throws Exception {
@@ -44,33 +33,33 @@
         deleteDirectory("target/filesorter");
 
         template.sendBodyAndHeader("file:target/filesorter/", "Hello Paris",
-            FileComponent.HEADER_FILE_NAME, "paris.txt");
+            FileComponent.HEADER_FILE_NAME, "paris.dat");
 
         template.sendBodyAndHeader("file:target/filesorter/", "Hello London",
             FileComponent.HEADER_FILE_NAME, "london.txt");
 
         template.sendBodyAndHeader("file:target/filesorter/", "Hello Copenhagen",
-            FileComponent.HEADER_FILE_NAME, "copenhagen.txt");
+            FileComponent.HEADER_FILE_NAME, "copenhagen.xml");
     }
 
     public void testSortFiles() throws Exception {
         MockEndpoint mock = getMockEndpoint("mock:result");
-        mock.expectedBodiesReceived("Hello Copenhagen", "Hello London", "Hello Paris");
+        mock.expectedBodiesReceived("Hello Paris", "Hello London", "Hello Copenhagen");
+
+        MockEndpoint reverse = getMockEndpoint("mock:reverse");
+        reverse.expectedBodiesReceived("Hello Copenhagen", "Hello London", "Hello Paris");
+
         assertMockEndpointsSatisfied();
     }
 
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() throws Exception {
-                from(fileUrl).to("mock:result");
+                from(fileUrl + "&sortBy=file:name.ext").to("mock:result");
+
+                from(fileUrl + "&sortBy=reverse:file:name.ext").to("mock:reverse");
             }
         };
     }
 
-    public class MyFileSorter implements Comparator<File> {
-        public int compare(File o1, File o2) {
-            return o1.getName().compareTo(o2.getName());
-        }
-    }
-
 }
\ No newline at end of file

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByExpressionTest.java
------------------------------------------------------------------------------
    svn:mergeinfo = 

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByNestedExpressionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByNestedExpressionTest.java?rev=721798&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByNestedExpressionTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSortByNestedExpressionTest.java
Sun Nov 30 03:36:04 2008
@@ -0,0 +1,68 @@
+/**
+ * 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.file;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * Unit test for  the file sort by expression with nested groups
+ */
+public class FileSortByNestedExpressionTest extends ContextTestSupport {
+
+    private String fileUrl = "file://target/filesorter/?noop=true";
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        deleteDirectory("target/filesorter");
+
+        template.sendBodyAndHeader("file:target/filesorter/", "Hello Paris",
+            FileComponent.HEADER_FILE_NAME, "paris.txt");
+
+        template.sendBodyAndHeader("file:target/filesorter/", "Hello London",
+            FileComponent.HEADER_FILE_NAME, "london.txt");
+
+        template.sendBodyAndHeader("file:target/filesorter/", "Hello Copenhagen",
+            FileComponent.HEADER_FILE_NAME, "copenhagen.xml");
+
+        template.sendBodyAndHeader("file:target/filesorter/", "Hello Dublin",
+            FileComponent.HEADER_FILE_NAME, "dublin.txt");
+    }
+
+    public void testSortNestedFiles() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello Dublin", "Hello London", "Hello Paris", "Hello
Copenhagen");
+
+        MockEndpoint reverse = getMockEndpoint("mock:reverse");
+        reverse.expectedBodiesReceived("Hello Paris", "Hello London", "Hello Dublin", "Hello
Copenhagen");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from(fileUrl + "&sortBy=file:name.ext;file:name").to("mock:result");
+
+                from(fileUrl + "&sortBy=file:name.ext;reverse:file:name").to("mock:reverse");
+            }
+        };
+    }
+
+}
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/file/FileSorterRefTest.java
Sun Nov 30 03:36:04 2008
@@ -67,10 +67,12 @@
         };
     }
 
+    // START SNIPPET: e1
     public class MyFileSorter implements Comparator<File> {
         public int compare(File o1, File o2) {
             return o1.getName().compareTo(o2.getName());
         }
     }
+    // END SNIPPET: e1
 
 }
\ No newline at end of file

Modified: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java?rev=721798&r1=721797&r2=721798&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
(original)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/language/FileLanguageTest.java
Sun Nov 30 03:36:04 2008
@@ -28,6 +28,7 @@
 import org.apache.camel.component.file.FileComponent;
 import org.apache.camel.component.file.FileExchange;
 import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.language.simple.FileLanguage;
 
 /**
  * Unit test for File Language.
@@ -58,12 +59,17 @@
 
     public void testFile() throws Exception {
         assertExpression("${file:name}", file.getName());
+        assertExpression("${file:name.ext}", "txt");
         assertExpression("${file:name.noext}", "hello");
         assertExpression("${file:parent}", file.getParent());
         assertExpression("${file:path}", file.getPath());
         assertExpression("${file:absolute.path}", file.getAbsolutePath());
         assertExpression("${file:canonical.path}", file.getCanonicalPath());
         assertExpression("${file:length}", file.length());
+
+        // modified is a Date object
+        Date modified = (Date) FileLanguage.file("file:modified").evaluate(exchange);
+        assertEquals(new Date(file.lastModified()), modified);
     }
 
     public void testDate() throws Exception {



Mime
View raw message