Skip to content

Commit f08fa62

Browse files
More Transaction Tests
1 parent 8410c50 commit f08fa62

2 files changed

Lines changed: 92 additions & 34 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.annotation.Transactional;
26+
27+
import software.xdev.spring.data.eclipse.store.helper.TestUtil;
28+
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
29+
30+
31+
@IsolatedTestAnnotations
32+
@ContextConfiguration(classes = {TransactionsTestConfiguration.class})
33+
@Transactional
34+
class TransactionsAnnotationTest
35+
{
36+
private final TransactionsTestConfiguration configuration;
37+
private final AccountRepository repository;
38+
39+
@Autowired
40+
public TransactionsAnnotationTest(
41+
final TransactionsTestConfiguration configuration,
42+
final AccountRepository repository)
43+
{
44+
this.configuration = configuration;
45+
this.repository = repository;
46+
}
47+
48+
@Test
49+
void accountTransaction_UnexpectedError_Annotation()
50+
{
51+
try
52+
{
53+
final Account account1 = new Account(1, BigDecimal.TEN);
54+
final Account account2 = new Account(2, BigDecimal.ZERO);
55+
this.repository.saveAll(List.of(account1, account2));
56+
57+
throw new RuntimeException("Unexpected error");
58+
}
59+
catch(final RuntimeException e)
60+
{
61+
}
62+
Assertions.assertEquals(0, TestUtil.iterableToList(this.repository.findAll()).size());
63+
}
64+
}

spring-data-eclipse-store/src/test/java/software/xdev/spring/data/eclipse/store/integration/isolated/tests/transactions/TransactionsTest.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.beans.factory.annotation.Autowired;
2525
import org.springframework.test.context.ContextConfiguration;
2626
import org.springframework.transaction.PlatformTransactionManager;
27-
import org.springframework.transaction.annotation.Transactional;
2827
import org.springframework.transaction.support.TransactionTemplate;
2928

3029
import software.xdev.spring.data.eclipse.store.integration.isolated.IsolatedTestAnnotations;
@@ -55,64 +54,59 @@ void initData()
5554
}
5655

5756
@Test
58-
void accountTransaction_Working(final PlatformTransactionManager transactionManager)
57+
void accountTransaction_Working(@Autowired final PlatformTransactionManager transactionManager)
5958
{
6059
new TransactionTemplate(transactionManager).execute(
6160
status ->
6261
{
6362
this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE));
6463
this.repository.save(this.account1);
6564

66-
this.account2.setBalance(this.account2.getBalance().subtract(BigDecimal.ONE));
65+
this.account2.setBalance(this.account2.getBalance().add(BigDecimal.ONE));
6766
this.repository.save(this.account2);
6867
return null;
6968
}
7069
);
7170

7271
Assertions.assertEquals(
73-
BigDecimal.valueOf(9.0),
72+
BigDecimal.valueOf(9),
7473
this.repository.findById(this.account1.getId()).get().getBalance());
7574
Assertions.assertEquals(BigDecimal.ONE, this.repository.findById(this.account2.getId()).get().getBalance());
7675
}
7776

7877
@Test
79-
void accountTransaction_UnexpectedError(final PlatformTransactionManager transactionManager)
78+
void accountTransaction_UnexpectedError(@Autowired final PlatformTransactionManager transactionManager)
8079
{
81-
new TransactionTemplate(transactionManager).execute(
82-
status ->
83-
{
84-
this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE));
85-
this.repository.save(this.account1);
86-
87-
throw new RuntimeException("Unexpected error");
88-
}
89-
);
80+
Assertions.assertThrows(RuntimeException.class, () ->
81+
new TransactionTemplate(transactionManager).execute(
82+
status ->
83+
{
84+
this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE));
85+
this.repository.save(this.account1);
86+
87+
throw new RuntimeException("Unexpected error");
88+
}
89+
));
9090

9191
Assertions.assertEquals(BigDecimal.TEN, this.repository.findById(this.account1.getId()).get().getBalance());
9292
Assertions.assertEquals(BigDecimal.ZERO, this.repository.findById(this.account2.getId()).get().getBalance());
9393
}
9494

95+
/**
96+
* This test should demonstrate the behavior of transactions.
97+
*/
9598
@Test
96-
void accountTransaction_UnexpectedError_Annotation()
99+
void findStoredEntityWithinTransaction(@Autowired final PlatformTransactionManager transactionManager)
97100
{
98-
try
99-
{
100-
this.makeSingleChangeAndThenThrowException();
101-
Assertions.fail("Somehow no exception was raised!");
102-
}
103-
catch(final RuntimeException e)
104-
{
105-
}
106-
Assertions.assertEquals(BigDecimal.TEN, this.repository.findById(this.account1.getId()).get().getBalance());
107-
Assertions.assertEquals(BigDecimal.ZERO, this.repository.findById(this.account2.getId()).get().getBalance());
108-
}
109-
110-
@Transactional
111-
void makeSingleChangeAndThenThrowException()
112-
{
113-
this.account1.setBalance(this.account1.getBalance().subtract(BigDecimal.ONE));
114-
this.repository.save(this.account1);
115-
116-
throw new RuntimeException("Unexpected error");
101+
new TransactionTemplate(transactionManager).execute(
102+
status ->
103+
{
104+
final Account account3 = new Account(3, BigDecimal.valueOf(100.0));
105+
this.repository.save(account3);
106+
107+
Assertions.assertFalse(this.repository.findById(account3.getId()).isPresent());
108+
return null;
109+
}
110+
);
117111
}
118112
}

0 commit comments

Comments
 (0)