2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
;
13 import static org
.junit
.Assert
.assertEquals
;
14 import static org
.junit
.Assert
.assertFalse
;
15 import static org
.junit
.Assert
.assertNotNull
;
16 import static org
.junit
.Assert
.assertNull
;
17 import static org
.junit
.Assert
.assertTrue
;
19 import java
.io
.FileNotFoundException
;
20 import java
.util
.ArrayList
;
21 import java
.util
.HashSet
;
22 import java
.util
.List
;
24 import java
.util
.UUID
;
26 import org
.hibernate
.Hibernate
;
27 import org
.junit
.After
;
28 import org
.junit
.Assert
;
29 import org
.junit
.Before
;
30 import org
.junit
.Test
;
31 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
33 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
34 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTermBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.ExtensionType
;
36 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Representation
;
38 import eu
.etaxonomy
.cdm
.model
.common
.TermType
;
39 import eu
.etaxonomy
.cdm
.model
.common
.TermVocabulary
;
40 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
41 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
42 import eu
.etaxonomy
.cdm
.model
.view
.AuditEvent
;
43 import eu
.etaxonomy
.cdm
.model
.view
.context
.AuditEventContextHolder
;
44 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IDefinedTermDao
;
45 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ITermVocabularyDao
;
46 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
47 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
.SortOrder
;
48 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
50 public class DefinedTermDaoImplTest
extends CdmTransactionalIntegrationTest
{
53 private IDefinedTermDao dao
;
56 private ITermVocabularyDao vocabularyDao
;
60 private UUID northernEuropeUuid
;
61 private UUID middleEuropeUuid
;
62 private UUID westTropicalAfricaUuid
;
63 private Set
<NamedArea
> namedAreas
;
64 private AuditEvent auditEvent
;
68 uuid
= UUID
.fromString("910307f1-dc3c-452c-a6dd-af5ac7cd365c");
69 armUuid
= UUID
.fromString("7a0fde13-26e9-4382-a5c9-5640fc2b3334");
70 northernEuropeUuid
= UUID
.fromString("22524ba2-6e57-4b71-89ab-89fc50fba6b4");
71 middleEuropeUuid
= UUID
.fromString("d292f237-da3d-408b-93a1-3257a8c80b97");
72 westTropicalAfricaUuid
= UUID
.fromString("931164ad-ec16-4133-afab-bdef25d67636");
73 namedAreas
= new HashSet
<NamedArea
>();
74 auditEvent
= new AuditEvent();
75 auditEvent
.setUuid(UUID
.fromString("6456c23d-6424-42dc-a240-36d34e77b249"));
76 auditEvent
.setRevisionNumber(1001);
77 AuditEventContextHolder
.clearContext();
81 public void cleantUp() {
82 AuditEventContextHolder
.clearContext();
86 public void findByTitle() throws Exception
{
87 List
<DefinedTermBase
> terms
= dao
.findByTitle("Diagnosis");
88 assertNotNull("findByTitle should return a List", terms
);
89 assertEquals("findByTitle should return one term ",terms
.size(),1);
90 assertEquals("findByTitle should return Feature.DIAGNOSIS",terms
.get(0),Feature
.DIAGNOSIS());
94 public void getTermByUUID() {
95 DefinedTermBase
<?
> term
= dao
.findByUuid(uuid
);
96 assertNotNull("findByUuid should return a term",term
);
97 assertEquals("findByUuid should return Feature.UNKNOWN",Feature
.UNKNOWN(),term
);
102 public void getLanguageByIso2() {
103 Language lang
= dao
.getLanguageByIso("arm");
105 assertEquals("getLanguageByIso should return the correct Language instance",lang
.getUuid(), armUuid
);
109 public void getLanguageByIso1() {
110 Language lang
= dao
.getLanguageByIso("hy");
112 assertEquals("getLanguageByIso should return the correct Language instance",lang
.getUuid(), armUuid
);
116 public void getLanguageByMalformedIso1() {
117 Language lang
= dao
.getLanguageByIso("a");
118 assertNull("getLanguageByIso should return null for this malformed Iso \'a\'",lang
);
122 public void getLanguageByMalformedIso2() {
123 Language lang
= dao
.getLanguageByIso("abcd");
124 assertNull("getLanguageByIso should return null for this malformed Iso \'abcd\'",lang
);
128 public void getDefinedTermByIdInVocabulary(){
129 UUID tdwgVocUuid
= NamedArea
.uuidTdwgAreaVocabulary
;
130 List
<NamedArea
> list
= dao
.getDefinedTermByIdInVocabulary("GER", tdwgVocUuid
, NamedArea
.class, null, null);
131 assertNotNull("Method should return a result", list
);
132 assertEquals("Method should return exactly 1 area", 1, list
.size());
133 NamedArea area
= list
.get(0);
134 assertEquals("GER", area
.getIdInVocabulary());
135 assertEquals(tdwgVocUuid
, area
.getVocabulary().getUuid());
139 public void testGetIncludes() {
140 NamedArea northernEurope
= (NamedArea
)dao
.findByUuid(northernEuropeUuid
);
141 assert northernEurope
!= null : "NamedArea must exist";
142 namedAreas
.add(northernEurope
);
144 List
<String
> propertyPaths
= new ArrayList
<String
>();
145 propertyPaths
.add("level");
147 List
<NamedArea
> includes
= dao
.getIncludes(namedAreas
, null, null,propertyPaths
);
149 assertNotNull("getIncludes should return a List",includes
);
150 assertFalse("The list should not be empty",includes
.isEmpty());
151 assertEquals("getIncludes should return 9 NamedArea entities",9,includes
.size());
152 assertTrue("NamedArea.level should be initialized",Hibernate
.isInitialized(includes
.get(0).getLevel()));
156 public void countIncludes() {
157 NamedArea northernEurope
= (NamedArea
)dao
.findByUuid(northernEuropeUuid
);
158 assert northernEurope
!= null : "NamedArea must exist";
159 namedAreas
.add(northernEurope
);
161 long numberOfIncludes
= dao
.countIncludes(namedAreas
);
162 assertEquals("countIncludes should return 9",9, numberOfIncludes
);
167 public void testGetPartOf() {
168 NamedArea northernEurope
= (NamedArea
)dao
.findByUuid(northernEuropeUuid
);
169 NamedArea middleEurope
= (NamedArea
)dao
.findByUuid(middleEuropeUuid
);
170 NamedArea westTropicalAfrica
= (NamedArea
)dao
.findByUuid(westTropicalAfricaUuid
);
171 assert northernEurope
!= null : "NamedArea must exist";
172 assert middleEurope
!= null : "NamedArea must exist";
173 assert westTropicalAfrica
!= null : "NamedArea must exist";
174 namedAreas
.add(northernEurope
);
175 namedAreas
.add(middleEurope
);
176 namedAreas
.add(westTropicalAfrica
);
178 List
<String
> propertyPaths
= new ArrayList
<String
>();
179 propertyPaths
.add("level");
181 List
<NamedArea
> partOf
= dao
.getPartOf(namedAreas
, null, null,propertyPaths
);
183 assertNotNull("getPartOf should return a List",partOf
);
184 assertFalse("The list should not be empty",partOf
.isEmpty());
185 assertEquals("getPartOf should return 2 NamedArea entities",2,partOf
.size());
186 assertTrue("NamedArea.level should be initialized",Hibernate
.isInitialized(partOf
.get(0).getLevel()));
190 public void countPartOf() {
191 NamedArea northernEurope
= (NamedArea
)dao
.findByUuid(northernEuropeUuid
);
192 NamedArea middleEurope
= (NamedArea
)dao
.findByUuid(middleEuropeUuid
);
193 NamedArea westTropicalAfrica
= (NamedArea
)dao
.findByUuid(westTropicalAfricaUuid
);
194 assert northernEurope
!= null : "NamedArea must exist";
195 assert middleEurope
!= null : "NamedArea must exist";
196 assert westTropicalAfrica
!= null : "NamedArea must exist";
198 namedAreas
.add(northernEurope
);
199 namedAreas
.add(middleEurope
);
200 namedAreas
.add(westTropicalAfrica
);
202 long numberOfPartOf
= dao
.countPartOf(namedAreas
);
203 assertEquals("countPartOf should return 2",2,numberOfPartOf
);
207 // NOTE: if this test is failing see
208 // http://dev.e-taxonomy.eu/trac/changeset/13291/trunk/cdmlib/cdmlib-persistence/src/test/resources/eu/etaxonomy/cdm/persistence/dao/hibernate/dataset.dtd
209 public void testListInitialization() {
210 AuditEventContextHolder
.getContext().setAuditEvent(auditEvent
);
211 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
212 orderHints
.add(new OrderHint("titleCache",SortOrder
.ASCENDING
));
214 List
<String
> propertyPaths
= new ArrayList
<String
>();
215 propertyPaths
.add("representations");
216 propertyPaths
.add("representations.language");
217 List
<ExtensionType
> extensionTypes
= dao
.list(ExtensionType
.class,null, null, orderHints
, propertyPaths
);
220 assertTrue(Hibernate
.isInitialized(extensionTypes
.get(0).getRepresentations()));
221 Set
<Representation
> representations
= extensionTypes
.get(0).getRepresentations();
222 //TODO currently the representations list is empty, is this wanted? If not,
223 //we should first check, if the list is not empty and then iterate.
224 //Why is it empty? Does ExtensionType not have representations?
225 for(Representation representation
: representations
) {
226 assertTrue(Hibernate
.isInitialized(representation
.getLanguage()));
231 public void testTitleCacheCreation() {
234 @SuppressWarnings("unchecked")
235 TermVocabulary
<DefinedTerm
> newVoc
= TermVocabulary
.NewInstance(TermType
.Modifier
);
236 UUID vocUuid
= UUID
.fromString("6ced4c45-9c1b-4053-9dc3-6b8c51d286ed");
237 newVoc
.setUuid(vocUuid
);
238 UUID termUuid
= UUID
.fromString("2ab69720-c06c-4cfc-8928-d2ae6f1e4a48");
239 DefinedTerm newModifier
= DefinedTerm
.NewModifierInstance("Test Modifier Description", "English Modifier", "TM");
240 Representation englishRepresentation
= newModifier
.getRepresentations().iterator().next();
241 newModifier
.setUuid(termUuid
);
242 newVoc
.addTerm(newModifier
);
243 vocabularyDao
.save(newVoc
);
246 newModifier
.setProtectedTitleCache(false);
247 newModifier
.setProtectedTitleCache(true);
248 Assert
.assertEquals("English Label should be the title cache", "English Modifier", newModifier
.getTitleCache());
250 //Change English label
251 newModifier
.setProtectedTitleCache(false);
252 newModifier
.setLabel("New English label");
253 dao
.saveOrUpdate(newModifier
);
254 newModifier
.setProtectedTitleCache(true);
255 Assert
.assertEquals("English (default language) label should still be the title cache", "New English label", newModifier
.getTitleCache());
258 newModifier
.setProtectedTitleCache(false);
259 Representation newRepresentation
= Representation
.NewInstance("Beschreibung", "Deutscher Modifier", "Abk.", Language
.GERMAN());
260 newModifier
.addRepresentation(newRepresentation
);
261 dao
.saveOrUpdate(newModifier
);
262 newModifier
.setProtectedTitleCache(true);
263 Assert
.assertEquals("English (default language) label should still be the title cache", "New English label", newModifier
.getTitleCache());
266 newModifier
.setProtectedTitleCache(false);
267 newModifier
.removeRepresentation(englishRepresentation
);
268 dao
.saveOrUpdate(newModifier
);
269 newVoc
.setProtectedTitleCache(true);
270 Assert
.assertEquals("German Label should be the new title cache again as English representation is not there anymore", "Deutscher Modifier", newModifier
.getTitleCache());
274 public void testListByTermType(){
276 TermType termType
= TermType
.Modifier
;
278 List
<DefinedTermBase
> existingList
= this.dao
.listByTermType(termType
, null, null, null, null);
279 int nExisting
= existingList
.size();
280 int nExistingTerms
= this.dao
.list(DefinedTerm
.class, null, null, null, null).size();
284 @SuppressWarnings("unchecked")
285 TermVocabulary
<DefinedTerm
> newVoc
= TermVocabulary
.NewInstance(termType
);
286 UUID vocUuid
= UUID
.fromString("6ced4c45-9c1b-4053-9dc3-6b8c51d286ed");
287 newVoc
.setUuid(vocUuid
);
288 UUID termUuid
= UUID
.fromString("2ab69720-c06c-4cfc-8928-d2ae6f1e4a48");
289 DefinedTerm newModifier
= DefinedTerm
.NewModifierInstance("Test Modifier Description", "English Modifier", "TM");
290 newModifier
.setUuid(termUuid
);
291 newVoc
.addTerm(newModifier
);
292 vocabularyDao
.save(newVoc
);
293 this.commitAndStartNewTransaction(null);
296 int nNow
= this.dao
.listByTermType(termType
, null, null, null, null).size();
297 Assert
.assertEquals("There should be exactly 1 more term now", nExisting
+ 1 , nNow
);
298 int nTermsNow
= this.dao
.list(DefinedTerm
.class, null, null, null, null).size();
299 Assert
.assertEquals("There should be exactly 1 more term now", nExistingTerms
+ 1 , nTermsNow
);
300 this.commitAndStartNewTransaction(null);
302 //Add German representation
303 Representation newRepresentation
= Representation
.NewInstance("Beschreibung", "Deutscher Modifier", "Abk.", Language
.GERMAN());
304 newModifier
.addRepresentation(newRepresentation
);
305 dao
.saveOrUpdate(newModifier
);
306 this.commitAndStartNewTransaction(null);
308 nNow
= this.dao
.listByTermType(termType
, null, null, null, null).size();
309 Assert
.assertEquals("There should still be only one more term (but with 2 representations)", nExisting
+ 1 , nNow
);
310 nTermsNow
= this.dao
.list(DefinedTerm
.class, null, null, null, null).size();
311 Assert
.assertEquals("There should be exactly 1 more term now", nExistingTerms
+ 1 , nTermsNow
);
314 List
<DefinedTerm
> languages
= this.dao
.listByTermType(TermType
.Language
, null, null, null, null);
315 Assert
.assertNotNull(languages
);
316 Assert
.assertEquals(485, languages
.size());
321 public void createTestDataSet() throws FileNotFoundException
{}