|
| 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.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.jupiter.api.Assertions.assertNotNull; |
| 20 | + |
| 21 | +//mvn -DtntHost=localhost -DtntPort=3301 -DtntUser=test -DtntPass=test verify |
| 22 | +public abstract class AbstractJdbcIT { |
| 23 | + private static final String host = System.getProperty("tntHost", "localhost"); |
| 24 | + private static final Integer port = Integer.valueOf(System.getProperty("tntPort", "3301")); |
| 25 | + private static final String user = System.getProperty("tntUser", "test"); |
| 26 | + private static final String pass = System.getProperty("tntPass", "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 | + @BeforeAll |
| 87 | + public static void setupEnv() throws Exception { |
| 88 | + sqlExec(initSql); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterAll |
| 92 | + public static void teardownEnv() throws Exception { |
| 93 | + sqlExec(cleanSql); |
| 94 | + } |
| 95 | + |
| 96 | + @BeforeEach |
| 97 | + public void setUpConnection() throws SQLException { |
| 98 | + conn = DriverManager.getConnection(URL); |
| 99 | + assertNotNull(conn); |
| 100 | + } |
| 101 | + |
| 102 | + @AfterEach |
| 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 | +} |
0 commit comments