1
|
/**
|
2
|
* Copyright (C) 2015 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.persistence.dao.initializer;
|
10
|
|
11
|
import static org.junit.Assert.assertEquals;
|
12
|
import static org.junit.Assert.assertFalse;
|
13
|
import static org.junit.Assert.assertTrue;
|
14
|
|
15
|
import java.io.FileNotFoundException;
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.Arrays;
|
18
|
import java.util.HashMap;
|
19
|
import java.util.HashSet;
|
20
|
import java.util.List;
|
21
|
import java.util.Map;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import org.apache.log4j.Logger;
|
26
|
import org.hibernate.FlushMode;
|
27
|
import org.hibernate.Hibernate;
|
28
|
import org.hibernate.HibernateException;
|
29
|
import org.hibernate.SessionFactory;
|
30
|
import org.junit.After;
|
31
|
import org.junit.Before;
|
32
|
import org.junit.Ignore;
|
33
|
import org.junit.Test;
|
34
|
import org.unitils.dbunit.annotation.DataSet;
|
35
|
import org.unitils.spring.annotation.SpringBeanByType;
|
36
|
|
37
|
import eu.etaxonomy.cdm.common.URI;
|
38
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
39
|
import eu.etaxonomy.cdm.model.agent.Address;
|
40
|
import eu.etaxonomy.cdm.model.agent.Contact;
|
41
|
import eu.etaxonomy.cdm.model.agent.Person;
|
42
|
import eu.etaxonomy.cdm.model.agent.Team;
|
43
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
44
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
45
|
import eu.etaxonomy.cdm.model.location.Country;
|
46
|
import eu.etaxonomy.cdm.model.location.Point;
|
47
|
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
|
48
|
import eu.etaxonomy.cdm.model.name.Rank;
|
49
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
50
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
51
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
52
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
53
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
54
|
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
|
55
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
56
|
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
|
57
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
|
58
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
59
|
|
60
|
/**
|
61
|
* @author a.mueller
|
62
|
* @since 16.11.2015
|
63
|
*/
|
64
|
public class AdvancedBeanInitializerTest<CDM extends CdmBase> extends CdmTransactionalIntegrationTest {
|
65
|
|
66
|
private static final Logger logger = Logger.getLogger(AdvancedBeanInitializerTest.class);
|
67
|
|
68
|
private static final UUID personUuid = UUID.fromString("d0568bb1-4dc8-40dc-a405-d0b9e714a7a9");
|
69
|
|
70
|
private static final UUID teamUuid = UUID.fromString("f2ab0cab-f8a4-4db0-9f2d-2f0a1b627597");
|
71
|
|
72
|
private static final UUID referenceUuid = UUID.fromString("f48196c6-854a-416e-8f2a-67bd39e988dc");
|
73
|
|
74
|
private static final UUID nameUuid = UUID.fromString("98cbb643-d521-4ca7-86f7-8180bea85d9f");
|
75
|
|
76
|
private static final UUID taxonUuid = UUID.fromString("07171a4c-f9f0-4459-a7e4-9f75981f7027");
|
77
|
|
78
|
@SpringBeanByType
|
79
|
private IAgentDao agentDao;
|
80
|
|
81
|
@SpringBeanByType
|
82
|
private IReferenceDao referenceDao;
|
83
|
|
84
|
@SpringBeanByType
|
85
|
private ITaxonNameDao nameDao;
|
86
|
|
87
|
@SpringBeanByType
|
88
|
private ITaxonDao taxonDao;
|
89
|
|
90
|
@SpringBeanByType
|
91
|
private AdvancedBeanInitializer<CDM> initializer;
|
92
|
|
93
|
@SpringBeanByType
|
94
|
private AdvancedBeanInitializer<CDM> defaultBeanInitializer;
|
95
|
|
96
|
@SpringBeanByType
|
97
|
private SessionFactory factory;
|
98
|
|
99
|
private Map<Class<CDM>, AutoPropertyInitializer<CDM>> deacivatedAutoIntitializers;
|
100
|
|
101
|
/**
|
102
|
* Checks that the AdvancedBeanInitializer is available and that the expected set of beanAutoInitializers is configured
|
103
|
* in the persitence.xml.
|
104
|
*/
|
105
|
@Before
|
106
|
public void assertAutoinitializers(){
|
107
|
|
108
|
assert defaultBeanInitializer != null;
|
109
|
|
110
|
Class<?>[] expectedAutoInitializers = new Class[]{
|
111
|
eu.etaxonomy.cdm.persistence.dao.initializer.TitleAndNameCacheAutoInitializer.class,
|
112
|
eu.etaxonomy.cdm.persistence.dao.initializer.AnnotationTypeAutoInitializer.class,
|
113
|
eu.etaxonomy.cdm.persistence.dao.initializer.MarkerTypeAutoInitializer.class,
|
114
|
eu.etaxonomy.cdm.persistence.dao.initializer.GatheringEventLocationAutoInitializer.class,
|
115
|
eu.etaxonomy.cdm.persistence.dao.initializer.TermBaseAutoInitializer.class,
|
116
|
eu.etaxonomy.cdm.persistence.dao.initializer.MediaAutoInitializer.class,
|
117
|
eu.etaxonomy.cdm.persistence.dao.initializer.TypeDesignationAutoInitializer.class,
|
118
|
eu.etaxonomy.cdm.persistence.dao.initializer.TeamAutoInitializer.class
|
119
|
};
|
120
|
|
121
|
Set<Class<?>> checkSet = new HashSet<>(Arrays.asList(expectedAutoInitializers));
|
122
|
|
123
|
Map<Class<CDM>, AutoPropertyInitializer<CDM>> beanAutoInitializers = defaultBeanInitializer.getBeanAutoInitializers();
|
124
|
for(AutoPropertyInitializer<CDM> api : beanAutoInitializers.values()){
|
125
|
assert checkSet.remove(api.getClass()) == true;
|
126
|
}
|
127
|
assert checkSet.size() == 0;
|
128
|
}
|
129
|
|
130
|
@After
|
131
|
public void restoreAutoinitializers() {
|
132
|
if(deacivatedAutoIntitializers != null){
|
133
|
defaultBeanInitializer.getBeanAutoInitializers().putAll(deacivatedAutoIntitializers);
|
134
|
deacivatedAutoIntitializers = null;
|
135
|
}
|
136
|
}
|
137
|
|
138
|
@DataSet
|
139
|
@Test
|
140
|
public void testContact() {
|
141
|
Person person = (Person)agentDao.findByUuid(personUuid);
|
142
|
|
143
|
final List<String> propPath = Arrays.asList(new String[]{
|
144
|
"contact.urls",
|
145
|
"contact.phoneNumbers",
|
146
|
"contact.addresses",
|
147
|
"contact.faxNumbers",
|
148
|
"contact.emailAddresses",
|
149
|
});
|
150
|
initializer.initialize(person, propPath);
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Attempt to reproduce #7331 without success (DO NOT REMOVE THIS COMMENT!)
|
155
|
*/
|
156
|
@DataSet
|
157
|
@Test
|
158
|
public void testFullNameGraphWithPreloadedReference() {
|
159
|
// find the reference by iD (not load!)
|
160
|
Reference ref = referenceDao.findById(5000);
|
161
|
TaxonName name = nameDao.findById(5000);
|
162
|
assertFalse("for this test to be significant the authorship must be uninitialized", Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
|
163
|
initializer.initialize(name, Arrays.asList(new String[]{"nomenclaturalSource.citation.authorship.$"}));
|
164
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
|
165
|
}
|
166
|
|
167
|
@DataSet
|
168
|
@Test
|
169
|
public void testToOneWildcard() {
|
170
|
|
171
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
172
|
assureSessionClear();
|
173
|
|
174
|
TaxonName name = nameDao.load(nameUuid, Arrays.asList("$"));
|
175
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
176
|
assertFalse(Hibernate.isInitialized(name.getAnnotations()));
|
177
|
|
178
|
name = nameDao.load(nameUuid, Arrays.asList("nomenclaturalSource.citation.$"));
|
179
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
180
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
|
181
|
assertFalse(Hibernate.isInitialized(name.getNomenclaturalReference().getAnnotations()));
|
182
|
}
|
183
|
|
184
|
@DataSet
|
185
|
@Test
|
186
|
@Ignore // TODO fix #7375
|
187
|
public void testComplexPath() {
|
188
|
|
189
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
190
|
assureSessionClear();
|
191
|
|
192
|
TaxonName name = nameDao.load(nameUuid, Arrays.asList("nomenclaturalSource.citation.$.*.contact.faxNumbers"));
|
193
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference())); // nomenclaturalReference
|
194
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship())); // $
|
195
|
assertFalse("must not be initialized by 'nomenclaturalSource.citation.$'", Hibernate.isInitialized(name.getNomenclaturalReference().getExtensions()));
|
196
|
Team team = HibernateProxyHelper.deproxy(name.getNomenclaturalReference().getAuthorship(), Team.class);
|
197
|
assertTrue(Hibernate.isInitialized(team.getTeamMembers())); // *
|
198
|
Person person1 = HibernateProxyHelper.deproxy(team.getTeamMembers().get(0), Person.class);
|
199
|
assertEquals(personUuid, person1.getUuid());
|
200
|
assertTrue(Hibernate.isInitialized(person1.getContact())); // contact
|
201
|
assertFalse("must not be initialized by 'nomenclaturalSource.citation.$.*.contact'", Hibernate.isInitialized(person1.getAnnotations()));
|
202
|
assertTrue(Hibernate.isInitialized(person1.getContact().getFaxNumbers())); // * // FIXME fails here #7375
|
203
|
}
|
204
|
|
205
|
@DataSet
|
206
|
@Test
|
207
|
public void testPersonContacts() {
|
208
|
|
209
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
210
|
assureSessionClear();
|
211
|
|
212
|
Person person1 = (Person) agentDao.load(personUuid, Arrays.asList("contact.faxNumbers"));
|
213
|
assertTrue(Hibernate.isInitialized(person1.getContact()));
|
214
|
assertTrue(Hibernate.isInitialized(person1.getContact().getFaxNumbers()));
|
215
|
}
|
216
|
|
217
|
@DataSet
|
218
|
@Test
|
219
|
public void testToOneWildcardDepth1() {
|
220
|
|
221
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
222
|
assureSessionClear();
|
223
|
|
224
|
TaxonName name = nameDao.load(nameUuid, Arrays.asList("nomenclaturalSource.citation.$"));
|
225
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
226
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
|
227
|
assertFalse(Hibernate.isInitialized(name.getNomenclaturalReference().getAnnotations()));
|
228
|
}
|
229
|
|
230
|
@DataSet
|
231
|
@Test
|
232
|
public void testToManyWildcard() {
|
233
|
|
234
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
235
|
assureSessionClear();
|
236
|
|
237
|
TaxonName name = nameDao.load(nameUuid, Arrays.asList("*"));
|
238
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
239
|
assertTrue(Hibernate.isInitialized(name.getAnnotations()));
|
240
|
}
|
241
|
|
242
|
@DataSet
|
243
|
@Test
|
244
|
public void testToManyWildcardDepth1() {
|
245
|
|
246
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
247
|
assureSessionClear();
|
248
|
|
249
|
TaxonName name = nameDao.load(nameUuid, Arrays.asList("nomenclaturalSource.citation.*"));
|
250
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
251
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship()));
|
252
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAnnotations()));
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Test for the {@link TitleAndNameCacheAutoInitializer}
|
257
|
* <p>
|
258
|
*/
|
259
|
@DataSet
|
260
|
@Test
|
261
|
public void testTitleAndNameCacheAutoInitializer() {
|
262
|
|
263
|
assureSessionClear();
|
264
|
|
265
|
//Logger.getLogger(AdvancedBeanInitializer.class).setLevel(Level.TRACE);
|
266
|
List<String> propertyPath = Arrays.asList("$"); //property path is actually not needed, test also succeeds with property path = null
|
267
|
Taxon taxon = (Taxon)taxonDao.load(taxonUuid, propertyPath);
|
268
|
assertTrue(Hibernate.isInitialized(taxon.getName()));
|
269
|
TaxonName name = taxon.getName();
|
270
|
// the TitleAndNameCacheAutoInitializer must not initialize the nomenclaturalReference
|
271
|
// since the authorship is only taken from the combinationAutors field
|
272
|
//
|
273
|
// retrospectively added explanation: the TitleAndNameCacheAutoInitializer only initializes
|
274
|
// the authorship fields, not the nomenclatural reference. In case of the taxon.name use in the test
|
275
|
// the combinationAutors are sufficient for creating the titlecache. The nomenclatural reference
|
276
|
// is not included into the initialization done by the TitleAndNameCacheAutoInitializer
|
277
|
assertFalse(Hibernate.isInitialized(name.getNomenclaturalReference()));
|
278
|
}
|
279
|
|
280
|
@DataSet
|
281
|
@Test
|
282
|
public void testTeamAutoInitializer() {
|
283
|
|
284
|
assureSessionClear();
|
285
|
|
286
|
//Logger.getLogger(AdvancedBeanInitializer.class).setLevel(Level.TRACE);
|
287
|
|
288
|
deacivatedAutoIntitializers = clearAutoinitializers();
|
289
|
// load bean with autoinitializers deactivated
|
290
|
factory.getCurrentSession().setFlushMode(FlushMode.MANUAL); // TODO this is only needed due to #7377 and should be removed otherwise
|
291
|
Taxon taxon = (Taxon)taxonDao.load(taxonUuid, Arrays.asList("name.nomenclaturalSource.citation.authorship"));
|
292
|
assertTrue(Hibernate.isInitialized(taxon.getName())); // name
|
293
|
TaxonName name = taxon.getName();
|
294
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference())); // nomenclaturalReference
|
295
|
assertTrue(Hibernate.isInitialized(name.getNomenclaturalReference().getAuthorship())); // authorship
|
296
|
Team team = HibernateProxyHelper.deproxy(name.getNomenclaturalReference().getAuthorship(), Team.class);
|
297
|
|
298
|
// FIXME : the below assertion fail due to #7377 if the session flushmode is AUTO, this is not critical but an inconsistency.
|
299
|
// In AdvancedBeanInitializer.bulkLoadLazyBeans(BeanInitNode node) the query.list()
|
300
|
// with "QueryImpl( SELECT c FROM TeamOrPersonBase as c WHERE c.id IN (:idSet) )" triggers an autoFlush.
|
301
|
// In turn of the autoflush the team.titleCache is generated which causes the teamMembers to be initialized
|
302
|
//
|
303
|
// members should not initialized since they where not included in the property path
|
304
|
assertFalse("members should not intitialized since they where not included in the property path", Hibernate.isInitialized(team.getTeamMembers()));
|
305
|
|
306
|
// activate the teamAutoInitializer again
|
307
|
AutoPropertyInitializer<CDM> teamAutoInitializer = deacivatedAutoIntitializers.get(TeamOrPersonBase.class);
|
308
|
deacivatedAutoIntitializers.remove(teamAutoInitializer);
|
309
|
defaultBeanInitializer.getBeanAutoInitializers().put((Class<CDM>) TeamOrPersonBase.class, teamAutoInitializer);
|
310
|
|
311
|
taxon = (Taxon)taxonDao.load(taxonUuid, Arrays.asList("name.nomenclaturalSource.citation.authorship"));
|
312
|
|
313
|
team = HibernateProxyHelper.deproxy(name.getNomenclaturalReference().getAuthorship(), Team.class);
|
314
|
assertTrue("members should have been intitialized by the ", Hibernate.isInitialized(team.getTeamMembers()));
|
315
|
|
316
|
}
|
317
|
|
318
|
// ============================== end of tests ========================= //
|
319
|
|
320
|
protected Map<Class<CDM>, AutoPropertyInitializer<CDM>> clearAutoinitializers() {
|
321
|
Map<Class<CDM>, AutoPropertyInitializer<CDM>> autoIntitializers = new HashMap<>(defaultBeanInitializer.getBeanAutoInitializers());
|
322
|
defaultBeanInitializer.getBeanAutoInitializers().clear();
|
323
|
return autoIntitializers;
|
324
|
}
|
325
|
|
326
|
protected void assureSessionClear() {
|
327
|
try {
|
328
|
factory.getCurrentSession().clear();
|
329
|
logger.debug("session cleared");
|
330
|
} catch (HibernateException e){
|
331
|
logger.debug("no session");
|
332
|
// IGNORE no session
|
333
|
}
|
334
|
}
|
335
|
|
336
|
|
337
|
@Override
|
338
|
// @Test
|
339
|
public void createTestDataSet() throws FileNotFoundException {
|
340
|
// 1. create person and a reference
|
341
|
Person person1 = Person.NewTitledInstance("A. Adonis");
|
342
|
Person person2 = Person.NewTitledInstance("B. Belalugosi");
|
343
|
Team team = Team.NewInstance();
|
344
|
team.setUuid(teamUuid);
|
345
|
team.addTeamMember(person1);
|
346
|
team.addTeamMember(person2);
|
347
|
Set<Address> addresses = new HashSet<>();
|
348
|
addresses.add(Address.NewInstance(Country.GERMANY(), "locality", "pobox", "postcode", "region", "street", Point.NewInstance(50.02,33.3, ReferenceSystem.GOOGLE_EARTH(), 3)));
|
349
|
List<String> emailAddresses = new ArrayList<>();
|
350
|
emailAddresses.add("My.email@web.de");
|
351
|
List<String> faxNumbers = new ArrayList<>();
|
352
|
faxNumbers.add("0049-30-1234545");
|
353
|
List<String> phoneNumbers = new ArrayList<>();
|
354
|
phoneNumbers.add("0049-30-1234546");
|
355
|
List<URI> urls = new ArrayList<>();
|
356
|
urls.add(URI.create("http://www.test.de"));
|
357
|
Contact contact = Contact.NewInstance(addresses, emailAddresses, faxNumbers, phoneNumbers, urls);
|
358
|
|
359
|
person1.setContact(contact);
|
360
|
person1.setUuid(personUuid);
|
361
|
person1 = agentDao.save(person1);
|
362
|
person2 = agentDao.save(person2);
|
363
|
team = agentDao.save(team);
|
364
|
|
365
|
Reference ref = ReferenceFactory.newBook();
|
366
|
ref.setUuid(referenceUuid);
|
367
|
ref.setAuthorship(team);
|
368
|
ref.setTitle("The Book");
|
369
|
referenceDao.save(ref);
|
370
|
|
371
|
TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
|
372
|
name.setUuid(nameUuid);
|
373
|
name.setNomenclaturalReference(ref);
|
374
|
name.setTitleCache("Species testii", true);
|
375
|
nameDao.save(name);
|
376
|
|
377
|
Taxon taxon = Taxon.NewInstance(name, null);
|
378
|
taxon.setUuid(taxonUuid);
|
379
|
taxonDao.save(taxon);
|
380
|
|
381
|
// 2. end the transaction so that all data is actually written to the db
|
382
|
setComplete();
|
383
|
endTransaction();
|
384
|
|
385
|
// use the fileNameAppendix if you are creating a data set file which need to be named differently
|
386
|
// from the standard name. For example if a single test method needs different data then the other
|
387
|
// methods the test class you may want to set the fileNameAppendix when creating the data for this method.
|
388
|
String fileNameAppendix = null;
|
389
|
|
390
|
// 3.
|
391
|
writeDbUnitDataSetFile(new String[] {
|
392
|
"ADDRESS", "AGENTBASE","AgentBase_contact_emailaddresses",
|
393
|
"AgentBase_contact_faxnumbers","AgentBase_contact_phonenumbers",
|
394
|
"AgentBase_contact_urls","AgentBase_Address", "AgentBase_AgentBase",
|
395
|
"REFERENCE", "TaxonName", "HomotypicalGroup", "TaxonBase",
|
396
|
"",
|
397
|
"HIBERNATE_SEQUENCES" // IMPORTANT!!!
|
398
|
},
|
399
|
fileNameAppendix, true );
|
400
|
}
|
401
|
}
|