Skip to content

Commit 206091c

Browse files
committed
Tests for Issue #324
1 parent 76ae621 commit 206091c

File tree

6 files changed

+429
-0
lines changed

6 files changed

+429
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issues.gh324;
17+
18+
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
19+
import org.apache.ibatis.jdbc.ScriptRunner;
20+
import org.apache.ibatis.mapping.Environment;
21+
import org.apache.ibatis.session.Configuration;
22+
import org.apache.ibatis.session.SqlSession;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25+
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
26+
import org.junit.jupiter.api.BeforeEach;
27+
import org.junit.jupiter.api.Test;
28+
29+
import java.io.InputStream;
30+
import java.io.InputStreamReader;
31+
import java.sql.Connection;
32+
import java.sql.DriverManager;
33+
import java.util.Optional;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
public class Issue324Test {
38+
private static final String JDBC_URL = "jdbc:hsqldb:mem:aname";
39+
private static final String JDBC_DRIVER = "org.hsqldb.jdbcDriver";
40+
41+
private SqlSessionFactory sqlSessionFactory;
42+
43+
@BeforeEach
44+
void setup() throws Exception {
45+
Class.forName(JDBC_DRIVER);
46+
InputStream is = getClass().getResourceAsStream("/issues/gh324/CreateDB.sql");
47+
try (Connection connection = DriverManager.getConnection(JDBC_URL, "sa", "")) {
48+
ScriptRunner sr = new ScriptRunner(connection);
49+
sr.setLogWriter(null);
50+
sr.runScript(new InputStreamReader(is));
51+
}
52+
53+
UnpooledDataSource ds = new UnpooledDataSource(JDBC_DRIVER, JDBC_URL, "sa", "");
54+
Environment environment = new Environment("test", new JdbcTransactionFactory(), ds);
55+
Configuration config = new Configuration(environment);
56+
config.addMapper(NameTableMapper.class);
57+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(config);
58+
}
59+
60+
@Test
61+
void testCacheWithAutoCommitOnUpdate() {
62+
insertRecord();
63+
Optional<NameRecord> returnedRecord = getRecord();
64+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
65+
assertThat(rr.getId()).isEqualTo(1);
66+
assertThat(rr.getName()).isEqualTo("Fred");
67+
});
68+
69+
updateRecordWithAutoCommit();
70+
returnedRecord = getRecord();
71+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
72+
assertThat(rr.getId()).isEqualTo(1);
73+
assertThat(rr.getName()).isEqualTo("Barney");
74+
});
75+
}
76+
77+
@Test
78+
void testCacheWithNoAutoCommitOnUpdateAndNoExplicitCommit() {
79+
insertRecord();
80+
Optional<NameRecord> returnedRecord = getRecord();
81+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
82+
assertThat(rr.getId()).isEqualTo(1);
83+
assertThat(rr.getName()).isEqualTo("Fred");
84+
});
85+
86+
// the update should rollback
87+
updateRecordWithoutAutoCommitAndNoExplicitCommit();
88+
returnedRecord = getRecord();
89+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
90+
assertThat(rr.getId()).isEqualTo(1);
91+
assertThat(rr.getName()).isEqualTo("Fred");
92+
});
93+
}
94+
95+
@Test
96+
void testCacheWithNoAutoCommitOnUpdateAndExplicitCommit() {
97+
insertRecord();
98+
Optional<NameRecord> returnedRecord = getRecord();
99+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
100+
assertThat(rr.getId()).isEqualTo(1);
101+
assertThat(rr.getName()).isEqualTo("Fred");
102+
});
103+
104+
updateRecordWithoutAutoCommitAndExplicitCommit();
105+
returnedRecord = getRecord();
106+
assertThat(returnedRecord).hasValueSatisfying(rr -> {
107+
assertThat(rr.getId()).isEqualTo(1);
108+
assertThat(rr.getName()).isEqualTo("Barney");
109+
});
110+
}
111+
112+
private void insertRecord() {
113+
try (SqlSession session = sqlSessionFactory.openSession(true)) {
114+
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
115+
NameRecord record = new NameRecord();
116+
record.setId(1);
117+
record.setName("Fred");
118+
mapper.insert(record);
119+
}
120+
}
121+
122+
private void updateRecordWithAutoCommit() {
123+
try (SqlSession session = sqlSessionFactory.openSession(true)) {
124+
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
125+
NameRecord record = new NameRecord();
126+
record.setId(1);
127+
record.setName("Barney");
128+
mapper.updateByPrimaryKey(record);
129+
}
130+
}
131+
132+
private void updateRecordWithoutAutoCommitAndNoExplicitCommit() {
133+
// this should rollback
134+
try (SqlSession session = sqlSessionFactory.openSession()) {
135+
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
136+
NameRecord record = new NameRecord();
137+
record.setId(1);
138+
record.setName("Barney");
139+
mapper.updateByPrimaryKey(record);
140+
}
141+
}
142+
143+
private void updateRecordWithoutAutoCommitAndExplicitCommit() {
144+
try (SqlSession session = sqlSessionFactory.openSession()) {
145+
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
146+
NameRecord record = new NameRecord();
147+
record.setId(1);
148+
record.setName("Barney");
149+
mapper.updateByPrimaryKey(record);
150+
session.commit();
151+
}
152+
}
153+
154+
private Optional<NameRecord> getRecord() {
155+
try (SqlSession session = sqlSessionFactory.openSession()) {
156+
NameTableMapper mapper = session.getMapper(NameTableMapper.class);
157+
return mapper.selectByPrimaryKey(1);
158+
}
159+
}
160+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issues.gh324;
17+
18+
import java.io.Serializable;
19+
20+
public class NameRecord implements Serializable {
21+
private Integer id;
22+
private String name;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issues.gh324;
17+
18+
import org.mybatis.dynamic.sql.SqlColumn;
19+
import org.mybatis.dynamic.sql.SqlTable;
20+
21+
import java.sql.JDBCType;
22+
23+
public class NameTableDynamicSqlSupport {
24+
public static final NameTable nameTable = new NameTable();
25+
public static final SqlColumn<Integer> id = nameTable.id;
26+
public static final SqlColumn<String> name = nameTable.name;
27+
28+
public static final class NameTable extends SqlTable {
29+
public NameTable() {
30+
super("NameTable");
31+
}
32+
public final SqlColumn<Integer> id = column("id", JDBCType.INTEGER);
33+
public final SqlColumn<String> name = column("name", JDBCType.VARCHAR);
34+
}
35+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2016-2021 the original author or authors.
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, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package issues.gh324;
17+
18+
import static issues.gh324.NameTableDynamicSqlSupport.*;
19+
import static org.mybatis.dynamic.sql.SqlBuilder.isEqualTo;
20+
21+
import org.apache.ibatis.annotations.*;
22+
import org.mybatis.dynamic.sql.BasicColumn;
23+
import org.mybatis.dynamic.sql.select.SelectDSLCompleter;
24+
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
25+
import org.mybatis.dynamic.sql.update.UpdateDSLCompleter;
26+
import org.mybatis.dynamic.sql.util.SqlProviderAdapter;
27+
import org.mybatis.dynamic.sql.util.mybatis3.*;
28+
29+
import java.util.List;
30+
import java.util.Optional;
31+
32+
@CacheNamespace
33+
public interface NameTableMapper extends CommonCountMapper, CommonDeleteMapper, CommonInsertMapper<NameRecord>, CommonUpdateMapper {
34+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
35+
@Results(id="NameTableResult", value={
36+
@Result(column="id", property="id", id=true),
37+
@Result(column="name", property="name")
38+
})
39+
List<NameRecord> selectMany(SelectStatementProvider selectStatement);
40+
41+
@SelectProvider(type=SqlProviderAdapter.class, method="select")
42+
@ResultMap("NameTableResult")
43+
Optional<NameRecord> selectOne(SelectStatementProvider selectStatement);
44+
45+
BasicColumn[] selectList = BasicColumn.columnList(id, name);
46+
47+
default Optional<NameRecord> selectOne(SelectDSLCompleter completer) {
48+
return MyBatis3Utils.selectOne(this::selectOne, selectList, nameTable, completer);
49+
}
50+
51+
default Optional<NameRecord> selectByPrimaryKey(Integer id_) {
52+
return selectOne(c ->
53+
c.where(id, isEqualTo(id_))
54+
);
55+
}
56+
57+
default int insert(NameRecord record) {
58+
return MyBatis3Utils.insert(this::insert, record, nameTable, c ->
59+
c.map(id).toProperty("id")
60+
.map(name).toProperty("name")
61+
);
62+
}
63+
64+
default int update(UpdateDSLCompleter completer) {
65+
return MyBatis3Utils.update(this::update, nameTable, completer);
66+
}
67+
68+
default int updateByPrimaryKey(NameRecord record) {
69+
return update(c ->
70+
c.set(name).equalTo(record::getName)
71+
.where(id, isEqualTo(record::getId))
72+
);
73+
}
74+
}

0 commit comments

Comments
 (0)