Project

General

Profile

Download (14 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.TaxonNameBase;
42
import eu.etaxonomy.cdm.model.occurrence.Collection;
43
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
44
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
45
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
46
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
47
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
48
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
49
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
50
import eu.etaxonomy.cdm.model.taxon.Taxon;
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
 * @date 03.06.2010
61
 *
62
 */
63
public class DerivedUnitFacadeCacheStrategyInjectionTest extends CdmTransactionalIntegrationTest {
64
	@SuppressWarnings("unused")
65
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeCacheStrategyInjectionTest.class);
66

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

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

    
76

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

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

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

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

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

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

    
119

    
120
//****************************** SET UP *****************************************/
121

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

    
132
	/**
133
	 * @throws java.lang.Exception
134
	 */
135
	@Before
136
	public void setUp() throws Exception {
137
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
138

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

    
154
		gatheringEvent.setTimeperiod(gatheringPeriod);
155
		gatheringEvent.setLocality(locality);
156
		gatheringEvent.setCountry(country);
157

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

    
173
		specimen.setAccessionNumber(accessionNumber);
174
		specimen.setCatalogNumber(catalogNumber);
175
		specimen.setStoredUnder(taxonName);
176
		specimen.setCollection(collection);
177
		specimen.setPreservation(preservationMethod);
178
		specimen.setExsiccatum(exsiccatum);
179

    
180
		//existing specimen with 2 derivation events in line
181
		collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
182
		DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
183
		firstFieldObject = FieldUnit.NewInstance();
184

    
185
		//TODO maybe we should define concrete event types here
186
		DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(null);
187
		DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(null);
188
		firstDerivationEvent = DerivationEvent.NewInstance(null);
189

    
190
		collectionSpecimen.setDerivedFrom(lastDerivationEvent);
191

    
192
		lastDerivationEvent.addOriginal(middleSpecimen);
193
		middleSpecimen.setDerivedFrom(firstDerivationEvent);
194
		firstDerivationEvent.addOriginal(firstFieldObject);
195
		existingGatheringEvent = GatheringEvent.NewInstance();
196
		firstFieldObject.setGatheringEvent(existingGatheringEvent);
197

    
198
	}
199
	/**
200
	 * Test method for {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.occurrence.Specimen)}.
201
	 */
202
	@Test
203
	public void testGetSpecimenTitleCache() {
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; Greuter, Pl. Dahlem. 456 (B 8909756); 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.5.2005, Kilian 5678, A. Muller & Kohlbecker; Greuter, Pl. Dahlem. 456 (B 8909756).";
206

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

    
215
		collection.setCode("B");
216
		Assert.assertEquals(correctCache, specimen.getTitleCache());
217

    
218

    
219

    
220
	}
221

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

    
230
//        DescriptionElementBase ecologyItem = TextData.NewInstance(Feature.ECOLOGY(), ecology, Language.DEFAULT(), null);
231
//        SpecimenDescription fieldUnitDescription = SpecimenDescription.NewInstance(fieldUnit);
232
//        fieldUnitDescription.addElement(ecologyItem);
233
//        DescriptionElementBase plantDescItem = TextData.NewInstance(Feature.DESCRIPTION(), plantDescription, Language.DEFAULT(), null);
234
//        fieldUnitDescription.addElement(plantDescItem);
235

    
236
        collection.setCode("B");  //no effect
237
        Assert.assertEquals(correctCache, fieldUnit.getTitleCache());
238
    }
239

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

    
246
        IndividualsAssociation indivAssoc = getDescriptionElement(taxon, 5000);
247
        SpecimenOrObservationBase<?> specimen = indivAssoc.getAssociatedSpecimenOrObservation();
248
        Assert.assertTrue("Specimen should be proxy otherwise the test does not test what it should", specimen instanceof HibernateProxy);
249
        DerivedUnit myUnit = CdmBase.deproxy(specimen, DerivedUnit.class);
250
        myUnit.setTitleCache(null);
251
        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).";
252
        Assert.assertEquals(expectedDerivedUnitCache, myUnit.getTitleCache());
253
    }
254

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

    
268

    
269

    
270
    /**
271
     * @param taxon
272
     * @param i
273
     * @return
274
     */
275
    private IndividualsAssociation getDescriptionElement(Taxon taxon, int id) {
276
        for (DescriptionElementBase el : taxon.getDescriptions().iterator().next().getElements()){
277
            if (el.getId() == id) {
278
                return (IndividualsAssociation)el;
279
            }
280
        }
281
        return null;
282
    }
283

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

    
309
}
(1-1/4)