1 package com.freemindcafe.apache.velocity.sample1;
3 import java.io.StringWriter;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
10 import org.
apache.velocity.VelocityContext;
11 import org.
apache.velocity.app.VelocityEngine;
12 import org.junit.Assert;
18 VelocityEngine velocityEngineTest=
new VelocityEngine();
19 Map<String, String> valTypes =
new HashMap<String, String>();
20 valTypes.put(
"hasNewData",
"Y");
21 valTypes.put(
"hasMissingData",
"N");
23 List<String> expressions = Arrays.asList(
"$hasNewData == 'Y' || $hasMissingData == 'Y'",
"true",
"false",
"$hasNewData == 'Y' && $hasMissingData == 'Y'");
24 List<String> expectedResults = Arrays.asList(
"true",
"true" ,
"false",
"false");
25 List<String> actualResults =
new ArrayList<String>();
27 String templateExpression =
"#if(%replace_this%)true #else false #end";
28 expressions.forEach((expression)->{
29 VelocityContext context =
new VelocityContext();
30 valTypes.forEach((valType,valid) -> {context.put(valType, valid);});
31 StringWriter writer =
new StringWriter();
33 velocityEngineTest.evaluate(context, writer,
"", templateExpression.replace(
"%replace_this%", expression));
34 }
catch (Exception e) {
38 actualResults.add(writer.toString().trim());
40 Assert.assertTrue(actualResults.equals(expectedResults));