/
SpringDataJPASaveTest.java
123 lines (102 loc) · 4.02 KB
/
SpringDataJPASaveTest.java
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
package com.vladmihalcea.spring.repository;
import com.vladmihalcea.spring.repository.config.SpringDataJPASaveConfiguration;
import com.vladmihalcea.spring.repository.domain.Post;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
import static org.junit.Assert.fail;
/**
* @author Vlad Mihalcea
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringDataJPASaveConfiguration.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class SpringDataJPASaveTest {
protected final Logger LOGGER = LoggerFactory.getLogger(getClass());
@Autowired
private TransactionTemplate transactionTemplate;
@Autowired
private PostRepository postRepository;
@PersistenceContext
private EntityManager entityManager;
@Test
public void testPersistAndMerge() {
String slug = "high-performance-java-persistence";
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
postRepository.persist(
new Post()
.setId(1L)
.setTitle("High-Performance Java Persistence")
.setSlug("high-performance-java-persistence")
);
postRepository.persistAndFlush(
new Post()
.setId(2L)
.setTitle("Hypersistence Optimizer")
.setSlug("hypersistence-optimizer")
);
postRepository.peristAllAndFlush(
LongStream.range(3, 1000)
.mapToObj(i -> new Post()
.setId(i)
.setTitle(String.format("Post %d", i))
.setSlug(String.format("post-%d", i))
)
.collect(Collectors.toList())
);
return null;
});
List<Post> posts = transactionTemplate.execute(transactionStatus ->
entityManager.createQuery(
"select p " +
"from Post p " +
"where p.id < 10", Post.class)
.getResultList()
);
posts.forEach(post -> post.setTitle(post.getTitle() + " rocks!"));
transactionTemplate.execute(transactionStatus ->
postRepository.updateAll(posts)
);
}
@Test
public void testSave() {
try {
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
postRepository.save(
new Post()
.setId(1L)
.setTitle("High-Performance Java Persistence")
.setSlug("high-performance-java-persistence")
);
return null;
});
fail("Should throw UnsupportedOperationException!");
} catch (UnsupportedOperationException expected) {
LOGGER.warn("You shouldn't call the JpaRepository save method!");
}
}
@Test
public void testFindAll() {
try {
transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> {
postRepository.findAll();
return null;
});
fail("Should throw UnsupportedOperationException!");
} catch (UnsupportedOperationException expected) {
LOGGER.warn("You shouldn't call the JpaRepository findAll method!");
}
}
}