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.setProperty( "resource.loader", "testrl" );
96 Velocity.setProperty( "testrl.resource.loader.instance", rl );
97 Velocity.setProperty( "testrl.resource.loader.path", FILE_RESOURCE_LOADER_PATH );
98
99
100 logger.on();
101 Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, logger);
102 Velocity.setProperty("runtime.log.logsystem.test.level", "debug");
103
104 Velocity.init();
105 }
106
107 public static Test suite ()
108 {
109 return new TestSuite(ResourceLoaderInstanceTestCase.class);
110 }
111
112
113
114
115 public void testResourceLoaderInstance ()
116 throws Exception
117 {
118
119 try
120 {
121 assureResultsDirectoryExists(RESULTS_DIR);
122
123 Template template = RuntimeSingleton.getTemplate(
124 getFileName(null, "testfile", TMPL_FILE_EXT));
125
126 FileOutputStream fos =
127 new FileOutputStream (
128 getFileName(RESULTS_DIR, "testfile", RESULT_FILE_EXT));
129
130 System.out.println("All needed files exist");
131
132 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
133
134
135
136
137
138 VelocityContext context = new VelocityContext();
139
140 template.merge(context, writer);
141 writer.flush();
142 writer.close();
143 }
144 catch (Exception e)
145 {
146 System.out.println("Log was: "+logger.getLog());
147 System.out.println(e);
148 e.printStackTrace();
149 }
150
151 if ( !isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
152 RESULT_FILE_EXT, CMP_FILE_EXT) )
153 {
154 String result = getFileContents(RESULT_DIR, "testfile", RESULT_FILE_EXT);
155 String compare = getFileContents(COMPARE_DIR, "testfile", CMP_FILE_EXT);
156
157 String msg = "Processed template did not match expected output\n"+
158 "-----Result-----\n"+ result +
159 "----Expected----\n"+ compare +
160 "----------------";
161
162
163 System.out.println(msg);
164 System.out.println("Log was: "+logger.getLog());
165 fail(msg);
166 }
167 }
168 }