You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by rh...@apache.org on 2006/02/23 19:22:42 UTC

svn commit: r380187 [2/4] - in /db/derby/code/trunk/java: client/org/apache/derby/client/ client/org/apache/derby/client/am/ client/org/apache/derby/client/net/ client/org/apache/derby/jdbc/ engine/org/apache/derby/impl/jdbc/ testing/org/apache/derbyTe...

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java?rev=380187&r1=380186&r2=380187&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods.java Thu Feb 23 10:22:35 2006
@@ -20,23 +20,50 @@
 
 package org.apache.derbyTesting.functionTests.tests.jdbc4;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.Reader;
 import java.io.InputStream;
+import java.io.IOException;
+import java.sql.Blob;
 import java.sql.Connection;
+import java.sql.Clob;
+import java.sql.DriverManager;
 import java.sql.PreparedStatement;
 import java.sql.SQLException;
 import java.sql.NClob;
+import java.sql.ResultSet;
 import java.sql.SQLXML;
+import java.sql.Statement;
 import org.apache.derby.shared.common.reference.SQLState;
-
-
 import org.apache.derby.tools.ij;
+/**
+ * This class is used to test the implementations of the JDBC 4.0 methods 
+ * in the PreparedStatement interface 
+ */
 
 public class TestPreparedStatementMethods {
     
-    Connection conn=null;
+    static Connection conn=null;
     PreparedStatement ps=null;
     
+    String filepath;
+    String sep;
+    boolean exists;
+    /*
+     * This function is used to build the path to the directory where the files 
+     * that are used to create the test blob and clob are present
+     */
+    void buildFilePath(String filename) {
+        filepath = "extin";
+        sep = System.getProperty("file.separator");
+        exists = (new File("extin", filename)).exists();
+        if(!exists){
+            String userDir = System.getProperty("user.dir");
+            filepath = userDir + sep + ".." + sep + filepath;
+        }
+    }
     void t_setRowId() {
         try {
             ps.setRowId(0,null);
@@ -92,34 +119,284 @@
             e.printStackTrace();
         }
     }
+    /*
+     * Compares the two clobs supplied to se if they are similar
+     * returns true if they are similar and false otherwise 
+     */
+    boolean compareClob(Clob clob1,Clob clob2) {
+        int c1,c2;
+        InputStream is1=null,is2=null;
+        try {
+            is1 = clob1.getAsciiStream();
+            is2 = clob2.getAsciiStream();
+            if(clob1.length()!=clob2.length())
+                return false;
+        } catch(SQLException sqle){
+            sqle.printStackTrace();
+        }
+        try {
+            for(long i=0;i<clob1.length();i++) {
+                c1=is1.read();
+                c2=is2.read();
+                if(c1!=c2)
+                    return false;
+            }
+        } catch(IOException e) {
+            e.printStackTrace();
+        } catch(SQLException e) {
+            e.printStackTrace();
+        }
+        return true;
+    }
+    /*
+     * Compares the two blobs supplied to se if they are similar
+     * returns true if they are similar and false otherwise 
+     */
+    boolean compareBlob(Blob blob1,Blob blob2) {
+        int c1,c2;
+        InputStream is1=null,is2=null;
+        try {
+            is1 = blob1.getBinaryStream();
+            is2 = blob2.getBinaryStream();
+            if(blob1.length()!=blob2.length())
+                return false;
+        } catch(SQLException sqle){
+            sqle.printStackTrace();
+        }
+        try {
+            for(long i=0;i<blob1.length();i++) {
+                c1=is1.read();
+                c2=is2.read();
+                if(c1!=c2)
+                    return false;
+            }
+        } catch(IOException e) {
+            e.printStackTrace();
+        } catch(SQLException e) {
+            e.printStackTrace();
+        }
+        return true;
+    }
+    /*
+     * Build the clob value to be inserted into the table and insert it using 
+     * the setClob method in the PreparedStatement interface
+     */
+    Clob buildAndInsertClobValue(int n,String filename,Connection conn){
+        int c;
+        byte [] fromFile = new byte[1024];
+        Clob clob=null;
+        try {
+            clob = conn.createClob();
+            java.io.OutputStream os = clob.setAsciiStream(1);
+            buildFilePath(filename);
+            File f = new File(filepath + sep + filename);
+            InputStream is = new FileInputStream(f);
+            c = is.read(fromFile);
+            while(c>0) {
+                os.write(fromFile,0,c);
+                c = is.read(fromFile);
+            }
+            PreparedStatement ps = conn.prepareStatement("insert into clobtable3 values(?,?)");
+            ps.setInt(1, n);
+            ps.setClob(2,clob);
+            ps.executeUpdate();
+        } catch(IOException ioe) {
+            ioe.printStackTrace();
+        } catch(SQLException sqle) {
+            sqle.printStackTrace();
+        }
+        return clob;
+    }
+    /*
+     * Build the clob value to be inserted into the table and insert it using
+     * the setBlob method in the PreparedStatement interface
+     */
+    Blob buildAndInsertBlobValue(int n,String filename,Connection conn){
+        int c;
+        byte [] fromFile = new byte[1024];
+        Blob blob=null;
+        try {
+            blob = conn.createBlob();
+            java.io.OutputStream os = blob.setBinaryStream(1);
+            buildFilePath(filename);
+            File f = new File(filepath + sep + filename);
+            InputStream is = new FileInputStream(f);
+            c = is.read(fromFile);
+            while(c>0) {
+                os.write(fromFile,0,c);
+                c = is.read(fromFile);
+            }
+            PreparedStatement ps = conn.prepareStatement("insert into blobtable3 values(?,?)");
+            ps.setInt(1, n);
+            ps.setBlob(2,blob);
+            ps.executeUpdate();
+        } catch(IOException ioe) {
+            ioe.printStackTrace();
+        } catch(SQLException sqle) {
+            sqle.printStackTrace();
+        }
+        return blob;
+    }
+    /*
+     * 1) Insert the clob in to the clob table by calling the 
+     *    buildAndInsertClobValue function
+     * 2) Check whether the clob value has been correctly inserted in to the 
+     *    table by using the compareClob function
+     */
     void t_setClob() {
         try {
-            ps.setClob(0,null,0);
-            System.out.println("UnImplemented Exception not thrown in code");
+            int c;
+            byte [] fromFile;
+            fromFile = new byte[1024];
+            Statement s = conn.createStatement();
+            s.execute("create table clobtable3(n int)");
+            s.execute("alter table clobtable3 add column clobCol CLOB(1M)");
+            s.close();
+            Clob clob = buildAndInsertClobValue(0000,"short.txt",conn);
+            Clob clob1 = buildAndInsertClobValue(1000, "aclob.txt",conn);
+            Clob clob2 = buildAndInsertClobValue(2000,"littleclob.txt",conn);
+            PreparedStatement ps3 = conn.prepareStatement("select * from " +
+                    "clobtable3 where n=1000");
+            ResultSet rs3 = ps3.executeQuery();
+            rs3.next();
+            Clob clob3 = rs3.getClob(2);
+            if(!compareClob(clob1,clob3)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Clob values");
+            }
+            PreparedStatement ps4 = conn.prepareStatement("select * from " +
+                    "clobtable3");
+            ResultSet rs4 = ps4.executeQuery();
+            rs4.next();
+            Clob clob4 = rs4.getClob(2);
+            if(!compareClob(clob,clob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Clob values");
+            }
+            rs4.next();
+            clob4 = rs4.getClob(2);
+            if(!compareClob(clob1,clob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Clob values");
+            }
+            rs4.next();
+            clob4 = rs4.getClob(2);
+            if(!compareClob(clob2,clob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Clob values");
+            }
+            
         } catch(SQLException e) {
             if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
-                System.out.println("Unexpected SQLException"+e);
+                e.printStackTrace();
             }
-            
         } catch(Exception e) {
-            System.out.println("Unexpected exception thrown in method"+e);
             e.printStackTrace();
         }
     }
+    /*
+     * 1) Insert the blob in to the blob table by calling the 
+     *    buildAndInsertBlobValue function
+     * 2) Check whether the blob value has been correctly inserted in to the 
+     *    table by using the compareBlob function
+     */
     void t_setBlob() {
         try {
-            ps.setBlob(0,null,0);
-            System.out.println("UnImplemented Exception not thrown in code");
+            int c;
+            byte [] fromFile;
+            fromFile = new byte[1024];
+            Statement s = conn.createStatement();
+            s.execute("create table blobtable3(n int)");
+            s.execute("alter table blobtable3 add column blobCol BLOB(1M)");
+            s.close();
+            Blob blob = buildAndInsertBlobValue(0000, "short.txt", conn);
+            Blob blob1 = buildAndInsertBlobValue(1000, "aclob.txt", conn);
+            Blob blob2 = buildAndInsertBlobValue(2000, "littleclob.txt", conn);
+            PreparedStatement ps3 = conn.prepareStatement("select * from " +
+                    "blobtable3 where n=1000");
+            ResultSet rs3 = ps3.executeQuery();
+            rs3.next();
+            Blob blob3 = rs3.getBlob(2);
+            if(!compareBlob(blob1,blob3)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Blob values");
+            }
+            PreparedStatement ps4 = conn.prepareStatement("select * from blobtable3");
+            ResultSet rs4 = ps4.executeQuery();
+            rs4.next();
+            Blob blob4 = rs4.getBlob(2);
+            if(!compareBlob(blob,blob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Blob values");
+            }
+            rs4.next();
+            blob4 = rs4.getBlob(2);
+            if(!compareBlob(blob1,blob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Blob values");
+            }
+            rs4.next();
+            blob4 = rs4.getBlob(2);
+            if(!compareBlob(blob2,blob4)) {
+                System.out.println("Difference between the inserted and the " +
+                        "queried Blob values");
+            }
+            
         } catch(SQLException e) {
             if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
-                System.out.println("Unexpected SQLException"+e);
+                e.printStackTrace();
             }
-            
         } catch(Exception e) {
-            System.out.println("Unexpected exception thrown in method"+e);
             e.printStackTrace();
         }
     }
+    /*
+     * The setClob method on the embedded side. Here functionality has still not
+     * been added. It still throws a notImplemented exception only
+     */
+    void t_Clob_setMethods_Embedded(Connection conn){
+        try {
+            Statement s = conn.createStatement();
+            ResultSet rs = s.executeQuery("select * from clobtable3");
+            rs.next();
+            Clob clob = rs.getClob(2);
+            PreparedStatement ps = conn.prepareStatement("insert into clobtable3" +
+                    " values(?,?)");
+            ps.setInt(1, 3000);
+            ps.setClob(2, clob);
+            ps.executeUpdate();
+            ps.close();
+        } catch(SQLException e){
+            if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
+                System.out.println("Unexpected SQLException"+e);
+                e.printStackTrace();
+            }
+        }
+    }
+    /*
+     * The setBlob method on the embedded side. Here functionality has still not
+     * been added. It still throws a notImplemented exception only
+     */
+    void t_Blob_setMethods_Embedded(Connection conn){
+        try {
+            Statement s = conn.createStatement();
+            ResultSet rs = s.executeQuery("select * from blobtable3");
+            rs.next();
+            Blob blob = rs.getBlob(2);
+            PreparedStatement ps = conn.prepareStatement("insert into blobtable3" +
+                    " values(?,?)");
+            ps.setInt(1, 3000);
+            ps.setBlob(2, blob);
+            ps.executeUpdate();
+            ps.close();
+        } catch(SQLException e){
+            if(SQLState.NOT_IMPLEMENTED.equals (e.getSQLState())) {
+                System.out.println("Unexpected SQLException"+e);
+                e.printStackTrace();
+            }
+        }
+    }
+    
     void t_setNClob2() {
         try {
             ps.setNClob(0,null,0);
@@ -177,61 +454,116 @@
             e.printStackTrace();
         }
     }
-    
-    
-    void startPreparedStatementMethodTest(Connection conn_in,PreparedStatement ps_in) {
-        conn = conn_in;
-        ps = ps_in;
-        t_setRowId();
-        t_setNString();
-        t_setNCharacterStream();
-        t_setNClob1();
-        t_setClob();
-        t_setBlob();
-        t_setNClob2();
-        t_setSQLXML();
-        t_setPoolable();
-        t_isPoolable();
-    }
-    
-    public static void main(String args[]) {
-        TestConnection tc=null;
+    /*
+     * Start the tests for the JDBC4.0 methods on the client side
+     */
+    void startClientTestMethods() {
         Connection conn_main=null;
         PreparedStatement ps_main=null;
         
         try {
-            tc = new TestConnection();
-            conn_main = tc.createEmbeddedConnection();
-        } catch(Exception e) {
-            e.printStackTrace();
+            Class.forName("org.apache.derby.jdbc.ClientDriver");
+            conn_main = DriverManager.getConnection("jdbc:derby:" +
+                    "//localhost:1527/mydb;" +
+                    "create=true");
+            ps_main = conn_main.prepareStatement("select count(*) from " +
+                    "sys.systables");
+            conn = conn_main;
+            ps = ps_main;
+            t_setRowId();
+            t_setNString();
+            t_setNCharacterStream();
+            t_setNClob1();
+            t_setClob();
+            t_setBlob();
+            t_setNClob2();
+            t_setSQLXML();
+            t_setPoolable();
+            t_isPoolable();
+        } catch(SQLException sqle) {
+            sqle.printStackTrace();
+        } catch(ClassNotFoundException cnfe) {
+            cnfe.printStackTrace();
+        } finally {
+            try {
+                conn_main.close();
+            } catch(SQLException sqle){
+                sqle.printStackTrace();
+            }
         }
+    }
+    /*
+     * Start the tests for testing the JDBC4.0 methods on the embedded side
+     */
+    void startEmbeddedTestMethods() {
+        Connection conn_main=null;
+        PreparedStatement ps_main=null;
         
         try {
-            ps_main = conn_main.prepareStatement("select count(*) from sys.systables");
-        } catch(SQLException e) {
-            System.out.println(""+e);
-            e.printStackTrace();
+            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
+            conn_main = DriverManager.getConnection("jdbc:derby:mydb1;" +
+                    "create=true");
+            
+            Statement s = conn_main.createStatement();
+            s.execute("create table clobtable3 (n int,clobcol CLOB)");
+            File file = new File("extin/short.txt");
+            int fileLength = (int) file.length();
+            InputStream fin = new FileInputStream(file);
+            PreparedStatement ps = conn_main.prepareStatement("INSERT INTO " +
+                    "clobtable3 " +
+                    "VALUES (?, ?)");
+            ps.setInt(1, 1000);
+            ps.setAsciiStream(2, fin, fileLength);
+            ps.execute();
+            
+            Statement s1 = conn_main.createStatement();
+            s1.execute("create table blobtable3 (n int,blobcol BLOB)");
+            File file1 = new File("extin/short.txt");
+            int fileLength1 = (int) file1.length();
+            InputStream fin1 = new FileInputStream(file1);
+            PreparedStatement ps1 = conn_main.prepareStatement("INSERT INTO " +
+                    "blobtable3 " +
+                    "VALUES (?, ?)");
+            ps1.setInt(1, 1000);
+            ps1.setBinaryStream(2, fin1, fileLength1);
+            ps1.execute();
+            
+            conn_main.commit();
+            t_Clob_setMethods_Embedded(conn_main);
+            t_Blob_setMethods_Embedded(conn_main);
+            ps_main = conn_main.prepareStatement("select count(*) from " +
+                    "sys.systables");
+            conn = conn_main;
+            ps = ps_main;
+            t_setRowId();
+            t_setNString();
+            t_setNCharacterStream();
+            t_setNClob1();
+            t_setNClob2();
+            t_setSQLXML();
+            t_setPoolable();
+            t_isPoolable();
+        }
+        catch(SQLException sqle) {
+            sqle.printStackTrace();
+        } catch(ClassNotFoundException cnfe) {
+            cnfe.printStackTrace();
+        } catch(FileNotFoundException fnfe) {
+            fnfe.printStackTrace();
+        } catch(IOException ioe) {
+            ioe.printStackTrace();
+        } finally {
+            try {
+                conn_main.close();
+            } catch(SQLException sqle){
+                sqle.printStackTrace();
+            }
         }
-        
+    }
+    
+    public static void main(String args[]) {
         TestPreparedStatementMethods tpsm = new TestPreparedStatementMethods();
-        tpsm.startPreparedStatementMethodTest(conn_main,ps_main);
-        
-        conn_main=null;
-        ps_main=null;
-        
-        try {
-            conn_main = tc.createClientConnection();
-        } catch(Exception e) {
-            e.printStackTrace();
-        }
-        
-        try {
-            ps_main = conn_main.prepareStatement("select count(*) from sys.systables");
-        } catch(SQLException e) {
-            System.out.println(""+e);
-            e.printStackTrace();
-        }
-        
-        tpsm.startPreparedStatementMethodTest(conn_main,ps_main);
+        tpsm.startClientTestMethods();
+        tpsm.startEmbeddedTestMethods();
     }
 }

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods_app.properties
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods_app.properties?rev=380187&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods_app.properties (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods_app.properties Thu Feb 23 10:22:35 2006
@@ -0,0 +1,11 @@
+#Test runs with mustang set other runwithjvms to false
+#Include the support files from the clob and the blob values are built
+runwithibm13=false
+runwithibm14=false
+runwithj9=false
+runwithjdk12=false
+runwithjdk13=false
+runwithjdk14=false
+supportfiles=tests/jdbc4/short.txt,tests/jdbc4/littleclob.txt,tests/jdbc4/aclob.txt
+useextdirs=true
+

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestPreparedStatementMethods_app.properties
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java?rev=380187&r1=380186&r2=380187&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/TestResultSetMethods.java Thu Feb 23 10:22:35 2006
@@ -30,6 +30,10 @@
 import java.sql.SQLException;
 import org.apache.derby.shared.common.reference.SQLState;
 
+/**
+ * This class is used to test the implementations of the JDBC 4.0 methods
+ * in the ResultSet interface
+ */
 public class TestResultSetMethods {
     
     Connection conn=null;