package org.drizzle.jdbc;

import org.drizzle.jdbc.exception.SQLQueryTimedOutException;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLNonTransientException;
import java.sql.Statement;

import static org.junit.Assert.assertTrue;

public class CancelTest {
    @Test
    public void cancelQuery() throws SQLException, InterruptedException {
        Connection conn = DriverManager.getConnection("jdbc:drizzle://"+DriverTest.host+":3306/test_units_jdbc");
        Statement stmt = conn.createStatement();

        new QueryThread(stmt).start();
        Thread.sleep(1000);
        stmt.cancel();
        Thread.sleep(100); // need to wait for server to properly finish - not likely in a real app.
// verify that the connection is still valid:
        ResultSet rs = stmt.executeQuery("SELECT 1 FROM DUAL");
        assertTrue(rs.next());
    }
    private static class QueryThread extends Thread {
        private final Statement stmt;

        public QueryThread(Statement stmt) {
            this.stmt = stmt;
        }
        @Override
        public void run() {
            try {
                stmt.execute("select * from information_schema.columns, information_schema.tables, information_schema.table_constraints");

            } catch (SQLException e) {
                System.out.println(e.getSQLState());
                //e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }

    @Test(expected = SQLQueryTimedOutException.class)
    public void timeoutQuery() throws SQLException, InterruptedException {
        Connection conn = DriverManager.getConnection("jdbc:drizzle://"+DriverTest.host+":3306/test_units_jdbc");
        Statement stmt = conn.createStatement();
        stmt.setQueryTimeout(1);
        stmt.executeQuery("select * from information_schema.columns, information_schema.tables, information_schema.table_constraints");

    }
    @Test(expected = SQLQueryTimedOutException.class)
    public void timeoutPrepQuery() throws SQLException, InterruptedException {
        Connection conn = DriverManager.getConnection("jdbc:drizzle://"+DriverTest.host+":3306/test_units_jdbc");
        PreparedStatement stmt = conn.prepareStatement("select * from information_schema.columns, information_schema.tables, information_schema.table_constraints");
        stmt.setQueryTimeout(1);
        stmt.execute();
    }
    @Test(expected = SQLNonTransientException.class)
    public void connectionTimeout() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:drizzle://www.google.com:1234/test_units_jdbc?connectTimeout=1");
    }

    
}

@@ -20,16 +20,78 @@
 .jdbc;%0A%0A
+import org.drizzle.jdbc.exception.SQLQueryCancelledException;%0A
 import o
@@ -467,16 +467,61 @@
    @Test
+(expected = SQLQueryCancelledException.class)
 %0A    pub
@@ -750,17 +750,17 @@
 ment();%0A
-%0A
+ 
         
@@ -763,21 +763,22 @@
     new 
-Query
+Cancel
 Thread(s
@@ -803,292 +803,131 @@
     
-Thread.sleep(1000);%0A        stmt.cancel();%0A        Thread.sleep(100); // need to wait for server to properly finish - not likely in a real app.%0A// verify that the connection is still valid:%0A        ResultSet rs = stmt.executeQuery(%22SELECT 1 FROM DUAL%22);%0A        assertTrue(rs.next());
+stmt.execute(%22select * from information_schema.columns, information_schema.tables, information_schema.table_constraints%22);%0A
 %0A   
@@ -954,21 +954,22 @@
 c class 
-Query
+Cancel
 Thread e
@@ -1038,21 +1038,22 @@
  public 
-Query
+Cancel
 Thread(S
@@ -1193,128 +1193,57 @@
     
-stmt.execute(%22select * from information_schema.columns, information_schema.tables, information_schema.table_constraints%22
+Thread.sleep(1000);%0A%0A                stmt.cancel(
 );%0A%0A
@@ -1301,43 +1301,27 @@
     
-System.out.println(e.getSQLStat
+e.printStackTrac
 e()
-)
 ;%0A  
@@ -1338,77 +1338,197 @@
     
-//e.printStackTrace();%0A                throw new RuntimeException(e);
+throw new RuntimeException(e);%0A            %7D catch (InterruptedException e) %7B%0A                e.printStackTrace();  //To change body of catch statement use File %7C Settings %7C File Templates.
 %0A   

fix test