You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by he...@apache.org on 2006/09/17 13:31:26 UTC

svn commit: r447048 - in /jakarta/velocity/engine/trunk: src/test/org/apache/velocity/test/sql/ test/ds/ test/ds/templates/

Author: henning
Date: Sun Sep 17 04:31:25 2006
New Revision: 447048

URL: http://svn.apache.org/viewvc?view=rev&rev=447048
Log:
Add test cases for the DataSourceResourceLoader.

Added:
    jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/
    jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java   (with props)
    jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java   (with props)
    jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java   (with props)
    jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java   (with props)
    jakarta/velocity/engine/trunk/test/ds/
    jakarta/velocity/engine/trunk/test/ds/create-db.sql
    jakarta/velocity/engine/trunk/test/ds/templates/
    jakarta/velocity/engine/trunk/test/ds/templates/testTemplate1.cmp
    jakarta/velocity/engine/trunk/test/ds/templates/testTemplate2.cmp
    jakarta/velocity/engine/trunk/test/ds/templates/testTemplate3.cmp
    jakarta/velocity/engine/trunk/test/ds/templates/testTemplate4.cmp

Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java (added)
+++ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java Sun Sep 17 04:31:25 2006
@@ -0,0 +1,45 @@
+package org.apache.velocity.test.sql;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.velocity.test.BaseTestCase;
+
+/**
+ * A base class to implement tests that need a running
+ * Velocity engine and an initialized Hsql Database. Yeah, I should probably
+ * use Derby at some point...
+ *
+ * @author <a href="mailto:henning@apache.org">Henning P. Schmiedehausen</a>
+ * @version $Id$
+ */
+
+public abstract class BaseSQLTest
+        extends BaseTestCase
+{
+    private static HsqlDB hsqlDB = null;
+
+    public BaseSQLTest(String name, String path)
+            throws Exception
+    {
+        super(name);
+
+        if (hsqlDB == null)
+        {
+            hsqlDB = new HsqlDB("jdbc:hsqldb:.", path + "/create-db.sql");
+        }
+    }
+}

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java (added)
+++ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java Sun Sep 17 04:31:25 2006
@@ -0,0 +1,180 @@
+package org.apache.velocity.test.sql;
+
+import java.io.BufferedWriter;
+import java.io.FileOutputStream;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+
+import javax.sql.DataSource;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.velocity.Template;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
+import org.apache.velocity.runtime.RuntimeSingleton;
+import org.apache.velocity.runtime.resource.loader.DataSourceResourceLoader;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class DataSourceResourceLoaderTestCase
+        extends BaseSQLTest
+{
+    /**
+     * Comparison file extension.
+     */
+    private static final String CMP_FILE_EXT = "cmp";
+
+    /**
+     * Comparison file extension.
+     */
+    private static final String RESULT_FILE_EXT = "res";
+
+    /**
+     * Path to template file.  This will get combined with the 
+     * application directory to form an absolute path
+     */
+    private final static String DATA_PATH = TEST_COMPARE_DIR + "/ds";
+
+    /**
+     * Results relative to the build directory.
+     */
+    private static final String RESULTS_DIR = TEST_RESULT_DIR + "/ds";
+
+    /**
+     * Results relative to the build directory.
+     */
+    private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/ds/templates";
+
+
+    public DataSourceResourceLoaderTestCase(final String name)
+    	throws Exception
+    {
+        super(name, DATA_PATH);
+    }
+
+    public static Test suite()
+    {
+        return new TestSuite(DataSourceResourceLoaderTestCase.class);
+    }
+
+    public void setUp()
+            throws Exception
+    {
+
+        assureResultsDirectoryExists(RESULTS_DIR);
+
+	DataSource ds = new HsqlDataSource("jdbc:hsqldb:.");
+	
+        DataSourceResourceLoader rl = new DataSourceResourceLoader();
+        rl.setDataSource(ds);
+
+        // pass in an instance to Velocity
+        Velocity.addProperty( "resource.loader", "ds" );
+        Velocity.setProperty( "ds.resource.loader.instance", rl );
+
+        Velocity.setProperty( "ds.resource.loader.resource.table",           "velocity_template");
+        Velocity.setProperty( "ds.resource.loader.resource.keycolumn",       "id");
+        Velocity.setProperty( "ds.resource.loader.resource.templatecolumn",  "def");
+        Velocity.setProperty( "ds.resource.loader.resource.timestampcolumn", "timestamp");
+
+        Velocity.init();
+    }
+
+    /**
+     * Tests loading and rendering of a simple template. If that works, we are able to get data
+     * from the database.
+     */
+    public void testSimpleTemplate()
+            throws Exception
+    {
+        Template t = executeTest("testTemplate1");
+        assertFalse("Timestamp is 0", 0 == t.getLastModified());
+    }
+
+    /**
+     * Now we have a more complex example. Run a very simple tool.
+     * from the database.
+     */
+    public void testRenderTool()
+            throws Exception
+    {
+	Template t = executeTest("testTemplate2");
+        assertFalse("Timestamp is 0", 0 == t.getLastModified());
+    }
+
+    /**
+     * Will a NULL timestamp choke the loader?
+     */
+    public void testNullTimestamp()
+            throws Exception
+    {
+        Template t = executeTest("testTemplate3");
+        assertEquals("Timestamp is not 0", 0, t.getLastModified());
+    }
+
+    /**
+     * Does it load the global Macros from the DB?
+     */
+    public void testMacroInvocation()
+            throws Exception
+    {
+        Template t = executeTest("testTemplate4");
+        assertFalse("Timestamp is 0", 0 == t.getLastModified());
+    }
+
+    protected Template executeTest(final String templateName)
+    	throws Exception
+    {
+        Template template = RuntimeSingleton.getTemplate(templateName);
+
+        FileOutputStream fos =
+                new FileOutputStream (
+                        getFileName(RESULTS_DIR, templateName, RESULT_FILE_EXT));
+        
+        Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
+        
+        VelocityContext context = new VelocityContext();
+        context.put("tool", new DSRLTCTool());
+        
+        template.merge(context, writer);
+        writer.flush();
+        writer.close();
+        
+        if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
+                        RESULT_FILE_EXT, CMP_FILE_EXT))
+        {
+            fail("Output incorrect for Template: " + templateName);
+        }
+        
+        return template;
+    }
+
+    public static final class DSRLTCTool
+    {
+	public int add(final int a, final int b)
+	{
+	    return a + b;
+	}
+	
+	public String getMessage()
+	{
+	    return "And the result is:";
+	}
+    }
+}

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java (added)
+++ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java Sun Sep 17 04:31:25 2006
@@ -0,0 +1,108 @@
+package org.apache.velocity.test.sql;
+
+/*
+ * Copyright 2001-2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.commons.lang.StringUtils;
+
+import org.hsqldb.jdbcDriver;
+
+import java.io.FileReader;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+
+public class HsqlDB {
+    private Connection connection = null;
+
+    public HsqlDB(String uri, String loadFile) throws Exception {
+        Class.forName(jdbcDriver.class.getName());
+
+        this.connection = DriverManager.getConnection(uri, "sa", "");
+
+        if (StringUtils.isNotEmpty(loadFile)) {
+            loadSqlFile(loadFile);
+        }
+    }
+
+    public Connection getConnection() {
+        return connection;
+    }
+
+    public void close() {
+
+        try {
+            connection.close();
+        } catch (Exception e) {
+            System.out.println("While closing Connection" + e.getMessage());
+        }
+    }
+
+    private void loadSqlFile(String fileName) throws Exception {
+        Statement statement = null;
+
+        try {
+            statement = connection.createStatement();
+
+            String commands = getFileContents(fileName);
+
+            for (int targetPos = commands.indexOf(';'); targetPos > -1;
+                    targetPos = commands.indexOf(';')) {
+                String cmd = commands.substring(0, targetPos + 1);
+
+                try {
+                    statement.execute(cmd);
+                } catch (SQLException sqle) {
+                    System.out.println("Statement: " + cmd + ": " +
+                        sqle.getMessage());
+                }
+
+                commands = commands.substring(targetPos + 2);
+            }
+        } finally {
+
+            if (statement != null) {
+                statement.close();
+            }
+        }
+    }
+
+    private String getFileContents(String fileName) throws Exception {
+        FileReader fr = null;
+
+        try {
+            fr = new FileReader(fileName);
+
+            char[] fileBuf = new char[1024];
+            StringBuffer sb = new StringBuffer(1000);
+            int res = -1;
+
+            while ((res = fr.read(fileBuf, 0, 1024)) > -1) {
+                sb.append(fileBuf, 0, res);
+            }
+
+            return sb.toString();
+        } finally {
+
+            if (fr != null) {
+                fr.close();
+            }
+        }
+    }
+}

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDB.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java (added)
+++ jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java Sun Sep 17 04:31:25 2006
@@ -0,0 +1,50 @@
+package org.apache.velocity.test.sql;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+import org.hsqldb.jdbcDriver;
+
+public class HsqlDataSource implements DataSource {
+
+    private final String url;
+    
+    private PrintWriter logWriter = null;
+    
+    private int loginTimeout = 0;
+    
+    public HsqlDataSource(final String url) throws Exception {
+	this.url = url;
+	Class.forName(jdbcDriver.class.getName());
+    }
+    
+    public Connection getConnection() throws SQLException {
+	return DriverManager.getConnection(url, "sa", "");
+    }
+
+    public Connection getConnection(final String username, final String password)
+	    throws SQLException {
+	return DriverManager.getConnection(url, username, password);
+    }
+
+    public PrintWriter getLogWriter() throws SQLException {
+	return logWriter;
+    }
+
+    public int getLoginTimeout() throws SQLException {
+	return loginTimeout;
+    }
+
+    public void setLogWriter(PrintWriter logWriter) throws SQLException {
+	this.logWriter = logWriter;
+    }
+
+    public void setLoginTimeout(int loginTimeout) throws SQLException {
+	this.loginTimeout = loginTimeout;
+    }
+
+}

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jakarta/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/HsqlDataSource.java
------------------------------------------------------------------------------
    svn:keywords = Id Author Date Revision

Added: jakarta/velocity/engine/trunk/test/ds/create-db.sql
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/ds/create-db.sql?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/test/ds/create-db.sql (added)
+++ jakarta/velocity/engine/trunk/test/ds/create-db.sql Sun Sep 17 04:31:25 2006
@@ -0,0 +1,37 @@
+-- Copyright 2001-2005 The Apache Software Foundation.
+--
+-- Licensed under the Apache License, Version 2.0 (the "License")
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+
+drop table velocity_template if exists;
+
+create table velocity_template (
+	id VARCHAR(64) not null,
+	timestamp TIMESTAMP,
+	def VARCHAR(255) not null
+);
+
+insert into velocity_template  (id, timestamp, def) VALUES
+	( 'testTemplate1', NOW(), 'I am a test through the data loader');
+
+insert into velocity_template  (id, timestamp, def) VALUES
+	( 'testTemplate2', NOW(), '$tool.message $tool.add(23, 19)');
+
+insert into velocity_template  (id, def) VALUES
+	( 'testTemplate3', 'This is a template with a null timestamp');
+
+insert into velocity_template  (id, timestamp, def) VALUES
+	( 'testTemplate4', NOW(), '#testMacro("foo")');
+
+insert into velocity_template  (id, timestamp, def) VALUES
+	( 'VM_global_library.vm', NOW(), '#macro (testMacro $param) I am a macro using $param #end');
+

Added: jakarta/velocity/engine/trunk/test/ds/templates/testTemplate1.cmp
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/ds/templates/testTemplate1.cmp?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/test/ds/templates/testTemplate1.cmp (added)
+++ jakarta/velocity/engine/trunk/test/ds/templates/testTemplate1.cmp Sun Sep 17 04:31:25 2006
@@ -0,0 +1 @@
+I am a test through the data loader
\ No newline at end of file

Added: jakarta/velocity/engine/trunk/test/ds/templates/testTemplate2.cmp
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/ds/templates/testTemplate2.cmp?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/test/ds/templates/testTemplate2.cmp (added)
+++ jakarta/velocity/engine/trunk/test/ds/templates/testTemplate2.cmp Sun Sep 17 04:31:25 2006
@@ -0,0 +1 @@
+And the result is: 42
\ No newline at end of file

Added: jakarta/velocity/engine/trunk/test/ds/templates/testTemplate3.cmp
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/ds/templates/testTemplate3.cmp?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/test/ds/templates/testTemplate3.cmp (added)
+++ jakarta/velocity/engine/trunk/test/ds/templates/testTemplate3.cmp Sun Sep 17 04:31:25 2006
@@ -0,0 +1 @@
+This is a template with a null timestamp
\ No newline at end of file

Added: jakarta/velocity/engine/trunk/test/ds/templates/testTemplate4.cmp
URL: http://svn.apache.org/viewvc/jakarta/velocity/engine/trunk/test/ds/templates/testTemplate4.cmp?view=auto&rev=447048
==============================================================================
--- jakarta/velocity/engine/trunk/test/ds/templates/testTemplate4.cmp (added)
+++ jakarta/velocity/engine/trunk/test/ds/templates/testTemplate4.cmp Sun Sep 17 04:31:25 2006
@@ -0,0 +1 @@
+ I am a macro using foo 
\ No newline at end of file



---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org