diff --git a/modules/siddhi-core/src/main/java/io/siddhi/core/util/parser/ExpressionParser.java b/modules/siddhi-core/src/main/java/io/siddhi/core/util/parser/ExpressionParser.java index eceb169731..2764b4ed6e 100644 --- a/modules/siddhi-core/src/main/java/io/siddhi/core/util/parser/ExpressionParser.java +++ b/modules/siddhi-core/src/main/java/io/siddhi/core/util/parser/ExpressionParser.java @@ -1014,7 +1014,7 @@ private static ConditionExpressionExecutor parseEqualCompare(ExpressionExecutor rightExpressionExecutor); default: throw new OperationNotSupportedException( - "sting cannot be compared with " + rightExpressionExecutor.getReturnType()); + "string cannot be compared with " + rightExpressionExecutor.getReturnType()); } case INT: switch (rightExpressionExecutor.getReturnType()) { @@ -1137,7 +1137,7 @@ private static ConditionExpressionExecutor parseNotEqualCompare(ExpressionExecut rightExpressionExecutor); default: throw new OperationNotSupportedException( - "sting cannot be compared with " + rightExpressionExecutor.getReturnType()); + "string cannot be compared with " + rightExpressionExecutor.getReturnType()); } case INT: switch (rightExpressionExecutor.getReturnType()) { diff --git a/modules/siddhi-query-compiler/pom.xml b/modules/siddhi-query-compiler/pom.xml index 75a10c5371..da98f47e47 100644 --- a/modules/siddhi-query-compiler/pom.xml +++ b/modules/siddhi-query-compiler/pom.xml @@ -43,6 +43,10 @@ mvel2 compile + + org.apache.commons + commons-lang3 + org.testng testng diff --git a/modules/siddhi-query-compiler/src/main/antlr4/io/siddhi/query/compiler/SiddhiQL.g4 b/modules/siddhi-query-compiler/src/main/antlr4/io/siddhi/query/compiler/SiddhiQL.g4 index 685370e37a..7c86ddef93 100644 --- a/modules/siddhi-query-compiler/src/main/antlr4/io/siddhi/query/compiler/SiddhiQL.g4 +++ b/modules/siddhi-query-compiler/src/main/antlr4/io/siddhi/query/compiler/SiddhiQL.g4 @@ -19,7 +19,7 @@ grammar SiddhiQL; @header { - //import io.siddhi.query.compiler.exception.SiddhiParserException; +import org.apache.commons.lang3.StringEscapeUtils; } parse @@ -865,9 +865,8 @@ STRING_LITERAL '\'' ( ~('\u0000'..'\u001f' | '\''| '"' ) )* '\'' |'"' ( ~('\u0000'..'\u001f' |'"') )* '"' ) {setText(getText().substring(1, getText().length()-1));} - |('"""'(.*?)'"""') {setText(getText().substring(3, getText().length()-3));} + |('"""'(~["\\\r\n] | EscapeSequence)*'"""') {setText(StringEscapeUtils.unescapeJava(getText().substring(3, getText().length()-3)));} ; - //Hidden channels SINGLE_LINE_COMMENT : '--' ~[\r\n]* -> channel(HIDDEN) @@ -889,6 +888,19 @@ SCRIPT : '{' SCRIPT_ATOM* '}' ; +fragment EscapeSequence + : '\\' 'u005c'? [btnfr"'\\] + | '\\' 'u005c'? ([0-3]? [0-7])? [0-7] + | '\\' 'u'+ HexDigit HexDigit HexDigit HexDigit + ; +fragment HexDigits + : HexDigit ((HexDigit | '_')* HexDigit)? + ; + +fragment HexDigit + : [0-9a-fA-F] + ; + fragment SCRIPT_ATOM : ~[{}] | '"' ~["]* '"' diff --git a/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/DefineStreamTestCase.java b/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/DefineStreamTestCase.java index bfb3a2c885..0c433547e0 100644 --- a/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/DefineStreamTestCase.java +++ b/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/DefineStreamTestCase.java @@ -175,7 +175,7 @@ public void testMultilevelNestedAnnotations3() throws SiddhiParserException { StreamDefinition streamDefinition = SiddhiCompiler.parseStreamDefinition( "@sink(url='http://foo.com/test/{{data}}', " + " @map(type=\"\"\"xml\"\"\", " + - "@payload(\"\"\"{ \"time\":{{time}}}\"\"\") " + + "@payload(\"\"\"{ \\\"time\\\":{{time}}}\"\"\") " + " )" + ") " + "define stream fooStream (id int, name string);" diff --git a/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/StringValueTestCase.java b/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/StringValueTestCase.java new file mode 100644 index 0000000000..05ae18cf1d --- /dev/null +++ b/modules/siddhi-query-compiler/src/test/java/io/siddhi/query/test/StringValueTestCase.java @@ -0,0 +1,201 @@ +package io.siddhi.query.test; + + +import io.siddhi.query.api.execution.query.Query; +import io.siddhi.query.api.expression.constant.StringConstant; +import io.siddhi.query.compiler.SiddhiCompiler; +import io.siddhi.query.compiler.exception.SiddhiParserException; +import org.apache.commons.lang3.StringEscapeUtils; +import org.testng.AssertJUnit; +import org.testng.annotations.Test; + +public class StringValueTestCase { + @Test + void test1() { + String literal = "abc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test + void test2() { + String literal = "a'bc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test3() { + String literal = "a\"bc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + } + + @Test + void test4() { + String literal = "a\"bc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, + StringEscapeUtils.escapeJava(literal))); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test5() { + String literal = "abc\""; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + } + + @Test + void test6() { + String literal = "abc\""; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, + StringEscapeUtils.escapeJava(literal))); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test7() { + String literal = "a\"\"\"bc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + } + + @Test + void test8() { + String literal = "a\"\"\"bc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, + StringEscapeUtils.escapeJava(literal))); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test + void test9() { + String literal = "a\\u005cbc"; + String queryStringFormat = "from a select \"\"\"%s\"\"\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals("a\\bc", + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test + void test10() { + String literal = "abc"; + String queryStringFormat = "from a select \"%s\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test + void test11() { + String literal = "abc"; + String queryStringFormat = "from a select '%s' as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test + void test12() { + String literal = "a'bc"; + String queryStringFormat = "from a select \"%s\" as b insert into c"; + Query query = SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + AssertJUnit.assertNotNull(query); + AssertJUnit.assertNotNull(query.getSelector()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList()); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0)); + AssertJUnit.assertNotNull(query.getSelector().getSelectionList().get(0).getExpression()); + AssertJUnit.assertTrue(query.getSelector().getSelectionList().get(0).getExpression() instanceof StringConstant); + AssertJUnit.assertEquals(literal, + ((StringConstant) query.getSelector().getSelectionList().get(0).getExpression()).getValue()); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test13() { + String literal = "a'bc"; + String queryStringFormat = "from a select '%s' as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test14() { + String literal = "a'bc"; + String queryStringFormat = "from a select '%s' as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, StringEscapeUtils.escapeJava(literal))); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test15() { + String literal = "a\"bc"; + String queryStringFormat = "from a select \"%s\" as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, literal)); + } + + @Test(expectedExceptions = SiddhiParserException.class) + void test16() { + String literal = "a\"bc"; + String queryStringFormat = "from a select \"%s\" as b insert into c"; + SiddhiCompiler.parseQuery(String.format(queryStringFormat, StringEscapeUtils.escapeJava(literal))); + } + + +}