-
-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathEbeanSpringModuleTest.java
More file actions
156 lines (132 loc) · 3.55 KB
/
EbeanSpringModuleTest.java
File metadata and controls
156 lines (132 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.example;
import io.ebean.DB;
import io.ebean.Transaction;
import io.ebean.test.PersistenceContextAccess;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.in;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for Ebean Spring Module.
*
* @author E Mc Greal
* @since 18.05.2009
*/
@ExtendWith(SpringExtension.class)
@ContextConfiguration(locations = {"/init-database.xml"})
public class EbeanSpringModuleTest {
/**
* The Constant logger.
*/
private final static Logger logger = LoggerFactory.getLogger(EbeanSpringModuleTest.class);
/**
* The user service.
*/
@Autowired
private UserService userService;
/**
* Create the test case.
*/
public EbeanSpringModuleTest() {
super();
}
@Transactional
@Rollback
@Test
public void testWithRollback() {
// setup
User user = new User();
user.setName("rollback1");
DB.save(user);
// this loads the user into the [transaction scoped] persistence context
User found = DB.find(User.class, user.getOid());
found.setName("mutated");
PersistenceContextAccess.clear();
userService.insideTestRollback(user.getOid());
}
/**
* Test app.
*/
@Test
public void testInactiveTransaction() {
Transaction transaction = DB.beginTransaction();
long id;
try {
User user = new User();
user.setName("save with txn 1");
DB.save(user);
transaction.commit();
id = user.getOid();
} finally {
// don't end the transaction ...
//transaction.end();
}
DB.delete(User.class, id);
}
@Test
public void testBatchInsert() {
userService.batchInsert();
}
@Test
public void testRequiresNew() {
userService.requiresNew();
}
/**
* Test app.
*/
@Test
public void testSaveUser() {
logger.info("Saving new User...");
User user = new User();
user.setName("ebean");
userService.save(user);
logger.info("Saved new User");
assertThat(user.getOid()).isGreaterThan(0);
final User found = DB.find(User.class, user.getOid());
assertThat(found).isNotNull();
}
/**
* Test app.
*/
@Test
public void testFindUser() {
logger.info("Finding User with OID = 1 ...");
User user = userService.find(1);
assertNotNull(user);
assertTrue("ebean".equals(user.getName()));
logger.info("Found User with OID = 1");
}
@Test
public void testNonTransactional() {
userService.nonTransactional();
logger.info("nonTransactional done");
}
@Test
public void testFindNonTransactional() {
logger.info("Finding User with OID = 1 ...");
User user = userService.find(1);
User user2 = userService.findNoCurrentTransaction(2);
logger.info("nonTransactional find user1:{} user2:{}", user, user2);
}
/**
* Return the user service.
*/
public UserService getUserService() {
return userService;
}
/**
* Sets the user service.
*/
public void setUserService(UserService userService) {
this.userService = userService;
}
}