You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2016/09/27 06:29:29 UTC
[1/4] camel git commit: CAMEL-10339: ...fixed tests
Repository: camel
Updated Branches:
refs/heads/master d2b911801 -> 3c593972b
CAMEL-10339: ...fixed tests
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3c593972
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3c593972
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3c593972
Branch: refs/heads/master
Commit: 3c593972b7384559ead1c1ba3d1decd8176bbec6
Parents: 04fda67
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Tue Sep 27 00:31:35 2016 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 27 07:39:30 2016 +0200
----------------------------------------------------------------------
.../camel/component/file/FileConsumerFilterFileTest.java | 5 ++---
.../test/java/org/apache/camel/language/simple/SimpleTest.java | 6 +++---
2 files changed, 5 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/3c593972/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
index e7b1d22..f2156cd 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
@@ -59,11 +59,10 @@ public class FileConsumerFilterFileTest extends ContextTestSupport {
public void testFilterFilesWithDate() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result2");
mock.expectedMessageCount(1);
- mock.expectedBodiesReceived("Hello World");
+ mock.expectedBodiesReceived("Something else");
mock.setResultMinimumWaitTime(1500);
- mock.setResultWaitTime(5000);
- template.sendBodyAndHeader("file:target/filefilter/", "Hello World", Exchange.FILE_NAME, "hello2.txt");
+ template.sendBodyAndHeader("file:target/filefilter/", "Something else", Exchange.FILE_NAME, "hello2.txt");
mock.assertIsSatisfied();
}
http://git-wip-us.apache.org/repos/asf/camel/blob/3c593972/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 5c79ce7..f0bb9b9 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -487,13 +487,13 @@ public class SimpleTest extends LanguageTestSupport {
public void testDateWithTimezone() throws Exception {
Calendar cal = Calendar.getInstance();
- cal.setTimeZone(TimeZone.getTimeZone("GMT+2"));
+ cal.setTimeZone(TimeZone.getTimeZone("GMT+8"));
cal.set(1974, Calendar.APRIL, 20, 8, 55, 47);
cal.set(Calendar.MILLISECOND, 123);
exchange.getIn().setHeader("birthday", cal.getTime());
- assertExpression("date:header.birthday:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T08:55:47:123");
- assertExpression("date-with-timezone:header.birthday:UTC:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T06:55:47:123");
+ assertExpression("date-with-timezone:header.birthday:GMT+8:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T08:55:47:123");
+ assertExpression("date-with-timezone:header.birthday:GMT:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T00:55:47:123");
}
public void testDatePredicates() throws Exception {
[3/4] camel git commit: CAMEL-10339: Updated documentation for date
expressions
Posted by da...@apache.org.
CAMEL-10339: Updated documentation for date expressions
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/04fda67b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/04fda67b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/04fda67b
Branch: refs/heads/master
Commit: 04fda67b6efd435eb98ce72dc55d26bee46fe7e1
Parents: a0fe445
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Mon Sep 26 10:10:27 2016 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 27 07:39:30 2016 +0200
----------------------------------------------------------------------
camel-core/src/main/docs/simple-language.adoc | 11 ++++++++---
.../apache/camel/language/simple/SimpleLanguage.java | 12 +++++-------
2 files changed, 13 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/04fda67b/camel-core/src/main/docs/simple-language.adoc
----------------------------------------------------------------------
diff --git a/camel-core/src/main/docs/simple-language.adoc b/camel-core/src/main/docs/simple-language.adoc
index 3f549ff..0d55d58 100644
--- a/camel-core/src/main/docs/simple-language.adoc
+++ b/camel-core/src/main/docs/simple-language.adoc
@@ -223,11 +223,16 @@ exception set on exchange. Will fallback and grab caught exceptions
*null* if no exception set on exchange. Will fallback and grab caught
exceptions (`Exchange.EXCEPTION_CAUGHT`) if the Exchange has any.
-|date:_command:pattern_ |String |Date formatting using the `java.text.SimpleDataFormat` patterns.
+|date:_command_ |Date |Evaluates to a Date object.
Supported commands are: *now* for current timestamp, *in.header.xxx* or
*header.xxx* to use the Date object in the IN header with the key xxx.
-*out.header.xxx* to use the Date object in the OUT header with the key
-xxx.
+*out.header.xxx* to use the Date object in the OUT header with the key xxx.
+*file* for the last modified timestamp of the file (available with a File consumer).
+Command accepts offsets such as: *now-24h* or *in.header.xxx+1h* or even *now+1h30m-100*.
+
+|date:_command:pattern_ |String |Date formatting using `java.text.SimpleDataFormat` patterns.
+
+|date-with-timezone:_command:timezone:pattern_ |String |Date formatting using `java.text.SimpleDataFormat` timezones and patterns.
|bean:_bean expression_ |Object |Invoking a bean expression using the link:bean.html[Bean] language.
Specifying a method name you must use dot as separator. We also support
http://git-wip-us.apache.org/repos/asf/camel/blob/04fda67b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
index 3a1c2e8..20878c9 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
@@ -46,13 +46,15 @@ import org.apache.camel.util.PredicateToExpressionAdapter;
* <li>sysenv.foo to access the system environment called 'foo'</li>
* <li>exception.messsage to access the exception message</li>
* <li>threadName to access the current thread name</li>
- * <li>date:<command>:<pattern> or date:<command>:<timezone>:<pattern> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
- * If the pattern is empty the expression will resolve to a Date object.
+ * <li>date:<command> evaluates to a Date object
* Supported commands are: <tt>now</tt> for current timestamp,
* <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header.
* <tt>out.header.xxx</tt> to use the Date object in the out header.
- * Command accepts offsets such as: <tt>now-24h</tt> or <tt>in.header.xxx+1h</tt> or <tt>now+1h-100</tt>.
+ * <tt>file</tt> for the last modified timestamp of the file (available with a File consumer).
+ * Command accepts offsets such as: <tt>now-24h</tt> or <tt>in.header.xxx+1h</tt> or even <tt>now+1h30m-100</tt>.
* </li>
+ * <li>date:<command>:<pattern> for date formatting using {@link java.text.SimpleDateFormat} patterns</li>
+ * <li>date-with-timezone:<command>:<timezone>:<pattern> for date formatting using {@link java.text.SimpleDateFormat} timezones and patterns</li>
* <li>bean:<bean expression> to invoke a bean using the
* {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li>
* <li>properties:<[locations]>:<key> for using property placeholders using the
@@ -79,10 +81,6 @@ import org.apache.camel.util.PredicateToExpressionAdapter;
* <li><tt>file:length</tt> to access the file length as a Long type</li>
* <li><tt>file:size</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:<command>:<pattern></tt> or <tt>date:<command>:<timezone>:<pattern></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 available.
- * </li>
* </ul>
* The <b>relative</b> file is the filename with the starting directory clipped, as opposed to <b>path</b> that will
* return the full path including the starting directory.
[4/4] camel git commit: CAMEL-10339: Allow date offsets and timezones
with Simple language
Posted by da...@apache.org.
CAMEL-10339: Allow date offsets and timezones with Simple language
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/b52e6f76
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/b52e6f76
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/b52e6f76
Branch: refs/heads/master
Commit: b52e6f7683031036f4c171170a8bdf6255feb7cf
Parents: d2b9118
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Thu Sep 22 15:47:37 2016 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 27 07:39:30 2016 +0200
----------------------------------------------------------------------
.../apache/camel/builder/ExpressionBuilder.java | 43 ++++++++++++++++++--
.../camel/language/simple/SimpleLanguage.java | 6 ++-
.../simple/ast/SimpleFunctionExpression.java | 21 +++++++---
.../file/FileConsumerFilterFileTest.java | 22 +++++++---
.../camel/language/simple/SimpleTest.java | 28 +++++++++----
5 files changed, 95 insertions(+), 25 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/b52e6f76/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index a138271..706746e 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -19,6 +19,7 @@ package org.apache.camel.builder;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
@@ -29,9 +30,11 @@ import java.util.Map;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;
+import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.camel.CamelContext;
@@ -43,6 +46,7 @@ import org.apache.camel.InvalidPayloadException;
import org.apache.camel.Message;
import org.apache.camel.NoSuchEndpointException;
import org.apache.camel.NoSuchLanguageException;
+import org.apache.camel.NoTypeConversionAvailableException;
import org.apache.camel.Producer;
import org.apache.camel.component.bean.BeanInvocation;
import org.apache.camel.component.properties.PropertiesComponent;
@@ -1863,9 +1867,30 @@ public final class ExpressionBuilder {
};
}
+ public static Expression dateExpression(final String command) {
+ return dateExpression(command, TimeZone.getDefault().getID(), "");
+ }
+
public static Expression dateExpression(final String command, final String pattern) {
+ return dateExpression(command, TimeZone.getDefault().getID(), pattern);
+ }
+
+ public static Expression dateExpression(final String commandWithOffsets, final String timezone, final String pattern) {
return new ExpressionAdapter() {
public Object evaluate(Exchange exchange) {
+ // Capture optional time offsets
+ String command = commandWithOffsets.split("[+-]", 2)[0].trim();
+ List<Long> offsets = new ArrayList<>();
+ Matcher offsetMatcher = Pattern.compile("([+-])([^+-]+)").matcher(commandWithOffsets);
+ while (offsetMatcher.find()) {
+ try {
+ long value = exchange.getContext().getTypeConverter().mandatoryConvertTo(long.class, exchange, offsetMatcher.group(2).trim());
+ offsets.add(offsetMatcher.group(1).equals("+") ? value : -value);
+ } catch (NoTypeConversionAvailableException e) {
+ e.printStackTrace();
+ }
+ }
+
Date date;
if ("now".equals(command)) {
date = new Date();
@@ -1895,13 +1920,25 @@ public final class ExpressionBuilder {
throw new IllegalArgumentException("Command not supported for dateExpression: " + command);
}
- SimpleDateFormat df = new SimpleDateFormat(pattern);
- return df.format(date);
+ // Apply offsets
+ long dateAsLong = date.getTime();
+ for (long offset : offsets) {
+ dateAsLong += offset;
+ }
+ date = new Date(dateAsLong);
+
+ if (null != pattern && !pattern.isEmpty()) {
+ SimpleDateFormat df = new SimpleDateFormat(pattern);
+ df.setTimeZone(TimeZone.getTimeZone(timezone));
+ return df.format(date);
+ } else {
+ return date;
+ }
}
@Override
public String toString() {
- return "date(" + command + ":" + pattern + ")";
+ return "date(" + commandWithOffsets + ":" + pattern + ":" + timezone + ")";
}
};
}
http://git-wip-us.apache.org/repos/asf/camel/blob/b52e6f76/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
index 1962a1c..3a1c2e8 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/SimpleLanguage.java
@@ -46,10 +46,12 @@ import org.apache.camel.util.PredicateToExpressionAdapter;
* <li>sysenv.foo to access the system environment called 'foo'</li>
* <li>exception.messsage to access the exception message</li>
* <li>threadName to access the current thread name</li>
- * <li>date:<command>:<pattern> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
+ * <li>date:<command>:<pattern> or date:<command>:<timezone>:<pattern> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
+ * If the pattern is empty the expression will resolve to a Date object.
* Supported commands are: <tt>now</tt> for current timestamp,
* <tt>in.header.xxx</tt> or <tt>header.xxx</tt> to use the Date object in the in header.
* <tt>out.header.xxx</tt> to use the Date object in the out header.
+ * Command accepts offsets such as: <tt>now-24h</tt> or <tt>in.header.xxx+1h</tt> or <tt>now+1h-100</tt>.
* </li>
* <li>bean:<bean expression> to invoke a bean using the
* {@link org.apache.camel.language.bean.BeanLanguage BeanLanguage}</li>
@@ -77,7 +79,7 @@ import org.apache.camel.util.PredicateToExpressionAdapter;
* <li><tt>file:length</tt> to access the file length as a Long type</li>
* <li><tt>file:size</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:<command>:<pattern></tt> for date formatting using the {@link java.text.SimpleDateFormat} patterns.
+ * <li><tt>date:<command>:<pattern></tt> or <tt>date:<command>:<timezone>:<pattern></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 available.
* </li>
http://git-wip-us.apache.org/repos/asf/camel/blob/b52e6f76/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index 89c7ea8..d481642 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -155,13 +155,22 @@ public class SimpleFunctionExpression extends LiteralExpression {
// date: prefix
remainder = ifStartsWithReturnRemainder("date:", function);
if (remainder != null) {
- String[] parts = remainder.split(":");
- if (parts.length < 2) {
- throw new SimpleParserException("Valid syntax: ${date:command:pattern} was: " + function, token.getIndex());
+ String[] parts = remainder.split(":", 2);
+ if (parts.length == 1) {
+ return ExpressionBuilder.dateExpression(parts[0]);
+ } else if (parts.length == 2) {
+ return ExpressionBuilder.dateExpression(parts[0], parts[1]);
+ }
+ }
+
+ // date-with-timezone: prefix
+ remainder = ifStartsWithReturnRemainder("date-with-timezone:", function);
+ if (remainder != null) {
+ String[] parts = remainder.split(":", 3);
+ if (parts.length < 3) {
+ throw new SimpleParserException("Valid syntax: ${date:command:timezone:pattern} was: " + function, token.getIndex());
}
- String command = ObjectHelper.before(remainder, ":");
- String pattern = ObjectHelper.after(remainder, ":");
- return ExpressionBuilder.dateExpression(command, pattern);
+ return ExpressionBuilder.dateExpression(parts[0], parts[1], parts[2]);
}
// bean: prefix
http://git-wip-us.apache.org/repos/asf/camel/blob/b52e6f76/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
index 7bbd4ce..e7b1d22 100644
--- a/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
+++ b/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerFilterFileTest.java
@@ -27,6 +27,7 @@ import org.apache.camel.component.mock.MockEndpoint;
public class FileConsumerFilterFileTest extends ContextTestSupport {
private String fileUrl = "file://target/filefilter/?filterFile=${bodyAs(String)} contains 'World'";
+ private String fileUrl2 = "file://target/filefilter/?filterFile=${file:modified} < ${date:now-2s}";
@Override
protected void setUp() throws Exception {
@@ -38,8 +39,7 @@ public class FileConsumerFilterFileTest extends ContextTestSupport {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(0);
- template.sendBodyAndHeader("file:target/filefilter/", "This is a file to be filtered",
- Exchange.FILE_NAME, "skipme.txt");
+ template.sendBodyAndHeader("file:target/filefilter/", "This is a file to be filtered", Exchange.FILE_NAME, "skipme.txt");
mock.setResultWaitTime(2000);
mock.assertIsSatisfied();
@@ -50,11 +50,20 @@ public class FileConsumerFilterFileTest extends ContextTestSupport {
mock.expectedMessageCount(1);
mock.expectedBodiesReceived("Hello World");
- template.sendBodyAndHeader("file:target/filefilter/", "This is a file to be filtered",
- Exchange.FILE_NAME, "skipme.txt");
+ template.sendBodyAndHeader("file:target/filefilter/", "This is a file to be filtered", Exchange.FILE_NAME, "skipme2.txt");
+ template.sendBodyAndHeader("file:target/filefilter/", "Hello World", Exchange.FILE_NAME, "hello.txt");
- template.sendBodyAndHeader("file:target/filefilter/", "Hello World",
- Exchange.FILE_NAME, "hello.txt");
+ mock.assertIsSatisfied();
+ }
+
+ public void testFilterFilesWithDate() throws Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result2");
+ mock.expectedMessageCount(1);
+ mock.expectedBodiesReceived("Hello World");
+ mock.setResultMinimumWaitTime(1500);
+ mock.setResultWaitTime(5000);
+
+ template.sendBodyAndHeader("file:target/filefilter/", "Hello World", Exchange.FILE_NAME, "hello2.txt");
mock.assertIsSatisfied();
}
@@ -63,6 +72,7 @@ public class FileConsumerFilterFileTest extends ContextTestSupport {
return new RouteBuilder() {
public void configure() throws Exception {
from(fileUrl).convertBodyTo(String.class).to("mock:result");
+ from(fileUrl2).convertBodyTo(String.class).to("mock:result2");
}
};
}
http://git-wip-us.apache.org/repos/asf/camel/blob/b52e6f76/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 22a89c1..8181d67 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -24,6 +24,7 @@ import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.TimeZone;
import org.apache.camel.CamelAuthorizationException;
import org.apache.camel.CamelExecutionException;
@@ -38,7 +39,6 @@ import org.apache.camel.component.bean.MethodNotFoundException;
import org.apache.camel.impl.JndiRegistry;
import org.apache.camel.language.bean.RuntimeBeanExpressionException;
import org.apache.camel.language.simple.types.SimpleIllegalSyntaxException;
-import org.apache.camel.language.simple.types.SimpleParserException;
import org.apache.camel.spi.Language;
/**
@@ -463,14 +463,9 @@ public class SimpleTest extends LanguageTestSupport {
cal.set(1974, Calendar.APRIL, 20);
exchange.getIn().setHeader("birthday", cal.getTime());
+ assertExpression("date:header.birthday", cal.getTime());
assertExpression("date:header.birthday:yyyyMMdd", "19740420");
-
- try {
- assertExpression("date:yyyyMMdd", "19740420");
- fail("Should thrown an exception");
- } catch (SimpleParserException e) {
- assertEquals("Valid syntax: ${date:command:pattern} was: date:yyyyMMdd", e.getMessage());
- }
+ assertExpression("date:header.birthday+24h:yyyyMMdd", "19740421");
}
public void testDateAndTimeExpressions() throws Exception {
@@ -479,9 +474,26 @@ public class SimpleTest extends LanguageTestSupport {
cal.set(Calendar.MILLISECOND, 123);
exchange.getIn().setHeader("birthday", cal.getTime());
+ assertExpression("date:header.birthday - 10s:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T08:55:37:123");
assertExpression("date:header.birthday:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T08:55:47:123");
}
+ public void testDateWithTimezone() throws Exception {
+ Calendar cal = Calendar.getInstance();
+ cal.setTimeZone(TimeZone.getTimeZone("GMT+2"));
+ cal.set(1974, Calendar.APRIL, 20, 8, 55, 47);
+ cal.set(Calendar.MILLISECOND, 123);
+ exchange.getIn().setHeader("birthday", cal.getTime());
+
+ assertExpression("date:header.birthday:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T08:55:47:123");
+ assertExpression("date-with-timezone:header.birthday:UTC:yyyy-MM-dd'T'HH:mm:ss:SSS", "1974-04-20T06:55:47:123");
+ }
+
+ public void testDatePredicates() throws Exception {
+ assertPredicate("${date:now} < ${date:now+60s}");
+ assertPredicate("${date:now-2s+2s} == ${date:now}");
+ }
+
public void testLanguagesInContext() throws Exception {
// evaluate so we know there is 1 language in the context
assertExpression("id", exchange.getIn().getMessageId());
[2/4] camel git commit: CAMEL-10339: ...fixes suggested by Claus Ibsen
Posted by da...@apache.org.
CAMEL-10339: ...fixes suggested by Claus Ibsen
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a0fe445d
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a0fe445d
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a0fe445d
Branch: refs/heads/master
Commit: a0fe445d333cb5c1231685e6591c2913bfe8f9f3
Parents: b52e6f7
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Fri Sep 23 13:25:16 2016 +0300
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Sep 27 07:39:30 2016 +0200
----------------------------------------------------------------------
.../org/apache/camel/builder/ExpressionBuilder.java | 16 ++++++++++------
.../simple/ast/SimpleFunctionExpression.java | 2 +-
.../apache/camel/language/simple/SimpleTest.java | 7 +++++++
3 files changed, 18 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/a0fe445d/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
index 706746e..47f1085 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/ExpressionBuilder.java
@@ -81,6 +81,8 @@ import org.apache.camel.util.StringHelper;
*/
public final class ExpressionBuilder {
+ private static final Pattern OFFSET_PATTERN = Pattern.compile("([+-])([^+-]+)");
+
/**
* Utility classes should not have a public constructor.
*/
@@ -1868,11 +1870,11 @@ public final class ExpressionBuilder {
}
public static Expression dateExpression(final String command) {
- return dateExpression(command, TimeZone.getDefault().getID(), "");
+ return dateExpression(command, null, null);
}
public static Expression dateExpression(final String command, final String pattern) {
- return dateExpression(command, TimeZone.getDefault().getID(), pattern);
+ return dateExpression(command, null, pattern);
}
public static Expression dateExpression(final String commandWithOffsets, final String timezone, final String pattern) {
@@ -1881,13 +1883,13 @@ public final class ExpressionBuilder {
// Capture optional time offsets
String command = commandWithOffsets.split("[+-]", 2)[0].trim();
List<Long> offsets = new ArrayList<>();
- Matcher offsetMatcher = Pattern.compile("([+-])([^+-]+)").matcher(commandWithOffsets);
+ Matcher offsetMatcher = OFFSET_PATTERN.matcher(commandWithOffsets);
while (offsetMatcher.find()) {
try {
long value = exchange.getContext().getTypeConverter().mandatoryConvertTo(long.class, exchange, offsetMatcher.group(2).trim());
offsets.add(offsetMatcher.group(1).equals("+") ? value : -value);
} catch (NoTypeConversionAvailableException e) {
- e.printStackTrace();
+ throw ObjectHelper.wrapCamelExecutionException(exchange, e);
}
}
@@ -1927,9 +1929,11 @@ public final class ExpressionBuilder {
}
date = new Date(dateAsLong);
- if (null != pattern && !pattern.isEmpty()) {
+ if (pattern != null && !pattern.isEmpty()) {
SimpleDateFormat df = new SimpleDateFormat(pattern);
- df.setTimeZone(TimeZone.getTimeZone(timezone));
+ if (timezone != null && !timezone.isEmpty()) {
+ df.setTimeZone(TimeZone.getTimeZone(timezone));
+ }
return df.format(date);
} else {
return date;
http://git-wip-us.apache.org/repos/asf/camel/blob/a0fe445d/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
index d481642..8bd5e57 100644
--- a/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
+++ b/camel-core/src/main/java/org/apache/camel/language/simple/ast/SimpleFunctionExpression.java
@@ -168,7 +168,7 @@ public class SimpleFunctionExpression extends LiteralExpression {
if (remainder != null) {
String[] parts = remainder.split(":", 3);
if (parts.length < 3) {
- throw new SimpleParserException("Valid syntax: ${date:command:timezone:pattern} was: " + function, token.getIndex());
+ throw new SimpleParserException("Valid syntax: ${date-with-timezone:command:timezone:pattern} was: " + function, token.getIndex());
}
return ExpressionBuilder.dateExpression(parts[0], parts[1], parts[2]);
}
http://git-wip-us.apache.org/repos/asf/camel/blob/a0fe445d/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
index 8181d67..5c79ce7 100644
--- a/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
+++ b/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTest.java
@@ -466,6 +466,13 @@ public class SimpleTest extends LanguageTestSupport {
assertExpression("date:header.birthday", cal.getTime());
assertExpression("date:header.birthday:yyyyMMdd", "19740420");
assertExpression("date:header.birthday+24h:yyyyMMdd", "19740421");
+
+ try {
+ assertExpression("date:yyyyMMdd", "19740420");
+ fail("Should thrown an exception");
+ } catch (IllegalArgumentException e) {
+ assertEquals("Command not supported for dateExpression: yyyyMMdd", e.getMessage());
+ }
}
public void testDateAndTimeExpressions() throws Exception {