Skip to content

Commit a73d413

Browse files
Added Transaction tests
1 parent 0ca8dc5 commit a73d413

4 files changed

Lines changed: 199 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
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 software.xdev.spring.data.eclipse.store.integration.isolated.tests.transactions;
17+
18+
import java.math.BigDecimal;
19+
20+
import jakarta.persistence.GeneratedValue;
21+
import jakarta.persistence.GenerationType;
22+
import jakarta.persistence.Id;
23+
24+
25+
public class Account
26+
{
27+
@Id
28+
@GeneratedValue(strategy = GenerationType.AUTO)
29+
private int id;
30+
31+
private BigDecimal balance;
32+
33+
public Account(final BigDecimal balance)
34+
{
35+
this.balance = balance;
36+
}
37+
38+
public int getId()
39+
{
40+
return this.id;
41+
}
42+
43+
public void setId(final int id)
44+
{
45+
this.id = id;
46+
}
47+
48+
public BigDecimal getBalance()
49+
{
50+
return this.balance;
51+
}
52+
53+
public void setBalance(final BigDecimal balance)
54+
{
55+
this.balance = balance;
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
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 software.xdev.spring.data.eclipse.store.integration.isolated.tests.transactions;
17+
18+
import org.springframework.data.repository.CrudRepository;
19+
20+
21+
public interface AccountRepository extends CrudRepository<Account, Integer>
22+
{
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
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 software.xdev.spring.data.eclipse.store.integration.isolated.tests.transactions;
17+
18+
import java.math.BigDecimal;
19+
import java.util.List;
20+
21+
import org.junit.jupiter.api.Assertions;
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.test.context.ContextConfiguration;
25+
import org.springframework.transaction.PlatformTransactionManager;
26+
import org.springframework.transaction.support.TransactionTemplate;
27+
28+
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
29+
30+
31+
@IsolatedTestAnnotations
32+
@ContextConfiguration(classes = {TransactionsTestConfiguration.class})
33+
class TransactionsTest
34+
{
35+
private final TransactionsTestConfiguration configuration;
36+
// single TransactionTemplate shared amongst all methods in this instance
37+
private final TransactionTemplate transactionTemplate;
38+
39+
@Autowired
40+
public TransactionsTest(
41+
final TransactionsTestConfiguration configuration,
42+
final PlatformTransactionManager transactionManager)
43+
{
44+
this.configuration = configuration;
45+
this.transactionTemplate = new TransactionTemplate(transactionManager);
46+
}
47+
48+
@Test
49+
void accountTransaction_Working(final AccountRepository repository)
50+
{
51+
final Account account1 = new Account(BigDecimal.TEN);
52+
final Account account2 = new Account(BigDecimal.ZERO);
53+
repository.saveAll(List.of(account1, account2));
54+
55+
this.transactionTemplate.execute(
56+
status ->
57+
{
58+
account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE));
59+
repository.save(account1);
60+
61+
account2.setBalance(account2.getBalance().subtract(BigDecimal.ONE));
62+
repository.save(account2);
63+
return null;
64+
}
65+
);
66+
67+
Assertions.assertEquals(BigDecimal.valueOf(9.0), repository.findById(account1.getId()).get().getBalance());
68+
Assertions.assertEquals(BigDecimal.ONE, repository.findById(account2.getId()).get().getBalance());
69+
}
70+
71+
@Test
72+
void accountTransaction_UnexpectedError(final AccountRepository repository)
73+
{
74+
final Account account1 = new Account(BigDecimal.TEN);
75+
final Account account2 = new Account(BigDecimal.ZERO);
76+
repository.saveAll(List.of(account1, account2));
77+
78+
this.transactionTemplate.execute(
79+
status ->
80+
{
81+
account1.setBalance(account1.getBalance().subtract(BigDecimal.ONE));
82+
repository.save(account1);
83+
84+
throw new RuntimeException("Unexpected error");
85+
}
86+
);
87+
88+
Assertions.assertEquals(BigDecimal.TEN, repository.findById(account1.getId()).get().getBalance());
89+
Assertions.assertEquals(BigDecimal.ZERO, repository.findById(account2.getId()).get().getBalance());
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright © 2024 XDEV Software (https://xdev.software)
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 software.xdev.spring.data.eclipse.store.integration.isolated.tests.transactions;
17+
18+
import org.springframework.context.annotation.Configuration;
19+
20+
import software.xdev.spring.data.eclipse.store.integration.TestConfiguration;
21+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
22+
23+
24+
@Configuration
25+
@EnableEclipseStoreRepositories(clientConfigurationClass = TransactionsTestConfiguration.class)
26+
public class TransactionsTestConfiguration extends TestConfiguration
27+
{
28+
}

0 commit comments

Comments
 (0)