Skip to content

Commit 9045e93

Browse files
committed
Add test cases for DomaPersistenceExceptionTranslator
1 parent d56e3bf commit 9045e93

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
* Copyright (C) 2004-2016 the Seasar Foundation and the Others.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13+
* either express or implied. See the License for the specific language
14+
* governing permissions and limitations under the License.
15+
*/
16+
package org.seasar.doma.boot.autoconfigure;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.assertThat;
20+
21+
import java.sql.SQLException;
22+
import java.util.Collections;
23+
24+
import org.junit.Test;
25+
import org.seasar.doma.DomaException;
26+
import org.seasar.doma.jdbc.*;
27+
import org.seasar.doma.message.Message;
28+
import org.springframework.dao.*;
29+
import org.springframework.jdbc.UncategorizedSQLException;
30+
import org.springframework.jdbc.support.SQLExceptionSubclassTranslator;
31+
32+
public class DomaPersistenceExceptionTranslatorTest {
33+
34+
private final DomaPersistenceExceptionTranslator translator = new DomaPersistenceExceptionTranslator(
35+
new SQLExceptionSubclassTranslator());
36+
37+
@Test
38+
public void testOccurNotJdbcException() {
39+
DataAccessException dataAccessException = translator
40+
.translateExceptionIfPossible(new DomaException(Message.DOMA2008));
41+
assertThat(dataAccessException, nullValue());
42+
}
43+
44+
@Test
45+
public void testOccurSqlExecutionException() {
46+
DataAccessException dataAccessException = translator.translateExceptionIfPossible(
47+
new SqlExecutionException(SqlLogType.FORMATTED, SqlKind.SELECT,
48+
"select * from todo where todo_id = ?",
49+
"select * from todo where todo_id = '000000001'",
50+
"TodoDao/findOne.sql", new SQLException(), null));
51+
assertThat(dataAccessException, is(instanceOf(UncategorizedSQLException.class)));
52+
assertThat(UncategorizedSQLException.class.cast(dataAccessException).getSql(),
53+
is("select * from todo where todo_id = ?"));
54+
}
55+
56+
@Test
57+
public void testOccurUniqueConstraintException() {
58+
DataAccessException dataAccessException = translator.translateExceptionIfPossible(
59+
new UniqueConstraintException(SqlLogType.FORMATTED, SqlKind.INSERT,
60+
"insert into todo (todo_id, title) values (?, ?)",
61+
"insert into todo (todo_id, title) values ('000000001', 'Title')",
62+
"TodoDao/insert.sql", new SQLException()));
63+
assertThat(dataAccessException, is(instanceOf(UncategorizedSQLException.class)));
64+
assertThat(UncategorizedSQLException.class.cast(dataAccessException).getSql(),
65+
is("insert into todo (todo_id, title) values (?, ?)"));
66+
}
67+
68+
@Test
69+
public void testThrowOptimisticLockingFailureException() {
70+
DataAccessException dataAccessException = translator.translateExceptionIfPossible(
71+
new OptimisticLockException(SqlLogType.FORMATTED, SqlKind.SELECT,
72+
"update todo set title = ? where todo_id = ? and version = ?",
73+
"update todo set title = 'Modified Title' where todo_id = '000000001' and version = 1",
74+
"TodoDao/update.sql"));
75+
assertThat(dataAccessException,
76+
is(instanceOf(OptimisticLockingFailureException.class)));
77+
}
78+
79+
@Test
80+
public void testThrowDuplicateKeyException() {
81+
DataAccessException dataAccessException = translator.translateExceptionIfPossible(
82+
new UniqueConstraintException(SqlLogType.FORMATTED, SqlKind.INSERT,
83+
"insert into todo (todo_id, title) values (?, ?)",
84+
"insert into todo (todo_id, title) values ('000000001', 'Title')",
85+
"TodoDao/insert.sql", null));
86+
assertThat(dataAccessException, is(instanceOf(DuplicateKeyException.class)));
87+
}
88+
89+
@Test
90+
public void testThrowIncorrectResultSizeDataAccessException() {
91+
{
92+
DataAccessException dataAccessException = translator
93+
.translateExceptionIfPossible(new NonUniqueResultException(
94+
SqlLogType.FORMATTED, SqlKind.SELECT,
95+
"select * from todo where created_at = ?",
96+
"select * from todo where created_at = '2016-03-06'",
97+
"TodoDao/findBy.sql"));
98+
assertThat(dataAccessException,
99+
is(instanceOf(IncorrectResultSizeDataAccessException.class)));
100+
}
101+
{
102+
DataAccessException dataAccessException = translator
103+
.translateExceptionIfPossible(new NonSingleColumnException(
104+
SqlLogType.FORMATTED, SqlKind.SELECT,
105+
"select todo_id, title from todo where created_at = ?",
106+
"select todo_id, title from todo where created_at = '2016-03-06'",
107+
"TodoDao/findBy.sql"));
108+
assertThat(dataAccessException,
109+
is(instanceOf(IncorrectResultSizeDataAccessException.class)));
110+
}
111+
}
112+
113+
@Test
114+
public void testThrowEmptyResultDataAccessException() {
115+
DataAccessException dataAccessException = translator
116+
.translateExceptionIfPossible(new NoResultException(SqlLogType.FORMATTED,
117+
SqlKind.SELECT, "select * from todo where todo_id = ?",
118+
"select * from todo where todo_id = '000000001'",
119+
"TodoDao/findOne.sql"));
120+
assertThat(dataAccessException,
121+
is(instanceOf(EmptyResultDataAccessException.class)));
122+
}
123+
124+
@Test
125+
public void testThrowTypeMismatchDataAccessException() {
126+
{
127+
DataAccessException dataAccessException = translator
128+
.translateExceptionIfPossible(new UnknownColumnException(
129+
SqlLogType.FORMATTED, "todo_id", "todoId", "model.Todo",
130+
SqlKind.SELECT, "select * from todo where created_at = ?",
131+
"select * from todo where created_at = '2016-03-06'",
132+
"TodoDao/findBy.sql"));
133+
assertThat(dataAccessException,
134+
is(instanceOf(TypeMismatchDataAccessException.class)));
135+
}
136+
{
137+
DataAccessException dataAccessException = translator
138+
.translateExceptionIfPossible(new ResultMappingException(
139+
SqlLogType.FORMATTED, "Todo",
140+
Collections.singletonList("finished"),
141+
Collections.singletonList("modified_at"), SqlKind.SELECT,
142+
"select todo_id, title from todo where created_at = ?",
143+
"select todo_id, title from todo where created_at = '2016-03-06'",
144+
"TodoDao/findBy.sql"));
145+
assertThat(dataAccessException,
146+
is(instanceOf(TypeMismatchDataAccessException.class)));
147+
}
148+
}
149+
150+
@Test
151+
public void testThrowUncategorizedDataAccessException() {
152+
DataAccessException dataAccessException = translator.translateExceptionIfPossible(
153+
new ConfigException("DomaConfig", "configure"));
154+
assertThat(dataAccessException,
155+
is(instanceOf(UncategorizedDataAccessException.class)));
156+
}
157+
158+
}

0 commit comments

Comments
 (0)