Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
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.
9
*/
10
package eu.etaxonomy.cdm.api.facade;
11

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

    
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.BeforeClass;
20
import org.junit.Ignore;
21
import org.junit.Test;
22
import org.unitils.dbunit.annotation.DataSet;
23
import org.unitils.spring.annotation.SpringBeanByType;
24

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

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

    
71
    @SpringBeanByType
72
    private IOccurrenceService occService;
73
    @SpringBeanByType
74
    private ITaxonService taxonService;
75
    @SpringBeanByType
76
    private IDescriptionService descService;
77

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

    
80

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

    
98
    private final String fieldNumber = "5678";
99
    private final String fieldNotes = "such a beautiful specimen";
100
    private Person primaryCollector;
101

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

    
108
    private final String exsiccatum = "Greuter, Pl. Dahlem. 456";
109
    private final String accessionNumber = "8909756";
110
    private final String catalogNumber = "UU879873590";
111
    private final TaxonNameBase<?,?> taxonName = BotanicalName.NewInstance(Rank.GENUS(), "Abies", null, null, null, null, null, null, null);
112
    private final String collectorsNumber = "234589913A34";
113
    private final Collection collection = Collection.NewInstance();
114

    
115
    private final PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
116

    
117
    private DerivedUnit collectionSpecimen;
118
    private GatheringEvent existingGatheringEvent;
119
    private DerivationEvent firstDerivationEvent;
120
    private FieldUnit firstFieldObject;
121
    private final Media media1 = Media.NewInstance();
122

    
123

    
124
//****************************** SET UP *****************************************/
125

    
126
     /**
127
      * @throws java.lang.Exception
128
      */
129
     @BeforeClass
130
     public static void setUpBeforeClass() throws Exception {
131
         // FIXME maybe this will cause problems in other tests
132
         // INDEED !!!! it causes problems thus this is replaced by making this test a  CdmIntegrationTest !!!
133
         new DefaultTermInitializer().initialize();
134
     }
135

    
136
	/**
137
	 * @throws java.lang.Exception
138
	 */
139
	@Before
140
	public void setUp() throws Exception {
141
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
142

    
143
		derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
144
		specimen.setDerivedFrom(derivationEvent);
145
		fieldUnit = FieldUnit.NewInstance();
146
		fieldUnit.addDerivationEvent(derivationEvent);
147
		gatheringEvent = GatheringEvent.NewInstance();
148
		fieldUnit.setGatheringEvent(gatheringEvent);
149
		gatheringEvent.setAbsoluteElevation(absoluteElevation);
150
//		gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
151
		gatheringEvent.setActor(collector);
152
		gatheringEvent.setCollectingMethod(collectingMethod);
153
		gatheringEvent.setDistanceToGround(distanceToGround);
154
		gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
155
		gatheringEvent.setExactLocation(exactLocation);
156
		gatheringEvent.setDescription(gatheringEventDescription);
157

    
158
		gatheringEvent.setTimeperiod(gatheringPeriod);
159
		gatheringEvent.setLocality(locality);
160
		gatheringEvent.setCountry(country);
161

    
162
		fieldUnit.setFieldNumber(fieldNumber);
163
		fieldUnit.setFieldNotes(fieldNotes);
164
		fieldUnit.setIndividualCount(individualCount);
165
		fieldUnit.setSex(sex);
166
		fieldUnit.setLifeStage(lifeStage);
167
		primaryCollector = Person.NewTitledInstance("Kilian");
168
		collector.addTeamMember(primaryCollector);
169
		Person secondCollector = Person.NewInstance();
170
		secondCollector.setFirstname("Andreas");
171
		secondCollector.setLastname("Muller");
172
		collector.addTeamMember(secondCollector);
173
		Person thirdCollector = Person.NewTitledInstance("Kohlbecker");
174
		collector.addTeamMember(thirdCollector);
175
		fieldUnit.setPrimaryCollector(primaryCollector);
176

    
177
		specimen.setAccessionNumber(accessionNumber);
178
		specimen.setCatalogNumber(catalogNumber);
179
		specimen.setStoredUnder(taxonName);
180
		specimen.setCollection(collection);
181
		specimen.setPreservation(preservationMethod);
182
		specimen.setExsiccatum(exsiccatum);
183

    
184
		//existing specimen with 2 derivation events in line
185
		collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
186
		DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
187
		firstFieldObject = FieldUnit.NewInstance();
188

    
189
		//TODO maybe we should define concrete event types here
190
		DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
191
		DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
192
		firstDerivationEvent = DerivationEvent.NewInstance(null);
193

    
194
		collectionSpecimen.setDerivedFrom(lastDerivationEvent);
195

    
196
		lastDerivationEvent.addOriginal(middleSpecimen);
197
		middleSpecimen.setDerivedFrom(firstDerivationEvent);
198
		firstDerivationEvent.addOriginal(firstFieldObject);
199
		existingGatheringEvent = GatheringEvent.NewInstance();
200
		firstFieldObject.setGatheringEvent(existingGatheringEvent);
201

    
202
	}
203
	/**
204
	 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
205
	 */
206
	@Test
207
	public void testGetSpecimenTitleCache() {
208
//		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.";
209
		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).";
210

    
211
//		DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
212
//		SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
213
//		fieldUnitDescription.addElement(ecologyItem);
214
////		specimenFacade.setEcology(ecology);
215
//		DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
216
//		fieldUnitDescription.addElement(plantDescItem);
217
////		specimenFacade.setPlantDescription(plantDescription);
218

    
219
		collection.setCode("B");
220
		Assert.assertEquals(correctCache, specimen.getTitleCache());
221

    
222

    
223

    
224
	}
225

    
226
	   /**
227
     * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
228
     */
229
    @Test
230
    public void testGetFieldUnitTitleCache() {
231
//        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.";
232
        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.";
233

    
234
//        DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
235
//        SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
236
//        fieldUnitDescription.addElement(ecologyItem);
237
//        DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
238
//        fieldUnitDescription.addElement(plantDescItem);
239

    
240
        collection.setCode("B");  //no effect
241
        Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
242
    }
243

    
244
    //Test if even a hibernate proxy (javassist) class correctly loads the DerivedUnitCacheStrategy.
245
    @Test
246
    @DataSet
247
    public void testPersistedDerivedUnit(){
248
        Taxon taxon = (Taxon)this.taxonService.find(taxonUuid);
249

    
250
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
251
        SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
252
        Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
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, specimen.getTitleCache());
255
    }
256

    
257
    @Test
258
    @DataSet
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, fieldUnit.getTitleCache());
268
    }
269

    
270

    
271

    
272
    /**
273
     * @param taxon
274
     * @param i
275
     * @return
276
     */
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;
281
            }
282
        }
283
        return null;
284
    }
285

    
286
    @Override
287
    @Test
288
    @Ignore
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);
295
//
296
//        IndividualsAssociation indAssoc = IndividualsAssociation.NewInstance(specimen);
297
//        desc.addElement(indAssoc);
298
//
299
//        this.taxonService.saveOrUpdate(taxon);
300
//
301
//        setComplete();
302
//        endTransaction();
303
//
304
//        writeDbUnitDataSetFile(new String[]{"SpecimenOrObservationBase",
305
//                "DerivationEvent", "DescriptionBase","DescriptionElementBase",
306
//                "GatheringEvent","AgentBase","LanguageString","TaxonNameBase",
307
//                "TaxonBase","Collection",
308
//                "MaterialOrMethodEvent","SpecimenOrObservationBase_DerivationEvent"});
309
    }
310

    
311
}
(1-1/4)