forked from xerial/sqlite-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResultSetTest.java
More file actions
160 lines (133 loc) · 5.56 KB
/
Copy pathResultSetTest.java
File metadata and controls
160 lines (133 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.sqlite;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ResultSetTest {
private Connection conn;
private Statement stat;
@BeforeEach
public void connect() throws Exception {
conn = DriverManager.getConnection("jdbc:sqlite:");
stat = conn.createStatement();
stat.executeUpdate(
"create table test (id int primary key, DESCRIPTION varchar(40), fOo varchar(3));");
stat.executeUpdate("insert into test values (1, 'description', 'bar')");
}
@AfterEach
public void close() throws SQLException {
stat.close();
conn.close();
}
@Test
public void testTableColumnLowerNowFindLowerCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(1, resultSet.findColumn("id"));
}
@Test
public void testTableColumnLowerNowFindUpperCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(1, resultSet.findColumn("ID"));
}
@Test
public void testTableColumnLowerNowFindMixedCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(1, resultSet.findColumn("Id"));
}
@Test
public void testTableColumnUpperNowFindLowerCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(2, resultSet.findColumn("description"));
}
@Test
public void testTableColumnUpperNowFindUpperCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(2, resultSet.findColumn("DESCRIPTION"));
}
@Test
public void testTableColumnUpperNowFindMixedCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(2, resultSet.findColumn("Description"));
}
@Test
public void testTableColumnMixedNowFindLowerCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(3, resultSet.findColumn("foo"));
}
@Test
public void testTableColumnMixedNowFindUpperCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(3, resultSet.findColumn("FOO"));
}
@Test
public void testTableColumnMixedNowFindMixedCaseColumn() throws SQLException {
ResultSet resultSet = stat.executeQuery("select * from test");
assertTrue(resultSet.next());
assertEquals(3, resultSet.findColumn("fOo"));
}
@Test
public void testSelectWithTableNameAliasNowFindWithoutTableNameAlias() throws SQLException {
ResultSet resultSet = stat.executeQuery("select t.id from test as t");
assertTrue(resultSet.next());
assertEquals(1, resultSet.findColumn("id"));
}
/**
* Can't produce a case where column name contains table name
* https://www.sqlite.org/c3ref/column_name.html : "If there is no AS clause then the name of
* the column is unspecified"
*/
@Test
public void testSelectWithTableNameAliasNowNotFindWithTableNameAlias() throws SQLException {
ResultSet resultSet = stat.executeQuery("select t.id from test as t");
assertTrue(resultSet.next());
assertThrows(SQLException.class, () -> resultSet.findColumn("t.id"));
}
@Test
public void testSelectWithTableNameNowFindWithoutTableName() throws SQLException {
ResultSet resultSet = stat.executeQuery("select test.id from test");
assertTrue(resultSet.next());
assertEquals(1, resultSet.findColumn("id"));
}
@Test
public void testSelectWithTableNameNowNotFindWithTableName() throws SQLException {
ResultSet resultSet = stat.executeQuery("select test.id from test");
assertTrue(resultSet.next());
assertThrows(SQLException.class, () -> resultSet.findColumn("test.id"));
}
@Test
public void testCloseStatement() throws SQLException {
ResultSet resultSet = stat.executeQuery("select test.id from test");
stat.close();
assertTrue(stat.isClosed());
assertTrue(resultSet.isClosed());
resultSet.close();
assertTrue(resultSet.isClosed());
}
@Test
public void testReturnsNonAsciiCodepoints() throws SQLException {
String nonAsciiString = "국정의 중요한 사항에 관한";
PreparedStatement pstat = conn.prepareStatement("select ?");
pstat.setString(1, nonAsciiString);
ResultSet resultSet = pstat.executeQuery();
assertTrue(resultSet.next());
assertEquals(nonAsciiString, resultSet.getString(1));
assertFalse(resultSet.next());
}
}