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.Velocity;
32 import org.apache.velocity.runtime.RuntimeSingleton;
33 import org.apache.velocity.runtime.log.NullLogChute;
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
75
76
77 AbsoluteFileResourceLoaderTestCase()
78 {
79 super("AbsoluteFileResourceLoaderTest");
80
81 try
82 {
83 assureResultsDirectoryExists(RESULTS_DIR);
84
85
86
87 Velocity.addProperty(
88 Velocity.FILE_RESOURCE_LOADER_PATH, "");
89
90 Velocity.setProperty(
91 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
92
93 Velocity.init();
94 }
95 catch (Exception e)
96 {
97 fail("Cannot setup AbsoluteFileResourceLoaderTest!");
98 }
99 }
100
101 public static Test suite ()
102 {
103 return new AbsoluteFileResourceLoaderTestCase();
104 }
105
106
107
108
109 public void runTest ()
110 {
111 try
112 {
113
114 String curdir = System.getProperty("user.dir");
115 String f = getFileName(curdir, TEMPLATE_PATH, TMPL_FILE_EXT);
116
117 System.out.println("Retrieving template at absolute path: " + f);
118
119 Template template1 = RuntimeSingleton.getTemplate(f);
120
121 FileOutputStream fos1 =
122 new FileOutputStream (
123 getFileName(RESULTS_DIR, "absolute", RESULT_FILE_EXT));
124
125 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
126
127
128
129
130 VelocityContext context = new VelocityContext();
131
132 template1.merge(context, writer1);
133 writer1.flush();
134 writer1.close();
135
136 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "absolute",
137 RESULT_FILE_EXT, CMP_FILE_EXT))
138 {
139 fail("Output incorrect.");
140 }
141 }
142 catch (Exception e)
143 {
144 fail(e.getMessage());
145 }
146 }
147 }