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