|
15 | 15 | */ |
16 | 16 | package issues.gh324; |
17 | 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 | 18 | import org.junit.jupiter.api.Test; |
28 | 19 |
|
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 | 20 | import static org.assertj.core.api.Assertions.assertThat; |
36 | 21 |
|
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 | | - } |
| 22 | +class Issue324Test { |
| 23 | + private NameService nameService = new NameService(); |
59 | 24 |
|
60 | 25 | @Test |
61 | 26 | 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 | | - }); |
| 27 | + nameService.resetDatabase(); |
| 28 | + |
| 29 | + nameService.insertRecord(); |
| 30 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 31 | + assertThat(ObservableCache.getInstance().getHits()).isZero(); |
| 32 | + |
| 33 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 34 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1); |
| 35 | + |
| 36 | + nameService.updateRecordWithAutoCommit(); |
| 37 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney); |
| 38 | + assertThat(ObservableCache.getInstance().getHits()).isZero(); |
| 39 | + |
| 40 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney); |
| 41 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1); |
75 | 42 | } |
76 | 43 |
|
77 | 44 | @Test |
78 | 45 | 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 | | - }); |
| 46 | + nameService.resetDatabase(); |
| 47 | + |
| 48 | + nameService.insertRecord(); |
| 49 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 50 | + assertThat(ObservableCache.getInstance().getHits()).isZero(); |
| 51 | + |
| 52 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 53 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1); |
85 | 54 |
|
86 | 55 | // 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 | | - }); |
| 56 | + nameService.updateRecordWithoutAutoCommitAndNoExplicitCommit(); |
| 57 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 58 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(2); |
93 | 59 | } |
94 | 60 |
|
95 | 61 | @Test |
96 | 62 | 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 | | - } |
| 63 | + nameService.resetDatabase(); |
111 | 64 |
|
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 | | - } |
| 65 | + nameService.insertRecord(); |
| 66 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 67 | + assertThat(ObservableCache.getInstance().getHits()).isZero(); |
121 | 68 |
|
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 | | - } |
| 69 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred); |
| 70 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1); |
131 | 71 |
|
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 | | - } |
| 72 | + nameService.updateRecordWithoutAutoCommitAndExplicitCommit(); |
| 73 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney); |
| 74 | + assertThat(ObservableCache.getInstance().getHits()).isZero(); |
153 | 75 |
|
154 | | - private Optional<NameRecord> getRecord() { |
155 | | - try (SqlSession session = sqlSessionFactory.openSession()) { |
156 | | - NameTableMapper mapper = session.getMapper(NameTableMapper.class); |
157 | | - return mapper.selectByPrimaryKey(1); |
158 | | - } |
| 76 | + assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney); |
| 77 | + assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1); |
159 | 78 | } |
160 | 79 | } |
0 commit comments