Skip to content

Commit f1426d2

Browse files
committed
jdbc: fix NPE in update
Updated constants be in sync with tarantool IPROTO. Added tests for the working JDBC functionality. Closes tarantool#39
1 parent d0b57fe commit f1426d2

9 files changed

+624
-3
lines changed

pom.xml

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<packaging>jar</packaging>
88
<properties>
99
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10+
<junit.jupiter.version>5.3.0</junit.jupiter.version>
1011
</properties>
1112
<name>Tarantool Connector for Java</name>
1213
<url>https://github.com/tarantool/tarantool-java</url>
@@ -43,6 +44,10 @@
4344
<target>1.6</target>
4445
</configuration>
4546
</plugin>
47+
<plugin>
48+
<artifactId>maven-surefire-plugin</artifactId>
49+
<version>2.22.0</version>
50+
</plugin>
4651
</plugins>
4752
</build>
4853
<dependencies>
@@ -52,6 +57,30 @@
5257
<version>4.12</version>
5358
<scope>test</scope>
5459
</dependency>
60+
<dependency>
61+
<groupId>org.junit.jupiter</groupId>
62+
<artifactId>junit-jupiter-api</artifactId>
63+
<version>${junit.jupiter.version}</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.junit.jupiter</groupId>
68+
<artifactId>junit-jupiter-engine</artifactId>
69+
<version>${junit.jupiter.version}</version>
70+
<scope>test</scope>
71+
</dependency>
72+
<dependency>
73+
<groupId>org.junit.vintage</groupId>
74+
<artifactId>junit-vintage-engine</artifactId>
75+
<version>${junit.jupiter.version}</version>
76+
<scope>test</scope>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.mockito</groupId>
80+
<artifactId>mockito-all</artifactId>
81+
<version>1.9.5</version>
82+
<scope>test</scope>
83+
</dependency>
5584
</dependencies>
5685

5786
<parent>

src/main/java/org/tarantool/Key.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ public enum Key implements Callable<Integer> {
1818
UPSERT_OPS(0x28),
1919
DATA(0x30), ERROR(0x31),
2020

21-
SQL_FIELD_NAME(0x29),
21+
SQL_FIELD_NAME(0),
2222
SQL_METADATA(0x32),
2323
SQL_TEXT(0x40),
2424
SQL_BIND(0x41),
2525
SQL_OPTIONS(0x42),
26-
SQL_INFO(0x43),
27-
SQL_ROW_COUNT(0x44);
26+
SQL_INFO(0x42),
27+
SQL_ROW_COUNT(0);
2828

2929
int id;
3030

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.tarantool.jdbc;
2+
3+
import org.junit.After;
4+
import org.junit.AfterClass;
5+
import org.junit.Before;
6+
import org.junit.BeforeClass;
7+
import org.tarantool.TarantoolConnection;
8+
9+
import java.io.IOException;
10+
import java.net.InetSocketAddress;
11+
import java.net.Socket;
12+
import java.sql.Connection;
13+
import java.sql.DriverManager;
14+
import java.sql.SQLException;
15+
import java.util.Arrays;
16+
import java.util.Collections;
17+
import java.util.List;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
21+
//mvn -Dtnt_host=localhost -Dtnt_port=3301 -Dtnt_user=test -Dtnt_pass=test test
22+
public abstract class AbstractJdbcTest {
23+
private static final String host = System.getProperty("tnt_host", "localhost");
24+
private static final Integer port = Integer.valueOf(System.getProperty("tnt_port", "3301"));
25+
private static final String user = System.getProperty("tnt_user", "test");
26+
private static final String pass = System.getProperty("tnt_pass", "test");
27+
private static String URL = String.format("tarantool://%s:%d?user=%s&password=%s", host, port, user, pass);
28+
29+
private static String[] initSql = new String[] {
30+
"DROP TABLE IF EXISTS test",
31+
"DROP TABLE IF EXISTS test_types",
32+
33+
"CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR(100))",
34+
"INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three')",
35+
36+
"CREATE TABLE test_types(" +
37+
"f1 INT PRIMARY KEY, " +
38+
"f2 CHAR(4), " +
39+
"f3 VARCHAR(100), " +
40+
"f4 LONGVARCHAR(100), " +
41+
"f5 NUMERIC, " +
42+
"f6 DECIMAL, " +
43+
"f7 BIT, " +
44+
"f8 TINYINT, " +
45+
"f9 SMALLINT, " +
46+
"f10 INTEGER, " +
47+
"f11 BIGINT," +
48+
"f12 REAL, " +
49+
"f13 FLOAT, " +
50+
"f14 BINARY(4), " +
51+
"f15 VARBINARY(128), " +
52+
"f16 LONGVARBINARY(2048), " +
53+
"f17 DATE, " +
54+
"f18 TIME, " +
55+
"f19 TIMESTAMP)",
56+
57+
"INSERT INTO test_types VALUES(" +
58+
"1," +
59+
"'abcd'," + //CHAR
60+
"'000000000000000000001'," + //VARCHAR
61+
"'0000000000000000000000000000000001'," + //LONGVARCHAR
62+
"100," + // NUMERIC
63+
"100.1," + // DECIMAL
64+
"1," + //BIT
65+
"7," + //TINYINT
66+
"1000," + //SMALLINT
67+
"100," + //INTEGER
68+
"100000000000000000," + //BIGINT
69+
"-100.2," + //REAL
70+
"100.3," + //FLOAT
71+
"X'01020304'," + //BINARY
72+
"X'0102030405'," +//VARBINARY
73+
"X'010203040506'," + //LONGVARBINARY
74+
"'1983-03-14'," + //DATE
75+
"'12:01:06'," + //TIME
76+
"129479994)" //TIMESTAMP
77+
};
78+
79+
private static String[] cleanSql = new String[] {
80+
"DROP TABLE IF EXISTS test",
81+
"DROP TABLE IF EXISTS test_types"
82+
};
83+
84+
Connection conn;
85+
86+
@BeforeClass
87+
public static void setupEnv() throws Exception {
88+
sqlExec(initSql);
89+
}
90+
91+
@AfterClass
92+
public static void teardownEnv() throws Exception {
93+
sqlExec(cleanSql);
94+
}
95+
96+
@Before
97+
public void setUpConnection() throws SQLException {
98+
conn = DriverManager.getConnection(URL);
99+
assertNotNull(conn);
100+
}
101+
102+
@After
103+
public void tearDownConnection() throws SQLException {
104+
if (conn != null && !conn.isClosed())
105+
conn.close();
106+
}
107+
108+
private static void sqlExec(String[] text) throws IOException {
109+
Socket socket = new Socket();
110+
try {
111+
socket.connect(new InetSocketAddress(host, port));
112+
TarantoolConnection con = new TarantoolConnection(user, pass, socket);
113+
try {
114+
for (String cmd : text)
115+
con.eval("box.sql.execute(\"" + cmd + "\")");
116+
}
117+
finally {
118+
con.close();
119+
socket = null;
120+
}
121+
}
122+
finally {
123+
if (socket != null)
124+
socket.close();
125+
}
126+
}
127+
128+
static List<?> getRow(String space, Object key) throws IOException {
129+
Socket socket = new Socket();
130+
try {
131+
socket.connect(new InetSocketAddress(host, port));
132+
TarantoolConnection con = new TarantoolConnection(user, pass, socket);
133+
try {
134+
List<?> l = con.select(281, 2, Arrays.asList(space.toUpperCase()), 0, 1, 0);
135+
Integer spaceId = (Integer) ((List) l.get(0)).get(0);
136+
l = con.select(spaceId, 0, Arrays.asList(key), 0, 1, 0);
137+
return (l == null || l.size() == 0) ? Collections.emptyList() : (List<?>) l.get(0);
138+
}
139+
finally {
140+
con.close();
141+
socket = null;
142+
}
143+
}
144+
finally {
145+
if (socket != null)
146+
socket.close();
147+
}
148+
}
149+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.tarantool.jdbc;
2+
3+
import org.junit.Test;
4+
5+
import java.sql.DatabaseMetaData;
6+
import java.sql.PreparedStatement;
7+
import java.sql.SQLException;
8+
import java.sql.Statement;
9+
10+
import static org.junit.Assert.assertFalse;
11+
import static org.junit.Assert.assertNotNull;
12+
import static org.junit.Assert.assertTrue;
13+
14+
public class JdbcConnectionTest extends AbstractJdbcTest {
15+
@Test
16+
public void testCreateStatement() throws SQLException {
17+
Statement stmt = conn.createStatement();
18+
assertNotNull(stmt);
19+
stmt.close();
20+
}
21+
22+
@Test
23+
public void testPrepareStatement() throws SQLException {
24+
PreparedStatement prep = conn.prepareStatement("INSERT INTO test(id, val) VALUES(?, ?)");
25+
assertNotNull(prep);
26+
prep.close();
27+
}
28+
29+
@Test
30+
public void testCloseIsClosed() throws SQLException {
31+
assertFalse(conn.isClosed());
32+
conn.close();
33+
assertTrue(conn.isClosed());
34+
conn.close();
35+
}
36+
37+
@Test
38+
public void testGetMetaData() throws SQLException {
39+
DatabaseMetaData meta = conn.getMetaData();
40+
assertNotNull(meta);
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package org.tarantool.jdbc;
2+
3+
import org.junit.Before;
4+
import org.junit.Ignore;
5+
import org.junit.Test;
6+
7+
import java.sql.DatabaseMetaData;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertFalse;
13+
import static org.junit.Assert.assertNotNull;
14+
import static org.junit.Assert.assertNull;
15+
import static org.junit.Assert.assertTrue;
16+
17+
public class JdbcDatabaseMetaDataTest extends AbstractJdbcTest {
18+
private DatabaseMetaData meta;
19+
20+
@Before
21+
public void setUp() throws Exception {
22+
meta = conn.getMetaData();
23+
}
24+
25+
@Test
26+
public void testGetTableTypes() throws SQLException {
27+
ResultSet rs = meta.getTableTypes();
28+
assertNotNull(rs);
29+
30+
assertTrue(rs.next());
31+
assertEquals("TABLE", rs.getString("TABLE_TYPE"));
32+
assertFalse(rs.next());
33+
34+
rs.close();
35+
}
36+
37+
@Test
38+
public void testGetAllTables() throws SQLException {
39+
ResultSet rs = meta.getTables(null, null, null, new String[] {"TABLE"});
40+
assertNotNull(rs);
41+
42+
assertTrue(rs.next());
43+
assertEquals("TEST", rs.getString("TABLE_NAME"));
44+
45+
assertTrue(rs.next());
46+
assertEquals("TEST_TYPES", rs.getString("TABLE_NAME"));
47+
48+
assertFalse(rs.next());
49+
50+
rs.close();
51+
}
52+
53+
@Test
54+
public void testGetTable() throws SQLException {
55+
ResultSet rs = meta.getTables(null, null, "TEST", new String[] {"TABLE"});
56+
assertNotNull(rs);
57+
assertTrue(rs.next());
58+
assertEquals("TEST", rs.getString("TABLE_NAME"));
59+
60+
assertFalse(rs.next());
61+
62+
rs.close();
63+
}
64+
65+
@Test
66+
public void testGetColumns() throws SQLException {
67+
ResultSet rs = meta.getColumns(null, null, "TEST", null);
68+
assertNotNull(rs);
69+
70+
assertTrue(rs.next());
71+
72+
assertEquals("TEST", rs.getString("TABLE_NAME"));
73+
assertEquals("ID", rs.getString("COLUMN_NAME"));
74+
assertEquals(1, rs.getInt("ORDINAL_POSITION"));
75+
76+
assertTrue(rs.next());
77+
78+
assertEquals("TEST", rs.getString("TABLE_NAME"));
79+
assertEquals("VAL", rs.getString("COLUMN_NAME"));
80+
assertEquals(2, rs.getInt("ORDINAL_POSITION"));
81+
82+
assertFalse(rs.next());
83+
84+
rs.close();
85+
}
86+
87+
@Ignore(value="Test ignored, issue#41")
88+
@Test
89+
public void testGetPrimaryKeys() throws SQLException {
90+
ResultSet rs = meta.getPrimaryKeys(null, null, "TEST");
91+
92+
assertNotNull(rs);
93+
assertTrue(rs.next());
94+
95+
assertNull(rs.getString("TABLE_CAT"));
96+
assertNull(rs.getString("TABLE_SCHEM"));
97+
assertEquals("TEST", rs.getString("TABLE_NAME"));
98+
assertEquals("ID", rs.getString("COLUMN_NAME"));
99+
assertEquals(1, rs.getInt("KEY_SEQ"));
100+
assertEquals("pk_unnamed_TEST_1", rs.getString("PK_NAME"));
101+
102+
assertFalse(rs.next());
103+
104+
rs.close();
105+
}
106+
}

0 commit comments

Comments
 (0)