Skip to content

Commit 63617ac

Browse files
committed
add test for transaction
1 parent 4131ef3 commit 63617ac

File tree

18 files changed

+311
-85
lines changed

18 files changed

+311
-85
lines changed

transaction-declarative/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/src/main/resources/application.properties
2+
/src/test/resources/application.properties
23
/target
34
/node_modules
45
.gradle

transaction-declarative/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
<artifactId>mysql-connector-java</artifactId>
4343
<scope>runtime</scope>
4444
</dependency>
45+
<dependency>
46+
<groupId>com.h2database</groupId>
47+
<artifactId>h2</artifactId>
48+
<version>1.4.194</version>
49+
</dependency>
4550
</dependencies>
4651

4752
<properties>

transaction-declarative/readme.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
# Installation
1+
# Example use declarative transaction
22

3-
1. create database see db.sql
4-
2. cp src/main/resources/application.properties.dist src/main/resources/application.properties
5-
3. set db config in application.properties
3+
For service layer - for class
4+
5+
`/src/main/java/hello/service/transaction/impl/EmployeeServiceImpl.java`
6+
7+
## Configuration
8+
9+
1. cp src/main/resources/application.properties.dist src/main/resources/application.properties
10+
2. cp src/test/resources/application.properties.dist src/test/resources/application.properties
11+
12+
## Test transaction
13+
14+
### With transaction
15+
16+
`/src/test/java/hello/service/transaction/impl/EmployeeServiceImplTest.java`
17+
18+
### Without transaction
19+
20+
`/src/test/java/hello/service/without_transaction/impl/EmployeeServiceImplTest.java`
Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
package hello;
22

3-
import org.springframework.beans.factory.annotation.Autowired;
43
import org.springframework.boot.SpringApplication;
54
import org.springframework.boot.autoconfigure.SpringBootApplication;
6-
import org.springframework.context.ApplicationContext;
7-
import org.springframework.context.ConfigurableApplicationContext;
85

96
@SpringBootApplication
107
public class Application {
11-
12-
@Autowired
13-
CheckTransaction testJdbcTemplate;
14-
158
public static void main(String[] args) {
16-
ApplicationContext context = SpringApplication.run(Application.class, args);
17-
18-
CheckTransaction checkTransaction = context.getBean(CheckTransaction.class);
19-
20-
// run in transaction - if exception - all rollback
21-
try {
22-
checkTransaction.insertSameEmails();
23-
} catch (Exception e) {
24-
System.out.println(e);
25-
}
26-
27-
checkTransaction.correctCreateNewEmployee();
28-
29-
((ConfigurableApplicationContext) context).close();
9+
SpringApplication.run(Application.class, args);
3010
}
3111
}

transaction-declarative/src/main/java/hello/CheckTransaction.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

transaction-declarative/src/main/java/hello/dao/impl/EmployeeDaoImpl.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,14 @@
2323

2424
@Repository
2525
public class EmployeeDaoImpl extends JdbcDaoSupport implements EmployeeDao {
26-
private final String SQL_SELECT_ALL = "SELECT * FROM employee";
27-
private final String SQL_DELETE_BY_ID = "DELETE FROM employee WHERE id = ?";
28-
private final String SQL_SELECT_BY_ID = "SELECT * FROM employee WHERE id = ?";
26+
private final String SQL_SELECT_ALL = "SELECT * FROM employee";
27+
private final String SQL_DELETE_BY_ID = "DELETE FROM employee WHERE id = ?";
28+
private final String SQL_SELECT_BY_ID = "SELECT * FROM employee WHERE id = ?";
2929
private final String SQL_SELECT_BY_EMAIL = "SELECT * FROM employee WHERE email = ?";
30-
private final String SQL_INSERT_NEW = "INSERT INTO employee (name, email) VALUES (?, ?)";
31-
32-
private final DataSource dataSource;
30+
private final String SQL_INSERT_NEW = "INSERT INTO employee (name, email) VALUES (?, ?)";
3331

3432
@Autowired
35-
public EmployeeDaoImpl(DataSource dataSource) {
36-
this.dataSource = dataSource;
37-
}
33+
DataSource dataSource;
3834

3935
@PostConstruct
4036
private void initialize(){
@@ -81,7 +77,7 @@ public List<Employee> getAll(){
8177
List<Employee> result = new ArrayList<>();
8278
for (Map<String, Object> row : rows) {
8379
Employee emp = new Employee(
84-
toIntExact((Long)row.get("id")),
80+
toIntExact((Integer)row.get("id")),
8581
(String)row.get("name"),
8682
(String)row.get("email")
8783
);

transaction-declarative/src/main/java/hello/service/EmployeeService.java renamed to transaction-declarative/src/main/java/hello/service/transaction/EmployeeService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package hello.service;
1+
package hello.service.transaction;
22

33
import hello.model.Employee;
44

transaction-declarative/src/main/java/hello/service/impl/EmployeeServiceImpl.java renamed to transaction-declarative/src/main/java/hello/service/transaction/impl/EmployeeServiceImpl.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
package hello.service.impl;
1+
package hello.service.transaction.impl;
22

33
import java.util.List;
44

55
import hello.dao.EmployeeDao;
66
import hello.model.Employee;
7-
import hello.service.EmployeeService;
7+
import hello.service.transaction.EmployeeService;
88
import org.springframework.beans.factory.annotation.Autowired;
99
import org.springframework.dao.EmptyResultDataAccessException;
1010
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
1112

12-
@Service
13+
@Service("EmployeeServiceWithTransaction")
14+
@Transactional
1315
public class EmployeeServiceImpl implements EmployeeService {
1416

1517
@Autowired
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hello.service.without_transaction;
2+
3+
import hello.model.Employee;
4+
5+
import java.util.List;
6+
7+
public interface EmployeeService {
8+
void insert(Employee emp);
9+
10+
int insertWithReturnInsertedId(Employee emp);
11+
12+
void insertList(List<Employee> employees);
13+
14+
List<Employee> getAll();
15+
16+
Employee getById(int employeeId);
17+
18+
Employee getByEmail(String email);
19+
20+
void delete(Employee employee);
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package hello.service.without_transaction.impl;
2+
3+
import hello.dao.EmployeeDao;
4+
import hello.model.Employee;
5+
import hello.service.without_transaction.EmployeeService;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.dao.EmptyResultDataAccessException;
8+
import org.springframework.stereotype.Service;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.util.List;
12+
13+
@Service("EmployeeServiceWithoutTransaction")
14+
public class EmployeeServiceImpl implements EmployeeService {
15+
16+
@Autowired
17+
EmployeeDao employeeDao;
18+
19+
@Override
20+
public void insert(Employee employee) {
21+
employeeDao.insert(employee);
22+
}
23+
24+
@Override
25+
public int insertWithReturnInsertedId(Employee employee) {
26+
return employeeDao.insertWithReturnInsertedId(employee);
27+
}
28+
29+
@Override
30+
public void insertList(List<Employee> employees) {
31+
employeeDao.insertList(employees);
32+
}
33+
34+
@Override
35+
public List<Employee> getAll() {
36+
return employeeDao.getAll();
37+
}
38+
39+
@Override
40+
public Employee getById(int employeeId) {
41+
try {
42+
return employeeDao.getById(employeeId);
43+
} catch (EmptyResultDataAccessException e) {
44+
return null;
45+
}
46+
}
47+
48+
@Override
49+
public Employee getByEmail(String email) {
50+
try {
51+
return employeeDao.getByEmail(email);
52+
} catch (EmptyResultDataAccessException e) {
53+
return null;
54+
}
55+
}
56+
57+
@Override
58+
public void delete(Employee employee) {
59+
employeeDao.delete(employee);
60+
}
61+
62+
}

transaction-declarative/src/main/resources/templates/uploadForm.html

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package hello.service.transaction.impl;
2+
3+
import hello.model.Employee;
4+
import hello.service.transaction.EmployeeService;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.dao.DuplicateKeyException;
10+
import org.springframework.test.context.ContextConfiguration;
11+
import org.springframework.test.context.jdbc.Sql;
12+
import org.springframework.test.context.jdbc.SqlGroup;
13+
import org.springframework.test.context.junit4.SpringRunner;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
import static org.hamcrest.CoreMatchers.equalTo;
19+
import static org.hamcrest.Matchers.containsInAnyOrder;
20+
import static org.junit.Assert.assertThat;
21+
22+
@ContextConfiguration(classes = EmployeeServiceImplTestConfig.class)
23+
@RunWith(SpringRunner.class)
24+
@SqlGroup({
25+
@Sql("/db.sql"),
26+
})
27+
public class EmployeeServiceImplTest {
28+
@Autowired
29+
@Qualifier("EmployeeServiceWithTransaction")
30+
private EmployeeService service;
31+
32+
@Test
33+
public void insertList() {
34+
Employee employee1 = new Employee(1, "dd", "[email protected]");
35+
Employee employee2 = new Employee(2, "dd2", "[email protected]");
36+
37+
List<Employee> expectedList = new ArrayList<>();
38+
expectedList.add(employee1);
39+
expectedList.add(employee2);
40+
41+
service.insertList(expectedList);
42+
43+
List<Employee> actualList = service.getAll();
44+
45+
assertThat(actualList, containsInAnyOrder(expectedList.toArray()));
46+
}
47+
48+
@Test
49+
public void insertList_WhenDuplicateByEmail() {
50+
String sameEmail = "[email protected]";
51+
Employee employee1 = new Employee(1, "dd", sameEmail);
52+
Employee employee2 = new Employee(2, "dd2", sameEmail);
53+
54+
List<Employee> expectedList = new ArrayList<>();
55+
expectedList.add(employee1);
56+
expectedList.add(employee2);
57+
58+
// will rollback
59+
try {
60+
service.insertList(expectedList);
61+
} catch (DuplicateKeyException e) {
62+
e.printStackTrace();
63+
}
64+
65+
List<Employee> actualList = service.getAll();
66+
67+
assertThat(actualList, equalTo(new ArrayList<Employee>()));
68+
}
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package hello.service.transaction.impl;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.EnableMBeanExport;
6+
import org.springframework.jmx.support.RegistrationPolicy;
7+
8+
@Configuration
9+
@ComponentScan("hello")
10+
@EnableMBeanExport(registration=RegistrationPolicy.IGNORE_EXISTING)
11+
public class EmployeeServiceImplTestConfig {
12+
13+
}

0 commit comments

Comments
 (0)