diff --git a/src/test/java/net/starschema/clouddb/jdbc/BQForwardOnlyResultSetFunctionTest.java b/src/test/java/net/starschema/clouddb/jdbc/BQForwardOnlyResultSetFunctionTest.java index a1e5a42..5ffaa53 100644 --- a/src/test/java/net/starschema/clouddb/jdbc/BQForwardOnlyResultSetFunctionTest.java +++ b/src/test/java/net/starschema/clouddb/jdbc/BQForwardOnlyResultSetFunctionTest.java @@ -29,7 +29,6 @@ import java.io.IOException; import java.math.BigDecimal; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -62,35 +61,43 @@ */ public class BQForwardOnlyResultSetFunctionTest extends CommonTestsForResultSets { - private static java.sql.Connection con = null; + private java.sql.Connection connection; + private java.sql.Connection standardSqlConnection; private java.sql.ResultSet resultForTest = null; Logger logger = LoggerFactory.getLogger(BQForwardOnlyResultSetFunctionTest.class); private Integer maxRows = null; - private String defaultProjectId = null; - private BQConnection defaultConn = null; + + private Connection connect(final String extraUrl) throws SQLException, IOException { + return ConnectionFromResources.connect("installedaccount1.properties", extraUrl); + } @Before - public void setup() throws SQLException, IOException { - Properties props = - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount.properties").getFile()); - String url = BQSupportFuncts.constructUrlFromPropertiesFile(props, true, null); - url += "&useLegacySql=false"; - this.defaultProjectId = props.getProperty("projectid"); - this.defaultConn = new BQConnection(url, new Properties()); + public void setConnection() throws SQLException, IOException { + connection = connect("&useLegacySql=true"); + } + + @Before + public void setStandardSqlConnection() throws SQLException, IOException { + standardSqlConnection = connect("&useLegacySql=false"); } @After - public void teardown() throws SQLException { - if (defaultConn != null) { - defaultConn.close(); - defaultConn = null; - } + public void closeConnection() throws SQLException { + connection.close(); } - private BQConnection conn() throws SQLException, IOException { - return this.defaultConn; + @After + public void closeStandardSqlConnection() throws SQLException { + standardSqlConnection.close(); + } + + private BQConnection bqConnection() { + return (BQConnection) connection; + } + + private BQConnection bqStandardSqlConnection() { + return (BQConnection) standardSqlConnection; } @Test @@ -143,7 +150,10 @@ public void databaseMetaDataGetTables() { this.QueryLoad(); ResultSet result = null; try { - result = con.getMetaData().getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); + result = + connection + .getMetaData() + .getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); } catch (SQLException e) { e.printStackTrace(); Assert.fail(); @@ -176,79 +186,43 @@ public void databaseMetaDataGetTables() { public void isClosedValidtest() { this.QueryLoad(); try { - Assert.assertEquals(true, BQForwardOnlyResultSetFunctionTest.con.isValid(0)); + Assert.assertEquals(true, connection.isValid(0)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - Assert.assertEquals(true, BQForwardOnlyResultSetFunctionTest.con.isValid(10)); + Assert.assertEquals(true, connection.isValid(10)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - BQForwardOnlyResultSetFunctionTest.con.isValid(-10); + connection.isValid(-10); } catch (SQLException e) { Assert.assertTrue(true); // e.printStackTrace(); } try { - BQForwardOnlyResultSetFunctionTest.con.close(); + connection.close(); } catch (SQLException e) { e.printStackTrace(); } try { - Assert.assertTrue(BQForwardOnlyResultSetFunctionTest.con.isClosed()); + Assert.assertTrue(connection.isClosed()); } catch (SQLException e1) { e1.printStackTrace(); } try { - BQForwardOnlyResultSetFunctionTest.con.isValid(0); + connection.isValid(0); } catch (SQLException e) { Assert.assertTrue(true); e.printStackTrace(); } } - /** - * Makes a new Bigquery Connection to URL in file and gives back the Connection to static con - * member. - */ - @Before - public void NewConnection() { - NewConnection("&useLegacySql=true"); - } - - void NewConnection(String extraUrl) { - this.logger.info("Testing the JDBC driver"); - try { - Class.forName("net.starschema.clouddb.jdbc.BQDriver"); - Properties props = - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile()); - String jdcbUrl = BQSupportFuncts.constructUrlFromPropertiesFile(props); - if (extraUrl != null) { - jdcbUrl += extraUrl; - } - if (BQForwardOnlyResultSetFunctionTest.con != null) { - BQForwardOnlyResultSetFunctionTest.con.close(); - } - BQForwardOnlyResultSetFunctionTest.con = - DriverManager.getConnection( - jdcbUrl, - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile())); - } catch (Exception e) { - e.printStackTrace(); - this.logger.error("Error in connection" + e.toString()); - Assert.fail("General Exception:" + e.toString()); - } - this.logger.info(((BQConnection) BQForwardOnlyResultSetFunctionTest.con).getURLPART()); - } - // Comprehensive Tests: public void QueryLoad() { @@ -260,8 +234,7 @@ public void QueryLoad() { try { // Statement stmt = BQResultSetFunctionTest.con.createStatement(); Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); + connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); if (this.maxRows != null) { stmt.setMaxRows(this.maxRows); @@ -460,11 +433,10 @@ public void testResultSetTypesInGetString() throws SQLException { + "STRUCT(1 as a, ['an', 'array'] as b)," + "TIMESTAMP('2012-01-01 00:00:03.0000') as t"; - this.NewConnection("&useLegacySql=false"); java.sql.ResultSet result = null; try { Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); result = stmt.executeQuery(sql); @@ -522,9 +494,8 @@ public void testResultSetDateTimeType() throws SQLException, ParseException { final DateFormat utcDateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); utcDateFormatter.setTimeZone(TimeZone.getTimeZone("UTC")); - this.NewConnection("&useLegacySql=false"); Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); ResultSet result = stmt.executeQuery(sql); @@ -556,9 +527,8 @@ public void testResultSetDateTimeType() throws SQLException, ParseException { public void testResultSetTimestampType() throws SQLException, ParseException { final String sql = "SELECT TIMESTAMP('2012-01-01 01:02:03.04567')"; - this.NewConnection("&useLegacySql=false"); Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); ResultSet result = stmt.executeQuery(sql); @@ -589,12 +559,10 @@ public void testResultSetTypesInGetObject() throws SQLException, ParseException + "CAST('2011-04-03' AS DATE), " + "CAST('nan' AS FLOAT)"; - this.NewConnection("&useLegacySql=true"); java.sql.ResultSet result = null; try { Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); + connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); result = stmt.executeQuery(sql); } catch (SQLException e) { @@ -617,11 +585,10 @@ public void testResultSetTypesInGetObject() throws SQLException, ParseException public void testResultSetArraysInGetObject() throws SQLException, ParseException { final String sql = "SELECT [1, 2, 3], [TIMESTAMP(\"2010-09-07 15:30:00 America/Los_Angeles\")]"; - this.NewConnection("&useLegacySql=false"); java.sql.ResultSet result = null; try { Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); result = stmt.executeQuery(sql); @@ -651,12 +618,11 @@ public void testResultSetArraysInGetObject() throws SQLException, ParseException @Test public void testResultSetTimeType() throws SQLException, ParseException { final String sql = "select current_time(), CAST('00:00:02.12345' AS TIME)"; - this.NewConnection("&useLegacySql=false"); java.sql.ResultSet result = null; try { Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); result = stmt.executeQuery(sql); @@ -709,11 +675,10 @@ public void testResultSetProcedures() throws SQLException, ParseException { final String sql = "CREATE PROCEDURE looker_test.procedure_test(target_id INT64)\n" + "BEGIN\n" + "END;"; - this.NewConnection("&useLegacySql=false"); java.sql.ResultSet result = null; try { Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); result = stmt.executeQuery(sql); @@ -723,23 +688,22 @@ public void testResultSetProcedures() throws SQLException, ParseException { } finally { String cleanupSql = "DROP PROCEDURE looker_test.procedure_test;\n"; Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); stmt.executeQuery(cleanupSql); } - System.out.println(result.toString()); + // System.out.println(result.toString()); } @Test public void testResultSetProceduresAsync() throws SQLException { final String sql = "CREATE PROCEDURE looker_test.long_procedure(target_id INT64)\n" + "BEGIN\n" + "END;"; - this.NewConnection("&useLegacySql=false"); try { - BQConnection bq = conn(); + BQConnection bq = bqStandardSqlConnection(); BQStatement stmt = new BQStatement( bq.getProjectId(), bq, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) { @@ -751,13 +715,13 @@ protected long getSyncTimeoutMillis() { stmt.setQueryTimeout(500); stmt.executeQuery(sql); - } catch (SQLException | IOException e) { + } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } finally { String cleanupSql = "DROP PROCEDURE looker_test.long_procedure;\n"; Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); stmt.executeQuery(cleanupSql); @@ -766,20 +730,20 @@ protected long getSyncTimeoutMillis() { @Test public void testBQForwardOnlyResultSetDoesntThrowNPE() throws Exception { - BQConnection bq = conn(); + BQConnection bq = bqConnection(); BQStatement stmt = (BQStatement) bq.createStatement(); QueryResponse qr = stmt.runSyncQuery("SELECT 1", false); Job ref = bq.getBigquery() .jobs() - .get(defaultProjectId, qr.getJobReference().getJobId()) + .get(bq.getProjectId(), qr.getJobReference().getJobId()) .setLocation(qr.getJobReference().getLocation()) .execute(); // Under certain race conditions we could close the connection after the job is complete but // before the results have been fetched. This was throwing a NPE. bq.close(); try { - new BQForwardOnlyResultSet(bq.getBigquery(), defaultProjectId, ref, null, stmt); + new BQForwardOnlyResultSet(bq.getBigquery(), bq.getProjectId(), ref, null, stmt); Assert.fail("Initalizing BQForwardOnlyResultSet should throw something other than a NPE."); } catch (SQLException e) { Assert.assertEquals(e.getMessage(), "Failed to fetch results. Connection is closed."); @@ -799,9 +763,8 @@ public void testHandlesAllNullResponseFields() throws Exception { @Test public void testHandlesNullTimeDateObjects() throws Exception { - this.NewConnection("&useLegacySql=false"); Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( + standardSqlConnection.createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); final String date = "2011-11-11"; @@ -888,20 +851,22 @@ private void mockResponse(String jsonResponse) throws Exception { } @Test - public void testStatelessQuery() throws SQLException { - NewConnection("&useLegacySql=false&jobcreationmode=JOB_CREATION_OPTIONAL"); - StatelessQuery.assumeStatelessQueriesEnabled( - BQForwardOnlyResultSetFunctionTest.con.getCatalog()); - final Statement stmt = - BQForwardOnlyResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); - final ResultSet result = stmt.executeQuery(StatelessQuery.exampleQuery()); - final String[][] rows = BQSupportMethods.GetQueryResult(result); - Assertions.assertThat(rows).isEqualTo(StatelessQuery.exampleValues()); - - final BQForwardOnlyResultSet bqResultSet = (BQForwardOnlyResultSet) result; - Assertions.assertThat(bqResultSet.getJobId()).isNull(); - Assertions.assertThat(bqResultSet.getQueryId()).contains("!"); + public void testStatelessQuery() throws SQLException, IOException { + try (Connection statelessConnection = + connect("&useLegacySql=false&jobcreationmode=JOB_CREATION_OPTIONAL")) { + StatelessQuery.assumeStatelessQueriesEnabled(statelessConnection.getCatalog()); + try (Statement stmt = + statelessConnection.createStatement( + ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) { + final ResultSet result = stmt.executeQuery(StatelessQuery.exampleQuery()); + final String[][] rows = BQSupportMethods.GetQueryResult(result); + Assertions.assertThat(rows).isEqualTo(StatelessQuery.exampleValues()); + + final BQForwardOnlyResultSet bqResultSet = (BQForwardOnlyResultSet) result; + Assertions.assertThat(bqResultSet.getJobId()).isNull(); + Assertions.assertThat(bqResultSet.getQueryId()).contains("!"); + } + } } @Override diff --git a/src/test/java/net/starschema/clouddb/jdbc/BQResultSetFunctionTest.java b/src/test/java/net/starschema/clouddb/jdbc/BQResultSetFunctionTest.java index ece2c36..4af5204 100644 --- a/src/test/java/net/starschema/clouddb/jdbc/BQResultSetFunctionTest.java +++ b/src/test/java/net/starschema/clouddb/jdbc/BQResultSetFunctionTest.java @@ -20,11 +20,12 @@ */ package net.starschema.clouddb.jdbc; -import java.sql.DriverManager; +import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import junit.framework.Assert; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -38,40 +39,111 @@ */ public class BQResultSetFunctionTest { - private static java.sql.Connection con = null; - private static java.sql.ResultSet Result = null; + private java.sql.Connection connection; + + private java.sql.ResultSet result; Logger logger = LoggerFactory.getLogger(BQResultSetFunctionTest.class); + @Before + public void setConnection() throws SQLException, IOException { + connection = + ConnectionFromResources.connect("installedaccount1.properties", "&useLegacySql=true"); + QueryLoad(); + } + + @After + public void closeConnection() throws SQLException { + connection.close(); + } + + /** + * Prints a String[][] QueryResult to Log + * + * @param input + */ + private void printer(String[][] input) { + for (int s = 0; s < input[0].length; s++) { + String Output = ""; + for (int i = 0; i < input.length; i++) { + if (i == input.length - 1) { + Output += input[i][s]; + } else { + Output += input[i][s] + "\t"; + } + } + this.logger.debug(Output); + } + } + + public void QueryLoad() { + final String sql = + "SELECT TOP(word,10) AS word, COUNT(*) as count FROM publicdata:samples.shakespeare"; + final String description = "The top 10 word from shakespeare #TOP #COUNT"; + String[][] expectation = + new String[][] { + {"you", "yet", "would", "world", "without", "with", "will", "why", "whose", "whom"}, + {"42", "42", "42", "42", "42", "42", "42", "42", "42", "42"} + }; + + this.logger.info("Test number: 01"); + this.logger.info("Running query:" + sql); + + try { + Statement stmt = + connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + stmt.setQueryTimeout(500); + result = stmt.executeQuery(sql); + } catch (SQLException e) { + this.logger.error("SQLexception" + e.toString()); + Assert.fail("SQLException" + e.toString()); + } + Assert.assertNotNull(result); + + this.logger.debug(description); + this.printer(expectation); + + try { + Assert.assertTrue( + "Comparing failed in the String[][] array", + this.comparer(expectation, BQSupportMethods.GetQueryResult(result))); + } catch (SQLException e) { + this.logger.error("SQLexception" + e.toString()); + Assert.fail(e.toString()); + } + } + + // Comprehensive Tests: + @Test public void ChainedCursorFunctionTest() { this.logger.info("ChainedFunctionTest"); try { - BQResultSetFunctionTest.Result.beforeFirst(); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertTrue(result.next()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQResultSetFunctionTest.Result.next()); + Assert.assertFalse(result.next()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -83,42 +155,42 @@ public void ChainedCursorFunctionTest() { } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.first()); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.isFirst()); - Assert.assertFalse(BQResultSetFunctionTest.Result.previous()); - BQResultSetFunctionTest.Result.afterLast(); - Assert.assertTrue(BQResultSetFunctionTest.Result.isAfterLast()); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(-1)); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.isFirst()); + Assert.assertFalse(result.previous()); + result.afterLast(); + Assert.assertTrue(result.isAfterLast()); + Assert.assertTrue(result.absolute(-1)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(-5)); - Assert.assertEquals("without", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.relative(-5)); + Assert.assertEquals("without", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQResultSetFunctionTest.Result.relative(6)); + Assert.assertFalse(result.relative(6)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("without", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("without", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -142,7 +214,10 @@ public void databaseMetaDataGetTables() { // tableNamePattern:OUTLET_LOOKUP, columnNamePattern: null // result = con.getMetaData().getTables("OUTLET_LOOKUP", null, "starschema_net__clouddb", null // ); - result = con.getMetaData().getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); + result = + connection + .getMetaData() + .getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); // Function call getTables(catalog: ARTICLE_COLOR_LOOKUP, schemaPattern: null, // tableNamePattern: starschema_net__clouddb, types: TABLE , VIEW , SYSTEM TABLE , SYNONYM , // ALIAS , ) @@ -196,143 +271,48 @@ private boolean comparer(String[][] expected, String[][] reality) { @Test public void isClosedValidtest() { try { - Assert.assertEquals(true, BQResultSetFunctionTest.con.isValid(0)); + Assert.assertEquals(true, connection.isValid(0)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - Assert.assertEquals(true, BQResultSetFunctionTest.con.isValid(10)); + Assert.assertEquals(true, connection.isValid(10)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - BQResultSetFunctionTest.con.isValid(-10); + connection.isValid(-10); } catch (SQLException e) { Assert.assertTrue(true); // e.printStackTrace(); } try { - BQResultSetFunctionTest.con.close(); + connection.close(); } catch (SQLException e) { e.printStackTrace(); } try { - Assert.assertTrue(BQResultSetFunctionTest.con.isClosed()); + Assert.assertTrue(connection.isClosed()); } catch (SQLException e1) { e1.printStackTrace(); } try { - BQResultSetFunctionTest.con.isValid(0); + connection.isValid(0); } catch (SQLException e) { Assert.assertTrue(true); e.printStackTrace(); } } - /** - * Makes a new Bigquery Connection to URL in file and gives back the Connection to static con - * member. - */ - @Before - public void NewConnection() { - - try { - if (BQResultSetFunctionTest.con == null || !BQResultSetFunctionTest.con.isValid(0)) { - this.logger.info("Testing the JDBC driver"); - try { - Class.forName("net.starschema.clouddb.jdbc.BQDriver"); - String jdbcUrl = - BQSupportFuncts.constructUrlFromPropertiesFile( - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile())); - jdbcUrl += "&useLegacySql=true"; - BQResultSetFunctionTest.con = - DriverManager.getConnection( - jdbcUrl, - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile())); - } catch (Exception e) { - e.printStackTrace(); - this.logger.error("Error in connection" + e.toString()); - Assert.fail("General Exception:" + e.toString()); - } - this.logger.info(((BQConnection) BQResultSetFunctionTest.con).getURLPART()); - } - } catch (SQLException e) { - logger.debug("Oops something went wrong", e); - } - this.QueryLoad(); - } - - // Comprehensive Tests: - - /** - * Prints a String[][] QueryResult to Log - * - * @param input - */ - private void printer(String[][] input) { - for (int s = 0; s < input[0].length; s++) { - String Output = ""; - for (int i = 0; i < input.length; i++) { - if (i == input.length - 1) { - Output += input[i][s]; - } else { - Output += input[i][s] + "\t"; - } - } - this.logger.debug(Output); - } - } - - public void QueryLoad() { - final String sql = - "SELECT TOP(word,10) AS word, COUNT(*) as count FROM publicdata:samples.shakespeare"; - final String description = "The top 10 word from shakespeare #TOP #COUNT"; - String[][] expectation = - new String[][] { - {"you", "yet", "would", "world", "without", "with", "will", "why", "whose", "whom"}, - {"42", "42", "42", "42", "42", "42", "42", "42", "42", "42"} - }; - - this.logger.info("Test number: 01"); - this.logger.info("Running query:" + sql); - - try { - Statement stmt = - BQResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - stmt.setQueryTimeout(500); - BQResultSetFunctionTest.Result = stmt.executeQuery(sql); - } catch (SQLException e) { - this.logger.error("SQLexception" + e.toString()); - Assert.fail("SQLException" + e.toString()); - } - Assert.assertNotNull(BQResultSetFunctionTest.Result); - - this.logger.debug(description); - this.printer(expectation); - - try { - Assert.assertTrue( - "Comparing failed in the String[][] array", - this.comparer( - expectation, BQSupportMethods.GetQueryResult(BQResultSetFunctionTest.Result))); - } catch (SQLException e) { - this.logger.error("SQLexception" + e.toString()); - Assert.fail(e.toString()); - } - } - @Test public void ResultSetMetadata() { try { - this.logger.debug(BQResultSetFunctionTest.Result.getMetaData().getSchemaName(1)); - this.logger.debug("{}", BQResultSetFunctionTest.Result.getMetaData().getScale(1)); + this.logger.debug(result.getMetaData().getSchemaName(1)); + this.logger.debug("{}", result.getMetaData().getScale(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); } @@ -342,7 +322,7 @@ public void ResultSetMetadata() { @Test public void TestResultIndexOutofBound() { try { - this.logger.debug("{}", BQResultSetFunctionTest.Result.getBoolean(99)); + this.logger.debug("{}", result.getBoolean(99)); } catch (SQLException e) { Assert.assertTrue(true); this.logger.error("SQLexception" + e.toString()); @@ -352,34 +332,34 @@ public void TestResultIndexOutofBound() { @Test public void TestResultSetAbsolute() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(2)); - Assert.assertEquals("yet", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(3)); - Assert.assertEquals("would", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(4)); - Assert.assertEquals("world", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(5)); - Assert.assertEquals("without", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(6)); - Assert.assertEquals("with", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(7)); - Assert.assertEquals("will", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(8)); - Assert.assertEquals("why", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(9)); - Assert.assertEquals("whose", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.absolute(2)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.absolute(3)); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.absolute(4)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.absolute(5)); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.absolute(6)); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.absolute(7)); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.absolute(8)); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.absolute(9)); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQResultSetFunctionTest.Result.absolute(0)); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertFalse(result.absolute(0)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -391,8 +371,8 @@ public void TestResultSetAbsolute() { } try { - Assert.assertFalse(BQResultSetFunctionTest.Result.absolute(11)); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertFalse(result.absolute(11)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -407,17 +387,17 @@ public void TestResultSetAbsolute() { @Test public void TestResultSetAfterlast() { try { - BQResultSetFunctionTest.Result.afterLast(); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); + result.afterLast(); + Assert.assertTrue(result.previous()); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQResultSetFunctionTest.Result.afterLast(); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + result.afterLast(); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -432,17 +412,17 @@ public void TestResultSetAfterlast() { @Test public void TestResultSetBeforeFirst() { try { - BQResultSetFunctionTest.Result.beforeFirst(); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertTrue(result.next()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQResultSetFunctionTest.Result.beforeFirst(); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -457,8 +437,8 @@ public void TestResultSetBeforeFirst() { @Test public void TestResultSetFirst() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.first()); - Assert.assertTrue(BQResultSetFunctionTest.Result.isFirst()); + Assert.assertTrue(result.first()); + Assert.assertTrue(result.isFirst()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -468,8 +448,8 @@ public void TestResultSetFirst() { @Test public void TestResultSetgetBoolean() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(Boolean.parseBoolean("42"), BQResultSetFunctionTest.Result.getBoolean(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(Boolean.parseBoolean("42"), result.getBoolean(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -479,8 +459,8 @@ public void TestResultSetgetBoolean() { @Test public void TestResultSetgetFloat() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(new Float(42), BQResultSetFunctionTest.Result.getFloat(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(new Float(42), result.getFloat(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -490,8 +470,8 @@ public void TestResultSetgetFloat() { @Test public void TestResultSetgetInteger() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(42, BQResultSetFunctionTest.Result.getInt(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(42, result.getInt(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -502,19 +482,19 @@ public void TestResultSetgetInteger() { public void TestResultSetgetRow() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(1, BQResultSetFunctionTest.Result.getRow()); - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals(10, BQResultSetFunctionTest.Result.getRow()); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(1, result.getRow()); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals(10, result.getRow()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQResultSetFunctionTest.Result.beforeFirst(); - Assert.assertEquals(0, BQResultSetFunctionTest.Result.getRow()); - BQResultSetFunctionTest.Result.afterLast(); - Assert.assertEquals(0, BQResultSetFunctionTest.Result.getRow()); + result.beforeFirst(); + Assert.assertEquals(0, result.getRow()); + result.afterLast(); + Assert.assertEquals(0, result.getRow()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -524,10 +504,10 @@ public void TestResultSetgetRow() { @Test public void TestResultSetgetString() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.first()); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.last()); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.last()); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -537,8 +517,8 @@ public void TestResultSetgetString() { @Test public void TestResultSetLast() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.last()); - Assert.assertTrue(BQResultSetFunctionTest.Result.isLast()); + Assert.assertTrue(result.last()); + Assert.assertTrue(result.isLast()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -548,33 +528,33 @@ public void TestResultSetLast() { @Test public void TestResultSetNext() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.first()); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("yet", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("would", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("world", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("without", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("with", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("will", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("why", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("whose", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.next()); - Assert.assertEquals("whom", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertFalse(BQResultSetFunctionTest.Result.next()); + Assert.assertTrue(result.first()); + Assert.assertTrue(result.next()); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("whom", result.getString(1)); + Assert.assertFalse(result.next()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -589,32 +569,32 @@ public void TestResultSetNext() { @Test public void TestResultSetPrevious() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.last()); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("whose", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("why", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("will", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("with", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("without", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("world", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("would", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("yet", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.previous()); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertFalse(BQResultSetFunctionTest.Result.previous()); + Assert.assertTrue(result.last()); + Assert.assertTrue(result.previous()); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("you", result.getString(1)); + Assert.assertFalse(result.previous()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -629,28 +609,28 @@ public void TestResultSetPrevious() { @Test public void TestResultSetRelative() { try { - Assert.assertTrue(BQResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(1)); - Assert.assertEquals("yet", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(2)); - Assert.assertEquals("world", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(5)); - Assert.assertEquals("whose", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(-5)); - Assert.assertEquals("world", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(-2)); - Assert.assertEquals("yet", BQResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQResultSetFunctionTest.Result.relative(-1)); - Assert.assertEquals("you", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.relative(1)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.relative(2)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.relative(5)); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.relative(-5)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.relative(-2)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.relative(-1)); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.first()); - Assert.assertFalse(BQResultSetFunctionTest.Result.relative(-1)); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertFalse(result.relative(-1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -662,9 +642,9 @@ public void TestResultSetRelative() { } try { - Assert.assertTrue(BQResultSetFunctionTest.Result.last()); - Assert.assertFalse(BQResultSetFunctionTest.Result.relative(1)); - Assert.assertEquals("", BQResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.last()); + Assert.assertFalse(result.relative(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { diff --git a/src/test/java/net/starschema/clouddb/jdbc/BQScrollableResultSetFunctionTest.java b/src/test/java/net/starschema/clouddb/jdbc/BQScrollableResultSetFunctionTest.java index d0dfa48..81b7762 100644 --- a/src/test/java/net/starschema/clouddb/jdbc/BQScrollableResultSetFunctionTest.java +++ b/src/test/java/net/starschema/clouddb/jdbc/BQScrollableResultSetFunctionTest.java @@ -22,8 +22,8 @@ import com.google.api.client.testing.http.MockHttpTransport; import com.google.api.client.testing.http.MockLowLevelHttpResponse; +import java.io.IOException; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -45,40 +45,39 @@ */ public class BQScrollableResultSetFunctionTest extends CommonTestsForResultSets { - private static java.sql.Connection con = null; - private static java.sql.ResultSet Result = null; - + private java.sql.Connection connection; + private java.sql.ResultSet result; Logger logger = LoggerFactory.getLogger(BQScrollableResultSetFunctionTest.class); @Test public void ChainedCursorFunctionTest() { this.logger.info("ChainedFunctionTest"); try { - BQScrollableResultSetFunctionTest.Result.beforeFirst(); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertTrue(result.next()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.next()); + Assert.assertFalse(result.next()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -90,42 +89,42 @@ public void ChainedCursorFunctionTest() { } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.first()); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.isFirst()); - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.previous()); - BQScrollableResultSetFunctionTest.Result.afterLast(); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.isAfterLast()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(-1)); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.isFirst()); + Assert.assertFalse(result.previous()); + result.afterLast(); + Assert.assertTrue(result.isAfterLast()); + Assert.assertTrue(result.absolute(-1)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(-5)); - Assert.assertEquals("without", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.relative(-5)); + Assert.assertEquals("without", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.relative(6)); + Assert.assertFalse(result.relative(6)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("without", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("without", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -149,7 +148,10 @@ public void databaseMetaDataGetTables() { // tableNamePattern:OUTLET_LOOKUP, columnNamePattern: null // result = con.getMetaData().getTables("OUTLET_LOOKUP", null, "starschema_net__clouddb", null // ); - result = con.getMetaData().getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); + result = + connection + .getMetaData() + .getColumns(null, "starschema_net__clouddb", "OUTLET_LOOKUP", null); // Function call getTables(catalog: ARTICLE_COLOR_LOOKUP, schemaPattern: null, // tableNamePattern: starschema_net__clouddb, types: TABLE , VIEW , SYSTEM TABLE , SYNONYM , // ALIAS , ) @@ -203,88 +205,56 @@ private boolean comparer(String[][] expected, String[][] reality) { @Test public void isClosedValidtest() { try { - Assert.assertEquals(true, BQScrollableResultSetFunctionTest.con.isValid(0)); + Assert.assertEquals(true, connection.isValid(0)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - Assert.assertEquals(true, BQScrollableResultSetFunctionTest.con.isValid(10)); + Assert.assertEquals(true, connection.isValid(10)); } catch (SQLException e) { Assert.fail("Got an exception" + e.toString()); e.printStackTrace(); } try { - BQScrollableResultSetFunctionTest.con.isValid(-10); + connection.isValid(-10); } catch (SQLException e) { Assert.assertTrue(true); // e.printStackTrace(); } try { - BQScrollableResultSetFunctionTest.con.close(); + connection.close(); } catch (SQLException e) { e.printStackTrace(); } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.con.isClosed()); + Assert.assertTrue(connection.isClosed()); } catch (SQLException e1) { e1.printStackTrace(); } try { - BQScrollableResultSetFunctionTest.con.isValid(0); + connection.isValid(0); } catch (SQLException e) { Assert.assertTrue(true); e.printStackTrace(); } } - /** - * Makes a new Bigquery Connection to URL in file and gives back the Connection to static con - * member. - */ + private Connection connect(final String extraUrl) throws SQLException, IOException { + return ConnectionFromResources.connect("installedaccount1.properties", extraUrl); + } + @Before - public void NewConnection() { - NewConnection("&useLegacySql=true"); + public void setConnection() throws SQLException, IOException { + connection = connect("&useLegacySql=true"); + QueryLoad(); } @After public void closeConnection() throws SQLException { - BQScrollableResultSetFunctionTest.con.close(); - BQScrollableResultSetFunctionTest.con = null; - } - - public void NewConnection(String extraUrl) { - try { - if (BQScrollableResultSetFunctionTest.con == null - || !BQScrollableResultSetFunctionTest.con.isValid(0)) { - this.logger.info("Testing the JDBC driver"); - try { - Class.forName("net.starschema.clouddb.jdbc.BQDriver"); - String jdbcUrl = - BQSupportFuncts.constructUrlFromPropertiesFile( - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile())); - if (jdbcUrl != null) { - jdbcUrl += extraUrl; - } - BQScrollableResultSetFunctionTest.con = - DriverManager.getConnection( - jdbcUrl, - BQSupportFuncts.readFromPropFile( - getClass().getResource("/installedaccount1.properties").getFile())); - } catch (Exception e) { - e.printStackTrace(); - this.logger.error("Error in connection" + e.toString()); - Assert.fail("General Exception:" + e.toString()); - } - this.logger.info(((BQConnection) BQScrollableResultSetFunctionTest.con).getURLPART()); - } - } catch (SQLException e) { - logger.debug("Oops something went wrong", e); - } - this.QueryLoad(); + connection.close(); } // Comprehensive Tests: @@ -303,15 +273,14 @@ public void QueryLoad() { try { Statement stmt = - BQScrollableResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stmt.setQueryTimeout(500); - BQScrollableResultSetFunctionTest.Result = stmt.executeQuery(sql); + result = stmt.executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } - Assert.assertNotNull(BQScrollableResultSetFunctionTest.Result); + Assert.assertNotNull(result); this.logger.debug(description); HelperFunctions.printer(expectation); @@ -319,9 +288,7 @@ public void QueryLoad() { try { Assert.assertTrue( "Comparing failed in the String[][] array", - this.comparer( - expectation, - BQSupportMethods.GetQueryResult(BQScrollableResultSetFunctionTest.Result))); + this.comparer(expectation, BQSupportMethods.GetQueryResult(result))); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail(e.toString()); @@ -331,8 +298,8 @@ public void QueryLoad() { @Test public void ResultSetMetadata() { try { - this.logger.debug(BQScrollableResultSetFunctionTest.Result.getMetaData().getSchemaName(1)); - this.logger.debug("{}", BQScrollableResultSetFunctionTest.Result.getMetaData().getScale(1)); + this.logger.debug(result.getMetaData().getSchemaName(1)); + this.logger.debug("{}", result.getMetaData().getScale(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); } @@ -342,7 +309,7 @@ public void ResultSetMetadata() { @Test public void TestResultIndexOutofBound() { try { - this.logger.debug("{}", BQScrollableResultSetFunctionTest.Result.getBoolean(99)); + this.logger.debug("{}", result.getBoolean(99)); } catch (SQLException e) { Assert.assertTrue(true); this.logger.error("SQLexception" + e.toString()); @@ -352,34 +319,34 @@ public void TestResultIndexOutofBound() { @Test public void TestResultSetAbsolute() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(2)); - Assert.assertEquals("yet", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(3)); - Assert.assertEquals("would", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(4)); - Assert.assertEquals("world", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(5)); - Assert.assertEquals("without", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(6)); - Assert.assertEquals("with", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(7)); - Assert.assertEquals("will", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(8)); - Assert.assertEquals("why", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(9)); - Assert.assertEquals("whose", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.absolute(2)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.absolute(3)); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.absolute(4)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.absolute(5)); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.absolute(6)); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.absolute(7)); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.absolute(8)); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.absolute(9)); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.absolute(0)); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertFalse(result.absolute(0)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -391,8 +358,8 @@ public void TestResultSetAbsolute() { } try { - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.absolute(11)); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertFalse(result.absolute(11)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -407,17 +374,17 @@ public void TestResultSetAbsolute() { @Test public void TestResultSetAfterlast() { try { - BQScrollableResultSetFunctionTest.Result.afterLast(); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); + result.afterLast(); + Assert.assertTrue(result.previous()); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQScrollableResultSetFunctionTest.Result.afterLast(); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + result.afterLast(); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -432,17 +399,17 @@ public void TestResultSetAfterlast() { @Test public void TestResultSetBeforeFirst() { try { - BQScrollableResultSetFunctionTest.Result.beforeFirst(); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertTrue(result.next()); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQScrollableResultSetFunctionTest.Result.beforeFirst(); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + result.beforeFirst(); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -457,8 +424,8 @@ public void TestResultSetBeforeFirst() { @Test public void TestResultSetFirst() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.first()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.isFirst()); + Assert.assertTrue(result.first()); + Assert.assertTrue(result.isFirst()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -468,9 +435,8 @@ public void TestResultSetFirst() { @Test public void TestResultSetgetBoolean() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals( - Boolean.parseBoolean("42"), BQScrollableResultSetFunctionTest.Result.getBoolean(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(Boolean.parseBoolean("42"), result.getBoolean(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -480,8 +446,8 @@ public void TestResultSetgetBoolean() { @Test public void TestResultSetgetFloat() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(new Float(42), BQScrollableResultSetFunctionTest.Result.getFloat(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(new Float(42), result.getFloat(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -491,8 +457,8 @@ public void TestResultSetgetFloat() { @Test public void TestResultSetgetInteger() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(42, BQScrollableResultSetFunctionTest.Result.getInt(2)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(42, result.getInt(2)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -503,19 +469,19 @@ public void TestResultSetgetInteger() { public void TestResultSetgetRow() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals(1, BQScrollableResultSetFunctionTest.Result.getRow()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(10)); - Assert.assertEquals(10, BQScrollableResultSetFunctionTest.Result.getRow()); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals(1, result.getRow()); + Assert.assertTrue(result.absolute(10)); + Assert.assertEquals(10, result.getRow()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - BQScrollableResultSetFunctionTest.Result.beforeFirst(); - Assert.assertEquals(0, BQScrollableResultSetFunctionTest.Result.getRow()); - BQScrollableResultSetFunctionTest.Result.afterLast(); - Assert.assertEquals(0, BQScrollableResultSetFunctionTest.Result.getRow()); + result.beforeFirst(); + Assert.assertEquals(0, result.getRow()); + result.afterLast(); + Assert.assertEquals(0, result.getRow()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -525,10 +491,10 @@ public void TestResultSetgetRow() { @Test public void TestResultSetgetString() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.first()); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.last()); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.last()); + Assert.assertEquals("whom", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -538,8 +504,8 @@ public void TestResultSetgetString() { @Test public void TestResultSetLast() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.last()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.isLast()); + Assert.assertTrue(result.last()); + Assert.assertTrue(result.isLast()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -549,33 +515,33 @@ public void TestResultSetLast() { @Test public void TestResultSetNext() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.first()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("yet", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("would", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("world", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("without", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("with", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("will", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("why", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("whose", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.next()); - Assert.assertEquals("whom", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.next()); + Assert.assertTrue(result.first()); + Assert.assertTrue(result.next()); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.next()); + Assert.assertEquals("whom", result.getString(1)); + Assert.assertFalse(result.next()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -590,32 +556,32 @@ public void TestResultSetNext() { @Test public void TestResultSetPrevious() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.last()); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("whose", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("why", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("will", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("with", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("without", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("world", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("would", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("yet", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.previous()); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.previous()); + Assert.assertTrue(result.last()); + Assert.assertTrue(result.previous()); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("why", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("will", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("with", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("without", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("would", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.previous()); + Assert.assertEquals("you", result.getString(1)); + Assert.assertFalse(result.previous()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -630,28 +596,28 @@ public void TestResultSetPrevious() { @Test public void TestResultSetRelative() { try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.absolute(1)); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(1)); - Assert.assertEquals("yet", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(2)); - Assert.assertEquals("world", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(5)); - Assert.assertEquals("whose", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(-5)); - Assert.assertEquals("world", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(-2)); - Assert.assertEquals("yet", BQScrollableResultSetFunctionTest.Result.getString(1)); - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.relative(-1)); - Assert.assertEquals("you", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.absolute(1)); + Assert.assertEquals("you", result.getString(1)); + Assert.assertTrue(result.relative(1)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.relative(2)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.relative(5)); + Assert.assertEquals("whose", result.getString(1)); + Assert.assertTrue(result.relative(-5)); + Assert.assertEquals("world", result.getString(1)); + Assert.assertTrue(result.relative(-2)); + Assert.assertEquals("yet", result.getString(1)); + Assert.assertTrue(result.relative(-1)); + Assert.assertEquals("you", result.getString(1)); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.first()); - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.relative(-1)); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.first()); + Assert.assertFalse(result.relative(-1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -663,9 +629,9 @@ public void TestResultSetRelative() { } try { - Assert.assertTrue(BQScrollableResultSetFunctionTest.Result.last()); - Assert.assertFalse(BQScrollableResultSetFunctionTest.Result.relative(1)); - Assert.assertEquals("", BQScrollableResultSetFunctionTest.Result.getString(1)); + Assert.assertTrue(result.last()); + Assert.assertFalse(result.relative(1)); + Assert.assertEquals("", result.getString(1)); } catch (SQLException e) { boolean ct = e.toString().contains("Cursor is not in a valid Position"); if (ct == true) { @@ -679,8 +645,8 @@ public void TestResultSetRelative() { @Test public void TestResultSetTotalBytesProcessedCacheHit() { - Assert.assertTrue(Result instanceof BQScrollableResultSet); - BQScrollableResultSet results = (BQScrollableResultSet) Result; + Assert.assertTrue(result instanceof BQScrollableResultSet); + BQScrollableResultSet results = (BQScrollableResultSet) result; final Boolean processedNoBytes = new Long(0L).equals(results.getTotalBytesProcessed()); Assert.assertEquals(processedNoBytes, results.getCacheHit()); } @@ -731,21 +697,23 @@ private void mockResponse(String jsonResponse) throws Exception { } @Test - public void testStatelessQuery() throws SQLException { + public void testStatelessQuery() throws SQLException, IOException { closeConnection(); - NewConnection("&useLegacySql=true&jobcreationmode=JOB_CREATION_OPTIONAL"); - StatelessQuery.assumeStatelessQueriesEnabled( - BQScrollableResultSetFunctionTest.con.getCatalog()); - final Statement stmt = - BQScrollableResultSetFunctionTest.con.createStatement( - ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); - final ResultSet result = stmt.executeQuery(StatelessQuery.exampleQuery()); - final String[][] rows = BQSupportMethods.GetQueryResult(result); - Assertions.assertThat(rows).isEqualTo(StatelessQuery.exampleValues()); - - final BQScrollableResultSet bqResultSet = (BQScrollableResultSet) result; - Assertions.assertThat(bqResultSet.getJobId()).isNull(); - Assertions.assertThat(bqResultSet.getQueryId()).contains("!"); + try (Connection statelessConnection = + connect("&useLegacySql=true&jobcreationmode=JOB_CREATION_OPTIONAL")) { + StatelessQuery.assumeStatelessQueriesEnabled(statelessConnection.getCatalog()); + try (Statement stmt = + statelessConnection.createStatement( + ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)) { + final ResultSet result = stmt.executeQuery(StatelessQuery.exampleQuery()); + final String[][] rows = BQSupportMethods.GetQueryResult(result); + Assertions.assertThat(rows).isEqualTo(StatelessQuery.exampleValues()); + + final BQScrollableResultSet bqResultSet = (BQScrollableResultSet) result; + Assertions.assertThat(bqResultSet.getJobId()).isNull(); + Assertions.assertThat(bqResultSet.getQueryId()).contains("!"); + } + } } @Override diff --git a/src/test/java/net/starschema/clouddb/jdbc/QueryResultTest.java b/src/test/java/net/starschema/clouddb/jdbc/QueryResultTest.java index 7431dea..8e23ffd 100644 --- a/src/test/java/net/starschema/clouddb/jdbc/QueryResultTest.java +++ b/src/test/java/net/starschema/clouddb/jdbc/QueryResultTest.java @@ -20,7 +20,8 @@ */ package net.starschema.clouddb.jdbc; -import java.sql.DriverManager; +import java.io.IOException; +import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; @@ -40,7 +41,9 @@ */ public class QueryResultTest { - private static java.sql.Connection con = null; + private java.sql.Connection connection; + private java.sql.Connection standardSqlConnection; + // Logger logger = new Logger(QueryResultTest.class.getName()); Logger logger = LoggerFactory.getLogger(QueryResultTest.class.getName()); @@ -64,61 +67,37 @@ private boolean comparer(String[][] expected, String[][] reality) { return true; } - /** - * Makes a new Bigquery Connection to Hardcoded URL and gives back the Connection to static con - * member. - */ - public void NewConnection(String extraUrl) { - try { - if (QueryResultTest.con == null || !QueryResultTest.con.isValid(0) || extraUrl != null) { - - this.logger.info("Testing the JDBC driver"); - try { - Class.forName("net.starschema.clouddb.jdbc.BQDriver"); - String jdbcUrl = - BQSupportFuncts.constructUrlFromPropertiesFile( - BQSupportFuncts.readFromPropFile( - getClass().getResource("/serviceaccount.properties").getFile())); - if (extraUrl != null) { - jdbcUrl += extraUrl; - } - QueryResultTest.con = - DriverManager.getConnection( - jdbcUrl, - BQSupportFuncts.readFromPropFile( - getClass().getResource("/serviceaccount.properties").getFile())); - } catch (Exception e) { - this.logger.error("Error in connection" + e.toString()); - Assert.fail("General Exception:" + e.toString()); - } - this.logger.info(((BQConnection) QueryResultTest.con).getURLPART()); - } - } catch (SQLException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + private Connection connect(final String extraUrl) throws SQLException, IOException { + return ConnectionFromResources.connect("installedaccount1.properties", extraUrl); + } + + @Before + public void setConnection() throws SQLException, IOException { + connection = connect("&useLegacySql=true"); } @Before - public void NewConnection() { - NewConnection("&useLegacySql=true"); + public void setStandardSqlConnection() throws SQLException, IOException { + standardSqlConnection = connect("&useLegacySql=false"); } @After - public void TeardownConnection() throws SQLException { - QueryResultTest.con.close(); - QueryResultTest.con = null; + public void closeConnection() throws SQLException { + connection.close(); + } + + @After + public void closeStandardSqlConnection() throws SQLException { + standardSqlConnection.close(); } @Test public void QueryResultTestWithDataset() throws SQLException { - NewConnection("&useLegacySql=false"); - - QueryResultTest.con.setSchema("foobar"); - Assert.assertEquals("foobar", QueryResultTest.con.getSchema()); + standardSqlConnection.setSchema("foobar"); + Assert.assertEquals("foobar", standardSqlConnection.getSchema()); - QueryResultTest.con.setSchema("tokyo_star"); - Assert.assertEquals("tokyo_star", QueryResultTest.con.getSchema()); + standardSqlConnection.setSchema("tokyo_star"); + Assert.assertEquals("tokyo_star", standardSqlConnection.getSchema()); final String sql = "SELECT meaning FROM meaning_of_life GROUP BY ROLLUP(meaning);"; String[][] expectation = new String[][] {{null, "42"}}; @@ -128,7 +107,7 @@ public void QueryResultTestWithDataset() throws SQLException { java.sql.ResultSet Result = null; try { - Statement s = QueryResultTest.con.createStatement(); + Statement s = standardSqlConnection.createStatement(); s.setMaxRows(1); Result = s.executeQuery(sql); } catch (SQLException e) { @@ -165,7 +144,7 @@ public void QueryResultTest01() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -215,7 +194,7 @@ public void QueryResultTest02() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -251,7 +230,7 @@ public void QueryResultTest03() { this.logger.debug(description); java.sql.ResultSet result = null; try { - Statement stmt = con.createStatement(); + Statement stmt = connection.createStatement(); // stmt.setQueryTimeout(60); result = stmt.executeQuery(sql); } catch (SQLException e) { @@ -277,7 +256,7 @@ public void QueryResultTest04() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -308,7 +287,7 @@ public void QueryResultTest05() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); this.logger.debug("{}", Result.getMetaData().getColumnCount()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); @@ -344,7 +323,7 @@ public void QueryResultTest06() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -382,7 +361,7 @@ public void QueryResultTest07() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -423,7 +402,7 @@ public void QueryResultTest08() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -461,7 +440,7 @@ public void QueryResultTest09() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -500,7 +479,7 @@ public void QueryResultTest10() { this.logger.info("Running query:" + sql); try { - Statement stmt = QueryResultTest.con.createStatement(); + Statement stmt = connection.createStatement(); stmt.setQueryTimeout(1); stmt.executeQuery(sql); } catch (SQLException e) { @@ -516,7 +495,7 @@ public void QueryResultTest11() { this.logger.info("Testing databesmetadata ... getSchemas() "); try { - QueryResultTest.con.getMetaData().getSchemas(); + connection.getMetaData().getSchemas(); } catch (SQLException e) { this.logger.warn("SQLexception" + e.toString()); Assert.fail("schema problem"); @@ -534,7 +513,7 @@ public void QueryResultTest12() { java.sql.ResultSet Result = null; try { Statement stm = - con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); stm.setFetchSize(1000); Result = stm.executeQuery(sql); } catch (SQLException e) { @@ -561,8 +540,6 @@ public void QueryResultTest12() { @Test public void QueryResultTestSyncQuery() { - // sync is the default, but let's test it explicitly declared anyway - NewConnection(); final String sql = "SELECT STRING(ROUND(weight_pounds)) FROM publicdata:samples.natality GROUP BY 1 ORDER BY" + " 1 DESC LIMIT 10;"; @@ -586,7 +563,7 @@ public void QueryResultTestSyncQuery() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -615,7 +592,7 @@ public void QueryResultTestTokyo01() { java.sql.ResultSet Result = null; try { - Result = QueryResultTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -636,7 +613,6 @@ public void QueryResultTestTokyo01() { @Test public void QueryResultTestTokyoForceFetchMoreRowsPath() { - NewConnection("&useLegacySql=false"); final String sql = "SELECT meaning FROM tokyo_star.meaning_of_life GROUP BY ROLLUP(meaning);"; String[][] expectation = new String[][] {{null, "42"}}; @@ -645,7 +621,7 @@ public void QueryResultTestTokyoForceFetchMoreRowsPath() { java.sql.ResultSet Result = null; try { - Statement s = QueryResultTest.con.createStatement(); + Statement s = standardSqlConnection.createStatement(); s.setMaxRows(1); Result = s.executeQuery(sql); } catch (SQLException e) { diff --git a/src/test/java/net/starschema/clouddb/jdbc/TimeoutTest.java b/src/test/java/net/starschema/clouddb/jdbc/TimeoutTest.java index 89d1e4f..8b5c476 100644 --- a/src/test/java/net/starschema/clouddb/jdbc/TimeoutTest.java +++ b/src/test/java/net/starschema/clouddb/jdbc/TimeoutTest.java @@ -20,10 +20,11 @@ */ package net.starschema.clouddb.jdbc; -import java.sql.DriverManager; +import java.io.IOException; import java.sql.ResultSet; import java.sql.SQLException; import junit.framework.Assert; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.slf4j.Logger; @@ -31,7 +32,7 @@ public class TimeoutTest { - private static java.sql.Connection con = null; + private java.sql.Connection connection; Logger logger = LoggerFactory.getLogger(TimeoutTest.class); /** @@ -53,42 +54,26 @@ private boolean comparer(String[][] expected, String[][] reality) { return true; } + @Before + public void setConnection() throws SQLException, IOException { + connection = + ConnectionFromResources.connect("installedaccount1.properties", "&useLegacySql=true"); + } + + @After + public void closeConnection() throws SQLException { + connection.close(); + } + @Test public void isvalidtest() { try { - Assert.assertTrue(TimeoutTest.con.isValid(0)); + Assert.assertTrue(connection.isValid(0)); } catch (SQLException e) { } } - /** - * Makes a new Bigquery Connection to Hardcoded URL and gives back the Connection to static con - * member. - */ - @Before - public void NewConnection() throws Exception { - if (TimeoutTest.con == null || !TimeoutTest.con.isValid(0)) { - this.logger.info("Testing the JDBC driver"); - try { - - Class.forName("net.starschema.clouddb.jdbc.BQDriver"); - TimeoutTest.con = - DriverManager.getConnection( - BQSupportFuncts.constructUrlFromPropertiesFile( - BQSupportFuncts.readFromPropFile( - getClass().getResource("/serviceaccount.properties").getFile())) - + "&useLegacySql=true", - BQSupportFuncts.readFromPropFile( - getClass().getResource("/serviceaccount.properties").getFile())); - } catch (Exception e) { - this.logger.error("Error in connection" + e.toString()); - Assert.fail("General Exception:" + e.toString()); - } - this.logger.info(((BQConnection) TimeoutTest.con).getURLPART()); - } - } - @Test public void QueryResultTest01() { final String sql = "SELECT TOP(word, 10), COUNT(*) FROM publicdata:samples.shakespeare"; @@ -104,7 +89,7 @@ public void QueryResultTest01() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -139,7 +124,7 @@ public void QueryResultTest02() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -176,7 +161,7 @@ public void QueryResultTest03() { this.logger.info("Running query:" + sql); this.logger.debug(description); try { - TimeoutTest.con.createStatement().executeQuery(sql); + connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.debug("SQLexception" + e.toString()); // fail("SQLException" + e.toString()); @@ -201,7 +186,7 @@ public void QueryResultTest04() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -234,7 +219,7 @@ public void QueryResultTest05() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); this.logger.debug("{}", Result.getMetaData().getColumnCount()); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); @@ -270,7 +255,7 @@ public void QueryResultTest06() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -310,7 +295,7 @@ public void QueryResultTest07() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -351,7 +336,7 @@ public void QueryResultTest08() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString()); @@ -389,7 +374,7 @@ public void QueryResultTest09() { java.sql.ResultSet Result = null; try { - Result = TimeoutTest.con.createStatement().executeQuery(sql); + Result = connection.createStatement().executeQuery(sql); } catch (SQLException e) { this.logger.error("SQLexception" + e.toString()); Assert.fail("SQLException" + e.toString());