未验证 提交 9a9e980d 编写于 作者: R Rubén Barroso 提交者: GitHub

JUEL date functions now return date data type instead of formatted string (#3742)

上级 32534836
......@@ -15,41 +15,24 @@
*/
package org.activiti.core.el;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class DateResolverHelper {
private static final String ISO_8601_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
private static final String DATE_FORMAT = "yyyy-MM-dd";
private static final String DATE_FUNCTION_NAME = "today";
private static final String DATE_INVOKE_METHOD = "today";
private static final String DATETIME_FUNCTION_NAME = "current";
private static final String DATETIME_INVOKE_METHOD = "current";
private static final String NOW_FUNCTION_NAME = "now";
private static final String NOW_INVOKE_METHOD = "now";
private static final TimeZone TIME_ZONE = TimeZone.getTimeZone("UTC");
public static final String today() {
return parseNow(DATE_FORMAT);
}
public static final String current() {
return parseNow(ISO_8601_FORMAT);
public static final Date now() {
return new Date();
}
private DateResolverHelper() {
}
private static String parseNow(String format) {
DateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setTimeZone(TIME_ZONE);
return dateFormat.format(new Date());
}
public static void addDateFunctions(ActivitiElContext elContext) throws NoSuchMethodException {
elContext.setFunction("", DATE_FUNCTION_NAME, DateResolverHelper.class.getMethod(DATE_INVOKE_METHOD));
elContext.setFunction("", DATETIME_FUNCTION_NAME, DateResolverHelper.class.getMethod(DATETIME_INVOKE_METHOD));
elContext.setFunction("", NOW_FUNCTION_NAME, DateResolverHelper.class.getMethod(NOW_INVOKE_METHOD));
}
}
......@@ -19,9 +19,10 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
import java.util.Collections;
import java.util.Date;
import java.util.Map;
import javax.el.ELException;
import javax.el.PropertyNotFoundException;
import org.hamcrest.MatcherAssert;
import org.junit.Test;
......@@ -96,33 +97,28 @@ public class JuelResolverTest {
}
@Test
public void should_returnDate_when_expressionIsTodayFunction() {
public void should_returnDate_when_expressionIsNowFunction() {
//given
String expressionString = "${today()}";
String expressionString = "${now()}";
ExpressionResolver expressionResolver = new JuelExpressionResolver();
//when
String value = expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), String.class);
Date value = expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), Date.class);
//then
MatcherAssert.assertThat(value, is(notNullValue()));
MatcherAssert.assertThat(value, matchesPattern("([0-9]{4})\\-([0-9]{2})\\-([0-9]{2})"));
}
@Test
public void should_returnDate_when_expressionIsCurrentFunction() {
public void should_throwException_when_unknownFunctionIsReferenced() {
//given
String expressionString = "${current()}";
ExpressionResolver expressionResolver = new JuelExpressionResolver();
//when
String value = expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), String.class);
//then
MatcherAssert.assertThat(value, is(notNullValue()));
MatcherAssert.assertThat(value, matchesPattern(
"^(?:[1-9]\\d{3}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1\\d|2[0-8])|(?:0[13-9]|1[0-2])-"
+ "(?:29|30)|(?:0[13578]|1[02])-31)|(?:[1-9]\\d(?:0[48]|[2468][048]|[13579][26])|"
+ "(?:[2468][048]|[13579][26])00)-02-29)T(?:[01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d(?:\\.\\d{1,9})?(?:Z|[+-][01]\\d:[0-5]\\d)$"));
assertThatExceptionOfType(ELException.class)
.as("Referencing an unknown function")
.isThrownBy(() -> expressionResolver.resolveExpression(expressionString, Collections.emptyMap(), Date.class))
.withMessage("Could not resolve function 'current'");
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册