19
19
import static org .junit .Assert .*;
20
20
import static org .springframework .data .jpa .support .EntityManagerTestUtils .*;
21
21
22
- import java .util .Iterator ;
23
22
import java .util .List ;
24
- import java .util .Map ;
25
23
26
- import javax .persistence .*;
24
+ import javax .persistence .EntityManager ;
25
+ import javax .persistence .Persistence ;
26
+ import javax .persistence .PersistenceUtil ;
27
27
28
28
import org .junit .Assume ;
29
29
import org .junit .Before ;
39
39
import org .springframework .test .context .ContextConfiguration ;
40
40
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
41
41
import org .springframework .transaction .annotation .Transactional ;
42
- import org .springframework .util .StringUtils ;
43
42
44
43
/**
45
44
* Integration tests for RepositoryMethodsWithEntityGraphConfigJpaRepository.
@@ -62,6 +61,8 @@ public class EntityGraphRepositoryMethodsIntegrationTests {
62
61
User christoph ;
63
62
Role role ;
64
63
64
+ PersistenceUtil util = Persistence .getPersistenceUtil ();
65
+
65
66
@ Before
66
67
public void setup () {
67
68
@@ -91,7 +92,7 @@ public void shouldRespectConfiguredJpaEntityGraph() {
91
92
List <User > result = repository .findAll ();
92
93
93
94
assertThat (result .size (), is (3 ));
94
- assertThat (Persistence . getPersistenceUtil () .isLoaded (result .get (0 ), "roles" ), is (true ));
95
+ assertThat (util .isLoaded (result .get (0 ), "roles" ), is (true ));
95
96
assertThat (result .get (0 ), is (tom ));
96
97
}
97
98
@@ -103,8 +104,8 @@ public void shouldRespectConfiguredJpaEntityGraphInFindOne() {
103
104
User user = repository .findOne (tom .getId ());
104
105
105
106
assertThat (user , is (notNullValue ()));
106
- assertThat ("colleages should be fetched with 'user.detail' fetchgraph" ,
107
- Persistence . getPersistenceUtil (). isLoaded ( user , "colleagues" ), is (true ));
107
+ assertThat ("colleages should be fetched with 'user.detail' fetchgraph" , util . isLoaded ( user , "colleagues" ),
108
+ is (true ));
108
109
}
109
110
110
111
@ Test // DATAJPA-696
@@ -115,8 +116,8 @@ public void shouldRespectInferFetchGraphFromMethodName() {
115
116
User user = repository .getOneWithDefinedEntityGraphById (tom .getId ());
116
117
117
118
assertThat (user , is (notNullValue ()));
118
- assertThat ("colleages should be fetched with 'user.detail' fetchgraph" ,
119
- Persistence . getPersistenceUtil (). isLoaded ( user , "colleagues" ), is (true ));
119
+ assertThat ("colleages should be fetched with 'user.detail' fetchgraph" , util . isLoaded ( user , "colleagues" ),
120
+ is (true ));
120
121
}
121
122
122
123
@ Test // DATAJPA-696
@@ -130,12 +131,13 @@ public void shouldRespectDynamicFetchGraphForGetOneWithAttributeNamesById() {
130
131
User user = repository .getOneWithAttributeNamesById (tom .getId ());
131
132
132
133
assertThat (user , is (notNullValue ()));
133
- assertThat ("colleages should be fetched with 'user.detail' fetchgraph" ,
134
- Persistence .getPersistenceUtil ().isLoaded (user , "colleagues" ), is (true ));
135
- assertThat (Persistence .getPersistenceUtil ().isLoaded (user , "colleagues" ), is (true ));
134
+
135
+ assertThat ("colleages should be fetched with 'user.detail' fetchgraph" , util .isLoaded (user , "colleagues" ),
136
+ is (true ));
137
+ assertThat (util .isLoaded (user , "colleagues" ), is (true ));
136
138
137
139
for (User colleague : user .getColleagues ()) {
138
- assertThat (Persistence . getPersistenceUtil () .isLoaded (colleague , "roles" ), is (true ));
140
+ assertThat (util .isLoaded (colleague , "roles" ), is (true ));
139
141
}
140
142
}
141
143
@@ -148,7 +150,7 @@ public void shouldRespectConfiguredJpaEntityGraphWithPaginationAndQueryDslPredic
148
150
List <User > result = page .getContent ();
149
151
150
152
assertThat (result .size (), is (3 ));
151
- assertThat (Persistence . getPersistenceUtil () .isLoaded (result .get (0 ).getRoles ()), is (true ));
153
+ assertThat (util .isLoaded (result .get (0 ).getRoles ()), is (true ));
152
154
assertThat (result .get (0 ), is (tom ));
153
155
}
154
156
@@ -165,11 +167,11 @@ public void shouldRespectNamedEntitySubGraph() {
165
167
assertThat (user , is (notNullValue ()));
166
168
167
169
assertThat ("colleagues on root should have been fetched by named 'User.colleagues' subgraph declaration" ,
168
- Persistence . getPersistenceUtil () .isLoaded (user , "colleagues" ), is (true ));
170
+ util .isLoaded (user , "colleagues" ), is (true ));
169
171
170
172
for (User colleague : user .getColleagues ()) {
171
- assertThat (Persistence . getPersistenceUtil () .isLoaded (colleague , "colleagues" ), is (true ));
172
- assertThat (Persistence . getPersistenceUtil () .isLoaded (colleague , "roles" ), is (true ));
173
+ assertThat (util .isLoaded (colleague , "colleagues" ), is (true ));
174
+ assertThat (util .isLoaded (colleague , "roles" ), is (true ));
173
175
}
174
176
}
175
177
@@ -186,55 +188,11 @@ public void shouldRespectMultipleSubGraphForSameAttributeWithDynamicFetchGraph()
186
188
assertThat (user , is (notNullValue ()));
187
189
188
190
assertThat ("colleagues on root should have been fetched by dynamic subgraph declaration" ,
189
- Persistence . getPersistenceUtil () .isLoaded (user , "colleagues" ), is (true ));
191
+ util .isLoaded (user , "colleagues" ), is (true ));
190
192
191
193
for (User colleague : user .getColleagues ()) {
192
- assertThat (Persistence .getPersistenceUtil ().isLoaded (colleague , "colleagues" ), is (true ));
193
- assertThat (Persistence .getPersistenceUtil ().isLoaded (colleague , "roles" ), is (true ));
194
- }
195
- }
196
-
197
- @ Test // DATAJPA-1041 - TODO: remove when done fighting with eclipselink.
198
- public void thisOneFailsWithEclipselink () {
199
-
200
- Assume .assumeTrue (currentEntityManagerIsAJpa21EntityManager (em ));
201
-
202
- em .flush ();
203
- em .clear ();
204
-
205
- javax .persistence .EntityGraph <?> graph = em .getEntityGraph ("User.withSubGraph" );
206
-
207
- printGraph (graph );
208
-
209
- User result = (User ) em .createQuery ("Select u from User u where u.id = " + tom .getId ())
210
- .setHint ("javax.persistence.loadgraph" , graph ).getResultList ().get (0 );
211
-
212
- assertThat (Persistence .getPersistenceUtil ().isLoaded (result , "roles" ), is (true ));
213
- assertThat (Persistence .getPersistenceUtil ().isLoaded (result , "colleagues" ), is (true ));
214
- }
215
-
216
- private void printGraph (javax .persistence .EntityGraph <?> graph ) {
217
-
218
- try {
219
- for (AttributeNode <?> node : graph .getAttributeNodes ()) {
220
- System .out .println ("|- node.getAttributeName(): " + node .getAttributeName ());
221
- for (Map .Entry <Class , Subgraph > subGraph : node .getSubgraphs ().entrySet ()) {
222
- System .out .print ("| +- subGraph: " + subGraph .getKey ().getSimpleName () + " -> [" );
223
-
224
- Iterator it = subGraph .getValue ().getAttributeNodes ().iterator ();
225
- while (it .hasNext ()) {
226
-
227
- AttributeNode <?> an = (AttributeNode <?>) it .next ();
228
- System .out .print (an .getAttributeName ());
229
- if (it .hasNext ()) {
230
- System .out .print (", " );
231
- }
232
- }
233
- System .out .println ("]" );
234
- }
235
- }
236
- } catch (Exception e ) {
237
- // o_O what happened here - ignore it - it's just debug output.
194
+ assertThat (util .isLoaded (colleague , "colleagues" ), is (true ));
195
+ assertThat (util .isLoaded (colleague , "roles" ), is (true ));
238
196
}
239
197
}
240
198
}
0 commit comments