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.RuntimeConstants;
34 import org.apache.velocity.runtime.RuntimeSingleton;
35 import org.apache.velocity.test.misc.TestLogChute;
36 import org.apache.velocity.runtime.resource.loader.FileResourceLoader;
37 import org.apache.velocity.runtime.resource.loader.ResourceLoader;
38
39
40
41
42
43
44
45 public class ResourceLoaderInstanceTestCase extends BaseTestCase
46 {
47
48
49
50 private static final String TMPL_FILE_EXT = "vm";
51
52
53
54
55 private static final String CMP_FILE_EXT = "cmp";
56
57
58
59
60 private static final String RESULT_FILE_EXT = "res";
61
62
63
64
65
66 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/resourceinstance";
67
68
69
70
71 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/resourceinstance";
72
73
74
75
76 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/resourceinstance/compare";
77
78 private TestLogChute logger = new TestLogChute();
79
80
81
82
83 public ResourceLoaderInstanceTestCase(String name)
84 {
85 super(name);
86 }
87
88 public void setUp()
89 throws Exception
90 {
91
92 ResourceLoader rl = new FileResourceLoader();
93
94
95 Velocity.reset();
96 Velocity.setProperty( "resource.loader", "testrl" );
97 Velocity.setProperty( "testrl.resource.loader.instance", rl );
98 Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
99
100
101 logger.on();
102 Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
103 Velocity.setProperty("runtime.log.logsystem.test.level", "debug");
104
105 Velocity.init();
106 }
107
108 public static Test suite ()
109 {
110 return new TestSuite(ResourceLoaderInstanceTestCase.class);
111 }
112
113
114
115
116 public void testResourceLoaderInstance ()
117 throws Exception
118 {
119
120 try
121 {
122 assureResultsDirectoryExists(RESULTS_DIR);
123
124 Template template = RuntimeSingleton.getTemplate(
125 getFileName(null, "testfile", TMPL_FILE_EXT));
126
127 FileOutputStream fos =
128 new FileOutputStream (
129 getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
130
131 System.out.println("All needed files exist");
132
133 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
134
135
136
137
138
139 VelocityContext context = new VelocityContext();
140
141 template.merge(context, writer);
142 writer.flush();
143 writer.close();
144 }
145 catch (Exception e)
146 {
147 System.out.println("Log was: "+logger.getLog());
148 System.out.println(e);
149 e.printStackTrace();
150 }
151
152 if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
153 RESULT_FILE_EXT, CMP_FILE_EXT) )
154 {
155 String result = getFileContents(RESULT_DIR, "testfile", RESULT_FILE_EXT);
156 String compare = getFileContents(COMPARE_DIR, "testfile", CMP_FILE_EXT);
157
158 String msg = "Processed template did not match expected output\n"+
159 "-----Result-----\n"+ result +
160 "----Expected----\n"+ compare +
161 "----------------";
162
163
164 System.out.println(msg);
165 System.out.println("Log was: "+logger.getLog());
166 fail(msg);
167 }
168 }
169 }