1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedWriter;
23 import java.io.FileOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.context.Context;
34 import org.apache.velocity.runtime.RuntimeConstants;
35
36
37
38
39
40
41
42 public class SetTestCase extends BaseTestCase
43 {
44
45
46
47 private static final String TMPL_FILE_EXT = "vm";
48
49
50
51
52 private static final String CMP_FILE_EXT = "cmp";
53
54
55
56
57 private static final String RESULT_FILE_EXT = "res";
58
59
60
61
62
63 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/set";
64
65
66
67
68 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/set";
69
70
71
72
73 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/set/compare";
74
75
76
77
78 public SetTestCase(String name)
79 {
80 super(name);
81 }
82
83 public void setUp()
84 throws Exception
85 {
86 assureResultsDirectoryExists(RESULTS_DIR);
87 }
88
89 public static Test suite ()
90 {
91 return new TestSuite(SetTestCase.class);
92 }
93
94
95
96
97 public void testSetNull()
98 throws Exception
99 {
100
101
102
103
104 VelocityEngine ve = new VelocityEngine();
105 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
106 ve.init();
107
108 checkTemplate(ve,"set1");
109
110
111
112
113 ve = new VelocityEngine();
114 ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "false");
115 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
116 ve.init();
117
118 checkTemplate(ve,"set1");
119
120
121
122
123 ve = new VelocityEngine();
124 ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "true");
125 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
126 ve.init();
127
128 checkTemplate(ve,"set2");
129 }
130
131 public void checkTemplate(VelocityEngine ve, String templateName)
132 throws Exception
133 {
134 Template template;
135 FileOutputStream fos;
136 Writer fwriter;
137 Context context;
138
139 template = ve.getTemplate( getFileName(null, templateName, TMPL_FILE_EXT) );
140
141 fos = new FileOutputStream (
142 getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
143
144 fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
145
146 context = new VelocityContext();
147 template.merge(context, fwriter);
148 fwriter.flush();
149 fwriter.close();
150
151 if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
152 {
153 fail("Output incorrect.");
154 }
155 }
156
157 }
158