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.Velocity;
33 import org.apache.velocity.runtime.RuntimeSingleton;
34 import org.apache.velocity.runtime.log.NullLogChute;
35 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
36 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
37
38
39
40
41
42
43
44 public class ResourceLoaderInstanceTestCase extends BaseTestCase
45 {
46
47
48
49 private static final String TMPL_FILE_EXT = "vm";
50
51
52
53
54 private static final String CMP_FILE_EXT = "cmp";
55
56
57
58
59 private static final String RESULT_FILE_EXT = "res";
60
61
62
63
64
65 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/resourceinstance";
66
67
68
69
70 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/resourceinstance";
71
72
73
74
75 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/resourceinstance/compare";
76
77
78
79
80 public ResourceLoaderInstanceTestCase(String name)
81 {
82 super(name);
83 }
84
85 public void setUp()
86 throws Exception
87 {
88
89 ResourceLoader rl = new FileResourceLoader();
90
91
92 Velocity.addProperty( "resource.loader", "testrl" );
93 Velocity.setProperty( "testrl.resource.loader.instance", rl );
94 Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
95
96 Velocity.setProperty(
97 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
98
99 Velocity.init();
100 }
101
102 public static Test suite ()
103 {
104 return new TestSuite(ResourceLoaderInstanceTestCase.class);
105 }
106
107
108
109
110 public void testResourceLoaderInstance ()
111 throws Exception
112 {
113 assureResultsDirectoryExists(RESULTS_DIR);
114
115 Template template = RuntimeSingleton.getTemplate(
116 getFileName(null, "testfile", TMPL_FILE_EXT));
117
118 FileOutputStream fos =
119 new FileOutputStream (
120 getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
121
122
123 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
124
125
126
127
128
129 VelocityContext context = new VelocityContext();
130
131 template.merge(context, writer);
132 writer.flush();
133 writer.close();
134
135 if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
136 RESULT_FILE_EXT, CMP_FILE_EXT) )
137 {
138 fail("Output incorrect.");
139 }
140 }
141 }