Project

General

Profile

Download (13.6 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.Ignore;
19
import org.junit.Test;
20
import org.unitils.dbunit.annotation.DataSet;
21
import org.unitils.spring.annotation.SpringBeanByType;
22

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

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

    
68
    @SpringBeanByType
69
    private IOccurrenceService occService;
70
    @SpringBeanByType
71
    private ITaxonService taxonService;
72
    @SpringBeanByType
73
    private IDescriptionService descService;
74

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

    
77

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

    
95
    private final String fieldNumber = "5678";
96
    private final String fieldNotes = "such a beautiful specimen";
97
    private Person primaryCollector;
98

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

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

    
112
    private final PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
113

    
114
    private DerivedUnit collectionSpecimen;
115
    private GatheringEvent existingGatheringEvent;
116
    private DerivationEvent firstDerivationEvent;
117
    private FieldUnit firstFieldObject;
118
    private final Media media1 = Media.NewInstance();
119

    
120

    
121
//****************************** SET UP *****************************************/
122

    
123

    
124
	/**
125
	 * @throws java.lang.Exception
126
	 */
127
	@Before
128
	public void setUp() throws Exception {
129
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
130

    
131
		derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
132
		specimen.setDerivedFrom(derivationEvent);
133
		fieldUnit = FieldUnit.NewInstance();
134
		fieldUnit.addDerivationEvent(derivationEvent);
135
		gatheringEvent = GatheringEvent.NewInstance();
136
		fieldUnit.setGatheringEvent(gatheringEvent);
137
		gatheringEvent.setAbsoluteElevation(absoluteElevation);
138
//		gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
139
		gatheringEvent.setActor(collector);
140
		gatheringEvent.setCollectingMethod(collectingMethod);
141
		gatheringEvent.setDistanceToGround(distanceToGround);
142
		gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
143
		gatheringEvent.setExactLocation(exactLocation);
144
		gatheringEvent.setDescription(gatheringEventDescription);
145

    
146
		gatheringEvent.setTimeperiod(gatheringPeriod);
147
		gatheringEvent.setLocality(locality);
148
		gatheringEvent.setCountry(country);
149

    
150
		fieldUnit.setFieldNumber(fieldNumber);
151
		fieldUnit.setFieldNotes(fieldNotes);
152
		fieldUnit.setIndividualCount(individualCount);
153
		fieldUnit.setSex(sex);
154
		fieldUnit.setLifeStage(lifeStage);
155
		primaryCollector = Person.NewTitledInstance("Kilian");
156
		collector.addTeamMember(primaryCollector);
157
		Person secondCollector = Person.NewInstance();
158
		secondCollector.setGivenName("Andreas");
159
		secondCollector.setFamilyName("Muller");
160
		collector.addTeamMember(secondCollector);
161
		Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
162
		collector.addTeamMember(thirdCollector);
163
		fieldUnit.setPrimaryCollector(primaryCollector);
164

    
165
		specimen.setAccessionNumber(accessionNumber);
166
		specimen.setCatalogNumber(catalogNumber);
167
		specimen.setStoredUnder(taxonName);
168
		specimen.setCollection(collection);
169
		specimen.setPreservation(preservationMethod);
170
		specimen.setExsiccatum(exsiccatum);
171

    
172
		//existing specimen with 2 derivation events in line
173
		collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
174
		DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
175
		firstFieldObject = FieldUnit.NewInstance();
176

    
177
		//TODO maybe we should define concrete event types here
178
		DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
179
		DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
180
		firstDerivationEvent = DerivationEvent.NewInstance(null);
181

    
182
		collectionSpecimen.setDerivedFrom(lastDerivationEvent);
183

    
184
		lastDerivationEvent.addOriginal(middleSpecimen);
185
		middleSpecimen.setDerivedFrom(firstDerivationEvent);
186
		firstDerivationEvent.addOriginal(firstFieldObject);
187
		existingGatheringEvent = GatheringEvent.NewInstance();
188
		firstFieldObject.setGatheringEvent(existingGatheringEvent);
189

    
190
	}
191
	/**
192
	 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
193
	 */
194
	@Test
195
	public void testGetSpecimenTitleCache() {
196
//		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.";
197
		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).";
198

    
199
//		DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
200
//		SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
201
//		fieldUnitDescription.addElement(ecologyItem);
202
////		specimenFacade.setEcology(ecology);
203
//		DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
204
//		fieldUnitDescription.addElement(plantDescItem);
205
////		specimenFacade.setPlantDescription(plantDescription);
206

    
207
		collection.setCode("B");
208
		Assert.assertEquals(correctCache, specimen.getTitleCache());
209

    
210

    
211

    
212
	}
213

    
214
	   /**
215
     * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
216
     */
217
    @Test
218
    public void testGetFieldUnitTitleCache() {
219
//        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.";
220
        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.";
221

    
222
//        DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
223
//        SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
224
//        fieldUnitDescription.addElement(ecologyItem);
225
//        DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
226
//        fieldUnitDescription.addElement(plantDescItem);
227

    
228
        collection.setCode("B");  //no effect
229
        Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
230
    }
231

    
232
    //Test if even a hibernate proxy (javassist) class correctly loads the DerivedUnitCacheStrategy.
233
    @Test
234
    @DataSet
235
    public void testPersistedDerivedUnit(){
236
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
237

    
238
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
239
        SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
240
        Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
241
        DerivedUnit myUnit = CdmBase.deproxy(specimen, DerivedUnit.class);
242
        myUnit.setTitleCache(null);
243
        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).";
244
        Assert.assertEquals(expectedDerivedUnitCache, myUnit.getTitleCache());
245
    }
246

    
247
    @Test
248
    @DataSet
249
    public void testPersistedFieldUnit(){
250
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
251
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5001);
252
        SpecimenOrObservationBase<?> fieldUnit = indivAssoc.getAssociatedSpecimenOrObservation();
253
        Assert.assertTrue("FieldUnit should be proxy otherwise the test does not test what it should", fieldUnit instanceof HibernateProxy);
254
        FieldUnit myFieldUnit = CdmBase.deproxy(fieldUnit, FieldUnit.class);
255
        myFieldUnit.setTitleCache(null);
256
        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.";
257
        Assert.assertEquals(expectedFieldUnitCache, myFieldUnit.getTitleCache());
258
    }
259

    
260

    
261

    
262
    /**
263
     * @param taxon
264
     * @param i
265
     * @return
266
     */
267
    private IndividualsAssociation getDescriptionElement(Taxon taxon, int id) {
268
        for (DescriptionElementBase el : taxon.getDescriptions().iterator().next().getElements()){
269
            if (el.getId() == id) {
270
                return (IndividualsAssociation)el;
271
            }
272
        }
273
        return null;
274
    }
275

    
276
    @Override
277
    @Test
278
    @Ignore
279
    public void createTestDataSet() throws FileNotFoundException {
280
//        specimen.setUuid(derivedUnitUuid);
281
//        fieldUnit.setUuid(fieldUnitUuid);
282
//        Taxon taxon = Taxon.NewInstance(null, null);
283
//        taxon.setUuid(taxonUuid);
284
//        TaxonDescription desc = TaxonDescription.NewInstance(taxon);
285
//
286
//        IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
287
//        desc.addElement(indAssoc);
288
//
289
//        this.taxonService.saveOrUpdate(taxon);
290
//
291
//        setComplete();
292
//        endTransaction();
293
//
294
//        writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
295
//                "DerivationEvent", "DescriptionBase","DescriptionElementBase",
296
//                "GatheringEvent","AgentBase","LanguageString","TaxonName",
297
//                "TaxonBase","Collection",
298
//                "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});
299
    }
300

    
301
}
(1-1/4)