3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.facade
;
12 import java
.io
.FileNotFoundException
;
13 import java
.util
.UUID
;
15 import org
.apache
.log4j
.Logger
;
16 import org
.hibernate
.proxy
.HibernateProxy
;
17 import org
.junit
.Assert
;
18 import org
.junit
.Before
;
19 import org
.junit
.Ignore
;
20 import org
.junit
.Test
;
21 import org
.unitils
.dbunit
.annotation
.DataSet
;
22 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
24 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.model
.common
.DefinedTerm
;
31 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
32 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
33 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
34 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
35 import eu
.etaxonomy
.cdm
.model
.description
.IndividualsAssociation
;
36 import eu
.etaxonomy
.cdm
.model
.location
.Country
;
37 import eu
.etaxonomy
.cdm
.model
.location
.NamedArea
;
38 import eu
.etaxonomy
.cdm
.model
.location
.Point
;
39 import eu
.etaxonomy
.cdm
.model
.location
.ReferenceSystem
;
40 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
41 import eu
.etaxonomy
.cdm
.model
.name
.BotanicalName
;
42 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
43 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
44 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
45 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEvent
;
46 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivationEventType
;
47 import eu
.etaxonomy
.cdm
.model
.occurrence
.DerivedUnit
;
48 import eu
.etaxonomy
.cdm
.model
.occurrence
.FieldUnit
;
49 import eu
.etaxonomy
.cdm
.model
.occurrence
.GatheringEvent
;
50 import eu
.etaxonomy
.cdm
.model
.occurrence
.PreservationMethod
;
51 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
52 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
53 import eu
.etaxonomy
.cdm
.strategy
.parser
.TimePeriodParser
;
54 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
57 * NOTE: The only reason for having this test is to test if injection of the cache strategy into a
58 * standard specimen works. Once we use another default cache strategy then the derived unit facade
59 * this test can be deleted or adapted and moved to cdmlib-model.
65 public class DerivedUnitFacadeCacheStrategyInjectionTest
extends CdmTransactionalIntegrationTest
{
66 @SuppressWarnings("unused")
67 private static final Logger logger
= Logger
.getLogger(DerivedUnitFacadeCacheStrategyInjectionTest
.class);
70 private IOccurrenceService occService
;
72 private ITaxonService taxonService
;
74 private IDescriptionService descService
;
76 private static final UUID taxonUuid
= UUID
.fromString("10cfb372-0b1a-4d82-9707-c5ffd2b93a55");
79 private DerivedUnit specimen
;
80 private DerivationEvent derivationEvent
;
81 private FieldUnit fieldUnit
;
82 private GatheringEvent gatheringEvent
;
83 private final Integer absoluteElevation
= 40;
84 private final Integer absoluteElevationError
= 2;
85 private final Team collector
= Team
.NewInstance();
86 private final String collectingMethod
= "Collection Method";
87 private final Double distanceToGround
= 22.0;
88 private final Double distanceToSurface
= 50.0;
89 private final ReferenceSystem referenceSystem
= ReferenceSystem
.WGS84();
90 private final Point exactLocation
= Point
.NewInstance(12.3, 10.567, referenceSystem
, 22);
91 private final String gatheringEventDescription
= "A nice gathering description";
92 private final TimePeriod gatheringPeriod
= TimePeriodParser
.parseString("03.05.2005");
93 private final String ecology
= "sand dunes";
94 private final String plantDescription
= "flowers blue";
96 private final String fieldNumber
= "5678";
97 private final String fieldNotes
= "such a beautiful specimen";
98 private Person primaryCollector
;
100 private final Integer individualCount
= 1;
101 private final DefinedTerm lifeStage
= DefinedTerm
.NewStageInstance("A wonderful stage", "stage", "st");
102 private final DefinedTerm sex
= DefinedTerm
.NewSexInstance("FemaleMale", "FM", "FM");
103 private final NamedArea country
= Country
.GERMANY();
104 private final LanguageString locality
= LanguageString
.NewInstance("Berlin-Dahlem, E side of Englerallee", Language
.DEFAULT());
106 private final String exsiccatum
= "Greuter, Pl. Dahlem. 456";
107 private final String accessionNumber
= "8909756";
108 private final String catalogNumber
= "UU879873590";
109 private final TaxonNameBase
<?
,?
> taxonName
= BotanicalName
.NewInstance(Rank
.GENUS(), "Abies", null, null, null, null, null, null, null);
110 private final String collectorsNumber
= "234589913A34";
111 private final Collection collection
= Collection
.NewInstance();
113 private final PreservationMethod preservationMethod
= PreservationMethod
.NewInstance(null, "my prservation");
115 private DerivedUnit collectionSpecimen
;
116 private GatheringEvent existingGatheringEvent
;
117 private DerivationEvent firstDerivationEvent
;
118 private FieldUnit firstFieldObject
;
119 private final Media media1
= Media
.NewInstance();
122 //****************************** SET UP *****************************************/
125 // * @throws java.lang.Exception
128 // public static void setUpBeforeClass() throws Exception {
129 // // FIXME maybe this will cause problems in other tests
130 // // INDEED !!!! it causes problems thus this is replaced by making this test a CdmIntegrationTest !!!
131 // new DefaultTermInitializer().initialize();
135 * @throws java.lang.Exception
138 public void setUp() throws Exception
{
139 specimen
= DerivedUnit
.NewPreservedSpecimenInstance();
141 derivationEvent
= DerivationEvent
.NewInstance(DerivationEventType
.ACCESSIONING());
142 specimen
.setDerivedFrom(derivationEvent
);
143 fieldUnit
= FieldUnit
.NewInstance();
144 fieldUnit
.addDerivationEvent(derivationEvent
);
145 gatheringEvent
= GatheringEvent
.NewInstance();
146 fieldUnit
.setGatheringEvent(gatheringEvent
);
147 gatheringEvent
.setAbsoluteElevation(absoluteElevation
);
148 // gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
149 gatheringEvent
.setActor(collector
);
150 gatheringEvent
.setCollectingMethod(collectingMethod
);
151 gatheringEvent
.setDistanceToGround(distanceToGround
);
152 gatheringEvent
.setDistanceToWaterSurface(distanceToSurface
);
153 gatheringEvent
.setExactLocation(exactLocation
);
154 gatheringEvent
.setDescription(gatheringEventDescription
);
156 gatheringEvent
.setTimeperiod(gatheringPeriod
);
157 gatheringEvent
.setLocality(locality
);
158 gatheringEvent
.setCountry(country
);
160 fieldUnit
.setFieldNumber(fieldNumber
);
161 fieldUnit
.setFieldNotes(fieldNotes
);
162 fieldUnit
.setIndividualCount(individualCount
);
163 fieldUnit
.setSex(sex
);
164 fieldUnit
.setLifeStage(lifeStage
);
165 primaryCollector
= Person
.NewTitledInstance("Kilian");
166 collector
.addTeamMember(primaryCollector
);
167 Person secondCollector
= Person
.NewInstance();
168 secondCollector
.setFirstname("Andreas");
169 secondCollector
.setLastname("Muller");
170 collector
.addTeamMember(secondCollector
);
171 Person thirdCollector
= Person
.NewTitledInstance("Kohlbecker");
172 collector
.addTeamMember(thirdCollector
);
173 fieldUnit
.setPrimaryCollector(primaryCollector
);
175 specimen
.setAccessionNumber(accessionNumber
);
176 specimen
.setCatalogNumber(catalogNumber
);
177 specimen
.setStoredUnder(taxonName
);
178 specimen
.setCollection(collection
);
179 specimen
.setPreservation(preservationMethod
);
180 specimen
.setExsiccatum(exsiccatum
);
182 //existing specimen with 2 derivation events in line
183 collectionSpecimen
= DerivedUnit
.NewPreservedSpecimenInstance();
184 DerivedUnit middleSpecimen
= DerivedUnit
.NewPreservedSpecimenInstance();
185 firstFieldObject
= FieldUnit
.NewInstance();
187 //TODO maybe we should define concrete event types here
188 DerivationEvent lastDerivationEvent
= DerivationEvent
.NewInstance(null);
189 DerivationEvent middleDerivationEvent
= DerivationEvent
.NewInstance(null);
190 firstDerivationEvent
= DerivationEvent
.NewInstance(null);
192 collectionSpecimen
.setDerivedFrom(lastDerivationEvent
);
194 lastDerivationEvent
.addOriginal(middleSpecimen
);
195 middleSpecimen
.setDerivedFrom(firstDerivationEvent
);
196 firstDerivationEvent
.addOriginal(firstFieldObject
);
197 existingGatheringEvent
= GatheringEvent
.NewInstance();
198 firstFieldObject
.setGatheringEvent(existingGatheringEvent
);
202 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
205 public void testGetSpecimenTitleCache() {
206 // String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756); flowers blue.";
207 String correctCache
= "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756).";
209 // DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
210 // SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
211 // fieldUnitDescription.addElement(ecologyItem);
212 //// specimenFacade.setEcology(ecology);
213 // DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
214 // fieldUnitDescription.addElement(plantDescItem);
215 //// specimenFacade.setPlantDescription(plantDescription);
217 collection
.setCode("B");
218 Assert
.assertEquals(correctCache
, specimen
.getTitleCache());
225 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
228 public void testGetFieldUnitTitleCache() {
229 // String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), sand dunes, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; flowers blue.";
230 String correctCache
= "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3.5.2005, Kilian 5678, A. Muller & Kohlbecker.";
232 // DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
233 // SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
234 // fieldUnitDescription.addElement(ecologyItem);
235 // DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
236 // fieldUnitDescription.addElement(plantDescItem);
238 collection
.setCode("B"); //no effect
239 Assert
.assertEquals(correctCache
, fieldUnit
.getTitleCache());
242 //Test if even a hibernate proxy (javassist) class correctly loads the DerivedUnitCacheStrategy.
245 public void testPersistedDerivedUnit(){
246 Taxon taxon
= (Taxon
)this.taxonService
.find(taxonUuid
);
248 IndividualsAssociation indivAssoc
= getDescriptionElement(taxon
, 5000);
249 SpecimenOrObservationBase
<?
> specimen
= indivAssoc
.getAssociatedSpecimenOrObservation();
250 Assert
.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen
instanceof HibernateProxy
);
251 DerivedUnit myUnit
= CdmBase
.deproxy(specimen
, DerivedUnit
.class);
252 myUnit
.setTitleCache(null);
253 String expectedDerivedUnitCache
= "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (8909756).";
254 Assert
.assertEquals(expectedDerivedUnitCache
, myUnit
.getTitleCache());
259 public void testPersistedFieldUnit(){
260 Taxon taxon
= (Taxon
)this.taxonService
.find(taxonUuid
);
261 IndividualsAssociation indivAssoc
= getDescriptionElement(taxon
, 5001);
262 SpecimenOrObservationBase
<?
> fieldUnit
= indivAssoc
.getAssociatedSpecimenOrObservation();
263 Assert
.assertTrue("FieldUnit should be proxy otherwise the test does not test what it should", fieldUnit
instanceof HibernateProxy
);
264 FieldUnit myFieldUnit
= CdmBase
.deproxy(fieldUnit
, FieldUnit
.class);
265 myFieldUnit
.setTitleCache(null);
266 String expectedFieldUnitCache
= "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3.5.2005, Kilian 5678, A. Muller & Kohlbecker.";
267 Assert
.assertEquals(expectedFieldUnitCache
, myFieldUnit
.getTitleCache());
277 private IndividualsAssociation
getDescriptionElement(Taxon taxon
, int id
) {
278 for (DescriptionElementBase el
: taxon
.getDescriptions().iterator().next().getElements()){
279 if (el
.getId() == id
) {
280 return (IndividualsAssociation
)el
;
289 public void createTestDataSet() throws FileNotFoundException
{
290 // specimen.setUuid(derivedUnitUuid);
291 // fieldUnit.setUuid(fieldUnitUuid);
292 // Taxon taxon = Taxon.NewInstance(null, null);
293 // taxon.setUuid(taxonUuid);
294 // TaxonDescription desc = TaxonDescription.NewInstance(taxon);
296 // IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
297 // desc.addElement(indAssoc);
299 // this.taxonService.saveOrUpdate(taxon);
304 // writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
305 // "DerivationEvent", "DescriptionBase","DescriptionElementBase",
306 // "GatheringEvent","AgentBase","LanguageString","TaxonNameBase",
307 // "TaxonBase","Collection",
308 // "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});