diff --git a/core/formula/src/main/java/org/csstudio/apputil/formula/string/StringConcatFunction.java b/core/formula/src/main/java/org/csstudio/apputil/formula/string/StringConcatFunction.java index 9b496c673d..11a4367994 100644 --- a/core/formula/src/main/java/org/csstudio/apputil/formula/string/StringConcatFunction.java +++ b/core/formula/src/main/java/org/csstudio/apputil/formula/string/StringConcatFunction.java @@ -3,12 +3,7 @@ import org.csstudio.apputil.formula.spi.FormulaFunction; import org.epics.util.array.ListNumber; -import org.epics.vtype.Alarm; -import org.epics.vtype.Time; -import org.epics.vtype.VNumberArray; -import org.epics.vtype.VString; -import org.epics.vtype.VStringArray; -import org.epics.vtype.VType; +import org.epics.vtype.*; import java.util.ArrayList; import java.util.Arrays; @@ -56,6 +51,9 @@ public VType compute(VType... args) throws Exception { } else if (isString(arg)) { stringBuilder.append(((VString)arg).getValue()); + } else if (isEnum(arg)) + { + stringBuilder.append(((VEnum)arg).getValue()); } }); return VString.of(stringBuilder.toString(), Alarm.none(), Time.now()); @@ -63,12 +61,12 @@ public VType compute(VType... args) throws Exception { /** * Returns true is the value is a StringArray or can be converted to a StringArray - * @param value + * @param value * @return boolean true if value can be used as a string array */ private boolean isStringArray(VType value) { - return value instanceof VStringArray + return value instanceof VStringArray || value instanceof VNumberArray; } @@ -93,4 +91,9 @@ private boolean isString(VType value) { return value instanceof VString; } + + private boolean isEnum(VType value) + { + return value instanceof VEnum; + } } diff --git a/core/formula/src/test/java/org/csstudio/apputil/formula/string/StringConcatFunctionTest.java b/core/formula/src/test/java/org/csstudio/apputil/formula/string/StringConcatFunctionTest.java new file mode 100644 index 0000000000..1fce7e2dd2 --- /dev/null +++ b/core/formula/src/test/java/org/csstudio/apputil/formula/string/StringConcatFunctionTest.java @@ -0,0 +1,69 @@ +package org.csstudio.apputil.formula.string; + +import org.epics.util.array.ArrayDouble; +import org.epics.vtype.*; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class StringConcatFunctionTest { + + @Test + void concatStrings() throws Exception { + StringConcatFunction concatFunction = new StringConcatFunction(); + + VString a = VString.of("a", Alarm.none(), Time.now()); + VString b = VString.of("b", Alarm.none(), Time.now()); + VString c = VString.of("c", Alarm.none(), Time.now()); + + VString res = (VString) concatFunction.compute(a,b,c); + assertEquals("abc", res.getValue()); + } + + @Test + void concatStringArray() throws Exception { + StringConcatFunction concatFunction = new StringConcatFunction(); + + VType array = VStringArray.of(Arrays.asList("a", "b", "c"), Alarm.none(), Time.now()); + + VString res = (VString) concatFunction.compute(array); + assertEquals("abc", res.getValue()); + } + + @Test + void concatDoubleArray() throws Exception { + StringConcatFunction concatFunction = new StringConcatFunction(); + + VType array = VNumberArray.of(ArrayDouble.of(1.0, 2.0, 3.0), Alarm.none(), Time.now(), Display.none()); + + VString res = (VString) concatFunction.compute(array); + assertEquals("1.02.03.0", res.getValue()); + } + + @Test + void concatInvalidVType() throws Exception { + StringConcatFunction concatFunction = new StringConcatFunction(); + + VType num1 = VNumber.of(1.0, Alarm.none(), Time.now(), Display.none()); + VType num2 = VNumber.of(2.0, Alarm.none(), Time.now(), Display.none()); + VType num3 = VNumber.of(3.0, Alarm.none(), Time.now(), Display.none()); + + VString res = (VString) concatFunction.compute(num1, num2, num3); + // Will not attempt to concat and will return empty string + assertEquals("", res.getValue()); + } + + @Test + void concatEnums() throws Exception { + StringConcatFunction concatFunction = new StringConcatFunction(); + + VEnum enum1 = VEnum.of(0, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now()); + VEnum enum2 = VEnum.of(1, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now()); + VEnum enum3 = VEnum.of(2, EnumDisplay.of("a", "b", "c"), Alarm.none(), Time.now()); + + VString res = (VString) concatFunction.compute(enum1, enum2, enum3); + assertEquals("abc", res.getValue()); + } +}