forked from xerial/sqlite-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorMessageTest.java
More file actions
136 lines (110 loc) · 4.77 KB
/
ErrorMessageTest.java
File metadata and controls
136 lines (110 loc) · 4.77 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
package org.sqlite;
import static org.junit.Assume.assumeTrue;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.junit.Rule;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;
import org.junit.rules.ExpectedException;
public class ErrorMessageTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
static class VendorCodeMatcher extends BaseMatcher<Object> {
final SQLiteErrorCode expected;
VendorCodeMatcher(SQLiteErrorCode expected) {this.expected = expected;}
public boolean matches(Object o) {
if (!(o instanceof SQLException)) {
return false;
}
SQLException e = (SQLException)o;
SQLiteErrorCode ec = SQLiteErrorCode.getErrorCode(e.getErrorCode());
return ec == expected;
}
public void describeTo(Description description) {
description
.appendText("SQLException with error code ")
.appendText(expected.name())
.appendText(" (")
.appendValue(expected.code)
.appendText(")");
}
}
static class ResultCodeMatcher extends BaseMatcher<Object> {
final SQLiteErrorCode expected;
ResultCodeMatcher(SQLiteErrorCode expected) {this.expected = expected;}
public boolean matches(Object o) {
if (!(o instanceof SQLiteException)) {
return false;
}
SQLiteException e = (SQLiteException)o;
return e.getResultCode() == expected;
}
public void describeTo(Description description) {
description
.appendText("SQLiteException with error code ")
.appendText(expected.name())
.appendText(" (")
.appendValue(expected.code)
.appendText(")");
}
}
@Test
public void moved() throws SQLException, IOException {
File from = File.createTempFile("error-message-test-moved-from", ".sqlite");
from.deleteOnExit();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + from.getAbsolutePath());
Statement stmt = conn.createStatement();
stmt.executeUpdate("create table sample(id, name)");
stmt.executeUpdate("insert into sample values(1, \"foo\")");
File to = File.createTempFile("error-message-test-moved-from", ".sqlite");
assumeTrue(to.delete());
assumeTrue(from.renameTo(to));
thrown.expectMessage(JUnitMatchers.containsString("[SQLITE_READONLY_DBMOVED]"));
stmt.executeUpdate("insert into sample values(2, \"bar\")");
stmt.close();
conn.close();
}
@Test
public void writeProtected() throws SQLException, IOException {
File file = File.createTempFile("error-message-test-write-protected", ".sqlite");
file.deleteOnExit();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
Statement stmt = conn.createStatement();
stmt.executeUpdate("create table sample(id, name)");
stmt.executeUpdate("insert into sample values(1, \"foo\")");
stmt.close();
conn.close();
assumeTrue(file.setReadOnly());
conn = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
stmt = conn.createStatement();
thrown.expectMessage(JUnitMatchers.containsString("[SQLITE_READONLY]"));
stmt.executeUpdate("insert into sample values(2, \"bar\")");
stmt.close();
conn.close();
}
@Test
public void shouldUsePlainErrorCodeAsVendorCodeAndExtendedAsResultCode() throws SQLException, IOException {
File from = File.createTempFile("error-message-test-plain-1", ".sqlite");
from.deleteOnExit();
Connection conn = DriverManager.getConnection("jdbc:sqlite:" + from.getAbsolutePath());
Statement stmt = conn.createStatement();
stmt.executeUpdate("create table sample(id, name)");
stmt.executeUpdate("insert into sample values(1, \"foo\")");
File to = File.createTempFile("error-message-test-plain-2", ".sqlite");
assumeTrue(to.delete());
assumeTrue(from.renameTo(to));
thrown.expectMessage(JUnitMatchers.containsString("[SQLITE_READONLY_DBMOVED]"));
thrown.expect(new VendorCodeMatcher(SQLiteErrorCode.SQLITE_READONLY));
thrown.expect(new ResultCodeMatcher(SQLiteErrorCode.SQLITE_READONLY_DBMOVED));
stmt.executeUpdate("insert into sample values(2, \"bar\")");
stmt.close();
conn.close();
}
}