Skip to content
Navigation Menu
{{ message }}
forked from aaberg/sql2o
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIssuesTest.java
More file actions
274 lines (217 loc) · 10 KB
/
Copy pathIssuesTest.java
File metadata and controls
274 lines (217 loc) · 10 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package org.sql2o.issues;
import junit.framework.TestCase;
import org.hsqldb.jdbcDriver;
import org.joda.time.DateTime;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.sql2o.Connection;
import org.sql2o.Query;
import org.sql2o.Sql2o;
import org.sql2o.Sql2oException;
import org.sql2o.data.Row;
import org.sql2o.data.Table;
import org.sql2o.issues.pojos.Issue1Pojo;
import org.sql2o.issues.pojos.KeyValueEntity;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import static org.junit.Assert.*;
/**
* Created by IntelliJ IDEA.
* User: lars
* Date: 10/17/11
* Time: 9:02 PM
* This class is to test for reported issues.
*/
@RunWith(Parameterized.class)
public class IssuesTest {
@Parameterized.Parameters(name = "{index} - {4}")
public static Collection<Object[]> getData(){
return Arrays.asList(new Object[][]{
{null, "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1","sa", "", "H2 test" },
{new jdbcDriver(), "jdbc:hsqldb:mem:testmemdb", "SA", "", "HyperSQL DB test"}
});
}
private Sql2o sql2o;
private String url;
private String user;
private String pass;
public IssuesTest(Driver driverToRegister, String url, String user, String pass, String testName){
if (driverToRegister != null) {
try {
DriverManager.registerDriver(driverToRegister);
} catch (SQLException e) {
throw new RuntimeException("could not register driver '" + driverToRegister.getClass().getName() + "'", e);
}
}
this.sql2o = new Sql2o(url, user, pass);
this.url = url;
this.user = user;
this.pass = pass;
if ("HyperSQL DB test".equals( testName )) {
sql2o.createQuery("set database sql syntax MSS true").executeUpdate();
}
}
/**
* Tests for issue #1 https://github.com/aaberg/sql2o/issues/1
*
* Issue:
* I have a case where I need to override/modify the value loaded from db.
* I want to do this in a setter but the current version of sql2o modifies the property directly.
*
* Comment:
* The priority was wrong. Sql2o would try to set the field first, and afterwards the setter. The priority should be
* the setter first and the field after.
*/
@Test public void testSetterPriority(){
Sql2o sql2o = new Sql2o(url, user, pass);
Issue1Pojo pojo = sql2o.createQuery("select 1 val from (values(0))").executeAndFetchFirst(Issue1Pojo.class);
assertEquals(2, pojo.val);
}
/**
* Tests for issue #2 https://github.com/aaberg/sql2o/issues/2
*
* Issue: NPE - should instead tell what the problem is
*
*/
@Test public void testForFieldDoesNotExistException(){
Sql2o sql2o = new Sql2o(url, user, pass);
try{
KeyValueEntity pojo = sql2o.createQuery("select 1 id, 'something' foo from (values(0))").executeAndFetchFirst(KeyValueEntity.class);
}
catch(Sql2oException ex){
assertTrue(ex.getMessage().contains("not found"));
}
}
/**
* Tests for issue #3 https://github.com/aaberg/sql2o/issues/3
*
* Issue: If an exception occures in the database, while executing batch update,
* the database connection is not closed correctly.
*/
@Test public void testForConnectionStateAfterBatchException() throws SQLException {
sql2o.createQuery("create table issue3table(id integer identity primary key, val varchar(5))").executeUpdate();
boolean failed = false;
Connection connection = sql2o.beginTransaction();
try{
connection.createQuery("insert into issue3table(val) values(:val)")
.addParameter("val", "abcde").addToBatch()
.addParameter("val", "abcdefg").addToBatch() // should fail
.addParameter("val", "hello").addToBatch()
.executeBatch().commit();
}
catch(Sql2oException ex){
failed = true;
System.out.println("expected exception occured, msg: " + ex.getMessage());
}
assertTrue(failed);
assertTrue("Assert that connection is correctly closed (with transaction)", connection.getJdbcConnection().isClosed() );
// same test, but not in a transaction
Query query = sql2o.createQuery("insert into issue3table(val) values(:val)")
.addParameter("val", "abcde").addToBatch()
.addParameter("val", "abcdefg").addToBatch() // should fail
.addParameter("val", "hello").addToBatch();
boolean failed2 = false;
try{
query.executeBatch();
}
catch(Sql2oException ex){
failed2 = true;
System.out.println("expected error: " + ex.toString());
}
assertTrue(failed2);
assertTrue("Assert that connection is correctly closed (no transaction)", query.getConnection().getJdbcConnection().isClosed());
}
/**
* Tests for issue #4 https://github.com/aaberg/sql2o/issues/4
*
* NPE when typing wrong column name in row.get(...)
* Also, column name should not be case sensitive, if sql2o not is in casesensitive property is false.
*/
@Test public void testForNpeInRowGet(){
sql2o.createQuery("create table issue4table(id integer identity primary key, val varchar(20))").executeUpdate();
sql2o.createQuery("insert into issue4table (val) values (:val)")
.addParameter("val", "something").addToBatch()
.addParameter("val", "something else").addToBatch()
.addParameter("val", "hello").addToBatch()
.executeBatch();
Table table = sql2o.createQuery("select * from issue4table").executeAndFetchTable();
Row row0 = table.rows().get(0);
String row0Val = row0.getString("vAl");
assertEquals("something", row0Val);
Row row1 = table.rows().get(1);
boolean failed = false;
try{
String row1Value = row1.getString("ahsHashah"); // Should fail with an sql2o exception
}
catch(Sql2oException ex){
failed = true;
assertTrue(ex.getMessage().startsWith("Column with name 'ahsHashah' does not exist"));
}
assertTrue("assert that exception occurred", failed);
}
public static class Issue5POJO{
public int id;
public int val;
}
public static class Issue5POJO2{
public int id;
public int val;
public int getVal() {
return val;
}
public void setVal(int val) {
this.val = val;
}
}
/**
* Tests for issue #5 https://github.com/aaberg/sql2o/issues/5
* crashes if the POJO has a int field where we try to set a null value
*/
@Test public void testForNullToSimpeType(){
sql2o.createQuery("create table issue5table(id int identity primary key, val integer)").executeUpdate();
sql2o.createQuery("insert into issue5table(val) values (:val)").addParameter("val", (Object)null).executeUpdate();
List<Issue5POJO> list1 = sql2o.createQuery("select * from issue5table").executeAndFetch(Issue5POJO.class);
List<Issue5POJO2> list2 = sql2o.createQuery("select * from issue5table").executeAndFetch(Issue5POJO2.class);
assertEquals(1, list1.size());
assertEquals(1, list2.size());
assertEquals(0, list1.get(0).val);
assertEquals(0, list2.get(0).getVal());
}
/**
* Tests for issue #9 https://github.com/aaberg/sql2o/issues/9
* When running a select query with column labels (aliases) in HSQLDB, sql2o is still trying to use column names
* wheWhen running a select query with column labels (aliases) in HSQLDB, sql2o is still trying to use column names when mapping to java classes. This is caused by a behavior in HSQLDB, that is different from most other databases. the ResultSet.getColumnName() method will still return the real column name, even though a label was used. To get the label with HSQLDB, ResultSet.getColumnLabel().n mapping to java classes. This is caused by a behavior in HSQLDB, that is different from most other databases.
* the ResultSet.getColumnName() method will still return the real column name, even though a label was used. To get
* the label with HSQLDB, ResultSet.getColumnLabel().
*/
@Test public void testForLabelErrorInHsqlDb(){
sql2o.createQuery("create table issue9test (id integer identity primary key, val varchar(50))").executeUpdate();
String insertSql = "insert into issue9test(val) values (:val)";
sql2o.createQuery(insertSql).addParameter("val", "something").executeUpdate();
sql2o.createQuery(insertSql).addParameter("val", "something else").executeUpdate();
sql2o.createQuery(insertSql).addParameter("val", "something third").executeUpdate();
List<Issue9Pojo> pojos = sql2o.createQuery("select id, val theVal from issue9Test").executeAndFetch(Issue9Pojo.class);
assertEquals(3, pojos.size());
assertEquals("something", pojos.get(0).theVal);
}
public static enum WhatEverEnum{
VAL, ANOTHER_VAL;
}
@Test public void testForNullPointerExceptionInAddParameterMethod() {
sql2o.createQuery("create table issue11test (id integer identity primary key, val varchar(50), adate datetime)").executeUpdate();
String insertSql = "insert into issue11test (val, adate) values (:val, :date)";
sql2o.createQuery(insertSql).addParameter("val", WhatEverEnum.VAL).addParameter("date", new DateTime()).executeUpdate();
DateTime dtNull = null;
WhatEverEnum enumNull = null;
sql2o.createQuery(insertSql).addParameter("val", enumNull).addParameter("date", dtNull).executeUpdate();
}
public static class Issue9Pojo {
public int id;
public String theVal;
}
}
You can’t perform that action at this time.
