1 package org.apache.velocity.test.sql;
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 javax.sql.DataSource;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.Velocity;
35 import org.apache.velocity.runtime.RuntimeSingleton;
36 import org.apache.velocity.runtime.log.NullLogChute;
37 import org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader;
38
39
40 public class DataSourceResourceLoaderTestCase
41 extends BaseSQLTest
42 {
43
44
45
46 private static final String CMP_FILE_EXT = "cmp";
47
48
49
50
51 private static final String RESULT_FILE_EXT = "res";
52
53
54
55
56
57 private final static String DATA_PATH = TEST_COMPARE_DIR + "/ds";
58
59
60
61
62 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/ds";
63
64
65
66
67 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/ds/templates";
68
69
70 public DataSourceResourceLoaderTestCase(final String name)
71 throws Exception
72 {
73 super(name, DATA_PATH);
74 }
75
76 public static Test suite()
77 {
78 return new TestSuite(DataSourceResourceLoaderTestCase.class);
79 }
80
81 public void setUp()
82 throws Exception
83 {
84
85 assureResultsDirectoryExists(RESULTS_DIR);
86
87 DataSource ds = new HsqlDataSource("jdbc:hsqldb:.");
88
89 DataSourceResourceLoader rl = new DataSourceResourceLoader();
90 rl.setDataSource(ds);
91
92
93 Velocity.addProperty( "resource.loader", "ds" );
94 Velocity.setProperty( "ds.resource.loader.instance", rl );
95
96 Velocity.setProperty( "ds.resource.loader.resource.table", "velocity_template");
97 Velocity.setProperty( "ds.resource.loader.resource.keycolumn", "id");
98 Velocity.setProperty( "ds.resource.loader.resource.templatecolumn", "def");
99 Velocity.setProperty( "ds.resource.loader.resource.timestampcolumn", "timestamp");
100
101 Velocity.setProperty(
102 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
103
104 Velocity.init();
105 }
106
107
108
109
110
111 public void testSimpleTemplate()
112 throws Exception
113 {
114 Template t = executeTest("testTemplate1");
115 assertFalse("Timestamp is 0", 0 == t.getLastModified());
116 }
117
118
119
120
121
122 public void testRenderTool()
123 throws Exception
124 {
125 Template t = executeTest("testTemplate2");
126 assertFalse("Timestamp is 0", 0 == t.getLastModified());
127 }
128
129
130
131
132 public void testNullTimestamp()
133 throws Exception
134 {
135 Template t = executeTest("testTemplate3");
136 assertEquals("Timestamp is not 0", 0, t.getLastModified());
137 }
138
139
140
141
142 public void testMacroInvocation()
143 throws Exception
144 {
145 Template t = executeTest("testTemplate4");
146 assertFalse("Timestamp is 0", 0 == t.getLastModified());
147 }
148
149 protected Template executeTest(final String templateName)
150 throws Exception
151 {
152 Template template = RuntimeSingleton.getTemplate(templateName);
153
154 FileOutputStream fos =
155 new FileOutputStream (
156 getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
157
158 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
159
160 VelocityContext context = new VelocityContext();
161 context.put("tool", new DSRLTCTool());
162
163 template.merge(context, writer);
164 writer.flush();
165 writer.close();
166
167 if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
168 RESULT_FILE_EXT, CMP_FILE_EXT))
169 {
170 fail("Output incorrect for Template: " + templateName);
171 }
172
173 return template;
174 }
175
176 public static final class DSRLTCTool
177 {
178 public int add(final int a, final int b)
179 {
180 return a + b;
181 }
182
183 public String getMessage()
184 {
185 return "And the result is:";
186 }
187 }
188 }