1
|
/**
|
2
|
* Copyright (C) 2009 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
|
|
10
|
package eu.etaxonomy.cdm.persistence.dao;
|
11
|
|
12
|
import static org.junit.Assert.assertFalse;
|
13
|
import static org.junit.Assert.assertNotNull;
|
14
|
import static org.junit.Assert.assertTrue;
|
15
|
|
16
|
import java.util.ArrayList;
|
17
|
import java.util.List;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.hibernate.Hibernate;
|
21
|
import org.hibernate.collection.PersistentCollection;
|
22
|
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.CollectionProxy;
|
23
|
import org.hibernate.envers.entities.mapper.relation.lazy.proxy.MapProxy;
|
24
|
import org.hibernate.proxy.HibernateProxy;
|
25
|
import org.junit.After;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.Test;
|
28
|
import org.unitils.dbunit.annotation.DataSet;
|
29
|
import org.unitils.spring.annotation.SpringBeanByType;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
32
|
import eu.etaxonomy.cdm.model.description.TextData;
|
33
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
34
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
35
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
36
|
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
|
37
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
|
38
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
39
|
import eu.etaxonomy.cdm.test.integration.CdmIntegrationTest;
|
40
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
41
|
|
42
|
@DataSet
|
43
|
public class BeanInitializerIntegration extends CdmTransactionalIntegrationTest {
|
44
|
|
45
|
@SpringBeanByType
|
46
|
ITaxonNameDao taxonNameDao;
|
47
|
|
48
|
@SpringBeanByType
|
49
|
IDescriptionElementDao descriptionElementDao;
|
50
|
|
51
|
UUID sphingidaeUuid;
|
52
|
UUID textDataUuid;
|
53
|
AuditEvent previousAuditEvent;
|
54
|
|
55
|
@Before
|
56
|
public void setUp() {
|
57
|
sphingidaeUuid = UUID.fromString("9640a158-2bdb-4cbc-bff6-8f77e781f86b");
|
58
|
textDataUuid = UUID.fromString("31a0160a-51b2-4565-85cf-2be58cb561d6");
|
59
|
previousAuditEvent = new AuditEvent();
|
60
|
previousAuditEvent.setRevisionNumber(1024);
|
61
|
previousAuditEvent.setUuid(UUID.fromString("1f868a29-9127-4634-90c2-5024cc46be9d"));
|
62
|
AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
|
63
|
}
|
64
|
|
65
|
@After
|
66
|
public void tearDown() {
|
67
|
AuditEventContextHolder.clearContext();
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* Basic behaviour here - we want to be able to initialize properties of this
|
72
|
* entity
|
73
|
*/
|
74
|
@Test
|
75
|
public void testInitializeManyToOneProperty() {
|
76
|
List<String> propertyPaths = new ArrayList<String>();
|
77
|
propertyPaths.add("nomenclaturalReference");
|
78
|
|
79
|
TaxonNameBase sphingidae = taxonNameDao.load(sphingidaeUuid, propertyPaths);
|
80
|
setComplete();
|
81
|
endTransaction();
|
82
|
|
83
|
assertNotNull("Sphingidae should not be null",sphingidae);
|
84
|
assertFalse("TaxonNameBase.homotypicalGroup should not be initialized",Hibernate.isInitialized(sphingidae.getHomotypicalGroup()));
|
85
|
assertTrue("TaxonNameBase.nomenclaturalReference should be initialized",Hibernate.isInitialized(sphingidae.getNomenclaturalReference()));
|
86
|
}
|
87
|
|
88
|
/**
|
89
|
* Slightly more advanced - what happens if we try to initialize
|
90
|
* a non-existent property - i.e. if we retrieve an object which
|
91
|
* might be one of many subclasses, and try to initialize a
|
92
|
* property belonging to another subclass.
|
93
|
*
|
94
|
* The bean initialization code should silently catch the
|
95
|
* MethodNotFound exception (otherwise we would not be able to initialize
|
96
|
* any properties of a subclass).
|
97
|
*/
|
98
|
@Test
|
99
|
public void testInitializeManyToOneSubclassProperty() {
|
100
|
List<String> propertyPaths = new ArrayList<String>();
|
101
|
propertyPaths.add("combinationAuthorTeam");
|
102
|
propertyPaths.add("hybridRelationships");
|
103
|
|
104
|
NonViralName sphingidae = (NonViralName)taxonNameDao.load(sphingidaeUuid, propertyPaths);
|
105
|
setComplete();
|
106
|
endTransaction();
|
107
|
|
108
|
assertNotNull("Sphingidae should not be null",sphingidae);
|
109
|
assertTrue("TaxonNameBase.nomenclaturalReference should be initialized",Hibernate.isInitialized(sphingidae.getCombinationAuthorTeam()));
|
110
|
}
|
111
|
|
112
|
/**
|
113
|
* Because java.util.Map is not an instanceof java.util.Collection, we need to
|
114
|
* add an extra clause to DefaultBeanInitializer to catch Map properties
|
115
|
*/
|
116
|
@Test
|
117
|
public void testInitializeMapProperty() {
|
118
|
List<String> propertyPaths = new ArrayList<String>();
|
119
|
propertyPaths.add("multilanguageText");
|
120
|
propertyPaths.add("multilanguageText.language");
|
121
|
|
122
|
TextData textData = (TextData)descriptionElementDao.load(textDataUuid, propertyPaths);
|
123
|
setComplete();
|
124
|
endTransaction();
|
125
|
|
126
|
assertNotNull("textData should not be null",textData);
|
127
|
assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(textData.getMultilanguageText()));
|
128
|
assertFalse("TextData.multilanguageText should not be empty",textData.getMultilanguageText().isEmpty());
|
129
|
LanguageString languageString = textData.getMultilanguageText().values().iterator().next();
|
130
|
assertTrue("LanguageString.language should be initialized",Hibernate.isInitialized(languageString.getLanguage()));
|
131
|
}
|
132
|
|
133
|
/**
|
134
|
* Interesting bug in envers where the three entity (object, parent and mapkey) query was not correct.
|
135
|
* Also Hibernate.initialize does not initalize *-to-Many relationships in envers as envers proxies dont implement
|
136
|
* HibernateProxy etc.
|
137
|
*/
|
138
|
@Test
|
139
|
public void testInitializeMapInPriorView() {
|
140
|
AuditEventContextHolder.getContext().setAuditEvent(previousAuditEvent);
|
141
|
List<String> propertyPaths = new ArrayList<String>();
|
142
|
propertyPaths.add("multilanguageText");
|
143
|
propertyPaths.add("multilanguageText.language");
|
144
|
|
145
|
TextData textData = (TextData)descriptionElementDao.load(textDataUuid, propertyPaths);
|
146
|
setComplete();
|
147
|
endTransaction();
|
148
|
|
149
|
assertNotNull("textData should not be null",textData);
|
150
|
assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(textData.getMultilanguageText()));
|
151
|
assertFalse("TextData.multilanguageText should not be empty",textData.getMultilanguageText().isEmpty());
|
152
|
LanguageString languageString = textData.getMultilanguageText().values().iterator().next();
|
153
|
assertTrue("LanguageString.language should be initialized",Hibernate.isInitialized(languageString.getLanguage()));
|
154
|
}
|
155
|
}
|