From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id BC4CF27B2C for ; Mon, 17 Sep 2018 11:09:22 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id sZPIezIjXwXq for ; Mon, 17 Sep 2018 11:09:22 -0400 (EDT) Received: from mail-lj1-f193.google.com (mail-lj1-f193.google.com [209.85.208.193]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 2F6EB202B0 for ; Mon, 17 Sep 2018 11:09:22 -0400 (EDT) Received: by mail-lj1-f193.google.com with SMTP id f1-v6so13503838ljc.9 for ; Mon, 17 Sep 2018 08:09:21 -0700 (PDT) From: Sergei Kalashnikov Subject: [tarantool-patches] [PATCH] jdbc: fix NPE in update Date: Mon, 17 Sep 2018 18:09:14 +0300 Message-Id: <1537196954-23804-1-git-send-email-ztarvos@gmail.com> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: tarantool-patches@freelists.org Cc: Sergei Kalashnikov Updated constants to be in sync with tarantool IPROTO. Added integration tests for the working JDBC functionality. Fixed travis script to use tarantool 2.0 which is required for SQL. Fixed failing user permission calls in tarantool initialization script. Closes #39 --- https://github.com/tarantool/tarantool-java/issues/39 https://github.com/ztarvos/tarantool-java/commits/ztarvos/gh-39-fix-update-npe .travis.yml | 2 +- pom.xml | 13 ++ src/main/java/org/tarantool/Key.java | 6 +- src/test/instance.lua | 4 +- .../java/org/tarantool/jdbc/AbstractJdbcIT.java | 149 +++++++++++++++++++++ .../java/org/tarantool/jdbc/JdbcConnectionIT.java | 42 ++++++ .../org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java | 106 +++++++++++++++ .../tarantool/jdbc/JdbcPreparedStatementIT.java | 67 +++++++++ .../java/org/tarantool/jdbc/JdbcResultSetIT.java | 129 ++++++++++++++++++ .../tarantool/jdbc/JdbcResultSetMetaDataIT.java | 33 +++++ .../java/org/tarantool/jdbc/JdbcStatementIT.java | 66 +++++++++ src/test/travis.pre.sh | 9 +- 12 files changed, 616 insertions(+), 10 deletions(-) create mode 100644 src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java create mode 100644 src/test/java/org/tarantool/jdbc/JdbcStatementIT.java diff --git a/.travis.yml b/.travis.yml index e0f434f..852807c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,5 +11,5 @@ before_script: - src/test/travis.pre.sh script: - - mvn test + - mvn verify - sudo cat /var/log/tarantool/jdk-testing.log diff --git a/pom.xml b/pom.xml index fcabe72..9cf0716 100644 --- a/pom.xml +++ b/pom.xml @@ -50,6 +50,19 @@ maven-surefire-plugin 2.22.0 + + org.apache.maven.plugins + maven-failsafe-plugin + 2.22.0 + + + + integration-test + verify + + + + diff --git a/src/main/java/org/tarantool/Key.java b/src/main/java/org/tarantool/Key.java index 35a91cd..32e761f 100644 --- a/src/main/java/org/tarantool/Key.java +++ b/src/main/java/org/tarantool/Key.java @@ -18,13 +18,13 @@ public enum Key implements Callable { UPSERT_OPS(0x28), DATA(0x30), ERROR(0x31), - SQL_FIELD_NAME(0x29), + SQL_FIELD_NAME(0), SQL_METADATA(0x32), SQL_TEXT(0x40), SQL_BIND(0x41), SQL_OPTIONS(0x42), - SQL_INFO(0x43), - SQL_ROW_COUNT(0x44); + SQL_INFO(0x42), + SQL_ROW_COUNT(0); int id; diff --git a/src/test/instance.lua b/src/test/instance.lua index fe8a80b..a763841 100644 --- a/src/test/instance.lua +++ b/src/test/instance.lua @@ -16,7 +16,7 @@ box.once('init', function() box.schema.user.create('test_ordin', { password = '2HWRXHfa' }) box.schema.user.create('test_admin', { password = '4pWBZmLEgkmKK5WP' }) - box.schema.user.grant('test_ordin', 'read,write', 'user') - box.schema.user.grant('test_admin', 'execute', 'super') + box.schema.user.grant('test_ordin', 'read,write', 'space', 'user') + box.schema.user.grant('test_admin', 'super') end) diff --git a/src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java b/src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java new file mode 100644 index 0000000..5c0304b --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/AbstractJdbcIT.java @@ -0,0 +1,149 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.tarantool.TarantoolConnection; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +//mvn -DtntHost=localhost -DtntPort=3301 -DtntUser=test -DtntPass=test verify +public abstract class AbstractJdbcIT { + private static final String host = System.getProperty("tntHost", "localhost"); + private static final Integer port = Integer.valueOf(System.getProperty("tntPort", "3301")); + private static final String user = System.getProperty("tntUser", "test_admin"); + private static final String pass = System.getProperty("tntPass", "4pWBZmLEgkmKK5WP"); + private static String URL = String.format("tarantool://%s:%d?user=%s&password=%s", host, port, user, pass); + + private static String[] initSql = new String[] { + "DROP TABLE IF EXISTS test", + "DROP TABLE IF EXISTS test_types", + + "CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR(100))", + "INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three')", + + "CREATE TABLE test_types(" + + "f1 INT PRIMARY KEY, " + + "f2 CHAR(4), " + + "f3 VARCHAR(100), " + + "f4 LONGVARCHAR(100), " + + "f5 NUMERIC, " + + "f6 DECIMAL, " + + "f7 BIT, " + + "f8 TINYINT, " + + "f9 SMALLINT, " + + "f10 INTEGER, " + + "f11 BIGINT," + + "f12 REAL, " + + "f13 FLOAT, " + + "f14 BINARY(4), " + + "f15 VARBINARY(128), " + + "f16 LONGVARBINARY(2048), " + + "f17 DATE, " + + "f18 TIME, " + + "f19 TIMESTAMP)", + + "INSERT INTO test_types VALUES(" + + "1," + + "'abcd'," + //CHAR + "'000000000000000000001'," + //VARCHAR + "'0000000000000000000000000000000001'," + //LONGVARCHAR + "100," + // NUMERIC + "100.1," + // DECIMAL + "1," + //BIT + "7," + //TINYINT + "1000," + //SMALLINT + "100," + //INTEGER + "100000000000000000," + //BIGINT + "-100.2," + //REAL + "100.3," + //FLOAT + "X'01020304'," + //BINARY + "X'0102030405'," +//VARBINARY + "X'010203040506'," + //LONGVARBINARY + "'1983-03-14'," + //DATE + "'12:01:06'," + //TIME + "129479994)" //TIMESTAMP + }; + + private static String[] cleanSql = new String[] { + "DROP TABLE IF EXISTS test", + "DROP TABLE IF EXISTS test_types" + }; + + Connection conn; + + @BeforeAll + public static void setupEnv() throws Exception { + sqlExec(initSql); + } + + @AfterAll + public static void teardownEnv() throws Exception { + sqlExec(cleanSql); + } + + @BeforeEach + public void setUpConnection() throws SQLException { + conn = DriverManager.getConnection(URL); + assertNotNull(conn); + } + + @AfterEach + public void tearDownConnection() throws SQLException { + if (conn != null && !conn.isClosed()) + conn.close(); + } + + private static void sqlExec(String[] text) throws IOException { + Socket socket = new Socket(); + try { + socket.connect(new InetSocketAddress(host, port)); + TarantoolConnection con = new TarantoolConnection(user, pass, socket); + try { + for (String cmd : text) + con.eval("box.sql.execute(\"" + cmd + "\")"); + } + finally { + con.close(); + socket = null; + } + } + finally { + if (socket != null) + socket.close(); + } + } + + static List getRow(String space, Object key) throws IOException { + Socket socket = new Socket(); + try { + socket.connect(new InetSocketAddress(host, port)); + TarantoolConnection con = new TarantoolConnection(user, pass, socket); + try { + List l = con.select(281, 2, Arrays.asList(space.toUpperCase()), 0, 1, 0); + Integer spaceId = (Integer) ((List) l.get(0)).get(0); + l = con.select(spaceId, 0, Arrays.asList(key), 0, 1, 0); + return (l == null || l.size() == 0) ? Collections.emptyList() : (List) l.get(0); + } + finally { + con.close(); + socket = null; + } + } + finally { + if (socket != null) + socket.close(); + } + } +} diff --git a/src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java b/src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java new file mode 100644 index 0000000..cc6bfb9 --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcConnectionIT.java @@ -0,0 +1,42 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.Test; + +import java.sql.DatabaseMetaData; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JdbcConnectionIT extends AbstractJdbcIT { + @Test + public void testCreateStatement() throws SQLException { + Statement stmt = conn.createStatement(); + assertNotNull(stmt); + stmt.close(); + } + + @Test + public void testPrepareStatement() throws SQLException { + PreparedStatement prep = conn.prepareStatement("INSERT INTO test(id, val) VALUES(?, ?)"); + assertNotNull(prep); + prep.close(); + } + + @Test + public void testCloseIsClosed() throws SQLException { + assertFalse(conn.isClosed()); + conn.close(); + assertTrue(conn.isClosed()); + conn.close(); + } + + @Test + public void testGetMetaData() throws SQLException { + DatabaseMetaData meta = conn.getMetaData(); + assertNotNull(meta); + } +} \ No newline at end of file diff --git a/src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java b/src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java new file mode 100644 index 0000000..b934bbe --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcDatabaseMetaDataIT.java @@ -0,0 +1,106 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; + +import java.sql.DatabaseMetaData; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JdbcDatabaseMetaDataIT extends AbstractJdbcIT { + private DatabaseMetaData meta; + + @BeforeEach + public void setUp() throws Exception { + meta = conn.getMetaData(); + } + + @Test + public void testGetTableTypes() throws SQLException { + ResultSet rs = meta.getTableTypes(); + assertNotNull(rs); + + assertTrue(rs.next()); + assertEquals("TABLE", rs.getString("TABLE_TYPE")); + assertFalse(rs.next()); + + rs.close(); + } + + @Test + public void testGetAllTables() throws SQLException { + ResultSet rs = meta.getTables(null, null, null, new String[] {"TABLE"}); + assertNotNull(rs); + + assertTrue(rs.next()); + assertEquals("TEST", rs.getString("TABLE_NAME")); + + assertTrue(rs.next()); + assertEquals("TEST_TYPES", rs.getString("TABLE_NAME")); + + assertFalse(rs.next()); + + rs.close(); + } + + @Test + public void testGetTable() throws SQLException { + ResultSet rs = meta.getTables(null, null, "TEST", new String[] {"TABLE"}); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals("TEST", rs.getString("TABLE_NAME")); + + assertFalse(rs.next()); + + rs.close(); + } + + @Test + public void testGetColumns() throws SQLException { + ResultSet rs = meta.getColumns(null, null, "TEST", null); + assertNotNull(rs); + + assertTrue(rs.next()); + + assertEquals("TEST", rs.getString("TABLE_NAME")); + assertEquals("ID", rs.getString("COLUMN_NAME")); + assertEquals(1, rs.getInt("ORDINAL_POSITION")); + + assertTrue(rs.next()); + + assertEquals("TEST", rs.getString("TABLE_NAME")); + assertEquals("VAL", rs.getString("COLUMN_NAME")); + assertEquals(2, rs.getInt("ORDINAL_POSITION")); + + assertFalse(rs.next()); + + rs.close(); + } + + @Disabled(value="Test ignored, issue#41") + @Test + public void testGetPrimaryKeys() throws SQLException { + ResultSet rs = meta.getPrimaryKeys(null, null, "TEST"); + + assertNotNull(rs); + assertTrue(rs.next()); + + assertNull(rs.getString("TABLE_CAT")); + assertNull(rs.getString("TABLE_SCHEM")); + assertEquals("TEST", rs.getString("TABLE_NAME")); + assertEquals("ID", rs.getString("COLUMN_NAME")); + assertEquals(1, rs.getInt("KEY_SEQ")); + assertEquals("pk_unnamed_TEST_1", rs.getString("PK_NAME")); + + assertFalse(rs.next()); + + rs.close(); + } +} diff --git a/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java b/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java new file mode 100644 index 0000000..f356f6b --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcPreparedStatementIT.java @@ -0,0 +1,67 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.AfterEach; + +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JdbcPreparedStatementIT extends AbstractJdbcIT { + private PreparedStatement prep; + + @AfterEach + public void tearDown() throws SQLException { + if (prep != null && !prep.isClosed()) + prep.close(); + } + + @Test + public void testExecuteQuery() throws SQLException { + prep = conn.prepareStatement("SELECT val FROM test WHERE id=?"); + assertNotNull(prep); + + prep.setInt(1, 1); + ResultSet rs = prep.executeQuery(); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals("one", rs.getString(1)); + assertFalse(rs.next()); + rs.close(); + + // Reuse the prepared statement. + prep.setInt(1, 2); + rs = prep.executeQuery(); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals("two", rs.getString(1)); + assertFalse(rs.next()); + rs.close(); + } + + @Test + public void testExecuteUpdate() throws Exception { + prep = conn.prepareStatement("INSERT INTO test VALUES(?, ?)"); + assertNotNull(prep); + + prep.setInt(1, 100); + prep.setString(2, "hundred"); + int count = prep.executeUpdate(); + assertEquals(1, count); + + assertEquals("hundred", getRow("test", 100).get(1)); + + // Reuse the prepared statement. + prep.setInt(1, 1000); + prep.setString(2, "thousand"); + count = prep.executeUpdate(); + assertEquals(1, count); + + assertEquals("thousand", getRow("test", 1000).get(1)); + } +} diff --git a/src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java b/src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java new file mode 100644 index 0000000..8042c28 --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcResultSetIT.java @@ -0,0 +1,129 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.BeforeEach; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.sql.Timestamp; +import java.util.Arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JdbcResultSetIT extends AbstractJdbcIT { + private Statement stmt; + + @BeforeEach + public void setUp() throws Exception { + stmt = conn.createStatement(); + } + + @AfterEach + public void tearDown() throws Exception { + if (stmt != null && !stmt.isClosed()) + stmt.close(); + } + + @Test + public void testEmpty() throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT * FROM test WHERE id < 0"); + assertNotNull(rs); + assertFalse(rs.next()); + rs.close(); + } + + @Test + public void testIteration() throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT * FROM test WHERE id IN (1,2,3) ORDER BY id"); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals(1, rs.getInt(1)); + assertTrue(rs.next()); + assertEquals(2, rs.getInt(1)); + assertTrue(rs.next()); + assertEquals(3, rs.getInt(1)); + assertFalse(rs.next()); + rs.close(); + } + + @Test + public void testGetColumnByIdx() throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT * FROM test_types"); + assertNotNull(rs); + assertTrue(rs.next()); + + assertEquals(1, rs.getInt(1));//INT + assertEquals("abcd", rs.getString(2));//CHAR + assertEquals("000000000000000000001", rs.getString(3));//VARCHAR + assertEquals("0000000000000000000000000000000001", rs.getString(4)); //LONGVARCHAR + assertEquals(BigDecimal.valueOf(100), rs.getBigDecimal(5));// NUMERIC + assertEquals(BigDecimal.valueOf(100.1), rs.getBigDecimal(6));// DECIMAL + assertEquals(Boolean.FALSE, rs.getBoolean(7));//BIT + assertEquals((byte)7, rs.getByte(8));//TINYINT + assertEquals((short)1000, rs.getShort(9));//SMALLINT + assertEquals(100, rs.getInt(10));//INTEGER + assertEquals(100000000000000000L, rs.getLong(11));//BIGINT + assertEquals(-100.2f, rs.getFloat(12), 1e-10f);//REAL + assertEquals(100.3d, rs.getDouble(13), 1e-10d);//FLOAT + + assertTrue(Arrays.equals(new BigInteger("01020304", 16) + .toByteArray(), rs.getBytes(14)));//BINARY + + assertTrue(Arrays.equals(new BigInteger("0102030405", 16) + .toByteArray(), rs.getBytes(15)));//VARBINARY + + assertTrue(Arrays.equals(new BigInteger("010203040506", 16) + .toByteArray(), rs.getBytes(16)));//LONGVARBINARY + + //Issue#44 + //assertEquals(Date.valueOf("1983-03-14"), rs.getDate(17));//DATE + //assertEquals(Time.valueOf("12:01:06"), rs.getTime(18));//TIME + assertEquals(new Timestamp(129479994), rs.getTimestamp(19)); //TIMESTAMP + + rs.close(); + } + + @Test + public void testGetColumnByName() throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT * FROM test_types"); + assertNotNull(rs); + assertTrue(rs.next()); + + assertEquals(1, rs.getInt("F1"));//INT + assertEquals("abcd", rs.getString("F2"));//CHAR + assertEquals("000000000000000000001", rs.getString("F3"));//VARCHAR + assertEquals("0000000000000000000000000000000001", rs.getString("F4")); //LONGVARCHAR + assertEquals(BigDecimal.valueOf(100), rs.getBigDecimal("F5"));// NUMERIC + assertEquals(BigDecimal.valueOf(100.1), rs.getBigDecimal("F6"));// DECIMAL + assertEquals(Boolean.FALSE, rs.getBoolean("F7"));//BIT + assertEquals((byte)7, rs.getByte("F8"));//TINYINT + assertEquals((short)1000, rs.getShort("F9"));//SMALLINT + assertEquals(100, rs.getInt("F10"));//INTEGER + assertEquals(100000000000000000L, rs.getLong("F11"));//BIGINT + assertEquals(-100.2f, rs.getFloat("F12"), 1e-10f);//REAL + assertEquals(100.3d, rs.getDouble("F13"), 1e-10d);//FLOAT + + assertTrue(Arrays.equals(new BigInteger("01020304", 16) + .toByteArray(), rs.getBytes("F14")));//BINARY + + assertTrue(Arrays.equals(new BigInteger("0102030405", 16) + .toByteArray(), rs.getBytes("F15")));//VARBINARY + + assertTrue(Arrays.equals(new BigInteger("010203040506", 16) + .toByteArray(), rs.getBytes("F16")));//LONGVARBINARY + + //Issue#44 + //assertEquals(Date.valueOf("1983-03-14"), rs.getDate("F17"));//DATE + //assertEquals(Time.valueOf("12:01:06"), rs.getTime("F18"));//TIME + assertEquals(new Timestamp(129479994), rs.getTimestamp("F19")); //TIMESTAMP + + rs.close(); + } +} diff --git a/src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java b/src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java new file mode 100644 index 0000000..f90d5b1 --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcResultSetMetaDataIT.java @@ -0,0 +1,33 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.Test; + +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JdbcResultSetMetaDataIT extends AbstractJdbcIT { + @Test + public void testColumnNames() throws SQLException { + Statement stmt = conn.createStatement(); + assertNotNull(stmt); + ResultSet rs = stmt.executeQuery("SELECT * FROM test_types"); + assertNotNull(rs); + assertTrue(rs.next()); + + ResultSetMetaData rsMeta = rs.getMetaData(); + + assertEquals(19, rsMeta.getColumnCount()); + + for (int i = 1; i <= 19; i++) + assertEquals("F" + i, rsMeta.getColumnName(i)); + + rs.close(); + stmt.close(); + } +} diff --git a/src/test/java/org/tarantool/jdbc/JdbcStatementIT.java b/src/test/java/org/tarantool/jdbc/JdbcStatementIT.java new file mode 100644 index 0000000..925556d --- /dev/null +++ b/src/test/java/org/tarantool/jdbc/JdbcStatementIT.java @@ -0,0 +1,66 @@ +package org.tarantool.jdbc; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class JdbcStatementIT extends AbstractJdbcIT { + private Statement stmt; + + @BeforeEach + public void setUp() throws SQLException { + stmt = conn.createStatement(); + } + + @AfterEach + public void tearDown() throws SQLException { + if (stmt != null && !stmt.isClosed()) + stmt.close(); + } + + @Test + public void testExecuteQuery() throws SQLException { + ResultSet rs = stmt.executeQuery("SELECT val FROM test WHERE id=1"); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals("one", rs.getString(1)); + assertFalse(rs.next()); + rs.close(); + } + + @Test + public void testExecuteUpdate() throws Exception { + assertEquals(2, stmt.executeUpdate("INSERT INTO test(id, val) VALUES (10, 'ten'), (20, 'twenty')")); + assertEquals("ten", getRow("test", 10).get(1)); + assertEquals("twenty", getRow("test", 20).get(1)); + } + + @Test + public void testExecuteReturnsResultSet() throws SQLException { + assertTrue(stmt.execute("SELECT val FROM test WHERE id=1")); + ResultSet rs = stmt.getResultSet(); + assertNotNull(rs); + assertTrue(rs.next()); + assertEquals("one", rs.getString(1)); + assertFalse(rs.next()); + rs.close(); + } + + @Test + public void testExecuteReturnsUpdateCount() throws Exception { + assertFalse(stmt.execute("INSERT INTO test(id, val) VALUES (100, 'hundred'), (1000, 'thousand')")); + assertEquals(2, stmt.getUpdateCount()); + + assertEquals("hundred", getRow("test", 100).get(1)); + assertEquals("thousand", getRow("test", 1000).get(1)); + } +} \ No newline at end of file diff --git a/src/test/travis.pre.sh b/src/test/travis.pre.sh index 86ecdd3..152e75c 100755 --- a/src/test/travis.pre.sh +++ b/src/test/travis.pre.sh @@ -2,13 +2,14 @@ set -e -curl http://download.tarantool.org/tarantool/1.9/gpgkey | sudo apt-key add - +# We need tarantool 2.0 for jdbc/sql. +curl http://download.tarantool.org/tarantool/2.0/gpgkey | sudo apt-key add - release=`lsb_release -c -s` sudo rm -f /etc/apt/sources.list.d/*tarantool*.list -sudo tee /etc/apt/sources.list.d/tarantool_1.9.list <<- EOF -deb http://download.tarantool.org/tarantool/1.9/ubuntu/ $release main -deb-src http://download.tarantool.org/tarantool/1.9/ubuntu/ $release main +sudo tee /etc/apt/sources.list.d/tarantool_2.0.list <<- EOF +deb http://download.tarantool.org/tarantool/2.0/ubuntu/ $release main +deb-src http://download.tarantool.org/tarantool/2.0/ubuntu/ $release main EOF sudo apt-get update -- 1.8.3.1