velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wgl...@apache.org
Subject svn commit: r676377 - in /velocity/engine/trunk/src: java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java test/org/apache/velocity/test/sql/BaseSQLTest.java test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
Date Sun, 13 Jul 2008 17:52:27 GMT
Author: wglass
Date: Sun Jul 13 10:52:27 2008
New Revision: 676377

URL: http://svn.apache.org/viewvc?rev=676377&view=rev
Log:
change getAsciiStream get to getBinaryStream for better unicode compatibility

Modified:
    velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java
    velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java

Modified: velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java?rev=676377&r1=676376&r2=676377&view=diff
==============================================================================
--- velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
(original)
+++ velocity/engine/trunk/src/java/org/apache/velocity/runtime/resource/loader/DataSourceResourceLoader.java
Sun Jul 13 10:52:27 2008
@@ -228,15 +228,15 @@
 
             if (rs.next())
             {
-                InputStream ascStream = rs.getAsciiStream(templateColumn);
-                if (ascStream == null)
+                InputStream stream = rs.getBinaryStream(templateColumn);
+                if (stream == null)
                 {
                     throw new ResourceNotFoundException("DataSourceResourceLoader: "
                                                         + "template column for '"
                                                         + name + "' is null");
                 }
 
-                return new BufferedInputStream(ascStream);
+                return new BufferedInputStream(stream);
             }
             else
             {

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java?rev=676377&r1=676376&r2=676377&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java (original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/sql/BaseSQLTest.java Sun Jul 13
10:52:27 2008
@@ -19,6 +19,12 @@
  * under the License.    
  */
 
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.sql.DataSource;
+
 import org.apache.velocity.test.BaseTestCase;
 
 /**
@@ -45,4 +51,12 @@
             hsqlDB = new HsqlDB("jdbc:hsqldb:.", path + "/create-db.sql");
         }
     }
+    
+    public void executeSQL(String sql)
+    throws SQLException
+    {
+        Connection connection = hsqlDB.getConnection();
+        Statement statement = connection.createStatement();
+        statement.executeUpdate(sql);
+    }
 }

Modified: velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java?rev=676377&r1=676376&r2=676377&view=diff
==============================================================================
--- velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
(original)
+++ velocity/engine/trunk/src/test/org/apache/velocity/test/sql/DataSourceResourceLoaderTestCase.java
Sun Jul 13 10:52:27 2008
@@ -22,6 +22,7 @@
 import java.io.BufferedWriter;
 import java.io.FileOutputStream;
 import java.io.OutputStreamWriter;
+import java.io.StringWriter;
 import java.io.Writer;
 
 import javax.sql.DataSource;
@@ -66,11 +67,21 @@
      */
     private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/ds/templates";
 
+    /**
+     * String (not containing any VTL) used to test unicode
+     */
+    private String UNICODE_TEMPLATE = "\\u00a9 test \\u0410 \\u0411";
+    
+    /**
+     * Name of template for testing unicode.
+     */
+    private String UNICODE_TEMPLATE_NAME = "testUnicode";
 
     public DataSourceResourceLoaderTestCase(final String name)
     	throws Exception
     {
         super(name, DATA_PATH);
+        setUpUnicode();
     }
 
     public static Test suite()
@@ -84,7 +95,7 @@
 
         assureResultsDirectoryExists(RESULTS_DIR);
 
-	DataSource ds = new HsqlDataSource("jdbc:hsqldb:.");
+	    DataSource ds = new HsqlDataSource("jdbc:hsqldb:.");
 
         DataSourceResourceLoader rl = new DataSourceResourceLoader();
         rl.setDataSource(ds);
@@ -103,6 +114,14 @@
 
         Velocity.init();
     }
+    
+    public void setUpUnicode()
+    throws Exception
+    {
+        String insertString = "insert into velocity_template  (id, timestamp, def) VALUES
" +
+        		"( '" + UNICODE_TEMPLATE_NAME + "', NOW(), '" + UNICODE_TEMPLATE + "');";
+        executeSQL(insertString);
+    }
 
     /**
      * Tests loading and rendering of a simple template. If that works, we are able to get
data
@@ -115,6 +134,26 @@
         assertFalse("Timestamp is 0", 0 == t.getLastModified());
     }
 
+    public void testUnicode()
+    throws Exception
+    {
+        Template template = RuntimeSingleton.getTemplate(UNICODE_TEMPLATE_NAME);
+
+        Writer writer = new StringWriter();
+        VelocityContext context = new VelocityContext();
+        template.merge(context, writer);
+        writer.flush();
+        writer.close();
+
+        String outputText = writer.toString();
+        
+        if (!normalizeNewlines(UNICODE_TEMPLATE).equals(
+                normalizeNewlines( outputText ) ))
+        {
+            fail("Output incorrect for Template: " + UNICODE_TEMPLATE_NAME);
+        }
+    }
+
     /**
      * Now we have a more complex example. Run a very simple tool.
      * from the database.



Mime
View raw message