1 package org.apache.velocity.test.issues;
2
3
4
5
6
7
8
9
10
11
12
13 import java.io.BufferedWriter;
14 import java.io.FileOutputStream;
15 import java.io.OutputStreamWriter;
16 import java.io.StringWriter;
17 import java.io.Writer;
18
19 import junit.framework.Test;
20 import junit.framework.TestSuite;
21
22 import org.apache.velocity.Template;
23 import org.apache.velocity.VelocityContext;
24 import org.apache.velocity.app.VelocityEngine;
25 import org.apache.velocity.runtime.RuntimeConstants;
26 import org.apache.velocity.test.BaseTestCase;
27 import org.apache.velocity.test.misc.TestLogChute;
28
29
30
31
32 public class Velocity580TestCase extends BaseTestCase
33 {
34
35
36
37 private static final String CMP_FILE_EXT = "cmp";
38
39
40
41
42 private static final String RESULT_FILE_EXT = "res";
43
44
45
46
47 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/issues/velocity-580";
48
49
50
51
52 private static final String TEMPLATE_DIR = TEST_COMPARE_DIR + "/issues/velocity-580/templates";
53
54
55
56
57 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/issues/velocity-580/compare";
58
59 VelocityEngine engine;
60
61 public Velocity580TestCase(final String name) throws Exception
62 {
63 super(name);
64 }
65
66 public static Test suite()
67 {
68 return new TestSuite(Velocity580TestCase.class);
69 }
70
71 public void setUp() throws Exception
72 {
73
74 assureResultsDirectoryExists(RESULTS_DIR);
75
76 engine = new VelocityEngine();
77
78 engine.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, TEMPLATE_DIR);
79
80 engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
81
82 engine.init();
83 }
84
85 public void testVelocity580() throws Exception
86 {
87 executeTest("velocity580.vm");
88 }
89
90 protected Template executeTest(final String templateName) throws Exception
91 {
92 Template template = engine.getTemplate(templateName);
93
94 FileOutputStream fos = new FileOutputStream(getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
95
96 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
97
98 VelocityContext context = new VelocityContext();
99
100 template.merge(context, writer);
101 writer.flush();
102 writer.close();
103
104 if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName, RESULT_FILE_EXT, CMP_FILE_EXT))
105 {
106
107 StringWriter out = new StringWriter();
108 template.merge(context, out);
109
110 String compare = getFileContents(COMPARE_DIR, templateName, CMP_FILE_EXT);
111
112 fail("Output incorrect for Template: " + templateName + ": \""+out+"\""+
113 "; it did not match: \""+compare+"\"");
114 }
115
116 return template;
117 }
118 }