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.io.FileNotFoundException;
|
17
|
import java.util.ArrayList;
|
18
|
import java.util.List;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.hibernate.Hibernate;
|
22
|
import org.junit.After;
|
23
|
import org.junit.Before;
|
24
|
import org.junit.Ignore;
|
25
|
import org.junit.Test;
|
26
|
import org.unitils.dbunit.annotation.DataSet;
|
27
|
import org.unitils.spring.annotation.SpringBeanByType;
|
28
|
|
29
|
import eu.etaxonomy.cdm.model.common.LanguageString;
|
30
|
import eu.etaxonomy.cdm.model.description.TextData;
|
31
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
32
|
import eu.etaxonomy.cdm.model.view.AuditEvent;
|
33
|
import eu.etaxonomy.cdm.model.view.context.AuditEventContextHolder;
|
34
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionElementDao;
|
35
|
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
|
36
|
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
|
37
|
|
38
|
@DataSet
|
39
|
public class BeanInitializerIntegration extends CdmTransactionalIntegrationTest {
|
40
|
|
41
|
@SpringBeanByType
|
42
|
ITaxonNameDao taxonNameDao;
|
43
|
|
44
|
@SpringBeanByType
|
45
|
IDescriptionElementDao descriptionElementDao;
|
46
|
|
47
|
UUID sphingidaeUuid;
|
48
|
UUID textDataUuid;
|
49
|
AuditEvent previousAuditEvent;
|
50
|
|
51
|
@Before
|
52
|
public void setUp() {
|
53
|
sphingidaeUuid = UUID.fromString("9640a158-2bdb-4cbc-bff6-8f77e781f86b");
|
54
|
textDataUuid = UUID.fromString("31a0160a-51b2-4565-85cf-2be58cb561d6");
|
55
|
previousAuditEvent = new AuditEvent();
|
56
|
previousAuditEvent.setRevisionNumber(1024);
|
57
|
previousAuditEvent.setUuid(UUID.fromString("1f868a29-9127-4634-90c2-5024cc46be9d"));
|
58
|
AuditEventContextHolder.clearContext(); // By default we're in the current view (i.e. view == null)
|
59
|
}
|
60
|
|
61
|
@After
|
62
|
public void tearDown() {
|
63
|
AuditEventContextHolder.clearContext();
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
* Basic behavior here - we want to be able to initialize properties of this
|
68
|
* entity
|
69
|
*/
|
70
|
@Test
|
71
|
@Ignore //FIXME homotypicalGroup is initialized even if it shouldn't
|
72
|
public void testInitializeManyToOneProperty() {
|
73
|
List<String> propertyPaths = new ArrayList<String>();
|
74
|
propertyPaths.add("nomenclaturalReference");
|
75
|
|
76
|
TaxonName sphingidae = taxonNameDao.load(sphingidaeUuid, propertyPaths);
|
77
|
setComplete();
|
78
|
endTransaction();
|
79
|
|
80
|
assertNotNull("Sphingidae should not be null",sphingidae);
|
81
|
assertFalse("TaxonName.homotypicalGroup should not be initialized",Hibernate.isInitialized(sphingidae.getHomotypicalGroup()));
|
82
|
assertTrue("TaxonName.nomenclaturalReference should be initialized",Hibernate.isInitialized(sphingidae.getNomenclaturalReference()));
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* Slightly more advanced - what happens if we try to initialize
|
87
|
* a non-existent property - i.e. if we retrieve an object which
|
88
|
* might be one of many subclasses, and try to initialize a
|
89
|
* property belonging to another subclass.
|
90
|
*
|
91
|
* The bean initialization code should silently catch the
|
92
|
* MethodNotFound exception (otherwise we would not be able to initialize
|
93
|
* any properties of a subclass).
|
94
|
*/
|
95
|
@Test
|
96
|
public void testInitializeManyToOneSubclassProperty() {
|
97
|
List<String> propertyPaths = new ArrayList<>();
|
98
|
propertyPaths.add("combinationAuthorship");
|
99
|
propertyPaths.add("hybridRelationships");
|
100
|
|
101
|
TaxonName sphingidae = taxonNameDao.load(sphingidaeUuid, propertyPaths);
|
102
|
setComplete();
|
103
|
endTransaction();
|
104
|
|
105
|
assertNotNull("Sphingidae should not be null",sphingidae);
|
106
|
assertTrue("TaxonName.nomenclaturalReference should be initialized",Hibernate.isInitialized(sphingidae.getCombinationAuthorship()));
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* Because java.util.Map is not an instanceof java.util.Collection, we need to
|
111
|
* add an extra clause to DefaultBeanInitializer to catch Map properties
|
112
|
*/
|
113
|
@Test
|
114
|
@Ignore //FIXME disabled since getMultilanguageText() fails when session is closed !!!!
|
115
|
public void testInitializeMapProperty() {
|
116
|
List<String> propertyPaths = new ArrayList<String>();
|
117
|
propertyPaths.add("multilanguageText");
|
118
|
propertyPaths.add("multilanguageText.language");
|
119
|
|
120
|
TextData textData = (TextData)descriptionElementDao.load(textDataUuid, propertyPaths);
|
121
|
setComplete();
|
122
|
endTransaction();
|
123
|
|
124
|
assertNotNull("textData should not be null",textData);
|
125
|
|
126
|
assertTrue("TextData.multilanguageText should be initialized",Hibernate.isInitialized(textData.getMultilanguageText()));
|
127
|
assertFalse("TextData.multilanguageText should not be empty",textData.getMultilanguageText().isEmpty());
|
128
|
LanguageString languageString = textData.getMultilanguageText().values().iterator().next();
|
129
|
assertTrue("LanguageString.language should be initialized",Hibernate.isInitialized(languageString.getLanguage()));
|
130
|
}
|
131
|
|
132
|
/**
|
133
|
* Interesting bug in envers where the three entity (object, parent and mapkey) query was not correct.
|
134
|
* Also Hibernate.initialize does not initalize *-to-Many relationships in envers as envers proxies dont implement
|
135
|
* HibernateProxy etc.
|
136
|
*/
|
137
|
@Test
|
138
|
@Ignore //FIXME disabled since getMultilanguageText() fails when session is closed !!!!
|
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
|
|
156
|
/* (non-Javadoc)
|
157
|
* @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
|
158
|
*/
|
159
|
@Override
|
160
|
public void createTestDataSet() throws FileNotFoundException {
|
161
|
// TODO Auto-generated method stub
|
162
|
|
163
|
}
|
164
|
}
|