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

Allow expressions in process variables (#3745)

* Allow expressions in process variables

* Expressions in process variables minor fixes
上级 9a9e980d
......@@ -16,7 +16,6 @@
package org.activiti.spring.process.variable.types;
import java.util.List;
import org.activiti.common.util.DateFormatterProvider;
import org.activiti.engine.ActivitiException;
......@@ -41,6 +40,9 @@ public class DateVariableType extends JavaObjectVariableType {
public Object parseFromValue(Object value) throws ActivitiException {
try {
if (isExpression(value)) {
return value;
}
return dateFormatterProvider.toDate(value);
} catch (Exception e) {
throw new ActivitiException("Error parsing date value " + value, e);
......
......@@ -42,9 +42,9 @@ public class JavaObjectVariableType extends VariableType {
}
@Override
public void validate(Object var,List<ActivitiException> errors) {
public void validate(Object var, List<ActivitiException> errors) {
if (var != null && !(var).getClass().isAssignableFrom(clazz)) {
if (var != null && !(var).getClass().isAssignableFrom(clazz) && !isExpression(var)) {
String message = var.getClass() + " is not assignable from " + clazz;
errors.add(new ActivitiException(message));
logger.error(message);
......
......@@ -16,15 +16,16 @@
package org.activiti.spring.process.variable.types;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import org.activiti.engine.ActivitiException;
/**
* Base variable type for types as defined in extension json files.
* Used to validate variables against definition.
* Base variable type for types as defined in extension json files. Used to validate variables against definition.
*/
public abstract class VariableType {
private static final Pattern EXPRESSION_PATTERN = Pattern.compile("^\\$\\{(.|\\n)*[\\}]$");
private String name;
public String getName() {
......@@ -40,4 +41,8 @@ public abstract class VariableType {
public Object parseFromValue(Object value) throws ActivitiException {
return value;
}
protected boolean isExpression(Object var) {
return Objects.nonNull(var) && var.getClass().isAssignableFrom(String.class) && EXPRESSION_PATTERN.matcher((CharSequence) var).matches();
}
}
/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* Licensed 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.activiti.spring.process.variable.types;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.activiti.common.util.DateFormatterProvider;
import org.activiti.engine.ActivitiException;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class DateVariableTypeTest {
private DateFormatterProvider provider = new DateFormatterProvider("yyyy-MM-dd[['T']HH:mm:ss[.SSS'Z']]");
private List<ActivitiException> exceptionList;
DateVariableType dateVariableType;
@BeforeEach
public void setUp() {
dateVariableType = new DateVariableType(Date.class, provider);
exceptionList = new ArrayList<>();
}
@Test
public void should_returnDate_when_parseValidString() {
Object result = dateVariableType.parseFromValue("1985-10-26T01:22:00.001Z");
assertTrue(result.getClass().getName().equals(Date.class.getName()));
}
@Test
public void should_throwException_when_parseInvalidString() {
Throwable thrown = catchThrowable(() -> dateVariableType.parseFromValue("${now()"));
Assertions.assertThat(thrown)
.isInstanceOf(ActivitiException.class)
.hasMessage("Error parsing date value ${now()");
}
@Test
public void should_returnExpressionString_when_parseValidExpression() {
String expression = "${now()}";
Object result = dateVariableType.parseFromValue(expression);
assertTrue(result.equals(expression));
}
}
/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* Licensed 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.activiti.spring.process.variable.types;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.activiti.engine.ActivitiException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class JavaObjectVariableTypeTest {
private List<ActivitiException> exceptionList;
JavaObjectVariableType javaObjectVariableType;
@BeforeEach
public void setUp() {
javaObjectVariableType = new JavaObjectVariableType(Boolean.class);
exceptionList = new ArrayList<>();
}
@Test
public void should_returnException_when_validateValueNotAssignableToClass() {
javaObjectVariableType.validate(1, exceptionList);
assertTrue(exceptionList.stream().anyMatch(error ->
error.getMessage().equals("class java.lang.Integer is not assignable from class java.lang.Boolean")
));
}
@Test
public void should_returnEmptyErrorList_when_validateValueAssignableToClass() {
javaObjectVariableType.validate(true, exceptionList);
assertTrue(exceptionList.isEmpty());
}
@Test
public void should_returnEmptyErrorList_when_validateValidExpression() {
javaObjectVariableType.validate("${now()}", exceptionList);
assertTrue(exceptionList.isEmpty());
}
@Test
public void should_returnException_when_validateIncompleteExpression() {
javaObjectVariableType.validate("${now()", exceptionList);
assertTrue(exceptionList.stream().anyMatch(error ->
error.getMessage().equals("class java.lang.String is not assignable from class java.lang.Boolean")
));
}
}
/*
* Copyright 2010-2020 Alfresco Software, Ltd.
*
* Licensed 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.activiti.spring.process.variable.types;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.activiti.engine.ActivitiException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
class JsonObjectVariableTypeTest {
private List<ActivitiException> exceptionList;
JsonObjectVariableType jsonObjectVariableType;
@BeforeEach
public void setUp() {
jsonObjectVariableType = new JsonObjectVariableType(new ObjectMapper());
exceptionList = new ArrayList<>();
}
@Test
public void should_returnException_when_validateValueNotAssignableToClass() {
jsonObjectVariableType.validate("${now()}", exceptionList);
assertTrue(exceptionList.isEmpty());
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册