argument = ArgumentCaptor.forClass(TSExecuteStatementReq.class);
247 | verify(client).executeStatement(argument.capture());
248 | assertEquals("INSERT INTO root.ln.wf01.wt01(timestamp,a,b,c,d,e,f) VALUES(2017-11-01 00:13:00.000,false,123,123234345,123.423,-1323.0,'abc')", argument.getValue().getStatement());
249 | }
250 | }
251 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/TsfileConnectionTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import org.junit.After;
6 | import org.junit.Before;
7 | import org.junit.Test;
8 | import org.mockito.Mock;
9 | import org.mockito.MockitoAnnotations;
10 |
11 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSGetTimeZoneResp;
12 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSIService;
13 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSSetTimeZoneReq;
14 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSSetTimeZoneResp;
15 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_Status;
16 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_StatusCode;
17 |
18 | import static org.mockito.Matchers.any;
19 | import static org.mockito.Mockito.when;
20 |
21 | import org.apache.thrift.TException;
22 |
23 |
24 | public class TsfileConnectionTest {
25 | @Mock
26 | private TSIService.Iface client;
27 |
28 | private TsfileConnection connection = new TsfileConnection();
29 | private TS_Status Status_SUCCESS = new TS_Status(TS_StatusCode.SUCCESS_STATUS);
30 |
31 | @Before
32 | public void setUp() throws Exception {
33 | MockitoAnnotations.initMocks(this);
34 | }
35 |
36 | @After
37 | public void tearDown() throws Exception {
38 | }
39 |
40 | @Test
41 | public void testSetTimeZone() throws TException, TsfileSQLException {
42 | String timeZone = "Asia/Shanghai";
43 | when(client.setTimeZone(any(TSSetTimeZoneReq.class))).thenReturn(new TSSetTimeZoneResp(Status_SUCCESS));
44 | connection.client = client;
45 | connection.setTimeZone(timeZone);
46 | assertEquals(connection.getTimeZone(), timeZone);
47 | }
48 |
49 | @Test
50 | public void testGetTimeZone() throws TsfileSQLException, TException {
51 | String timeZone = "GMT+:08:00";
52 | when(client.getTimeZone()).thenReturn(new TSGetTimeZoneResp(Status_SUCCESS, timeZone));
53 | connection.client = client;
54 | assertEquals(connection.getTimeZone(), timeZone);
55 | }
56 |
57 |
58 | }
59 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/TsfileDatabaseMetadataTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import cn.edu.tsinghua.iotdb.jdbc.thrift.*;
6 | import org.junit.Assert;
7 | import org.junit.Before;
8 | import org.junit.Test;
9 | import org.mockito.Mock;
10 | import org.mockito.MockitoAnnotations;
11 |
12 | import java.sql.*;
13 | import java.util.ArrayList;
14 | import java.util.HashSet;
15 | import java.util.List;
16 | import java.util.Set;
17 |
18 | import static org.mockito.Matchers.any;
19 | import static org.mockito.Mockito.when;
20 |
21 | /**
22 | * This class is designed to test the function of databaseMetaData which is used to fetch metadata from IoTDB.
23 | * (1) get all columns' name under a given path,
24 | * e.g., databaseMetaData.getColumns(“col”, “root”, null, null);
25 | * (2) get all delta objects under a given column
26 | * e.g., databaseMetaData.getColumns(“delta”, “vehicle”, null, null);
27 | * (3) show timeseries path
28 | * e.g., databaseMetaData.getColumns(“ts”, “root.vehicle.d0.s0”, null, null);
29 | * (4) show storage group
30 | * databaseMetaData.getColumns(“sg”, null, null, null);
31 | * (5) show metadata in json
32 | * ((TsfileDatabaseMetadata)databaseMetaData).getMetadataInJson()
33 | *
34 | * The tests utilize the mockito framework to mock responses from an IoTDB server.
35 | * The status of the IoTDB server mocked here is determined by the following four sql commands:
36 | * SET STORAGE GROUP TO root.vehicle;
37 | * CREATE TIMESERIES root.vehicle.d0.s0 WITH DATATYPE=INT32, ENCODING=RLE;
38 | * CREATE TIMESERIES root.vehicle.d0.s1 WITH DATATYPE=INT64, ENCODING=RLE;
39 | * CREATE TIMESERIES root.vehicle.d0.s2 WITH DATATYPE=FLOAT, ENCODING=RLE;
40 | */
41 |
42 | public class TsfileDatabaseMetadataTest {
43 | @Mock
44 | private TsfileConnection connection;
45 |
46 | @Mock
47 | private TSIService.Iface client;
48 |
49 | @Mock
50 | private TSFetchMetadataResp fetchMetadataResp;
51 |
52 | private TS_Status Status_SUCCESS = new TS_Status(TS_StatusCode.SUCCESS_STATUS);
53 |
54 | private DatabaseMetaData databaseMetaData;
55 |
56 | @Before
57 | public void before() throws Exception {
58 | MockitoAnnotations.initMocks(this);
59 | when(connection.getMetaData()).thenReturn(new TsfileDatabaseMetadata(connection, client));
60 |
61 | when(client.fetchMetadata(any(TSFetchMetadataReq.class))).thenReturn(fetchMetadataResp);
62 | when(fetchMetadataResp.getStatus()).thenReturn(Status_SUCCESS);
63 |
64 | databaseMetaData = connection.getMetaData();
65 | }
66 |
67 | /**
68 | * get all columns' name under a given path
69 | */
70 | @SuppressWarnings("resource")
71 | @Test
72 | public void AllColumns() throws Exception {
73 | List columnList = new ArrayList<>();
74 | columnList.add("root.vehicle.d0.s0");
75 | columnList.add("root.vehicle.d0.s1");
76 | columnList.add("root.vehicle.d0.s2");
77 |
78 | when(fetchMetadataResp.getColumnsList()).thenReturn(columnList);
79 |
80 | String standard = "Column,\n" +
81 | "root.vehicle.d0.s0,\n" +
82 | "root.vehicle.d0.s1,\n" +
83 | "root.vehicle.d0.s2,\n";
84 | try {
85 | ResultSet resultSet = databaseMetaData.getColumns(TsFileDBConstant.CatalogColumn, "root", null, null);
86 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
87 | int colCount = resultSetMetaData.getColumnCount();
88 | StringBuilder resultStr = new StringBuilder();
89 | for (int i = 1; i < colCount + 1; i++) {
90 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
91 | }
92 | resultStr.append("\n");
93 | while (resultSet.next()) {
94 | for (int i = 1; i <= colCount; i++) {
95 | resultStr.append(resultSet.getString(i)).append(",");
96 | }
97 | resultStr.append("\n");
98 | }
99 | Assert.assertEquals(resultStr.toString(), standard);
100 | } catch (SQLException e) {
101 | System.out.println(e);
102 | }
103 | }
104 |
105 | /**
106 | * get all delta objects under a given column
107 | */
108 | @SuppressWarnings("resource")
109 | @Test
110 | public void DeltaObject() throws Exception {
111 | List columnList = new ArrayList<>();
112 | columnList.add("root.vehicle.d0");
113 |
114 | when(fetchMetadataResp.getColumnsList()).thenReturn(columnList);
115 |
116 | String standard = "Column,\n" +
117 | "root.vehicle.d0,\n";
118 | try {
119 | ResultSet resultSet = databaseMetaData.getColumns(TsFileDBConstant.CatalogDeltaObject, "vehicle", null, null);
120 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
121 | int colCount = resultSetMetaData.getColumnCount();
122 | StringBuilder resultStr = new StringBuilder();
123 | for (int i = 1; i < colCount + 1; i++) {
124 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
125 | }
126 | resultStr.append("\n");
127 | while (resultSet.next()) {
128 | for (int i = 1; i <= colCount; i++) {
129 | resultStr.append(resultSet.getString(i)).append(",");
130 | }
131 | resultStr.append("\n");
132 | }
133 | Assert.assertEquals(resultStr.toString(), standard);
134 | } catch (SQLException e) {
135 | System.out.println(e);
136 | }
137 | }
138 |
139 | /**
140 | * show timeseries
141 | * usage 1
142 | */
143 | @SuppressWarnings({ "resource", "serial" })
144 | @Test
145 | public void ShowTimeseriesPath1() throws Exception {
146 | List> tslist = new ArrayList<>();
147 | tslist.add(new ArrayList(4) {{
148 | add("root.vehicle.d0.s0");
149 | add("root.vehicle");
150 | add("INT32");
151 | add("RLE");
152 | }});
153 | tslist.add(new ArrayList(4) {{
154 | add("root.vehicle.d0.s1");
155 | add("root.vehicle");
156 | add("INT64");
157 | add("RLE");
158 | }});
159 | tslist.add(new ArrayList(4) {{
160 | add("root.vehicle.d0.s2");
161 | add("root.vehicle");
162 | add("FLOAT");
163 | add("RLE");
164 | }});
165 |
166 | when(fetchMetadataResp.getShowTimeseriesList()).thenReturn(tslist);
167 |
168 | String standard = "Timeseries,Storage Group,DataType,Encoding,\n" +
169 | "root.vehicle.d0.s0,root.vehicle,INT32,RLE,\n" +
170 | "root.vehicle.d0.s1,root.vehicle,INT64,RLE,\n" +
171 | "root.vehicle.d0.s2,root.vehicle,FLOAT,RLE,\n";
172 | try {
173 | ResultSet resultSet = databaseMetaData.getColumns(TsFileDBConstant.CatalogTimeseries, "root", null, null);
174 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
175 | int colCount = resultSetMetaData.getColumnCount();
176 | StringBuilder resultStr = new StringBuilder();
177 | for (int i = 1; i < colCount + 1; i++) {
178 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
179 | }
180 | resultStr.append("\n");
181 | while (resultSet.next()) {
182 | for (int i = 1; i <= colCount; i++) {
183 | resultStr.append(resultSet.getString(i)).append(",");
184 | }
185 | resultStr.append("\n");
186 | }
187 | Assert.assertEquals(resultStr.toString(), standard);
188 | } catch (SQLException e) {
189 | System.out.println(e);
190 | }
191 | }
192 |
193 | /**
194 | * show timeseries
195 | * usage 2: Get information about a specific column, e.g., DataType
196 | */
197 | @SuppressWarnings({ "resource", "serial" })
198 | @Test
199 | public void ShowTimeseriesPath2() throws Exception {
200 | List> tslist = new ArrayList<>();
201 | tslist.add(new ArrayList(4) {{
202 | add("root.vehicle.d0.s0");
203 | add("root.vehicle");
204 | add("INT32");
205 | add("RLE");
206 | }});
207 |
208 | when(fetchMetadataResp.getShowTimeseriesList()).thenReturn(tslist);
209 |
210 | String standard = "DataType,\n" +
211 | "INT32,\n";
212 | try {
213 | ResultSet resultSet = databaseMetaData.getColumns(TsFileDBConstant.CatalogTimeseries, "root.vehicle.d0.s0", null, null);
214 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
215 | StringBuilder resultStr = new StringBuilder();
216 | resultStr.append(resultSetMetaData.getColumnName(3)).append(",\n");
217 | while (resultSet.next()) {
218 | resultStr.append(resultSet.getString(TsfileMetadataResultSet.GET_STRING_TIMESERIES_DATATYPE)).append(",");
219 | resultStr.append("\n");
220 | }
221 | Assert.assertEquals(resultStr.toString(), standard);
222 | } catch (SQLException e) {
223 | System.out.println(e);
224 | }
225 | }
226 |
227 | /**
228 | * show storage group
229 | */
230 | @SuppressWarnings("resource")
231 | @Test
232 | public void ShowStorageGroup() throws Exception {
233 | Set sgSet = new HashSet<>();
234 | sgSet.add("root.vehicle");
235 | when(fetchMetadataResp.getShowStorageGroups()).thenReturn(sgSet);
236 |
237 | String standard = "Storage Group,\n" +
238 | "root.vehicle,\n";
239 | try {
240 | ResultSet resultSet = databaseMetaData.getColumns(TsFileDBConstant.CatalogStorageGroup, null, null, null);
241 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
242 | int colCount = resultSetMetaData.getColumnCount();
243 | StringBuilder resultStr = new StringBuilder();
244 | for (int i = 1; i < colCount + 1; i++) {
245 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
246 | }
247 | resultStr.append("\n");
248 | while (resultSet.next()) {
249 | for (int i = 1; i <= colCount; i++) {
250 | resultStr.append(resultSet.getString(i)).append(",");
251 | }
252 | resultStr.append("\n");
253 | }
254 | Assert.assertEquals(resultStr.toString(), standard);
255 | } catch (SQLException e) {
256 | System.out.println(e);
257 | }
258 | }
259 |
260 | /**
261 | * show metadata in json
262 | */
263 | @SuppressWarnings("resource")
264 | @Test
265 | public void ShowTimeseriesInJson() throws Exception {
266 | String metadataInJson = "=== Timeseries Tree ===\n" +
267 | "\n" +
268 | "root:{\n" +
269 | " vehicle:{\n" +
270 | " d0:{\n" +
271 | " s0:{\n" +
272 | " DataType: INT32,\n" +
273 | " Encoding: RLE,\n" +
274 | " args: {},\n" +
275 | " StorageGroup: root.vehicle \n" +
276 | " },\n" +
277 | " s1:{\n" +
278 | " DataType: INT64,\n" +
279 | " Encoding: RLE,\n" +
280 | " args: {},\n" +
281 | " StorageGroup: root.vehicle \n" +
282 | " },\n" +
283 | " s2:{\n" +
284 | " DataType: FLOAT,\n" +
285 | " Encoding: RLE,\n" +
286 | " args: {},\n" +
287 | " StorageGroup: root.vehicle \n" +
288 | " }\n" +
289 | " }\n" +
290 | " }\n" +
291 | "}";
292 |
293 | when(fetchMetadataResp.getMetadataInJson()).thenReturn(metadataInJson);
294 |
295 | String res = ((TsfileDatabaseMetadata)databaseMetaData).getMetadataInJson();
296 | assertEquals(metadataInJson, res);
297 | }
298 | }
299 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/TsfileMetadataResultMetadataTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.assertEquals;
4 |
5 | import java.sql.SQLException;
6 |
7 | import org.junit.After;
8 | import org.junit.Before;
9 | import org.junit.Test;
10 |
11 | public class TsfileMetadataResultMetadataTest {
12 |
13 | private TsfileMetadataResultMetadata metadata;
14 | private String[] cols = {"a1", "a2", "a3", "a4"};
15 |
16 | @Before
17 | public void setUp() throws Exception {
18 |
19 | }
20 |
21 | @After
22 | public void tearDown() throws Exception {
23 | }
24 |
25 | @Test
26 | public void testGetColumnCount() throws SQLException {
27 | boolean flag = false;
28 | try {
29 | metadata = new TsfileMetadataResultMetadata(null);
30 | assertEquals((long)metadata.getColumnCount(), 0);
31 | } catch (Exception e) {
32 | flag = true;
33 | }
34 | assertEquals(flag, true);
35 |
36 | flag = false;
37 | try {
38 | String[] nullArray = {};
39 | metadata = new TsfileMetadataResultMetadata(nullArray);
40 | assertEquals((long)metadata.getColumnCount(), 0);
41 | } catch (Exception e) {
42 | flag = true;
43 | }
44 | assertEquals(flag, true);
45 |
46 | metadata = new TsfileMetadataResultMetadata(cols);
47 | assertEquals((long)metadata.getColumnCount(), cols.length);
48 | }
49 |
50 |
51 | @Test
52 | public void testGetColumnName() throws SQLException {
53 | boolean flag = false;
54 | metadata = new TsfileMetadataResultMetadata(null);
55 | try {
56 | metadata.getColumnName(1);
57 | } catch (Exception e) {
58 | flag = true;
59 | }
60 | assertEquals(flag, true);
61 | try {
62 | String[] nullArray = {};
63 | metadata = new TsfileMetadataResultMetadata(nullArray);
64 | metadata.getColumnName(1);
65 | } catch (Exception e) {
66 | flag = true;
67 | }
68 | assertEquals(flag, true);
69 |
70 | metadata = new TsfileMetadataResultMetadata(cols);
71 | try {
72 | metadata.getColumnName(0);
73 | } catch (Exception e) {
74 | flag = true;
75 | }
76 | assertEquals(flag, true);
77 |
78 | flag = false;
79 | try {
80 | metadata.getColumnName(cols.length+1);
81 | } catch (Exception e) {
82 | flag = true;
83 | }
84 | assertEquals(flag, true);
85 |
86 | for(int i = 1; i <= cols.length; i++){
87 | assertEquals(metadata.getColumnName(i), cols[i-1]);
88 | }
89 |
90 | }
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/TsfileResultMetadataTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import java.sql.SQLException;
6 | import java.sql.Types;
7 | import java.util.ArrayList;
8 | import java.util.Arrays;
9 | import java.util.List;
10 |
11 | import org.junit.After;
12 | import org.junit.Before;
13 | import org.junit.Test;
14 |
15 | public class TsfileResultMetadataTest {
16 | private TsfileResultMetadata metadata;
17 |
18 | @Before
19 | public void setUp() throws Exception {
20 | }
21 |
22 | @After
23 | public void tearDown() throws Exception {
24 | }
25 |
26 | @Test
27 | public void testGetColumnCount() throws SQLException {
28 | metadata = new TsfileResultMetadata(null, null, null);
29 | boolean flag = false;
30 | try {
31 | metadata.getColumnCount();
32 | } catch (Exception e) {
33 | flag = true;
34 | }
35 | assertEquals(flag, true);
36 |
37 | List columnInfoList = new ArrayList<>();
38 | flag = false;
39 | try {
40 | metadata = new TsfileResultMetadata(columnInfoList, null, null);
41 | metadata.getColumnCount();
42 | } catch (Exception e) {
43 | flag = true;
44 | }
45 | assertEquals(flag, true);
46 |
47 | columnInfoList.add("root.a.b.c");
48 | assertEquals(metadata.getColumnCount(), 1);
49 | }
50 |
51 |
52 | @Test
53 | public void testGetColumnName() throws SQLException {
54 | metadata = new TsfileResultMetadata(null, null, null);
55 | boolean flag = false;
56 | try {
57 | metadata.getColumnName(1);
58 | } catch (Exception e) {
59 | flag = true;
60 | }
61 | assertEquals(flag, true);
62 |
63 | List columnInfoList = new ArrayList<>();
64 | metadata = new TsfileResultMetadata(columnInfoList, null, null);
65 | flag = false;
66 | try {
67 | metadata.getColumnName(1);
68 | } catch (Exception e) {
69 | flag = true;
70 | }
71 | assertEquals(flag, true);
72 |
73 | String[] colums = {"root.a.b.c1", "root.a.b.c2", "root.a.b.c3"};
74 | metadata = new TsfileResultMetadata(Arrays.asList(colums), null, null);
75 | flag = false;
76 | try {
77 | metadata.getColumnName(colums.length+1);
78 | } catch (Exception e) {
79 | flag = true;
80 | }
81 | assertEquals(flag, true);
82 |
83 | flag = false;
84 | try {
85 | metadata.getColumnName(0);
86 | } catch (Exception e) {
87 | flag = true;
88 | }
89 | assertEquals(flag, true);
90 |
91 | for(int i = 1; i <= colums.length;i++) {
92 | assertEquals(metadata.getColumnLabel(i), colums[i-1]);
93 | }
94 |
95 | }
96 |
97 | @Test
98 | public void testGetColumnType() throws SQLException {
99 | metadata = new TsfileResultMetadata(null, null, null);
100 | boolean flag = false;
101 | try {
102 | metadata.getColumnType(1);
103 | } catch (Exception e) {
104 | flag = true;
105 | }
106 | assertEquals(flag, true);
107 |
108 | List columnInfoList = new ArrayList<>();
109 | metadata = new TsfileResultMetadata(columnInfoList, null, null);
110 | flag = false;
111 | try {
112 | metadata.getColumnType(1);
113 | } catch (Exception e) {
114 | flag = true;
115 | }
116 | assertEquals(flag, true);
117 |
118 | String[] columns = {"timestamp", "root.a.b.boolean", "root.a.b.int32", "root.a.b.int64" , "root.a.b.float", "root.a.b.double", "root.a.b.text"};
119 | String[] typesString = {"BOOLEAN", "INT32", "INT64", "FLOAT", "DOUBLE", "TEXT"};
120 | int[] types = {Types.BOOLEAN, Types.INTEGER, Types.BIGINT, Types.FLOAT, Types.DOUBLE, Types.VARCHAR};
121 | metadata = new TsfileResultMetadata(Arrays.asList(columns), null, Arrays.asList(typesString));
122 | flag = false;
123 | try {
124 | metadata.getColumnType(columns.length+1);
125 | } catch (Exception e) {
126 | flag = true;
127 | }
128 | assertEquals(flag, true);
129 |
130 | flag = false;
131 | try {
132 | metadata.getColumnType(0);
133 | } catch (Exception e) {
134 | flag = true;
135 | }
136 | assertEquals(flag, true);
137 |
138 | assertEquals(metadata.getColumnType(1), Types.TIMESTAMP);
139 | for(int i = 1; i <= types.length; i++) {
140 | assertEquals(metadata.getColumnType(i+1), types[i-1]);
141 | }
142 | }
143 |
144 | @Test
145 | public void testGetColumnTypeName() throws SQLException {
146 | String operationType = "sum";
147 | metadata = new TsfileResultMetadata(null, operationType, null);
148 | assertEquals(metadata.getColumnTypeName(1), operationType);
149 | }
150 |
151 | }
152 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/TsfileStatementTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.*;
4 | import static org.mockito.Mockito.when;
5 | import static org.mockito.Matchers.any;
6 |
7 | import java.sql.ResultSet;
8 | import java.sql.ResultSetMetaData;
9 | import java.sql.SQLException;
10 | import java.util.ArrayList;
11 | import java.util.HashSet;
12 | import java.util.List;
13 | import java.util.Set;
14 |
15 | import org.apache.thrift.TException;
16 | import org.junit.After;
17 | import org.junit.Assert;
18 | import org.junit.Before;
19 | import org.junit.Test;
20 | import org.mockito.Mock;
21 | import org.mockito.MockitoAnnotations;
22 |
23 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSFetchMetadataReq;
24 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSFetchMetadataResp;
25 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_SessionHandle;
26 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_Status;
27 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_StatusCode;
28 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSIService.Iface;
29 |
30 | public class TsfileStatementTest {
31 | @Mock
32 | private TsfileConnection connection;
33 |
34 | @Mock
35 | private Iface client;
36 |
37 | @Mock
38 | private TS_SessionHandle sessHandle;
39 |
40 | @Mock
41 | private TSFetchMetadataResp fetchMetadataResp;
42 |
43 | private TS_Status Status_SUCCESS = new TS_Status(TS_StatusCode.SUCCESS_STATUS);
44 |
45 | @Before
46 | public void setUp() throws Exception {
47 | MockitoAnnotations.initMocks(this);
48 | when(connection.getMetaData()).thenReturn(new TsfileDatabaseMetadata(connection, client));
49 | when(connection.isClosed()).thenReturn(false);
50 | when(client.fetchMetadata(any(TSFetchMetadataReq.class))).thenReturn(fetchMetadataResp);
51 | when(fetchMetadataResp.getStatus()).thenReturn(Status_SUCCESS);
52 | }
53 |
54 | @After
55 | public void tearDown() throws Exception {
56 | }
57 |
58 | @SuppressWarnings("resource")
59 | @Test(expected = SQLException.class)
60 | public void testExecuteSQL1() throws SQLException {
61 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
62 | stmt.execute("show timeseries");
63 | }
64 |
65 | @SuppressWarnings({ "resource", "serial" })
66 | @Test
67 | public void testExecuteSQL2() throws SQLException, TException {
68 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
69 | List> tslist = new ArrayList<>();
70 | tslist.add(new ArrayList(4) {{
71 | add("root.vehicle.d0.s0");
72 | add("root.vehicle");
73 | add("INT32");
74 | add("RLE");
75 | }});
76 | tslist.add(new ArrayList(4) {{
77 | add("root.vehicle.d0.s1");
78 | add("root.vehicle");
79 | add("INT64");
80 | add("RLE");
81 | }});
82 | tslist.add(new ArrayList(4) {{
83 | add("root.vehicle.d0.s2");
84 | add("root.vehicle");
85 | add("FLOAT");
86 | add("RLE");
87 | }});
88 | String standard = "Timeseries,Storage Group,DataType,Encoding,\n" +
89 | "root.vehicle.d0.s0,root.vehicle,INT32,RLE,\n" +
90 | "root.vehicle.d0.s1,root.vehicle,INT64,RLE,\n" +
91 | "root.vehicle.d0.s2,root.vehicle,FLOAT,RLE,\n";
92 | when(fetchMetadataResp.getShowTimeseriesList()).thenReturn(tslist);
93 | boolean res = stmt.execute("show timeseries root");
94 | assertEquals(res, true);
95 | try {
96 | ResultSet resultSet = stmt.getResultSet();
97 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
98 | int colCount = resultSetMetaData.getColumnCount();
99 | StringBuilder resultStr = new StringBuilder();
100 | for (int i = 1; i < colCount + 1; i++) {
101 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
102 | }
103 | resultStr.append("\n");
104 | while (resultSet.next()) {
105 | for (int i = 1; i <= colCount; i++) {
106 | resultStr.append(resultSet.getString(i)).append(",");
107 | }
108 | resultStr.append("\n");
109 | }
110 | Assert.assertEquals(resultStr.toString(), standard);
111 | } catch (SQLException e) {
112 | System.out.println(e);
113 | }
114 | }
115 |
116 | @SuppressWarnings({ "resource"})
117 | @Test
118 | public void testExecuteSQL3() throws SQLException, TException {
119 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
120 | Set sgSet = new HashSet<>();
121 | sgSet.add("root.vehicle");
122 | when(fetchMetadataResp.getShowStorageGroups()).thenReturn(sgSet);
123 | String standard = "Storage Group,\nroot.vehicle,\n";
124 | boolean res = stmt.execute("show storage group");
125 | assertEquals(res, true);
126 | try {
127 | ResultSet resultSet = stmt.getResultSet();
128 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
129 | int colCount = resultSetMetaData.getColumnCount();
130 | StringBuilder resultStr = new StringBuilder();
131 | for (int i = 1; i < colCount + 1; i++) {
132 | resultStr.append(resultSetMetaData.getColumnName(i)).append(",");
133 | }
134 | resultStr.append("\n");
135 | while (resultSet.next()) {
136 | for (int i = 1; i <= colCount; i++) {
137 | resultStr.append(resultSet.getString(i)).append(",");
138 | }
139 | resultStr.append("\n");
140 | }
141 | Assert.assertEquals(resultStr.toString(), standard);
142 | } catch (SQLException e) {
143 | System.out.println(e);
144 | }
145 | }
146 |
147 | @SuppressWarnings("resource")
148 | @Test
149 | public void testSetFetchSize1() throws SQLException {
150 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
151 | stmt.setFetchSize(123);
152 | assertEquals(123, stmt.getFetchSize());
153 | }
154 |
155 | @SuppressWarnings("resource")
156 | @Test
157 | public void testSetFetchSize2() throws SQLException {
158 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
159 | int initial = stmt.getFetchSize();
160 | stmt.setFetchSize(0);
161 | assertEquals(initial, stmt.getFetchSize());
162 | }
163 |
164 | @SuppressWarnings("resource")
165 | @Test
166 | public void testSetFetchSize3() throws SQLException {
167 | final int fetchSize = 10000;
168 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle, fetchSize);
169 | assertEquals(fetchSize, stmt.getFetchSize());
170 | }
171 |
172 | @SuppressWarnings("resource")
173 | @Test(expected = SQLException.class)
174 | public void testSetFetchSize4() throws SQLException {
175 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
176 | stmt.setFetchSize(-1);
177 | }
178 |
179 | @SuppressWarnings("resource")
180 | @Test
181 | public void testSetMaxRows1() throws SQLException {
182 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
183 | stmt.setMaxRows(123);
184 | assertEquals(123, stmt.getMaxRows());
185 | }
186 |
187 | @SuppressWarnings("resource")
188 | @Test(expected = SQLException.class)
189 | public void testSetMaxRows2() throws SQLException {
190 | TsfileStatement stmt = new TsfileStatement(connection, client, sessHandle);
191 | stmt.setMaxRows(-1);
192 | }
193 | }
194 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/UtilsTest.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc;
2 |
3 | import static org.junit.Assert.*;
4 |
5 | import java.nio.ByteBuffer;
6 | import java.util.ArrayList;
7 | import java.util.LinkedHashMap;
8 | import java.util.List;
9 | import java.util.Properties;
10 |
11 | import cn.edu.tsinghua.tsfile.timeseries.read.common.Path;
12 | import cn.edu.tsinghua.tsfile.timeseries.read.datatype.RowRecord;
13 | import cn.edu.tsinghua.tsfile.timeseries.read.datatype.TsPrimitiveType;
14 | import org.junit.After;
15 | import org.junit.Before;
16 | import org.junit.Test;
17 |
18 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSDataValue;
19 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSQueryDataSet;
20 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TSRowRecord;
21 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_Status;
22 | import cn.edu.tsinghua.iotdb.jdbc.thrift.TS_StatusCode;
23 | import cn.edu.tsinghua.tsfile.file.metadata.enums.TSDataType;
24 |
25 | public class UtilsTest {
26 |
27 | @Before
28 | public void setUp() throws Exception {
29 | }
30 |
31 | @After
32 | public void tearDown() throws Exception {
33 | }
34 |
35 | @Test
36 | public void testParseURL() throws TsfileURLException {
37 | String userName = "test";
38 | String userPwd = "test";
39 | String host = "localhost";
40 | int port = 6667;
41 | Properties properties = new Properties();
42 | properties.setProperty(TsfileJDBCConfig.AUTH_USER, userName);
43 | properties.setProperty(TsfileJDBCConfig.AUTH_PASSWORD, userPwd);
44 | TsfileConnectionParams params = Utils.parseURL(String.format("jdbc:tsfile://%s:%s/", host, port), properties);
45 | assertEquals(params.getHost(), host);
46 | assertEquals(params.getPort(), port);
47 | assertEquals(params.getUsername(), userName);
48 | assertEquals(params.getPassword(), userPwd);
49 | }
50 |
51 | @Test
52 | public void testVerifySuccess() {
53 | try {
54 | Utils.verifySuccess(new TS_Status(TS_StatusCode.SUCCESS_STATUS));
55 | } catch (Exception e) {
56 | fail();
57 | }
58 |
59 | try {
60 | Utils.verifySuccess(new TS_Status(TS_StatusCode.ERROR_STATUS));
61 | } catch (Exception e) {
62 | return;
63 | }
64 | fail();
65 | }
66 |
67 | @Test
68 | public void testConvertRowRecords() {
69 | final int DATA_TYPE_NUM = 6;
70 | Object[][] input = {
71 | {
72 | 100L,
73 | "sensor1_boolean", TSDataType.BOOLEAN, false,
74 | "sensor1_int32", TSDataType.INT32, 100,
75 | "sensor1_int64", TSDataType.INT64, 9999999999L,
76 | "sensor1_float", TSDataType.FLOAT, 1.23f,
77 | "sensor1_double", TSDataType.DOUBLE, 1004234.435d,
78 | "sensor1_text", TSDataType.TEXT, "iotdb-jdbc",
79 | },
80 | {
81 | 200L,
82 | "sensor2_boolean", TSDataType.BOOLEAN, true,
83 | "sensor2_int32", TSDataType.INT32, null,
84 | "sensor2_int64", TSDataType.INT64, -9999999999L,
85 | "sensor2_float", TSDataType.FLOAT, null,
86 | "sensor2_double", TSDataType.DOUBLE, -1004234.435d,
87 | "sensor2_text", TSDataType.TEXT, null,
88 | },
89 | {
90 | 300L,
91 | "sensor3_boolean", TSDataType.BOOLEAN, null,
92 | "sensor3_int32", TSDataType.INT32, -100,
93 | "sensor3_int64", TSDataType.INT64, null,
94 | "sensor3_float", TSDataType.FLOAT, -1.23f,
95 | "sensor3_double", TSDataType.DOUBLE, null,
96 | "sensor3_text", TSDataType.TEXT, "jdbc-iotdb",
97 | },
98 | };
99 | TSQueryDataSet tsQueryDataSet = new TSQueryDataSet(new ArrayList<>());
100 | for(Object[] item: input) {
101 | TSRowRecord record = new TSRowRecord();
102 | record.setTimestamp((long)item[0]);
103 | List keys = new ArrayList<>();
104 | List values = new ArrayList<>();
105 | for(int i = 0; i < DATA_TYPE_NUM;i++) {
106 | keys.add((String)item[3*i+1]);
107 | TSDataValue value = new TSDataValue(false);
108 | if(item[3*i+3] == null) {
109 | value.setIs_empty(true);
110 | } else {
111 | if(i == 0) {
112 | value.setBool_val((boolean)item[3*i+3]);
113 | value.setType(((TSDataType) item[3*i+2]).toString());
114 | } else if (i == 1) {
115 | value.setInt_val((int) item[3 * i + 3]);
116 | value.setType(((TSDataType) item[3*i+2]).toString());
117 | } else if (i == 2) {
118 | value.setLong_val((long) item[3 * i + 3]);
119 | value.setType(((TSDataType) item[3*i+2]).toString());
120 | } else if (i == 3) {
121 | value.setFloat_val((float) item[3 * i + 3]);
122 | value.setType(((TSDataType) item[3*i+2]).toString());
123 | } else if (i == 4) {
124 | value.setDouble_val((double) item[3 * i + 3]);
125 | value.setType(((TSDataType) item[3*i+2]).toString());
126 | } else {
127 | value.setBinary_val(ByteBuffer.wrap(((String) item[3*i+3]).getBytes()));
128 | value.setType(((TSDataType) item[3*i+2]).toString());
129 | }
130 | }
131 | values.add(value);
132 | }
133 | record.setKeys(keys);
134 | record.setValues(values);
135 | tsQueryDataSet.getRecords().add(record);
136 | }
137 | List convertlist = Utils.convertRowRecords(tsQueryDataSet);
138 | int index = 0;
139 | for (RowRecord r : convertlist) {
140 | assertEquals(input[index][0], r.getTimestamp());
141 | LinkedHashMap fields = r.getFields();
142 | int j = 0;
143 | for (Path p : fields.keySet()) {
144 | String pString = p.getFullPath();
145 | assertEquals(input[index][3 * j + 1], pString);
146 | // System.out.println(String.format("%d--%d", index, j));
147 | if (j == 0) {
148 | if(input[index][3 * j + 3] == null){
149 | assertEquals(input[index][3 * j + 3], fields.get(p));
150 | } else {
151 | assertEquals(input[index][3 * j + 3], fields.get(p).getBoolean());
152 | }
153 | } else if (j == 1) {
154 | if(input[index][3 * j + 3] == null){
155 | assertEquals(input[index][3 * j + 3], fields.get(p));
156 | } else {
157 | assertEquals(input[index][3 * j + 3], fields.get(p).getInt());
158 | }
159 | } else if (j == 2) {
160 | if(input[index][3 * j + 3] == null){
161 | assertEquals(input[index][3 * j + 3], fields.get(p));
162 | } else {
163 | assertEquals(input[index][3 * j + 3], fields.get(p).getLong());
164 | }
165 | } else if (j == 3) {
166 | if(input[index][3 * j + 3] == null){
167 | assertEquals(input[index][3 * j + 3], fields.get(p));
168 | } else {
169 | assertEquals(input[index][3 * j + 3], fields.get(p).getFloat());
170 | }
171 | } else if (j == 4) {
172 | if(input[index][3 * j + 3] == null){
173 | assertEquals(input[index][3 * j + 3], fields.get(p));
174 | } else {
175 | assertEquals(input[index][3 * j + 3], fields.get(p).getDouble());
176 | }
177 | } else {
178 | if(input[index][3 * j + 3] == null){
179 | assertEquals(input[index][3 * j + 3], fields.get(p));
180 | } else {
181 | assertEquals(input[index][3 * j + 3], fields.get(p).getStringValue());
182 | }
183 | }
184 | j++;
185 | }
186 | index++;
187 | }
188 | }
189 |
190 | }
191 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/demo/MetadataDemo.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc.demo;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DatabaseMetaData;
5 | import java.sql.DriverManager;
6 | import java.sql.PreparedStatement;
7 | import java.sql.ResultSet;
8 | import java.sql.ResultSetMetaData;
9 | import java.sql.SQLException;
10 | import java.sql.Statement;
11 | import java.sql.Timestamp;
12 |
13 | import cn.edu.tsinghua.iotdb.jdbc.TsfileJDBCConfig;
14 |
15 | public class MetadataDemo {
16 |
17 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
18 | // Class.forName(TsfileJDBCConfig.JDBC_DRIVER_NAME);
19 | // Connection connection = null;
20 | // try {
21 | // connection = DriverManager.getConnection("jdbc:tsfile://127.0.0.1:6667/", "root", "root");
22 | // DatabaseMetaData databaseMetaData = connection.getMetaData();
23 | // } finally {
24 | // connection.close();
25 | // }
26 | }
27 |
28 | }
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/demo/PrepareStatementDemo.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc.demo;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.PreparedStatement;
6 | import java.sql.ResultSet;
7 | import java.sql.ResultSetMetaData;
8 | import java.sql.SQLException;
9 | import java.sql.Timestamp;
10 |
11 | public class PrepareStatementDemo {
12 |
13 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
14 | Class.forName("cn.edu.tsinghua.iotdb.jdbc.TsfileDriver");
15 | Connection connection = null;
16 | try {
17 | connection = DriverManager.getConnection("jdbc:tsfile://127.0.0.1:6667/", "root", "root");
18 | PreparedStatement preparedStatement = connection.prepareStatement("insert into root.ln.wf01.wt01(timestamp,status,temperature) values(?,?,?)");
19 | preparedStatement.setLong(1, 1509465600000L);
20 | preparedStatement.setBoolean(2, true);
21 | preparedStatement.setFloat(3, 25.957603f);
22 | preparedStatement.execute();
23 | preparedStatement.clearParameters();
24 |
25 | preparedStatement.setLong(1, 1509465660000L);
26 | preparedStatement.setBoolean(2, true);
27 | preparedStatement.setFloat(3, 24.359503f);
28 | preparedStatement.execute();
29 | preparedStatement.clearParameters();
30 |
31 | preparedStatement.setLong(1, 1509465720000L);
32 | preparedStatement.setBoolean(2, false);
33 | preparedStatement.setFloat(3, 20.092794f);
34 | preparedStatement.execute();
35 | preparedStatement.clearParameters();
36 |
37 | preparedStatement.setTimestamp(1, Timestamp.valueOf("2017-11-01 00:03:00"));
38 | preparedStatement.setBoolean(2, false);
39 | preparedStatement.setFloat(3, 20.092794f);
40 | preparedStatement.execute();
41 | preparedStatement.clearParameters();
42 |
43 | preparedStatement.close();
44 |
45 | ResultSet resultSet = preparedStatement.executeQuery("select * from root");
46 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
47 | while(resultSet.next()){
48 | StringBuilder builder = new StringBuilder();
49 | for (int i = 1; i <= resultSetMetaData.getColumnCount();i++) {
50 | builder.append(resultSet.getString(i)).append(",");
51 | }
52 | System.out.println(builder);
53 | }
54 | preparedStatement.close();
55 | for(int i = 1; i <= resultSetMetaData.getColumnCount();i++) {
56 | System.out.println(resultSetMetaData.getColumnType(i)+"-"+resultSetMetaData.getColumnName(i));
57 | }
58 | } finally {
59 | connection.close();
60 | }
61 | }
62 |
63 | }
64 |
--------------------------------------------------------------------------------
/src/test/java/cn/edu/tsinghua/iotdb/jdbc/demo/StatementDemo.java:
--------------------------------------------------------------------------------
1 | package cn.edu.tsinghua.iotdb.jdbc.demo;
2 |
3 | import java.sql.Connection;
4 | import java.sql.DriverManager;
5 | import java.sql.ResultSet;
6 | import java.sql.ResultSetMetaData;
7 | import java.sql.SQLException;
8 | import java.sql.Statement;
9 |
10 | public class StatementDemo {
11 |
12 | public static void main(String[] args) throws ClassNotFoundException, SQLException {
13 | Class.forName("cn.edu.tsinghua.iotdb.jdbc.TsfileDriver");
14 | Connection connection = null;
15 | try {
16 | connection = DriverManager.getConnection("jdbc:tsfile://127.0.0.1:6667/", "root", "root");
17 | Statement statement = connection.createStatement();
18 | statement.execute("SET STORAGE GROUP TO root.ln.wf01.wt01");
19 | statement.execute("CREATE TIMESERIES root.ln.wf01.wt01.status WITH DATATYPE=BOOLEAN, ENCODING=PLAIN");
20 | statement.execute("CREATE TIMESERIES root.ln.wf01.wt01.temperature WITH DATATYPE=FLOAT, ENCODING=RLE");
21 | statement.execute("insert into root.ln.wf01.wt01(timestamp,status) values(1509465600000,true)");
22 | statement.execute("insert into root.ln.wf01.wt01(timestamp,status) values(1509465660000,true)");
23 | statement.execute("insert into root.ln.wf01.wt01(timestamp,status) values(1509465720000,false)");
24 | statement.execute("insert into root.ln.wf01.wt01(timestamp,temperature) values(1509465600000,25.957603)");
25 | statement.execute("insert into root.ln.wf01.wt01(timestamp,temperature) values(1509465660000,24.359503)");
26 | statement.execute("insert into root.ln.wf01.wt01(timestamp,temperature) values(1509465720000,20.092794)");
27 | ResultSet resultSet = statement.executeQuery("select * from root");
28 | ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
29 | while(resultSet.next()){
30 | StringBuilder builder = new StringBuilder();
31 | for (int i = 1; i <= resultSetMetaData.getColumnCount();i++) {
32 | builder.append(resultSet.getString(i)).append(",");
33 | }
34 | System.out.println(builder);
35 | }
36 | statement.close();
37 |
38 | } finally {
39 | connection.close();
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------