Project

General

Profile

Download (13.2 KB) Statistics
| Branch: | Tag: | Revision:
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.api.facade;
10

    
11
import java.io.FileNotFoundException;
12
import java.util.UUID;
13

    
14
import org.apache.log4j.Logger;
15
import org.hibernate.proxy.HibernateProxy;
16
import org.junit.Assert;
17
import org.junit.Before;
18
import org.junit.Test;
19
import org.unitils.dbunit.annotation.DataSet;
20
import org.unitils.spring.annotation.SpringBeanByType;
21

    
22
import eu.etaxonomy.cdm.api.service.IDescriptionService;
23
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
24
import eu.etaxonomy.cdm.api.service.ITaxonService;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.CdmBase;
28
import eu.etaxonomy.cdm.model.common.Language;
29
import eu.etaxonomy.cdm.model.common.LanguageString;
30
import eu.etaxonomy.cdm.model.common.TimePeriod;
31
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
32
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
33
import eu.etaxonomy.cdm.model.location.Country;
34
import eu.etaxonomy.cdm.model.location.NamedArea;
35
import eu.etaxonomy.cdm.model.location.Point;
36
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
37
import eu.etaxonomy.cdm.model.media.Media;
38
import eu.etaxonomy.cdm.model.name.Rank;
39
import eu.etaxonomy.cdm.model.name.TaxonName;
40
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
41
import eu.etaxonomy.cdm.model.occurrence.Collection;
42
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
43
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
44
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
45
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
46
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
47
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
48
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.term.DefinedTerm;
51
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
52
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
53

    
54
/**
55
 * NOTE: The only reason for having this test is to test if injection of the cache strategy into a
56
 * standard specimen works. Once we use another default cache strategy then the derived unit facade
57
 * this test can be deleted or adapted and moved to cdmlib-model.
58
 *
59
 * @author a.mueller
60
 * @since 2010 Jun 3
61
 */
62
public class DerivedUnitFacadeCacheStrategyInjectionTest extends CdmTransactionalIntegrationTest {
63
	@SuppressWarnings("unused")
64
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeCacheStrategyInjectionTest.class);
65

    
66
    @SpringBeanByType
67
    private IOccurrenceService occService;
68
    @SpringBeanByType
69
    private ITaxonService taxonService;
70
    @SpringBeanByType
71
    private IDescriptionService descService;
72

    
73
    private static final UUID taxonUuid = UUID.fromString("10cfb372-0b1a-4d82-9707-c5ffd2b93a55");
74

    
75
    private DerivedUnit specimen;
76
    private DerivationEvent derivationEvent;
77
    private FieldUnit fieldUnit;
78
    private GatheringEvent gatheringEvent;
79
    private final Integer absoluteElevation = 40;
80
    private final Integer absoluteElevationError = 2;
81
    private final Team collector = Team.NewInstance();
82
    private final String collectingMethod = "Collection Method";
83
    private final Double distanceToGround = 22.0;
84
    private final Double distanceToSurface = 50.0;
85
	private final ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
86
    private final Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
87
    private final String gatheringEventDescription = "A nice gathering description";
88
    private final TimePeriod gatheringPeriod = TimePeriodParser.parseString("03.05.2005");
89
    private final String ecology = "sand dunes";
90
    private final String plantDescription = "flowers blue";
91

    
92
    private final String fieldNumber = "5678";
93
    private final String fieldNotes = "such a beautiful specimen";
94
    private Person primaryCollector;
95

    
96
    private final String individualCount = "1";
97
	private final DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
98
	private final DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
99
	private final NamedArea country = Country.GERMANY();
100
    private final LanguageString locality = LanguageString.NewInstance("Berlin-Dahlem, E side of Englerallee", Language.DEFAULT());
101

    
102
    private final String exsiccatum = "Greuter, Pl. Dahlem. 456";
103
    private final String accessionNumber = "8909756";
104
    private final String catalogNumber = "UU879873590";
105
    private final TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
106
    private final String collectorsNumber = "234589913A34";
107
    private final Collection collection = Collection.NewInstance();
108

    
109
    private final PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
110

    
111
    private DerivedUnit collectionSpecimen;
112
    private GatheringEvent existingGatheringEvent;
113
    private DerivationEvent firstDerivationEvent;
114
    private FieldUnit firstFieldObject;
115
    private final Media media1 = Media.NewInstance();
116

    
117

    
118
//****************************** SET UP *****************************************/
119

    
120
	@Before
121
	public void setUp() throws Exception {
122
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
123

    
124
		derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
125
		specimen.setDerivedFrom(derivationEvent);
126
		fieldUnit = FieldUnit.NewInstance();
127
		fieldUnit.addDerivationEvent(derivationEvent);
128
		gatheringEvent = GatheringEvent.NewInstance();
129
		fieldUnit.setGatheringEvent(gatheringEvent);
130
		gatheringEvent.setAbsoluteElevation(absoluteElevation);
131
//		gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
132
		gatheringEvent.setActor(collector);
133
		gatheringEvent.setCollectingMethod(collectingMethod);
134
		gatheringEvent.setDistanceToGround(distanceToGround);
135
		gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
136
		gatheringEvent.setExactLocation(exactLocation);
137
		gatheringEvent.setDescription(gatheringEventDescription);
138

    
139
		gatheringEvent.setTimeperiod(gatheringPeriod);
140
		gatheringEvent.setLocality(locality);
141
		gatheringEvent.setCountry(country);
142

    
143
		fieldUnit.setFieldNumber(fieldNumber);
144
		fieldUnit.setFieldNotes(fieldNotes);
145
		fieldUnit.setIndividualCount(individualCount);
146
		fieldUnit.setSex(sex);
147
		fieldUnit.setLifeStage(lifeStage);
148
		primaryCollector = Person.NewTitledInstance("Kilian");
149
		collector.addTeamMember(primaryCollector);
150
		Person secondCollector = Person.NewInstance();
151
		secondCollector.setGivenName("Andreas");
152
		secondCollector.setFamilyName("Muller");
153
		collector.addTeamMember(secondCollector);
154
		Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
155
		collector.addTeamMember(thirdCollector);
156
		fieldUnit.setPrimaryCollector(primaryCollector);
157

    
158
		specimen.setAccessionNumber(accessionNumber);
159
		specimen.setCatalogNumber(catalogNumber);
160
		specimen.setStoredUnder(taxonName);
161
		specimen.setCollection(collection);
162
		specimen.setPreservation(preservationMethod);
163
		specimen.setExsiccatum(exsiccatum);
164

    
165
		//existing specimen with 2 derivation events in line
166
		collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
167
		DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
168
		firstFieldObject = FieldUnit.NewInstance();
169

    
170
		//TODO maybe we should define concrete event types here
171
		DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
172
		DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
173
		firstDerivationEvent = DerivationEvent.NewInstance(null);
174

    
175
		collectionSpecimen.setDerivedFrom(lastDerivationEvent);
176

    
177
		lastDerivationEvent.addOriginal(middleSpecimen);
178
		middleSpecimen.setDerivedFrom(firstDerivationEvent);
179
		firstDerivationEvent.addOriginal(firstFieldObject);
180
		existingGatheringEvent = GatheringEvent.NewInstance();
181
		firstFieldObject.setGatheringEvent(existingGatheringEvent);
182

    
183
	}
184

    
185
	@Test
186
	public void testGetSpecimenTitleCache() {
187
//		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.";
188
		String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756).";
189

    
190
//		DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
191
//		SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
192
//		fieldUnitDescription.addElement(ecologyItem);
193
////		specimenFacade.setEcology(ecology);
194
//		DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
195
//		fieldUnitDescription.addElement(plantDescItem);
196
////		specimenFacade.setPlantDescription(plantDescription);
197

    
198
		collection.setCode("B");
199
		Assert.assertEquals(correctCache, specimen.getTitleCache());
200
	}
201

    
202
    @Test
203
    public void testGetFieldUnitTitleCache() {
204
//        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.";
205
        String correctCache = "Germany, Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10\u00B034'1.2\"N, 12\u00B018'E (WGS84), 3 May 2005, Kilian 5678, A. Muller & Kohlbecker.";
206

    
207
//        DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
208
//        SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
209
//        fieldUnitDescription.addElement(ecologyItem);
210
//        DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
211
//        fieldUnitDescription.addElement(plantDescItem);
212

    
213
        collection.setCode("B");  //no effect
214
        Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
215
    }
216

    
217
    //Test if even a hibernate proxy (javassist) class correctly loads the DerivedUnitCacheStrategy.
218
    @Test
219
    @DataSet
220
    public void testPersistedDerivedUnit(){
221
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
222

    
223
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
224
        SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
225
        Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
226
        DerivedUnit myUnit = CdmBase.deproxy(specimen, DerivedUnit.class);
227
        myUnit.setTitleCache(null);
228
        String expectedDerivedUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3 May 2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (8909756).";
229
        Assert.assertEquals(expectedDerivedUnitCache, myUnit.getTitleCache());
230
    }
231

    
232
    @Test
233
    @DataSet
234
    public void testPersistedFieldUnit(){
235
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
236
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5001);
237
        SpecimenOrObservationBase<?> fieldUnit = indivAssoc.getAssociatedSpecimenOrObservation();
238
        Assert.assertTrue("FieldUnit should be proxy otherwise the test does not test what it should", fieldUnit instanceof HibernateProxy);
239
        FieldUnit myFieldUnit = CdmBase.deproxy(fieldUnit, FieldUnit.class);
240
        myFieldUnit.setTitleCache(null);
241
        String expectedFieldUnitCache = "Berlin-Dahlem, E side of Englerallee, alt. 40 m, 10°34'1.2\"N, 12°18'E, 3 May 2005, Kilian 5678, A. Muller & Kohlbecker.";
242
        Assert.assertEquals(expectedFieldUnitCache, myFieldUnit.getTitleCache());
243
    }
244

    
245
    private IndividualsAssociation getDescriptionElement(Taxon taxon, int id) {
246
        for (DescriptionElementBase el : taxon.getDescriptions().iterator().next().getElements()){
247
            if (el.getId() == id) {
248
                return (IndividualsAssociation)el;
249
            }
250
        }
251
        return null;
252
    }
253

    
254
    @Override
255
//  @Test
256
    public void createTestDataSet() throws FileNotFoundException {
257
//        specimen.setUuid(derivedUnitUuid);
258
//        fieldUnit.setUuid(fieldUnitUuid);
259
//        Taxon taxon = Taxon.NewInstance(null, null);
260
//        taxon.setUuid(taxonUuid);
261
//        TaxonDescription desc = TaxonDescription.NewInstance(taxon);
262
//
263
//        IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
264
//        desc.addElement(indAssoc);
265
//
266
//        this.taxonService.saveOrUpdate(taxon);
267
//
268
//        setComplete();
269
//        endTransaction();
270
//
271
//        writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
272
//                "DerivationEvent", "DescriptionBase","DescriptionElementBase",
273
//                "GatheringEvent","AgentBase","LanguageString","TaxonName",
274
//                "TaxonBase","Collection",
275
//                "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});
276
    }
277

    
278
}
(1-1/4)