Changeset: 0eef53e06007 for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java?cmd=changeset;node=0eef53e06007
Added Files:
        tests/JDBC_API_Tester.java
Modified Files:
        tests/build.xml
Branch: default
Log Message:

Add JDBC_API_Tester program to test JDBC Driver API methods and behavior of 
MonetDB server.
It combines 30+ tests which were previous individual test programs
into one large test program, reusing the connection.
This speeds up testing considerably as the overhead of starting a JVM and
loading the java test program class and MonetDB JDBC driver is now reduced
to only one time instead of 30+ times.
Also all output is no longer send to system out/err but collected in a 
StringBuilder.
The contents of it is compared with the expected output at the end of each test.
Only when it deviates the output is sent to system out, see 
compareExpectedOutput().


diffs (truncated from 1430 to 300 lines):

diff --git a/tests/JDBC_API_Tester.java b/tests/JDBC_API_Tester.java
new file mode 100644
--- /dev/null
+++ b/tests/JDBC_API_Tester.java
@@ -0,0 +1,1392 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0.  If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
+ */
+
+import java.sql.*;
+import java.util.*;
+
+/**
+ * class to test JDBC Driver API methods and behavior of MonetDB server.
+ *
+ * It combines 30+ tests which were previous individual test programs
+ * into one large test program, reusing the connection.
+ * This speeds up testing considerably as the overhead of starting a JVM and
+ * loading the java test program class and MonetDB JDBC driver is now reduced
+ * to only one time instead of 30+ times.
+ * Also all output is no longer send to system out/err but collected in a 
StringBuilder.
+ * The contents of it is compared with the expected output at the end of each 
test.
+ * Only when it deviates the output is sent to system out, see 
compareExpectedOutput().
+ *
+ * @author Martin van Dinther
+ * @version 0.1
+ */
+final public class JDBC_API_Tester {
+       StringBuilder sb;       // buffer to collect the test output
+       Connection con; // main connection shared by all tests
+
+       public static void main(String[] args) throws Exception {
+               String con_URL = args[0];
+
+               JDBC_API_Tester jt = new JDBC_API_Tester();
+               jt.sb = new StringBuilder(4200);
+               jt.con = DriverManager.getConnection(con_URL);
+               // we are now connected
+
+               // run the tests
+               jt.Test_Cautocommit(con_URL);
+               jt.Test_CisValid();
+               jt.Test_Clargequery();
+               jt.Test_Cmanycon(con_URL);
+               jt.Test_Creplysize();
+               jt.Test_Csavepoints();
+               jt.Test_Ctransaction();
+               jt.Test_Dobjects();
+               jt.Test_FetchSize();
+               jt.Test_PSgeneratedkeys();
+               jt.Test_PSgetObject();
+               jt.Test_PSlargebatchval();
+               jt.Test_PSlargeresponse();
+               jt.Test_PSmanycon();
+               jt.Test_PSmetadata();
+               jt.Test_PSsomeamount();
+               jt.Test_PSsqldata();
+               jt.Test_PStimedate();
+               jt.Test_PStimezone();
+               jt.Test_PStypes();
+               jt.Test_CallableStmt();
+               jt.Test_Rbooleans();
+               jt.Test_Rmetadata();
+               jt.Test_Rpositioning();
+               jt.Test_Rsqldata();
+               jt.Test_Rtimedate();
+               jt.Test_Sbatching();
+               jt.Test_Smoreresults();
+               jt.Test_Wrapper();
+
+               jt.closeConx(jt.con);
+       }
+
+       private void Test_Cautocommit(String arg0) {
+               sb.setLength(0);        // clear the output log buffer
+
+               Connection con1 = con;
+               Connection con2 = null;
+               Statement stmt1 = null;
+               Statement stmt2 = null;
+               ResultSet rs = null;
+               try {
+                       con2 = DriverManager.getConnection(arg0);
+
+                       // >> true: auto commit should be on by default
+                       if (con1.getAutoCommit() != true)
+                               sb.append("expecting con1 to have autocommit 
on/true");
+                       if (con2.getAutoCommit() != true)
+                               sb.append("expecting con2 to have autocommit 
on/true");
+
+                       // test commit by checking if a change is visible in 
another connection
+                       stmt1 = con1.createStatement();
+                       sb.append("1. create...");
+                       stmt1.executeUpdate("CREATE TABLE 
table_Test_Cautocommit ( id int )");
+                       sb.append("passed :)\n");
+
+                       stmt2 = con2.createStatement();
+                       sb.append("2. select...");
+                       rs = stmt2.executeQuery("SELECT * FROM 
table_Test_Cautocommit");
+                       sb.append("passed :)\n");
+               } catch (SQLException e) {
+                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
+                       closeStmtResSet(stmt2, rs);
+                       closeStmtResSet(stmt1, null);
+                       closeConx(con2);
+                       sb.append("ABORTING TEST!!!");
+                       return;
+               }
+
+               try {
+                       // turn off auto commit
+                       con1.setAutoCommit(false);
+                       con2.setAutoCommit(false);
+
+                       // >> false: we just disabled it
+                       if (con1.getAutoCommit() != false)
+                               sb.append("expecting con1 to have autocommit 
off/false");
+                       if (con2.getAutoCommit() != false)
+                               sb.append("expecting con2 to have autocommit 
off/false");
+
+                       // a change would not be visible now
+                       sb.append("3. drop...");
+                       stmt2.executeUpdate("DROP TABLE 
table_Test_Cautocommit");
+                       sb.append("passed :)\n");
+
+                       sb.append("4. select...");
+                       rs = stmt1.executeQuery("SELECT * FROM 
table_Test_Cautocommit");
+                       sb.append("passed :)\n");
+
+                       sb.append("5. commit...");
+                       con2.commit();
+                       sb.append("passed :)\n");
+
+                       sb.append("6. select...");
+                       rs = stmt1.executeQuery("SELECT * FROM 
table_Test_Cautocommit");
+                       sb.append("passed :)\n");
+
+                       sb.append("7. commit...");
+                       con1.commit();
+                       sb.append("passed :)\n");
+
+                       // restore original auto commit setting
+                       con1.setAutoCommit(true);
+                       con2.setAutoCommit(true);
+               } catch (SQLException e) {
+                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
+               }
+
+               closeStmtResSet(stmt1, rs);
+               closeStmtResSet(stmt2, null);
+
+               closeConx(con2);
+
+               compareExpectedOutput("Test_Cautocommit",
+                               "1. create...passed :)\n" +
+                               "2. select...passed :)\n" +
+                               "3. drop...passed :)\n" +
+                               "4. select...passed :)\n" +
+                               "5. commit...passed :)\n" +
+                               "6. select...passed :)\n" +
+                               "7. commit...passed :)\n");
+       }
+
+       private void Test_CisValid() {
+               sb.setLength(0);        // clear the output log buffer
+
+               Statement stmt = null;
+               try {
+                       stmt = con.createStatement();
+                       con.setAutoCommit(false); // start a transaction
+                       stmt.executeQuery("SELECT COUNT(*) FROM 
doesnotexist;"); // let's trigger an error
+               } catch (SQLException e) {
+                       // e.printStackTrace();
+                       sb.append("Expected error: " + e).append("\n");
+                       try {
+                               // test calling conn.isValid()
+                               sb.append("Validating connection: con.isValid? 
" + con.isValid(30));
+                               // Can we rollback on this connection without 
causing an error?
+                               con.rollback();
+                       } catch (SQLException e2) {
+                               sb.append("UnExpected error: " + e2);
+                       }
+               }
+
+               try {
+                       // restore auto commit mode
+                       con.setAutoCommit(true);
+               } catch (SQLException e) {
+                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
+               }
+
+               closeStmtResSet(stmt, null);
+
+               compareExpectedOutput("Test_CisValid",
+                               "Expected error: java.sql.SQLException: SELECT: 
no such table 'doesnotexist'\n" +
+                               "Validating connection: con.isValid? true");
+       }
+
+       private void Test_Clargequery() {
+               sb.setLength(0);        // clear the output log buffer
+               final String query =
+                       "-- When a query larger than the send buffer is being " 
+
+                       "sent, a deadlock situation can occur when the server 
writes " +
+                       "data back, blocking because we as client are sending 
as well " +
+                       "and not reading.  Hence, to avoid this deadlock, in 
JDBC a " +
+                       "separate thread is started in the background such that 
results " +
+                       "from the server can be read, while data is still being 
sent to " +
+                       "the server.  To test this, we need to trigger the 
SendThread " +
+                       "being started, which we do with a quite large query.  
We " +
+                       "construct it by repeating some stupid query plus a 
comment " +
+                       "a lot of times.  And as you're guessing by now, you're 
reading " +
+                       "this stupid comment that we use :)\n" +
+                       "select 1;\n";
+
+               final int size = 1234;
+               StringBuilder bigq = new StringBuilder(query.length() * size);
+               for (int i = 0; i < size; i++) {
+                       bigq.append(query);
+               }
+
+               Statement stmt = null;
+               try {
+                       // >> true: auto commit should be on by default
+                       sb.append("0. true\t" + 
con.getAutoCommit()).append("\n");
+                       stmt = con.createStatement();
+
+                       // sending big script with many simple queries
+                       sb.append("1. executing script").append("\n");
+                       stmt.execute(bigq.toString());
+
+                       int i = 1;      // we skip the first "getResultSet()"
+                       while (stmt.getMoreResults() != false) {
+                               i++;
+                       }
+                       if (stmt.getUpdateCount() != -1) {
+                               sb.append("Error: found an update count for a 
SELECT query").append("\n");
+                       }
+                       if (i != size) {
+                               sb.append("Error: expecting " + size + " 
tuples, only got " + i).append("\n");
+                       }
+                       sb.append("2. queries processed").append("\n");
+               } catch (SQLException e) {
+                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
+               }
+
+               closeStmtResSet(stmt, null);
+
+               compareExpectedOutput("Test_Clargequery",
+                               "0. true        true\n" +
+                               "1. executing script\n" +
+                               "2. queries processed\n");
+       }
+
+       private void Test_Cmanycon(String arg0) {
+               sb.setLength(0);        // clear the output log buffer
+
+               final int maxCons = 60; // default max_clients is 64, 2 
connections are already open from this program
+               List<Connection> cons = new ArrayList<Connection>(maxCons);     
// Connections go in here
+               try {
+                       // spawn a lot of Connections, just for fun...
+                       int i = 1;
+                       sb.append("Establishing Connection ");
+                       for (; i <= maxCons; i++) {
+                               sb.append(i);
+                               Connection conx = 
DriverManager.getConnection(arg0);
+                               sb.append(",");
+                               cons.add(conx);
+
+                               // do something with the connection to test if 
it works
+                               conx.setAutoCommit(false);
+                               sb.append(" ");
+                               conx.createStatement();
+                       }
+                       sb.append("\n");
+
+                       // now try to nicely close them
+                       i = 1;
+                       sb.append("Closing Connection ");
+                       for (Iterator<Connection> it = cons.iterator(); 
it.hasNext(); i++) {
+                               Connection conx = it.next();
+                               // see if the connection still works
+                               sb.append(i);
+                               conx.setAutoCommit(true);
+                               sb.append(",");
+                               conx.close();   // this will also implicitly 
close the created statement object
+                               sb.append(" ");
+                       }
+               } catch (SQLException e) {
+                       sb.append("FAILED: 
").append(e.getMessage()).append("\n");
+               }
+
+               compareExpectedOutput("Test_Cmanycon",
+                       "Establishing Connection 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
" +
+                       "11, 12, 13, 14, 15, 16, 17, 18, 19, 20, " +
+                       "21, 22, 23, 24, 25, 26, 27, 28, 29, 30, " +
+                       "31, 32, 33, 34, 35, 36, 37, 38, 39, 40, " +
_______________________________________________
checkin-list mailing list
checkin-list@monetdb.org
https://www.monetdb.org/mailman/listinfo/checkin-list

Reply via email to