View Javadoc
1   /*
2    * Copyright 2010-2023 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 org.mybatis.spring.submitted.xa;
17  
18  import static org.assertj.core.api.Assertions.assertThat;
19  
20  import jakarta.transaction.UserTransaction;
21  
22  import org.junit.jupiter.api.Disabled;
23  import org.junit.jupiter.api.Test;
24  import org.junit.jupiter.api.extension.ExtendWith;
25  import org.springframework.beans.factory.annotation.Autowired;
26  import org.springframework.test.context.junit.jupiter.SpringExtension;
27  import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28  
29  @Disabled("Yet not found OSS implementation that supported Jakarta EE 9+ APIs")
30  @ExtendWith(SpringExtension.class)
31  @SpringJUnitConfig(locations = "classpath:org/mybatis/spring/submitted/xa/applicationContext.xml")
32  class UserServiceTest {
33  
34    @Autowired
35    UserTransaction userTransaction;
36  
37    @Autowired
38    private UserService userService;
39  
40    @Test
41    void testCommit() {
42      User user = new User(1, "Pocoyo");
43      userService.saveWithNoFailure(user);
44      assertThat(userService.checkUserExists(user.getId())).isTrue();
45    }
46  
47    @Test
48    void testRollback() {
49      User user = new User(2, "Pocoyo");
50      try {
51        userService.saveWithFailure(user);
52      } catch (RuntimeException ignore) {
53        // ignored
54      }
55      assertThat(userService.checkUserExists(user.getId())).isFalse();
56    }
57  
58    @Test
59    void testCommitWithExistingTx() throws Exception {
60      userTransaction.begin();
61      User user = new User(3, "Pocoyo");
62      userService.saveWithNoFailure(user);
63      userTransaction.commit();
64      assertThat(userService.checkUserExists(user.getId())).isTrue();
65    }
66  
67    // TODO when the outer JTA tx is rolledback,
68    // SqlSession should be rolledback but it is committed
69    // because Spring calls beforeCommmit from its TX interceptor
70    // then, the JTA TX may be rolledback.
71    @Test
72    void testRollbackWithExistingTx() throws Exception {
73      userTransaction.begin();
74      User user = new User(5, "Pocoyo");
75      userService.saveWithNoFailure(user);
76      userTransaction.rollback();
77      assertThat(userService.checkUserExists(user.getId())).isFalse();
78    }
79  
80  }