Skip to content

Commit a34b011

Browse files
committed
jdbc: fix NPE in update
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 tarantool#39
1 parent 04078db commit a34b011

12 files changed

+628
-10
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ before_script:
1111
- src/test/travis.pre.sh
1212

1313
script:
14-
- mvn test
14+
- mvn verify
1515
- sudo cat /var/log/tarantool/jdk-testing.log

pom.xml

+13
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,19 @@
5050
<artifactId>maven-surefire-plugin</artifactId>
5151
<version>2.22.0</version>
5252
</plugin>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-failsafe-plugin</artifactId>
56+
<version>2.22.0</version>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>integration-test</goal>
61+
<goal>verify</goal>
62+
</goals>
63+
</execution>
64+
</executions>
65+
</plugin>
5366
</plugins>
5467
</build>
5568

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

src/test/instance.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ box.once('init', function()
1616
box.schema.user.create('test_ordin', { password = '2HWRXHfa' })
1717
box.schema.user.create('test_admin', { password = '4pWBZmLEgkmKK5WP' })
1818

19-
box.schema.user.grant('test_ordin', 'read,write', 'user')
20-
box.schema.user.grant('test_admin', 'execute', 'super')
19+
box.schema.user.grant('test_ordin', 'read,write', 'space', 'user')
20+
box.schema.user.grant('test_admin', 'super')
2121
end)
2222

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
package org.tarantool.jdbc;
2+
3+
import org.junit.jupiter.api.AfterAll;
4+
import org.junit.jupiter.api.AfterEach;
5+
import org.junit.jupiter.api.BeforeAll;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.tarantool.TarantoolConnection;
8+
9+
import java.io.IOException;
10+
import java.math.BigDecimal;
11+
import java.math.BigInteger;
12+
import java.net.InetSocketAddress;
13+
import java.net.Socket;
14+
import java.sql.Connection;
15+
import java.sql.Date;
16+
import java.sql.DriverManager;
17+
import java.sql.SQLException;
18+
import java.sql.Time;
19+
import java.sql.Timestamp;
20+
import java.util.Arrays;
21+
import java.util.Collections;
22+
import java.util.List;
23+
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
26+
//mvn -DtntHost=localhost -DtntPort=3301 -DtntUser=test -DtntPass=test verify
27+
public abstract class AbstractJdbcIT {
28+
private static final String host = System.getProperty("tntHost", "localhost");
29+
private static final Integer port = Integer.valueOf(System.getProperty("tntPort", "3301"));
30+
private static final String user = System.getProperty("tntUser", "test_admin");
31+
private static final String pass = System.getProperty("tntPass", "4pWBZmLEgkmKK5WP");
32+
private static String URL = String.format("tarantool://%s:%d?user=%s&password=%s", host, port, user, pass);
33+
34+
private static String[] initSql = new String[] {
35+
"DROP TABLE IF EXISTS test",
36+
"DROP TABLE IF EXISTS test_types",
37+
38+
"CREATE TABLE test(id INT PRIMARY KEY, val VARCHAR(100))",
39+
"INSERT INTO test VALUES (1, 'one'), (2, 'two'), (3, 'three')",
40+
41+
"CREATE TABLE test_types(" +
42+
"f1 INT PRIMARY KEY, " +
43+
"f2 CHAR(4), " +
44+
"f3 VARCHAR(100), " +
45+
"f4 LONGVARCHAR(100), " +
46+
"f5 NUMERIC, " +
47+
"f6 DECIMAL, " +
48+
"f7 BIT, " +
49+
"f8 TINYINT, " +
50+
"f9 SMALLINT, " +
51+
"f10 INTEGER, " +
52+
"f11 BIGINT," +
53+
"f12 REAL, " +
54+
"f13 FLOAT, " +
55+
"f14 BINARY(4), " +
56+
"f15 VARBINARY(128), " +
57+
"f16 LONGVARBINARY(2048), " +
58+
"f17 DATE, " +
59+
"f18 TIME, " +
60+
"f19 TIMESTAMP)",
61+
62+
"INSERT INTO test_types VALUES(" +
63+
"1," +
64+
"'abcd'," + //CHAR
65+
"'000000000000000000001'," + //VARCHAR
66+
"'0000000000000000000000000000000001'," + //LONGVARCHAR
67+
"100," + // NUMERIC
68+
"100.1," + // DECIMAL
69+
"1," + //BIT
70+
"7," + //TINYINT
71+
"1000," + //SMALLINT
72+
"100," + //INTEGER
73+
"100000000000000000," + //BIGINT
74+
"-100.2," + //REAL
75+
"100.3," + //FLOAT
76+
"X'01020304'," + //BINARY
77+
"X'0102030405'," +//VARBINARY
78+
"X'010203040506'," + //LONGVARBINARY
79+
"'1983-03-14'," + //DATE
80+
"'12:01:06'," + //TIME
81+
"129479994)" //TIMESTAMP
82+
};
83+
84+
private static String[] cleanSql = new String[] {
85+
"DROP TABLE IF EXISTS test",
86+
"DROP TABLE IF EXISTS test_types"
87+
};
88+
89+
static Object[] testRow = new Object[] {
90+
1,
91+
"abcd",
92+
"000000000000000000001",
93+
"0000000000000000000000000000000001",
94+
BigDecimal.valueOf(100),
95+
BigDecimal.valueOf(100.1),
96+
Boolean.FALSE,
97+
(byte)7,
98+
(short)1000,
99+
100,
100+
100000000000000000L,
101+
-100.2f,
102+
100.3d,
103+
new BigInteger("01020304", 16).toByteArray(),
104+
new BigInteger("0102030405", 16).toByteArray(),
105+
new BigInteger("010203040506", 16).toByteArray(),
106+
Date.valueOf("1983-03-14"),
107+
Time.valueOf("12:01:06"),
108+
new Timestamp(129479994)
109+
};
110+
111+
Connection conn;
112+
113+
@BeforeAll
114+
public static void setupEnv() throws Exception {
115+
sqlExec(initSql);
116+
}
117+
118+
@AfterAll
119+
public static void teardownEnv() throws Exception {
120+
sqlExec(cleanSql);
121+
}
122+
123+
@BeforeEach
124+
public void setUpConnection() throws SQLException {
125+
conn = DriverManager.getConnection(URL);
126+
assertNotNull(conn);
127+
}
128+
129+
@AfterEach
130+
public void tearDownConnection() throws SQLException {
131+
if (conn != null && !conn.isClosed())
132+
conn.close();
133+
}
134+
135+
private static void sqlExec(String[] text) throws IOException {
136+
Socket socket = new Socket();
137+
try {
138+
socket.connect(new InetSocketAddress(host, port));
139+
TarantoolConnection con = new TarantoolConnection(user, pass, socket);
140+
try {
141+
for (String cmd : text)
142+
con.eval("box.sql.execute(\"" + cmd + "\")");
143+
}
144+
finally {
145+
con.close();
146+
socket = null;
147+
}
148+
}
149+
finally {
150+
if (socket != null)
151+
socket.close();
152+
}
153+
}
154+
155+
static List<?> getRow(String space, Object key) throws IOException {
156+
Socket socket = new Socket();
157+
try {
158+
socket.connect(new InetSocketAddress(host, port));
159+
TarantoolConnection con = new TarantoolConnection(user, pass, socket);
160+
try {
161+
List<?> l = con.select(281, 2, Arrays.asList(space.toUpperCase()), 0, 1, 0);
162+
Integer spaceId = (Integer) ((List) l.get(0)).get(0);
163+
l = con.select(spaceId, 0, Arrays.asList(key), 0, 1, 0);
164+
return (l == null || l.size() == 0) ? Collections.emptyList() : (List<?>) l.get(0);
165+
}
166+
finally {
167+
con.close();
168+
socket = null;
169+
}
170+
}
171+
finally {
172+
if (socket != null)
173+
socket.close();
174+
}
175+
}
176+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.tarantool.jdbc;
2+
3+
import org.junit.jupiter.api.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.jupiter.api.Assertions.assertFalse;
11+
import static org.junit.jupiter.api.Assertions.assertNotNull;
12+
import static org.junit.jupiter.api.Assertions.assertTrue;
13+
14+
public class JdbcConnectionIT extends AbstractJdbcIT {
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.jupiter.api.Disabled;
4+
import org.junit.jupiter.api.Test;
5+
import org.junit.jupiter.api.BeforeEach;
6+
7+
import java.sql.DatabaseMetaData;
8+
import java.sql.ResultSet;
9+
import java.sql.SQLException;
10+
11+
import static org.junit.jupiter.api.Assertions.assertEquals;
12+
import static org.junit.jupiter.api.Assertions.assertFalse;
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
import static org.junit.jupiter.api.Assertions.assertNull;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
17+
public class JdbcDatabaseMetaDataIT extends AbstractJdbcIT {
18+
private DatabaseMetaData meta;
19+
20+
@BeforeEach
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+
@Disabled(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)