View Javadoc
1   /*
2    *    Copyright 2016-2025 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    *       https://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 org.assertj.core.api.Assertions.assertThat;
19  
20  import org.junit.jupiter.api.Test;
21  
22  class Issue324Test {
23      private final NameService nameService = new NameService();
24  
25      @Test
26      void testCacheWithAutoCommitOnUpdate() {
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);
42      }
43  
44      @Test
45      void testCacheWithNoAutoCommitOnUpdateAndNoExplicitCommit() {
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);
54  
55          // the update should rollback
56          nameService.updateRecordWithoutAutoCommitAndNoExplicitCommit();
57          assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred);
58          assertThat(ObservableCache.getInstance().getHits()).isEqualTo(2);
59      }
60  
61      @Test
62      void testCacheWithNoAutoCommitOnUpdateAndExplicitCommit() {
63          nameService.resetDatabase();
64  
65          nameService.insertRecord();
66          assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred);
67          assertThat(ObservableCache.getInstance().getHits()).isZero();
68  
69          assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsFred);
70          assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1);
71  
72          nameService.updateRecordWithoutAutoCommitAndExplicitCommit();
73          assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney);
74          assertThat(ObservableCache.getInstance().getHits()).isZero();
75  
76          assertThat(nameService.getRecord()).hasValueSatisfying(TestUtils::recordIsBarney);
77          assertThat(ObservableCache.getInstance().getHits()).isEqualTo(1);
78      }
79  }