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