1   package org.apache.velocity.test.sql;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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       * Comparison file extension.
45       */
46      private static final String CMP_FILE_EXT = "cmp";
47  
48      /**
49       * Comparison file extension.
50       */
51      private static final String RESULT_FILE_EXT = "res";
52  
53      /**
54       * Path to template file.  This will get combined with the
55       * application directory to form an absolute path
56       */
57      private final static String DATA_PATH = TEST_COMPARE_DIR + "/ds";
58  
59      /**
60       * Results relative to the build directory.
61       */
62      private static final String RESULTS_DIR = TEST_RESULT_DIR + "/ds";
63  
64      /**
65       * Results relative to the build directory.
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          // pass in an instance to Velocity
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      * Tests loading and rendering of a simple template. If that works, we are able to get data
109      * from the database.
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      * Now we have a more complex example. Run a very simple tool.
120      * from the database.
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      * Will a NULL timestamp choke the loader?
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      * Does it load the global Macros from the DB?
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 }