001: package org.apache.velocity.test.sql;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.BufferedWriter;
023: import java.io.FileOutputStream;
024: import java.io.OutputStreamWriter;
025: import java.io.Writer;
026:
027: import javax.sql.DataSource;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.apache.velocity.Template;
033: import org.apache.velocity.VelocityContext;
034: import org.apache.velocity.app.Velocity;
035: import org.apache.velocity.runtime.RuntimeSingleton;
036: import org.apache.velocity.runtime.log.NullLogChute;
037: import org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader;
038:
039: public class DataSourceResourceLoaderTestCase extends BaseSQLTest {
040: /**
041: * Comparison file extension.
042: */
043: private static final String CMP_FILE_EXT = "cmp";
044:
045: /**
046: * Comparison file extension.
047: */
048: private static final String RESULT_FILE_EXT = "res";
049:
050: /**
051: * Path to template file. This will get combined with the
052: * application directory to form an absolute path
053: */
054: private final static String DATA_PATH = TEST_COMPARE_DIR + "/ds";
055:
056: /**
057: * Results relative to the build directory.
058: */
059: private static final String RESULTS_DIR = TEST_RESULT_DIR + "/ds";
060:
061: /**
062: * Results relative to the build directory.
063: */
064: private static final String COMPARE_DIR = TEST_COMPARE_DIR
065: + "/ds/templates";
066:
067: public DataSourceResourceLoaderTestCase(final String name)
068: throws Exception {
069: super (name, DATA_PATH);
070: }
071:
072: public static Test suite() {
073: return new TestSuite(DataSourceResourceLoaderTestCase.class);
074: }
075:
076: public void setUp() throws Exception {
077:
078: assureResultsDirectoryExists(RESULTS_DIR);
079:
080: DataSource ds = new HsqlDataSource("jdbc:hsqldb:.");
081:
082: DataSourceResourceLoader rl = new DataSourceResourceLoader();
083: rl.setDataSource(ds);
084:
085: // pass in an instance to Velocity
086: Velocity.addProperty("resource.loader", "ds");
087: Velocity.setProperty("ds.resource.loader.instance", rl);
088:
089: Velocity.setProperty("ds.resource.loader.resource.table",
090: "velocity_template");
091: Velocity.setProperty("ds.resource.loader.resource.keycolumn",
092: "id");
093: Velocity.setProperty(
094: "ds.resource.loader.resource.templatecolumn", "def");
095: Velocity.setProperty(
096: "ds.resource.loader.resource.timestampcolumn",
097: "timestamp");
098:
099: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
100: NullLogChute.class.getName());
101:
102: Velocity.init();
103: }
104:
105: /**
106: * Tests loading and rendering of a simple template. If that works, we are able to get data
107: * from the database.
108: */
109: public void testSimpleTemplate() throws Exception {
110: Template t = executeTest("testTemplate1");
111: assertFalse("Timestamp is 0", 0 == t.getLastModified());
112: }
113:
114: /**
115: * Now we have a more complex example. Run a very simple tool.
116: * from the database.
117: */
118: public void testRenderTool() throws Exception {
119: Template t = executeTest("testTemplate2");
120: assertFalse("Timestamp is 0", 0 == t.getLastModified());
121: }
122:
123: /**
124: * Will a NULL timestamp choke the loader?
125: */
126: public void testNullTimestamp() throws Exception {
127: Template t = executeTest("testTemplate3");
128: assertEquals("Timestamp is not 0", 0, t.getLastModified());
129: }
130:
131: /**
132: * Does it load the global Macros from the DB?
133: */
134: public void testMacroInvocation() throws Exception {
135: Template t = executeTest("testTemplate4");
136: assertFalse("Timestamp is 0", 0 == t.getLastModified());
137: }
138:
139: protected Template executeTest(final String templateName)
140: throws Exception {
141: Template template = RuntimeSingleton.getTemplate(templateName);
142:
143: FileOutputStream fos = new FileOutputStream(getFileName(
144: RESULTS_DIR, templateName, RESULT_FILE_EXT));
145:
146: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
147:
148: VelocityContext context = new VelocityContext();
149: context.put("tool", new DSRLTCTool());
150:
151: template.merge(context, writer);
152: writer.flush();
153: writer.close();
154:
155: if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
156: RESULT_FILE_EXT, CMP_FILE_EXT)) {
157: fail("Output incorrect for Template: " + templateName);
158: }
159:
160: return template;
161: }
162:
163: public static final class DSRLTCTool {
164: public int add(final int a, final int b) {
165: return a + b;
166: }
167:
168: public String getMessage() {
169: return "And the result is:";
170: }
171: }
172: }
|