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
29 import org.apache.velocity.Template;
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.app.VelocityEngine;
32 import org.apache.velocity.runtime.RuntimeConstants;
33 import org.apache.velocity.test.misc.TestLogChute;
34
35
36
37
38
39
40
41 public class AbsoluteFileResourceLoaderTestCase extends BaseTestCase
42 {
43
44
45
46 private static final String TMPL_FILE_EXT = "vm";
47
48
49
50
51 private static final String CMP_FILE_EXT = "cmp";
52
53
54
55
56 private static final String RESULT_FILE_EXT = "res";
57
58
59
60
61
62 private final static String TEMPLATE_PATH = TEST_COMPARE_DIR + "/absolute/absolute";
63
64
65
66
67 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/absolute/results";
68
69
70
71
72 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/absolute/compare";
73
74 VelocityEngine engine;
75
76
77
78
79 AbsoluteFileResourceLoaderTestCase()
80 {
81 super("AbsoluteFileResourceLoaderTest");
82
83 try
84 {
85 assureResultsDirectoryExists(RESULTS_DIR);
86
87 engine = new VelocityEngine();
88
89
90 engine.addProperty(
91 RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "");
92
93 engine.setProperty(
94 RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
95
96 engine.init();
97 }
98 catch (Exception e)
99 {
100 fail("Cannot setup AbsoluteFileResourceLoaderTest!");
101 }
102 }
103
104 public static Test suite ()
105 {
106 return new AbsoluteFileResourceLoaderTestCase();
107 }
108
109
110
111
112 public void runTest ()
113 {
114 try
115 {
116
117 String curdir = System.getProperty("user.dir");
118 String f = getFileName(curdir, TEMPLATE_PATH, TMPL_FILE_EXT);
119
120 System.out.println("Retrieving template at absolute path: " + f);
121
122 Template template1 = engine.getTemplate(f);
123
124 FileOutputStream fos1 =
125 new FileOutputStream (
126 getFileName(RESULTS_DIR, "absolute", RESULT_FILE_EXT));
127
128 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
129
130
131
132
133 VelocityContext context = new VelocityContext();
134
135 template1.merge(context, writer1);
136 writer1.flush();
137 writer1.close();
138
139 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "absolute",
140 RESULT_FILE_EXT, CMP_FILE_EXT))
141 {
142 fail("Output incorrect.");
143 }
144 }
145 catch (Exception e)
146 {
147 fail(e.getMessage());
148 }
149 }
150 }