WithStatement implements NullSafety { static public WithStatement parse(String statement, String templatePath) throws TokenException { boolean nullSafe = false; if (statement.startsWith("?")) { nullSafe = true; statement = statement.substring(1).trim(); } if (!statement.startsWith("(")) { throw new TokenException("With block does not start with parenthese"); } if (!statement.endsWith(")")) { throw new TokenException("With block does not end with parenthese"); } statement = statement.substring(1, statement.length() - 1); final List<VariableWithExpression> variables = new ArrayList<>(); final List<String> withStatements = parseWithStatement(statement, templatePath); for(int i = 0; i < withStatements.size(); i++) { final String withStatement = withStatements.get(i); final int equalsPos = withStatement.indexOf('='); if (equalsPos < 0) { throw new TokenException("With block invalid: no equals symbol found for assignment: " + withStatement); } if (withStatement.indexOf('=', equalsPos+1) > 0) { throw new TokenException("With block invalid: multiple equals symbols found for assignment " + withStatement); } final String varPart = withStatement.substring(0, equalsPos); final JavaVariable variable = JavaVariable.parse(varPart); final String valueExpression = withStatement.substring(equalsPos+1).trim(); if (valueExpression.equals("")) { throw new TokenException("With block contains an empty string for value part (e.g. var = value)"); } variables.add(new VariableWithExpression(variable, valueExpression)); } if(nullSafe && variables.size() > 1) { throw new TokenException("Nullsafe option not allowed for with block with multiple arguments"); } return new WithStatement(variables, nullSafe); } WithStatement(List<VariableWithExpression> variables);  WithStatement(List<VariableWithExpression> variables, boolean nullSafe); List<VariableWithExpression> getVariables(); boolean hasAnyVariableNullType(); @Override boolean isNullSafe(); static WithStatement parse(String statement, String templatePath);  }
@Test public void endsWithComma() throws Exception { try { WithStatement.parse(("(String s = \"test\",)"), "template-path"); fail("Expected exception"); } catch(ParserException e) { assertThat(e.getMessage(), containsString("template-path:[1,18] missing ARGUMENT at '<EOF>'")); } }
@Test public void notStartOrEndWithParenthese() throws Exception { try { WithStatement.parse("String s = \"test\"", "template-path"); fail("Expected exception"); } catch (TokenException e) { assertThat(e.getMessage(), containsString("start with parenthese")); } try { WithStatement.parse("(String s = \"test\"", "template-path"); fail("Expected exception"); } catch (TokenException e) { assertThat(e.getMessage(), containsString("end with parenthese")); } }
@Test public void noEqualsChar() throws Exception { try { WithStatement.parse("(String s -> \"test\")", "template-path"); fail("Expected exception"); } catch (TokenException e) { assertThat(e.getMessage(), containsString("no equals symbol")); } }
@Test public void multipleEqualsChar() throws Exception { try { WithStatement.parse("(String s = \"test\"; String t = \"test\")", "template-path"); fail("Expected exception"); } catch (TokenException e) { assertThat(e.getMessage(), containsString("multiple equals symbol")); } }