Samples JDK
Test.java
1 package com.freemindcafe.apache.velocity.sample1;
2 
3 import java.io.StringWriter;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.HashMap;
7 import java.util.List;
8 import java.util.Map;
9 
10 import org.apache.velocity.VelocityContext;
11 import org.apache.velocity.app.VelocityEngine;
12 import org.junit.Assert;
13 
14 public class Test {
15 
16  @org.junit.Test
17  public void test1() {
18  VelocityEngine velocityEngineTest=new VelocityEngine();
19  Map<String, String> valTypes = new HashMap<String, String>();
20  valTypes.put("hasNewData", "Y");
21  valTypes.put("hasMissingData", "N");
22  //valTypes.put("customValType", "Y");
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>();
26 
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();
32  try {
33  velocityEngineTest.evaluate(context, writer, "", templateExpression.replace("%replace_this%", expression));
34  } catch (Exception e) {
35  // TODO Auto-generated catch block
36  e.printStackTrace();
37  }
38  actualResults.add(writer.toString().trim());
39  });
40  Assert.assertTrue(actualResults.equals(expectedResults));
41 
42  }
43 
44 
45 }