Repository: falcon Updated Branches: refs/heads/master d5212d515 -> c3f681711
FALCON-1025 lastWeek and CurrentWeek functions are getting evaluated incorrect. Contributed by pavan kumar kolamuri Project: http://git-wip-us.apache.org/repos/asf/falcon/repo Commit: http://git-wip-us.apache.org/repos/asf/falcon/commit/c3f68171 Tree: http://git-wip-us.apache.org/repos/asf/falcon/tree/c3f68171 Diff: http://git-wip-us.apache.org/repos/asf/falcon/diff/c3f68171 Branch: refs/heads/master Commit: c3f6817119491a11cfbdc285e1e333c07eca4092 Parents: d5212d5 Author: Suhas Vasu <[email protected]> Authored: Tue Mar 10 12:34:12 2015 +0530 Committer: Suhas Vasu <[email protected]> Committed: Tue Mar 10 12:34:12 2015 +0530 ---------------------------------------------------------------------- CHANGES.txt | 3 + .../falcon/expression/ExpressionHelper.java | 15 +++- .../falcon/expression/ExpressionHelperTest.java | 84 ++++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/falcon/blob/c3f68171/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 3b9b243..69528e3 100755 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -101,6 +101,9 @@ Trunk (Unreleased) (Suhas vasu) BUG FIXES + FALCON-1025 lastWeek and CurrentWeek functions are getting + evaluated incorrect (pavan kumar kolamuri via Suhas Vasu) + FALCON-1020 validate command produces different results when run via prism and server (pavan kumar kolamuri via Suhas Vasu) http://git-wip-us.apache.org/repos/asf/falcon/blob/c3f68171/common/src/main/java/org/apache/falcon/expression/ExpressionHelper.java ---------------------------------------------------------------------- diff --git a/common/src/main/java/org/apache/falcon/expression/ExpressionHelper.java b/common/src/main/java/org/apache/falcon/expression/ExpressionHelper.java index e04f046..33ec59c 100644 --- a/common/src/main/java/org/apache/falcon/expression/ExpressionHelper.java +++ b/common/src/main/java/org/apache/falcon/expression/ExpressionHelper.java @@ -27,6 +27,7 @@ import javax.servlet.jsp.el.ExpressionEvaluator; import javax.servlet.jsp.el.FunctionMapper; import javax.servlet.jsp.el.VariableResolver; import java.lang.reflect.Method; +import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; @@ -49,6 +50,15 @@ public final class ExpressionHelper implements FunctionMapper, VariableResolver private static final ExpressionEvaluator EVALUATOR = new ExpressionEvaluatorImpl(); private static final ExpressionHelper RESOLVER = ExpressionHelper.get(); + public static final ThreadLocal<SimpleDateFormat> FORMATTER = new ThreadLocal<SimpleDateFormat>() { + @Override + protected SimpleDateFormat initialValue() { + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); + format.setTimeZone(TimeZone.getTimeZone("UTC")); + return format; + } + }; + public static ExpressionHelper get() { return INSTANCE; } @@ -113,6 +123,7 @@ public final class ExpressionHelper implements FunctionMapper, VariableResolver private static int getDayOffset(String weekDayName) { int day; Calendar nominalTime = Calendar.getInstance(); + nominalTime.setTime(referenceDate.get()); int currentWeekDay = nominalTime.get(Calendar.DAY_OF_WEEK); int weekDay = DayOfWeek.valueOf(weekDayName).ordinal() + 1; //to map to Calendar.SUNDAY ... day = weekDay - currentWeekDay; @@ -174,12 +185,12 @@ public final class ExpressionHelper implements FunctionMapper, VariableResolver public static Date currentWeek(String weekDay, int hour, int minute) { int day = getDayOffset(weekDay); - return getRelative(referenceDate.get(), Calendar.MONTH, 0, day, hour, minute); + return getRelative(referenceDate.get(), Calendar.DAY_OF_MONTH, 0, day, hour, minute); } public static Date lastWeek(String weekDay, int hour, int minute) { int day = getDayOffset(weekDay); - return getRelative(referenceDate.get(), Calendar.MONTH, -1, day, hour, minute); + return getRelative(referenceDate.get(), Calendar.DAY_OF_MONTH, 0, day - 7, hour, minute); } public static Date currentYear(int month, int day, int hour, int minute) { http://git-wip-us.apache.org/repos/asf/falcon/blob/c3f68171/common/src/test/java/org/apache/falcon/expression/ExpressionHelperTest.java ---------------------------------------------------------------------- diff --git a/common/src/test/java/org/apache/falcon/expression/ExpressionHelperTest.java b/common/src/test/java/org/apache/falcon/expression/ExpressionHelperTest.java new file mode 100644 index 0000000..da5dbca --- /dev/null +++ b/common/src/test/java/org/apache/falcon/expression/ExpressionHelperTest.java @@ -0,0 +1,84 @@ +/** + * 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.falcon.expression; + +import org.apache.falcon.FalconException; +import org.testng.Assert; +import org.testng.annotations.BeforeTest; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Test; + +import java.text.ParseException; +import java.util.Date; + +/** + * Unit test cases for EL Expressions. + */ +public class ExpressionHelperTest { + + private ExpressionHelper expressionHelper = ExpressionHelper.get(); + + @BeforeTest + public void init() throws ParseException { + Date referenceDate = ExpressionHelper.FORMATTER.get().parse("2015-02-01T00:00Z"); + expressionHelper.setReferenceDate(referenceDate); + } + + @Test(dataProvider = "ElExpressions") + public void testStartOffset(String expression, String expectedDateStr) throws FalconException { + Date evalDate = expressionHelper.evaluate(expression, Date.class); + String evalDateStr = ExpressionHelper.FORMATTER.get().format(evalDate); + Assert.assertEquals(evalDateStr, expectedDateStr); + } + + + @DataProvider(name = "ElExpressions") + public Object[][] createOffsets() { + return new Object[][] { + {"now(-10,-30)", "2015-01-31T13:30Z"}, + {"now(10,-30)", "2015-02-01T09:30Z"}, + + {"today(0,0)", "2015-02-01T00:00Z"}, + {"today(-1,0)", "2015-01-31T23:00Z"}, + {"yesterday(0,0)", "2015-01-31T00:00Z"}, + {"yesterday(-1,0)", "2015-01-30T23:00Z"}, + {"yesterday(1,30)", "2015-01-31T01:30Z"}, + + {"currentMonth(2,0,0)", "2015-02-03T00:00Z"}, + {"currentMonth(-2,1,30)", "2015-01-30T01:30Z"}, + {"lastMonth(3,0,0)", "2015-01-04T00:00Z"}, + {"lastMonth(-3,0,0)", "2014-12-29T00:00Z"}, + + {"currentWeek('THU',0,0)", "2015-01-29T00:00Z"}, + {"currentWeek('SUN',0,0)", "2015-02-01T00:00Z"}, + {"lastWeek('THU',0,0)", "2015-01-22T00:00Z"}, + {"lastWeek('SUN',0,0)", "2015-01-25T00:00Z"}, + + {"currentYear(1,1,0,0)", "2015-02-02T00:00Z"}, + {"currentYear(-1,1,0,0)", "2014-12-02T00:00Z"}, + {"lastYear(1,1,0,0)", "2014-02-02T00:00Z"}, + {"lastYear(-1,1,0,0)", "2013-12-02T00:00Z"}, + + // latest and future will return the reference time + {"latest(0)", "2015-02-01T00:00Z"}, + {"latest(-1)", "2015-02-01T00:00Z"}, + {"future(0,0)", "2015-02-01T00:00Z"}, + {"future(1,0)", "2015-02-01T00:00Z"}, + }; + } +}
