Project

General

Profile

Download (81.5 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 static org.junit.Assert.assertEquals;
12
import static org.junit.Assert.assertFalse;
13
import static org.junit.Assert.assertNotNull;
14
import static org.junit.Assert.assertNotSame;
15
import static org.junit.Assert.assertNull;
16
import static org.junit.Assert.assertSame;
17
import static org.junit.Assert.assertTrue;
18
import static org.junit.Assert.fail;
19

    
20
import java.io.FileNotFoundException;
21
import java.lang.reflect.Field;
22
import java.net.URI;
23
import java.text.ParseException;
24
import java.util.HashSet;
25
import java.util.List;
26
import java.util.UUID;
27

    
28
import org.apache.log4j.Logger;
29
import org.junit.Before;
30
import org.junit.Ignore;
31
import org.junit.Test;
32
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
33
import org.springframework.security.core.context.SecurityContextHolder;
34
import org.unitils.dbunit.annotation.DataSet;
35
import org.unitils.dbunit.annotation.DataSets;
36
import org.unitils.dbunit.annotation.ExpectedDataSet;
37
import org.unitils.spring.annotation.SpringBeanByType;
38

    
39
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
40
import eu.etaxonomy.cdm.api.service.ITermService;
41
import eu.etaxonomy.cdm.api.service.IUserService;
42
import eu.etaxonomy.cdm.common.UTF8;
43
import eu.etaxonomy.cdm.model.agent.AgentBase;
44
import eu.etaxonomy.cdm.model.agent.Team;
45
import eu.etaxonomy.cdm.model.common.DefinedTerm;
46
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
47
import eu.etaxonomy.cdm.model.common.Language;
48
import eu.etaxonomy.cdm.model.common.LanguageString;
49
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
50
import eu.etaxonomy.cdm.model.common.TimePeriod;
51
import eu.etaxonomy.cdm.model.common.User;
52
import eu.etaxonomy.cdm.model.description.Feature;
53
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
54
import eu.etaxonomy.cdm.model.description.TextData;
55
import eu.etaxonomy.cdm.model.location.Country;
56
import eu.etaxonomy.cdm.model.location.NamedArea;
57
import eu.etaxonomy.cdm.model.location.Point;
58
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
59
import eu.etaxonomy.cdm.model.media.Media;
60
import eu.etaxonomy.cdm.model.name.Rank;
61
import eu.etaxonomy.cdm.model.name.TaxonName;
62
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
63
import eu.etaxonomy.cdm.model.occurrence.Collection;
64
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
65
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
66
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
67
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
68
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
69
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
70
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
71
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
72
import eu.etaxonomy.cdm.model.reference.Reference;
73
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
74
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
75
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
76

    
77
/**
78
 * Test class for {@link DerivedUnitFacade}
79
 * @author a.mueller
80
 \* @since 17.05.2010
81
 */
82

    
83
public class DerivedUnitFacadeTest extends CdmTransactionalIntegrationTest {
84
    @SuppressWarnings("unused")
85
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeTest.class);
86

    
87

    
88
    @SpringBeanByType
89
    private IOccurrenceService service;
90

    
91
    @SpringBeanByType
92
    private ITermService termService;
93

    
94
    @SpringBeanByType
95
    private IUserService userService;
96

    
97

    
98
    DerivedUnit specimen;
99
    DerivationEvent derivationEvent;
100
    FieldUnit fieldUnit;
101
    GatheringEvent gatheringEvent;
102
    Integer absoluteElevation = 10;
103
    Integer absoluteElevationMaximum = 14;
104
    AgentBase<?> collector = Team.NewInstance();
105
    String collectingMethod = "Collection Method";
106
    Double distanceToGround = 22.0;
107
    Double distanceToSurface = 0.3;
108
    Double distanceToSurfaceMax = 0.7;
109

    
110
    ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
111
    Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
112
    String gatheringEventDescription = "A nice gathering description";
113
    TimePeriod gatheringPeriod = TimePeriod.NewInstance(1888, 1889);
114

    
115
    String fieldNumber = "15p23B";
116
    String fieldNotes = "such a beautiful specimen";
117

    
118
    Integer individualCount = 1;
119
    DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
120
    DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
121
    LanguageString locality = LanguageString.NewInstance("My locality",
122
            Language.DEFAULT());
123
    DefinedTerm kindOfUnit = DefinedTerm.NewKindOfUnitInstance("Test kind of unit for field unit", "Test kind of unit", "Tkou");
124

    
125

    
126
    String accessionNumber = "888462535";
127
    String catalogNumber = "UU879873590";
128
    TaxonName taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies",
129
            null, null, null, null, null, null, null);
130
    String collectorsNumber = "234589913A34";
131
    Collection collection = Collection.NewInstance();
132
    PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
133
    String originalLabelInfo = "original label info";
134
    URI stableUri = URI.create("http://www.abc.de");
135

    
136

    
137
    DerivedUnitFacade specimenFacade;
138

    
139
    DerivedUnit collectionSpecimen;
140
    GatheringEvent existingGatheringEvent;
141
    DerivationEvent firstDerivationEvent;
142
    FieldUnit firstFieldObject;
143
    Media media1 = Media.NewInstance();
144

    
145
    DerivedUnitFacade emptyFacade;
146

    
147
    // ****************************** SET UP **********************************/
148

    
149

    
150
    /**
151
     * @throws java.lang.Exception
152
     */
153
    @Before
154
    public void setUp() throws Exception {
155

    
156
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
157

    
158
        derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
159
        specimen.setDerivedFrom(derivationEvent);
160
        fieldUnit = FieldUnit.NewInstance();
161
        fieldUnit.addDerivationEvent(derivationEvent);
162
        gatheringEvent = GatheringEvent.NewInstance();
163
        fieldUnit.setGatheringEvent(gatheringEvent);
164
        gatheringEvent.setAbsoluteElevation(absoluteElevation);
165
        gatheringEvent.setAbsoluteElevationMax(absoluteElevationMaximum);
166
        gatheringEvent.setActor(collector);
167
        gatheringEvent.setCollectingMethod(collectingMethod);
168
        gatheringEvent.setDistanceToGround(distanceToGround);
169
        gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
170
        gatheringEvent.setDistanceToWaterSurfaceMax(distanceToSurfaceMax);
171
        gatheringEvent.setExactLocation(exactLocation);
172
        gatheringEvent.setDescription(gatheringEventDescription);
173
        gatheringEvent.setCountry(Country.GERMANY());
174

    
175
        gatheringEvent.setTimeperiod(gatheringPeriod);
176
        gatheringEvent.setLocality(locality);
177

    
178
        fieldUnit.setFieldNumber(fieldNumber);
179
        fieldUnit.setFieldNotes(fieldNotes);
180
        fieldUnit.setIndividualCount(individualCount);
181
        fieldUnit.setSex(sex);
182
        fieldUnit.setLifeStage(lifeStage);
183
        fieldUnit.setKindOfUnit(kindOfUnit);
184

    
185
        specimen.setAccessionNumber(accessionNumber);
186
        specimen.setCatalogNumber(catalogNumber);
187
        specimen.setStoredUnder(taxonName);
188
        specimen.setCollection(collection);
189
        specimen.setPreservation(preservationMethod);
190
        specimen.setOriginalLabelInfo(originalLabelInfo);
191
        specimen.setPreferredStableUri(stableUri);
192

    
193
        specimenFacade = DerivedUnitFacade.NewInstance(specimen);
194

    
195
        // existing specimen with 2 derivation events in line
196
        collectionSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
197
        DerivedUnit middleSpecimen = DerivedUnit.NewPreservedSpecimenInstance();
198
        firstFieldObject = FieldUnit.NewInstance();
199

    
200
		//TODO maybe we should define concrete event types here
201
        DerivationEventType eventType = DerivationEventType.ACCESSIONING();
202
        DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance(eventType);
203
        DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance(eventType);
204
        firstDerivationEvent = DerivationEvent.NewInstance(eventType);
205

    
206
        collectionSpecimen.setDerivedFrom(lastDerivationEvent);
207

    
208
        lastDerivationEvent.addOriginal(middleSpecimen);
209
        middleSpecimen.setDerivedFrom(firstDerivationEvent);
210
        firstDerivationEvent.addOriginal(firstFieldObject);
211
        existingGatheringEvent = GatheringEvent.NewInstance();
212
        firstFieldObject.setGatheringEvent(existingGatheringEvent);
213

    
214
        // empty facade
215
        emptyFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
216

    
217
    }
218

    
219
    // ****************************** TESTS*****************************/
220

    
221
    @Ignore //doesn't run in suite
222
    @Test
223
    @DataSet("DerivedUnitFacadeTest.testSetFieldObjectImageGallery.xml")
224
    @ExpectedDataSet
225
    public void testSetFieldObjectImageGallery() {
226
        UUID imageFeatureUuid = Feature.IMAGE().getUuid();
227
        Feature imageFeature = (Feature) termService.find(imageFeatureUuid);
228

    
229
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
230
        facade.innerDerivedUnit().setUuid(UUID.fromString("77af784f-931b-4857-be9a-48ccf31ed3f1"));
231
        facade.setFieldNumber("12345");
232
        Media media = Media.NewInstance(URI.create("www.abc.de"), 200, null,"jpeg");
233

    
234
        try {
235
            SpecimenDescription imageGallery = SpecimenDescription.NewInstance();
236
            imageGallery.setDescribedSpecimenOrObservation(facade.innerFieldUnit());
237
            imageGallery.setImageGallery(true);
238
            TextData textData = TextData.NewInstance();
239
            textData.setFeature(imageFeature);
240
            imageGallery.addElement(textData);
241
            textData.addMedia(media);
242
            facade.setFieldObjectImageGallery(imageGallery);
243

    
244
        } catch (DerivedUnitFacadeNotSupportedException e1) {
245
            e1.printStackTrace();
246
            fail(e1.getLocalizedMessage());
247
        }
248
        this.service.save(facade.innerDerivedUnit());
249

    
250
         setComplete(); endTransaction();
251
//         try {if (true){printDataSet(System.out, new
252
//         String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
253
//         ,"DERIVATIONEVENT",
254
//         "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA",
255
//         "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
256
//         } catch(Exception e) { logger.warn(e);}
257

    
258
    }
259

    
260
    @Test
261
    @Ignore
262
    // TODO generally works but has id problems when running together with above
263
    // test ()setFieldObjectImageGallery. Therefore set to ignore.
264
    @DataSet("DerivedUnitFacadeTest.testSetDerivedUnitImageGallery.xml")
265
    @ExpectedDataSet
266
    public void testSetDerivedUnitImageGallery() {
267
        // UUID specimenUUID =
268
        // UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
269
        // Specimen specimen = (Specimen)service.find(specimenUUID);
270
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
271
        Media media = Media.NewInstance(URI.create("www.derivedUnitImage.de"),200, null, "png");
272

    
273
        try {
274
            SpecimenDescription imageGallery = SpecimenDescription
275
                    .NewInstance();
276
            imageGallery.setDescribedSpecimenOrObservation(facade.innerDerivedUnit());
277
            imageGallery.setImageGallery(true);
278
            TextData textData = TextData.NewInstance();
279
            imageGallery.addElement(textData);
280
            textData.addMedia(media);
281
            facade.setDerivedUnitImageGallery(imageGallery);
282

    
283
        } catch (DerivedUnitFacadeNotSupportedException e1) {
284
            e1.printStackTrace();
285
            fail(e1.getLocalizedMessage());
286
        }
287
        this.service.save(facade.innerDerivedUnit());
288

    
289
        // setComplete(); endTransaction();
290
        // try {if (true){printDataSet(System.out, new
291
        // String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
292
        // ,"DERIVATIONEVENT",
293
        // "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA",
294
        // "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
295
        // } catch(Exception e) { logger.warn(e);}
296

    
297

    
298
    }
299

    
300
    @Test
301
    @DataSet
302
    @Ignore // TODO generally works has id problems with following tests when running in suite
303
    public void testGetFieldObjectImageGalleryBooleanPersisted() {
304
        UUID specimenUUID = UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
305
        DerivedUnit specimen = (DerivedUnit) service.load(specimenUUID);
306
        assertNotNull("Specimen should exist (persisted)", specimen);
307
        try {
308
            DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
309
            SpecimenDescription imageGallery = facade.getFieldObjectImageGallery(true);
310
            assertNotNull("Image gallery should exist", imageGallery);
311
            assertEquals("UUID should be equal to the persisted uuid",
312
                    UUID.fromString("8cb772e9-1577-45c6-91ab-dbec1413c060"),
313
                    imageGallery.getUuid());
314
            assertEquals("The image gallery should be flagged as such",true, imageGallery.isImageGallery());
315
            assertEquals("There should be one TextData in image gallery", 1,
316
                    imageGallery.getElements().size());
317
            List<Media> media = imageGallery.getElements().iterator().next().getMedia();
318
            assertEquals("There should be 1 media", 1, media.size());
319
        } catch (DerivedUnitFacadeNotSupportedException e) {
320
            e.printStackTrace();
321
            fail();
322
        }
323
    }
324

    
325
    @Test
326
    @DataSet
327
//	@Ignore // TODO generally works has id problems with following tests when running in suite
328
	public void testGetDerivedUnitImageGalleryBooleanPersisted() {
329
		UUID specimenUUID = UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
330
		DerivedUnit specimen = (DerivedUnit) service.load(specimenUUID);
331
		assertNotNull("Specimen should exist (persisted)", specimen);
332
		try {
333
			DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
334
			SpecimenDescription imageGallery = facade.getDerivedUnitImageGallery(true);
335
			assertNotNull("Image gallery should exist", imageGallery);
336
			assertEquals("UUID should be equal to the persisted uuid",
337
					UUID.fromString("cb03acc4-8363-4020-aeef-ea8a8bcc0fe9"),
338
					imageGallery.getUuid());
339
			assertEquals("The image gallery should be flagged as such",
340
					true, imageGallery.isImageGallery());
341
			assertEquals(
342
					"There should be one TextData in image gallery", 1,
343
					imageGallery.getElements().size());
344
			List<Media> media = imageGallery.getElements().iterator().next().getMedia();
345
			assertEquals("There should be 1 media", 1, media.size());
346
		} catch (DerivedUnitFacadeNotSupportedException e) {
347
			e.printStackTrace();
348
			fail();
349
		}
350
	}
351

    
352
	@Test
353
	public void testGetDerivedUnitImageGalleryBoolean() {
354
		DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
355
		try {
356
			DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
357
			SpecimenDescription imageGallery = facade.getDerivedUnitImageGallery(true);
358
			assertNotNull("Image Gallery should have been created",imageGallery);
359
			assertEquals("The image gallery should be flagged as such",true, imageGallery.isImageGallery());
360
		} catch (DerivedUnitFacadeNotSupportedException e) {
361
			e.printStackTrace();
362
			fail();
363
		}
364

    
365
    }
366

    
367
    /**
368
     * Test method for
369
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance()}.
370
     */
371
    @Test
372
    public void testNewInstanceGatheringEventCreated() {
373
        assertNotNull("The specimen should have been created",
374
                specimenFacade.innerDerivedUnit());
375
        // ???
376
         assertNotNull("The derivation event should have been created",
377
        		 specimenFacade.innerDerivedUnit().getDerivedFrom());
378
         assertNotNull("The field unit should have been created",
379
        		 specimenFacade.innerFieldUnit());
380
         assertNotNull("The gathering event should have been created",
381
        		 specimenFacade.innerGatheringEvent());
382
    }
383

    
384

    
385

    
386

    
387
    /**
388
     * Test method for
389
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance(eu.etaxonomy.cdm.model.occurrence.Specimen)}
390
     * .
391
     */
392
    @Test
393
    public void testNewInstanceSpecimen() {
394
        assertSame("Specimen should be same", specimen,
395
                specimenFacade.innerDerivedUnit());
396
        assertSame("Derivation event should be same", derivationEvent,
397
                specimenFacade.innerDerivedUnit().getDerivedFrom());
398
        assertSame("Field unit should be same", fieldUnit,
399
                specimenFacade.innerFieldUnit());
400
        assertSame("Gathering event should be same", gatheringEvent,
401
                specimenFacade.innerGatheringEvent());
402
    }
403

    
404
    @Test
405
    public void testNewInstanceSpecimenWithoutFieldUnit() {
406
        DerivedUnit parent = DerivedUnit.NewPreservedSpecimenInstance();
407
        DerivedUnit child = DerivedUnit.NewPreservedSpecimenInstance();
408
        DerivationEvent.NewSimpleInstance(parent, child, DerivationEventType.ACCESSIONING());
409

    
410
        DerivedUnitFacade facade;
411
		try {
412
			facade = DerivedUnitFacade.NewInstance(child);
413
		       	assertTrue(facade.innerDerivedUnit().getDerivedFrom().getOriginals().size() == 1);
414
		       	assertNull(facade.getFieldUnit(false));
415
		       	DerivationEvent.NewSimpleInstance(FieldUnit.NewInstance(), parent, DerivationEventType.ACCESSIONING());
416

    
417
		} catch (DerivedUnitFacadeNotSupportedException e) {
418
			e.printStackTrace();
419
			fail();
420
		}
421

    
422
    }
423

    
424
    @Test
425
    public void testGatheringEventIsConnectedToDerivedUnit() {
426
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
427
        DerivedUnitFacade specimenFacade;
428
        try {
429
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
430
            specimenFacade.setDistanceToGround(2.0);
431
            FieldUnit specimenFieldUnit = (FieldUnit) specimen.getDerivedFrom().getOriginals().iterator().next();
432
            assertSame(
433
                    "Facade gathering event and specimen gathering event should be the same",
434
                    specimenFacade.innerGatheringEvent(),
435
                    specimenFieldUnit.getGatheringEvent());
436
        } catch (DerivedUnitFacadeNotSupportedException e) {
437
            fail("An error should not occur in NewInstance()");
438
        }
439
    }
440

    
441
    @Test
442
    public void testNoGatheringEventAndFieldUnit() {
443
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
444
        DerivedUnitFacade specimenFacade;
445
        try {
446
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
447
            assertNull("No field unit should exists",
448
                    specimenFacade.innerFieldUnit());
449
        } catch (DerivedUnitFacadeNotSupportedException e) {
450
            fail("An error should not occur in NewInstance()");
451
        }
452
    }
453

    
454
    @Test
455
    public void testInititializeTextDataWithSupportTest() {
456
        // TODO
457
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
458
        DerivedUnitFacade specimenFacade;
459
        try {
460
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
461
            specimenFacade.setEcology("Ecology");
462
            String plantDescription = specimenFacade.getPlantDescription();
463
            assertNull(
464
                    "No plantDescription should exist yet and no NPE should be thrown until now",
465
                    plantDescription);
466
        } catch (DerivedUnitFacadeNotSupportedException e) {
467
            fail("An error should not occur in NewInstance()");
468
        }
469
    }
470

    
471
    @Test
472
    public void testGetSetType() {
473
        assertEquals("Type must be same", SpecimenOrObservationType.PreservedSpecimen, specimenFacade.getType());
474
        SpecimenOrObservationType newType = SpecimenOrObservationType.Fossil;
475
        specimenFacade.setType(newType);
476
        assertEquals("New type must be Fossil", newType, specimenFacade.getType());
477
        assertEquals("DerivedUnit recordBasis must be set to Fossil", newType, specimenFacade.innerDerivedUnit().getRecordBasis());
478

    
479
    }
480

    
481
    @Test
482
    public void testGetSetCountry() {
483
        assertEquals("Country must be same", Country.GERMANY(), specimenFacade.getCountry());
484
        specimenFacade.setCountry(Country.FRANCEFRENCHREPUBLIC());
485
        assertEquals("New country must be France", Country.FRANCEFRENCHREPUBLIC(), specimenFacade.getCountry());
486
    }
487

    
488
    /**
489
     * Test method for
490
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
491
     * .
492
     */
493
    @Test
494
    public void testAddGetRemoveCollectingArea() {
495
        String tdwgLabel = "GER";
496
        NamedArea tdwgArea = termService.getAreaByTdwgAbbreviation(tdwgLabel);
497
        NamedArea newCollectingArea = NamedArea.NewInstance("A nice area",
498
                "nice", "n");
499
        specimenFacade.addCollectingArea(newCollectingArea);
500
        assertEquals("Exactly 1 area must exist", 1, specimenFacade
501
                .getCollectingAreas().size());
502
        assertSame("Areas should be same", newCollectingArea,
503
                specimenFacade.innerFieldUnit().getGatheringEvent()
504
                        .getCollectingAreas().iterator().next());
505
        specimenFacade.addCollectingArea(tdwgArea);
506
        assertEquals("Exactly 2 areas must exist", 2, specimenFacade
507
                .getCollectingAreas().size());
508
        specimenFacade.removeCollectingArea(newCollectingArea);
509
        assertEquals("Exactly 1 area must exist", 1, specimenFacade
510
                .getCollectingAreas().size());
511
        NamedArea remainingArea = specimenFacade.getCollectingAreas()
512
                .iterator().next();
513
        assertEquals("Areas should be same", tdwgArea, remainingArea);
514
        specimenFacade.removeCollectingArea(tdwgArea);
515
        assertEquals("No area should remain", 0, specimenFacade
516
                .getCollectingAreas().size());
517
    }
518

    
519
    /**
520
     * Test method for
521
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
522
     * .
523
     */
524
    @Test
525
    public void testAddCollectingAreas() {
526
        NamedArea firstArea = NamedArea.NewInstance("A nice area", "nice", "n");
527
        assertEquals("No area must exist", 0, specimenFacade.getCollectingAreas().size());
528
        specimenFacade.addCollectingArea(firstArea);
529
        assertEquals("Exactly 1 area must exist", 1, specimenFacade.getCollectingAreas().size());
530

    
531
        String tdwgLabel = "GER";
532
        NamedArea tdwgArea = termService.getAreaByTdwgAbbreviation(tdwgLabel);
533
        NamedArea secondArea = NamedArea.NewInstance("A nice area", "nice", "n");
534

    
535
        java.util.Collection<NamedArea> areaCollection = new HashSet<NamedArea>();
536
        areaCollection.add(secondArea);
537
        areaCollection.add(tdwgArea);
538
        specimenFacade.addCollectingAreas(areaCollection);
539
        assertEquals("Exactly 3 areas must exist", 3, specimenFacade
540
                .getCollectingAreas().size());
541

    
542
    }
543

    
544
    /**
545
     * Test method for getting and setting absolute elevation.
546
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevation()}
547
     * .
548
     */
549
    @Test
550
    public void testGetSetAbsoluteElevation() {
551
        assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
552
        specimenFacade.setAbsoluteElevation(400);
553
        assertEquals("Absolute elevation must be 400",Integer.valueOf(400), specimenFacade.getAbsoluteElevation());
554
    }
555

    
556
    /**
557
     * Test method for
558
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevationError()}
559
     * .
560
     */
561
    @Test
562
    public void testGetSetAbsoluteElevationMaximum() {
563
        assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
564
        assertEquals("Absolute elevation maximum must be same",absoluteElevationMaximum, specimenFacade.getAbsoluteElevationMaximum());
565
        specimenFacade.setAbsoluteElevationMax(4);
566
        assertEquals("Absolute elevation maximum must be 4", Integer.valueOf(4), specimenFacade.getAbsoluteElevationMaximum());
567
        assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
568

    
569
    }
570

    
571
    @Test
572
    public void testGetSetAbsoluteElevationRange() {
573
        assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
574
        assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
575

    
576
        specimenFacade.setAbsoluteElevationRange(30, 36);
577
        assertEquals("", Integer.valueOf(36),specimenFacade.getAbsoluteElevationMaximum());
578
        assertEquals("", Integer.valueOf(30),specimenFacade.getAbsoluteElevation());
579
        assertEquals("", "30" + UTF8.EN_DASH_SPATIUM + "36 m",specimenFacade.absoluteElevationToString());
580
        assertEquals("", null,specimenFacade.getAbsoluteElevationText());
581

    
582
        specimenFacade.setAbsoluteElevationRange(30, 35);
583
        assertEquals("Odd range should not throw an exception anymore",
584
        		String.format("30%s35 m", UTF8.EN_DASH_SPATIUM),specimenFacade.absoluteElevationToString());
585

    
586
        specimenFacade.setAbsoluteElevationRange(41, null);
587
        assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
588
        assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevation());
589
        assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevation());
590
        assertNull("", specimenFacade.getAbsoluteElevationText());
591
        assertEquals("", "41 m",specimenFacade.absoluteElevationToString());
592

    
593
        specimenFacade.setAbsoluteElevationRange(null, null);
594
        assertNull("", specimenFacade.getAbsoluteElevation());
595
        assertNull("", specimenFacade.getAbsoluteElevationMaximum());
596
        assertNull("", specimenFacade.absoluteElevationToString());
597

    
598
    }
599

    
600
    @Test
601
    public void testGetSetAbsoluteElevationText() {
602
        assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
603
        assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
604
        assertEquals("", null,specimenFacade.getAbsoluteElevationText());
605

    
606
        String elevText = "approx. 30 - 35";
607
        specimenFacade.setAbsoluteElevationText(elevText);
608
        assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
609
        assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
610
        assertEquals("", elevText,specimenFacade.absoluteElevationToString());
611

    
612
        specimenFacade.setAbsoluteElevationRange(30, 35);
613
        assertEquals("ToString should not change by setting range if text is set", elevText,specimenFacade.absoluteElevationToString());
614
        assertEquals("", Integer.valueOf(30), specimenFacade.getAbsoluteElevation());
615
        assertEquals("", Integer.valueOf(35),specimenFacade.getAbsoluteElevationMaximum());
616

    
617

    
618
        specimenFacade.setAbsoluteElevationRange(41, null);
619
        assertEquals("ToString should not change by setting range if text is set", elevText,specimenFacade.absoluteElevationToString());
620
        assertEquals("", Integer.valueOf(41), specimenFacade.getAbsoluteElevation());
621
        assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
622

    
623

    
624
        specimenFacade.setAbsoluteElevationText(null);
625
        assertNull("", specimenFacade.getAbsoluteElevationText());
626
        assertEquals("ToString should change by setting text to null", "41 m",specimenFacade.absoluteElevationToString());
627
        assertEquals("", Integer.valueOf(41), specimenFacade.getAbsoluteElevation());
628
        assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
629
    }
630

    
631

    
632
    /**
633
     */
634
    @Test
635
    public void testGetSetCollector() {
636
        assertNotNull("Collector must not be null",
637
                specimenFacade.getCollector());
638
        assertEquals("Collector must be same", collector,
639
                specimenFacade.getCollector());
640
        specimenFacade.setCollector(null);
641
        assertNull("Collector must be null",
642
                specimenFacade.getCollector());
643
    }
644

    
645
    /**
646
     * Test method for
647
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectingMethod()}
648
     * .
649
     */
650
    @Test
651
    public void testGetSetCollectingMethod() {
652
        assertEquals("Collecting method must be same", collectingMethod,
653
                specimenFacade.getCollectingMethod());
654
        specimenFacade.setCollectingMethod("new method");
655
        assertEquals("Collecting method must be 'new method'",
656
                "new method", specimenFacade.getCollectingMethod());
657
    }
658

    
659
    /**
660
     * Test method for
661
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToGround()}
662
     * .
663
     */
664
    @Test
665
    public void testGetSetDistanceToGround() {
666
        assertEquals("Distance to ground must be same",distanceToGround, specimenFacade.getDistanceToGround());
667
        specimenFacade.setDistanceToGround(5.0);
668
        assertEquals("Distance to ground must be 5", Double.valueOf(5), specimenFacade.getDistanceToGround());
669
    }
670

    
671
    /**
672
     * Test method for
673
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToWaterSurface()}
674
     * .
675
     */
676
    @Test
677
    public void testGetDistanceToWaterSurface() {
678
        assertEquals("Distance to surface must be same", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
679
        specimenFacade.setDistanceToWaterSurface(6.0);
680
        assertEquals("Distance to surface must be 6", Double.valueOf(6), specimenFacade.getDistanceToWaterSurface());
681
        // empty facade tests
682
        assertNull("Empty facace must not have any gathering values", emptyFacade.getDistanceToWaterSurface());
683
        emptyFacade.setDistanceToWaterSurface(13.0);
684
        assertNotNull("Field unit must exist if distance to water exists", emptyFacade.getFieldUnit(false));
685
        assertNotNull("Gathering event must exist if distance to water exists", emptyFacade.getGatheringEvent(false));
686
        FieldUnit specimenFieldUnit = (FieldUnit) emptyFacade
687
                .innerDerivedUnit().getDerivedFrom().getOriginals().iterator().next();
688
        assertSame("Gathering event of facade and of specimen must be the same",
689
                specimenFieldUnit.getGatheringEvent(), emptyFacade.getGatheringEvent(false));
690
    }
691

    
692
    @Test
693
    public void testGetSetDistanceToWaterText() {
694
        assertEquals("", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
695
        assertEquals("", distanceToSurfaceMax ,specimenFacade.getDistanceToWaterSurfaceMax());
696
        assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceText());
697

    
698
        String distText = "approx. 0.3 - 0.6";
699
        specimenFacade.setDistanceToWaterSurfaceText(distText);
700
        assertEquals("", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
701
        assertEquals("", distanceToSurfaceMax,specimenFacade.getDistanceToWaterSurfaceMax());
702
        assertEquals("", distText,specimenFacade.distanceToWaterSurfaceToString());
703

    
704
        specimenFacade.setDistanceToWaterSurfaceRange(0.6, 1.4);
705
        assertEquals("ToString should not change by setting range if text is set", distText,specimenFacade.distanceToWaterSurfaceToString());
706
        assertEquals("", Double.valueOf(0.6), specimenFacade.getDistanceToWaterSurface());
707
        assertEquals("", Double.valueOf(1.4),specimenFacade.getDistanceToWaterSurfaceMax());
708

    
709
        specimenFacade.setDistanceToWaterSurfaceRange(41.2, null);
710
        assertEquals("ToString should not change by setting range if text is set", distText,specimenFacade.distanceToWaterSurfaceToString());
711
        assertEquals("", Double.valueOf(41.2), specimenFacade.getDistanceToWaterSurface());
712
        assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceMax());
713

    
714
        specimenFacade.setDistanceToWaterSurfaceText(null);
715
        assertNull("", specimenFacade.getDistanceToWaterSurfaceText());
716
        assertEquals("ToString should change by setting text to null", "41.2 m",specimenFacade.distanceToWaterSurfaceToString());
717
        assertEquals("", Double.valueOf(41.2), specimenFacade.getDistanceToWaterSurface());
718
        assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceMax());
719
    }
720

    
721

    
722
    /**
723
     * Test method for
724
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getExactLocation()}.
725
     */
726
    @Test
727
    public void testGetSetExactLocation() {
728
        assertNotNull("Exact location must not be null",
729
                specimenFacade.getExactLocation());
730
        assertEquals("Exact location must be same", exactLocation,
731
                specimenFacade.getExactLocation());
732
        specimenFacade.setExactLocation(null);
733
        assertNull("Exact location must be null",
734
                specimenFacade.getExactLocation());
735
    }
736

    
737
    @Test
738
    public void testSetExactLocationByParsing() {
739
        Point point1;
740
        try {
741
            specimenFacade.setExactLocationByParsing("112\u00B034'20\"W",
742
                    "34\u00B030,34'N", null, null);
743
            point1 = specimenFacade.getExactLocation();
744
            assertNotNull("", point1.getLatitude());
745
            System.out.println(point1.getLatitude().toString());
746
            assertTrue("",
747
                    point1.getLatitude().toString().startsWith("34.505"));
748
            System.out.println(point1.getLongitude().toString());
749
            assertTrue("",
750
                    point1.getLongitude().toString().startsWith("-112.5722"));
751

    
752
        } catch (ParseException e) {
753
            fail("No parsing error should occur");
754
        }
755
    }
756

    
757
    /**
758
     * Test method for
759
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getGatheringEventDescription()}
760
     * .
761
     */
762
    @Test
763
    public void testGetSetGatheringEventDescription() {
764
        assertEquals("Gathering event description must be same",
765
                gatheringEventDescription,
766
                specimenFacade.getGatheringEventDescription());
767
        specimenFacade.setGatheringEventDescription("new description");
768
        assertEquals(
769
                "Gathering event description must be 'new description' now",
770
                "new description",
771
                specimenFacade.getGatheringEventDescription());
772
    }
773

    
774
    /**
775
     * Test method for
776
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTimeperiod()}.
777
     */
778
    @Test
779
    public void testGetTimeperiod() {
780
        assertNotNull("Gathering period must not be null", specimenFacade.getGatheringPeriod());
781
        assertFalse("Gathering period must not be empty", specimenFacade.getGatheringPeriod().isEmpty());
782
        assertEquals("Gathering period must be same", gatheringPeriod, specimenFacade.getGatheringPeriod());
783
        specimenFacade.setGatheringPeriod(null);
784
        assertTrue("Gathering period must be null", specimenFacade.getGatheringPeriod().isEmpty());
785
    }
786

    
787
    @Test
788
    public void testHasFieldObject() throws SecurityException,
789
            NoSuchFieldException, IllegalArgumentException,
790
            IllegalAccessException {
791
        // this test depends on the current implementation of SpecimenFacade. In
792
        // future
793
        // field unit may not be initialized from the beginning. Than the
794
        // following
795
        // assert should be set to assertNull
796
        assertTrue("field object should not be null (depends on specimen facade initialization !!)",
797
                specimenFacade.hasFieldObject());
798

    
799
        Field fieldUnitField = DerivedUnitFacade.class.getDeclaredField("fieldUnit");
800
        fieldUnitField.setAccessible(true);
801
        fieldUnitField.set(specimenFacade, null);
802
        assertFalse("The field unit should be null now",
803
                specimenFacade.hasFieldObject());
804

    
805
        specimenFacade.setDistanceToGround(33.0);
806
        assertTrue(
807
                "The field unit should have been created again",
808
                specimenFacade.hasFieldObject());
809
    }
810

    
811
    /**
812
     * Test method for
813
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
814
     * .
815
     */
816
    @Test
817
    public void testAddGetRemoveFieldObjectDefinition() {
818
        assertEquals("There should be no definition yet", 0,
819
                specimenFacade.getFieldObjectDefinition().size());
820
        specimenFacade.addFieldObjectDefinition("Tres interesant",
821
                Language.FRENCH());
822
        assertEquals("There should be exactly one definition", 1,
823
                specimenFacade.getFieldObjectDefinition().size());
824
        assertEquals(
825
                "The French definition should be 'Tres interesant'",
826
                "Tres interesant", specimenFacade.getFieldObjectDefinition()
827
                        .get(Language.FRENCH()).getText());
828
        assertEquals(
829
                "The French definition should be 'Tres interesant'",
830
                "Tres interesant",
831
                specimenFacade.getFieldObjectDefinition(Language.FRENCH()));
832
        specimenFacade.addFieldObjectDefinition("Sehr interessant",
833
                Language.GERMAN());
834
        assertEquals("There should be exactly 2 definition", 2,
835
                specimenFacade.getFieldObjectDefinition().size());
836
        specimenFacade.removeFieldObjectDefinition(Language.FRENCH());
837
        assertEquals("There should remain exactly 1 definition", 1,
838
                specimenFacade.getFieldObjectDefinition().size());
839
        assertEquals(
840
                "The remaining German definition should be 'Sehr interessant'",
841
                "Sehr interessant",
842
                specimenFacade.getFieldObjectDefinition(Language.GERMAN()));
843
        specimenFacade.removeFieldObjectDefinition(Language.GERMAN());
844
        assertEquals("There should remain no definition", 0,
845
                specimenFacade.getFieldObjectDefinition().size());
846
    }
847

    
848
    /**
849
     * Test method for
850
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
851
     * .
852
     */
853
    @Test
854
    public void testAddGetHasRemoveFieldObjectMedia() {
855
        assertFalse("There should be no image gallery yet",
856
                specimenFacade.hasFieldObjectImageGallery());
857
        assertFalse("There should be no specimen image gallery either",
858
                specimenFacade.hasDerivedUnitImageGallery());
859

    
860
        List<Media> media = specimenFacade.getFieldObjectMedia();
861
        assertFalse("There should still not be an image gallery now",
862
                specimenFacade.hasFieldObjectImageGallery());
863
        assertEquals("There should be no media yet in the gallery", 0,
864
                media.size());
865

    
866
        Media media1 = Media.NewInstance();
867
        specimenFacade.addFieldObjectMedia(media1);
868
        assertEquals("There should be exactly one specimen media", 1,
869
                specimenFacade.getFieldObjectMedia().size());
870
        assertEquals("The only media should be media 1", media1,
871
                specimenFacade.getFieldObjectMedia().get(0));
872
        assertFalse(
873
                "There should still no specimen image gallery exist",
874
                specimenFacade.hasDerivedUnitImageGallery());
875

    
876
        Media media2 = Media.NewInstance();
877
        specimenFacade.addFieldObjectMedia(media2);
878
        assertEquals("There should be exactly 2 specimen media", 2,
879
                specimenFacade.getFieldObjectMedia().size());
880
        assertEquals("The first media should be media1", media1,
881
                specimenFacade.getFieldObjectMedia().get(0));
882
        assertEquals("The second media should be media2", media2,
883
                specimenFacade.getFieldObjectMedia().get(1));
884

    
885
        specimenFacade.removeFieldObjectMedia(media1);
886
        assertEquals("There should be exactly one specimen media", 1,
887
                specimenFacade.getFieldObjectMedia().size());
888
        assertEquals("The only media should be media2", media2,
889
                specimenFacade.getFieldObjectMedia().get(0));
890

    
891
        specimenFacade.removeFieldObjectMedia(media1);
892
        assertEquals("There should still be exactly one specimen media",
893
                1, specimenFacade.getFieldObjectMedia().size());
894

    
895
        specimenFacade.removeFieldObjectMedia(media2);
896
        assertEquals("There should remain no media in the gallery", 0,
897
                media.size());
898

    
899
    }
900

    
901
    /**
902
     * Test method for
903
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getEcology()}
904
     * .
905
     */
906
    @Test
907
    public void testGetSetEcology() {
908
        assertNotNull(
909
                "An empty ecology data should be created when calling getEcology()",
910
                specimenFacade.getEcologyAll());
911
        assertEquals(
912
                "An empty ecology data should be created when calling getEcology()",
913
                0, specimenFacade.getEcologyAll().size());
914
        specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
915
        assertEquals("Ecology data should exist for 1 language", 1,
916
                specimenFacade.getEcologyAll().size());
917
        assertEquals(
918
                "Ecology data should be 'Tres jolie ici' for French",
919
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
920
        assertNull(
921
                "Ecology data should be null for the default language",
922
                specimenFacade.getEcology());
923
        specimenFacade.setEcology("Nice here");
924
        assertEquals("Ecology data should exist for 2 languages", 2,
925
                specimenFacade.getEcologyAll().size());
926
        assertEquals("Ecology data should be 'Tres jolie ici'",
927
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
928
        assertEquals(
929
                "Ecology data should be 'Nice here' for the default language",
930
                "Nice here", specimenFacade.getEcology());
931
        assertEquals("Ecology data should be 'Nice here' for english",
932
                "Nice here", specimenFacade.getEcology());
933

    
934
        specimenFacade.setEcology("Vert et rouge", Language.FRENCH());
935
        assertEquals("Ecology data should exist for 2 languages", 2,
936
                specimenFacade.getEcologyAll().size());
937
        assertEquals("Ecology data should be 'Vert et rouge'",
938
                "Vert et rouge", specimenFacade.getEcology(Language.FRENCH()));
939
        assertEquals(
940
                "Ecology data should be 'Nice here' for the default language",
941
                "Nice here", specimenFacade.getEcology());
942

    
943
        specimenFacade.setEcology(null, Language.FRENCH());
944
        assertEquals("Ecology data should exist for 1 languages", 1,
945
                specimenFacade.getEcologyAll().size());
946
        assertEquals(
947
                "Ecology data should be 'Nice here' for the default language",
948
                "Nice here", specimenFacade.getEcology());
949
        assertNull("Ecology data should be 'null' for French",
950
                specimenFacade.getEcology(Language.FRENCH()));
951

    
952
        specimenFacade.removeEcology(null);
953
        assertEquals("There should be no ecology left", 0,
954
                specimenFacade.getEcologyAll().size());
955
        assertNull("Ecology data should be 'null' for default language",
956
                specimenFacade.getEcology());
957

    
958
    }
959

    
960
    /**
961
     * Test method for
962
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getPlantDescription()}
963
     * .
964
     */
965
    @Test
966
    public void testGetSetPlantDescription() {
967
        assertNotNull(
968
                "An empty plant description data should be created when calling getPlantDescriptionAll()",
969
                specimenFacade.getPlantDescriptionAll());
970
        assertEquals(
971
                "An empty plant description data should be created when calling getPlantDescription()",
972
                0, specimenFacade.getPlantDescriptionAll().size());
973
        specimenFacade.setPlantDescription("bleu", Language.FRENCH());
974
        assertEquals(
975
                "Plant description data should exist for 1 language", 1,
976
                specimenFacade.getPlantDescriptionAll().size());
977
        assertEquals(
978
                "Plant description data should be 'bleu' for French", "bleu",
979
                specimenFacade.getPlantDescription(Language.FRENCH()));
980
        assertNull(
981
                "Plant description data should be null for the default language",
982
                specimenFacade.getPlantDescription());
983
        specimenFacade.setPlantDescription("Nice here");
984
        assertEquals(
985
                "Plant description data should exist for 2 languages", 2,
986
                specimenFacade.getPlantDescriptionAll().size());
987
        assertEquals("Plant description data should be 'bleu'", "bleu",
988
                specimenFacade.getPlantDescription(Language.FRENCH()));
989
        assertEquals(
990
                "Plant description data should be 'Nice here' for the default language",
991
                "Nice here", specimenFacade.getPlantDescription());
992
        assertEquals(
993
                "Plant description data should be 'Nice here' for english",
994
                "Nice here", specimenFacade.getPlantDescription());
995

    
996
        specimenFacade.setPlantDescription("Vert et rouge", Language.FRENCH());
997
        assertEquals(
998
                "Plant description data should exist for 2 languages", 2,
999
                specimenFacade.getPlantDescriptionAll().size());
1000
        assertEquals("Plant description data should be 'Vert et rouge'",
1001
                "Vert et rouge",
1002
                specimenFacade.getPlantDescription(Language.FRENCH()));
1003
        assertEquals(
1004
                "Plant description data should be 'Nice here' for the default language",
1005
                "Nice here", specimenFacade.getPlantDescription());
1006

    
1007
        specimenFacade.setPlantDescription(null, Language.FRENCH());
1008
        assertEquals(
1009
                "Plant description data should exist for 1 languages", 1,
1010
                specimenFacade.getPlantDescriptionAll().size());
1011
        assertEquals(
1012
                "Plant description data should be 'Nice here' for the default language",
1013
                "Nice here", specimenFacade.getPlantDescription());
1014
        assertNull("Plant description data should be 'null' for French",
1015
                specimenFacade.getPlantDescription(Language.FRENCH()));
1016

    
1017
        // test interference with ecology
1018
        specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
1019
        assertEquals("Ecology data should exist for 1 language", 1,
1020
                specimenFacade.getEcologyAll().size());
1021
        assertEquals(
1022
                "Ecology data should be 'Tres jolie ici' for French",
1023
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
1024
        assertNull(
1025
                "Ecology data should be null for the default language",
1026
                specimenFacade.getEcology());
1027

    
1028
        // repeat above test
1029
        assertEquals(
1030
                "Plant description data should exist for 1 languages", 1,
1031
                specimenFacade.getPlantDescriptionAll().size());
1032
        assertEquals(
1033
                "Plant description data should be 'Nice here' for the default language",
1034
                "Nice here", specimenFacade.getPlantDescription());
1035
        assertNull("Plant description data should be 'null' for French",
1036
                specimenFacade.getPlantDescription(Language.FRENCH()));
1037

    
1038
        specimenFacade.removePlantDescription(null);
1039
        assertEquals("There should be no plant description left", 0,
1040
                specimenFacade.getPlantDescriptionAll().size());
1041
        assertNull(
1042
                "Plant description data should be 'null' for default language",
1043
                specimenFacade.getPlantDescription());
1044

    
1045
    }
1046

    
1047
    /**
1048
     * Test method for
1049
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLifeform()}
1050
     * .
1051
     */
1052
    @Test
1053
    public void testGetSetLifeform() {
1054
        Feature lifeform = Feature.LIFEFORM();
1055
        assertNotNull("Lifeform must exist in testdata",lifeform);
1056
        assertNotNull(
1057
                "An empty life-form data should be created when calling getLifeformAll()",
1058
                specimenFacade.getLifeformAll());
1059
        assertEquals(
1060
                "An empty life-form data should be created when calling getLifeform()",
1061
                0, specimenFacade.getLifeformAll().size());
1062
        specimenFacade.setLifeform("grande", Language.FRENCH());
1063
        assertEquals(
1064
                "Life-form data should exist for 1 language", 1,
1065
                specimenFacade.getLifeformAll().size());
1066
        assertEquals(
1067
                "Life-form data should be 'grande' for French", "grande",
1068
                specimenFacade.getLifeform(Language.FRENCH()));
1069
        assertNull(
1070
                "Lifeform data should be null for the default language",
1071
                specimenFacade.getLifeform());
1072
        specimenFacade.setLifeform("Very big");
1073
        assertEquals(
1074
                "Life-form data should exist for 2 languages", 2,
1075
                specimenFacade.getLifeformAll().size());
1076
        assertEquals("French life-form data should be 'grande'", "grande",
1077
                specimenFacade.getLifeform(Language.FRENCH()));
1078
        assertEquals(
1079
                "Default language life-form data should be 'Very big' for the default language",
1080
                "Very big", specimenFacade.getLifeform());
1081
        assertEquals(
1082
                "Life-form data should be 'Very big' for English",
1083
                "Very big", specimenFacade.getLifeform());
1084

    
1085
        specimenFacade.setLifeform("Petite", Language.FRENCH());
1086
        assertEquals(
1087
                "Plant description data should exist for 2 languages", 2,
1088
                specimenFacade.getLifeformAll().size());
1089
        assertEquals("Life-form data should be 'Petite'",
1090
                "Petite",
1091
                specimenFacade.getLifeform(Language.FRENCH()));
1092
        assertEquals(
1093
                "Plant description data should be 'Very big' for the default language",
1094
                "Very big", specimenFacade.getLifeform());
1095

    
1096
        specimenFacade.setLifeform(null, Language.FRENCH());
1097
        assertEquals(
1098
                "Life-form data should exist for 1 languages", 1,
1099
                specimenFacade.getLifeformAll().size());
1100
        assertEquals(
1101
                "Life-form data should be 'Very big' for the default language",
1102
                "Very big", specimenFacade.getLifeform());
1103
        assertNull("Life-form data should be 'null' for French",
1104
                specimenFacade.getLifeform(Language.FRENCH()));
1105

    
1106
        // test interference with ecology
1107
        specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
1108
        assertEquals("Ecology data should exist for 1 language", 1,
1109
                specimenFacade.getEcologyAll().size());
1110
        assertEquals(
1111
                "Ecology data should be 'Tres jolie ici' for French",
1112
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
1113
        assertNull(
1114
                "Ecology data should be null for the default language",
1115
                specimenFacade.getEcology());
1116

    
1117
        // repeat above test
1118
        assertEquals(
1119
                "Life-form data should exist for 1 languages", 1,
1120
                specimenFacade.getLifeformAll().size());
1121
        assertEquals(
1122
                "Lifeform data should be 'Very big' for the default language",
1123
                "Very big", specimenFacade.getLifeform());
1124
        assertNull("Life-form data should be 'null' for French",
1125
                specimenFacade.getLifeform(Language.FRENCH()));
1126

    
1127
        specimenFacade.removeLifeform(null);
1128
        assertEquals("There should be no life-form left", 0,
1129
                specimenFacade.getLifeformAll().size());
1130
        assertNull(
1131
                "Life-form data should be 'null' for default language",
1132
                specimenFacade.getLifeform());
1133
    }
1134

    
1135
    /**
1136
     * Test method for
1137
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNumber()}.
1138
     */
1139
    @Test
1140
    public void testGetSetFieldNumber() {
1141
        assertEquals("Field number must be same", fieldNumber,
1142
                specimenFacade.getFieldNumber());
1143
        specimenFacade.setFieldNumber("564AB");
1144
        assertEquals("New field number must be '564AB'", "564AB",
1145
                specimenFacade.getFieldNumber());
1146
        // empty facade tests
1147
        assertNull("Empty facace must not have any field value",
1148
                emptyFacade.getFieldNumber());
1149
        emptyFacade.setFieldNumber("1256A");
1150
        assertNotNull(
1151
                "Field unit must exist if field number exists",
1152
                emptyFacade.getFieldUnit(false));
1153
        FieldUnit specimenFieldUnit = (FieldUnit) emptyFacade
1154
                .innerDerivedUnit().getDerivedFrom().getOriginals().iterator()
1155
                .next();
1156
        assertSame(
1157
                "Field unit of facade and of specimen must be the same",
1158
                specimenFieldUnit,
1159
                emptyFacade.getFieldUnit(false));
1160
        assertEquals("1256A", emptyFacade.getFieldNumber());
1161
    }
1162

    
1163
    /**
1164
     * Test method for
1165
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNotes()}.
1166
     */
1167
    @Test
1168
    public void testGetSetFieldNotes() {
1169
        assertEquals("Field notes must be same", fieldNotes,
1170
                specimenFacade.getFieldNotes());
1171
        specimenFacade.setFieldNotes("A completely new info");
1172
        assertEquals("New field note must be 'A completely new info'",
1173
                "A completely new info", specimenFacade.getFieldNotes());
1174
    }
1175

    
1176
    /**
1177
     * Test method for
1178
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setGatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent)}
1179
     * .
1180
     */
1181
    @Test
1182
    @Ignore // #######DerivationEvent ---------------------------------------- 1
1183
    public void testSetGatheringEvent() {
1184
        GatheringEvent newGatheringEvent = GatheringEvent.NewInstance();
1185
        newGatheringEvent.setDistanceToGround(43.0);
1186
        assertFalse("The initial distance to ground should not be 43",
1187
                specimenFacade.getDistanceToGround() == 43.0);
1188
        specimenFacade.setGatheringEvent(newGatheringEvent);
1189
        assertTrue("The final distance to ground should be 43",
1190
                specimenFacade.getDistanceToGround() == 43.0);
1191
        assertSame(
1192
                "The new gathering event should be 'newGatheringEvent'",
1193
                newGatheringEvent, specimenFacade.innerGatheringEvent());
1194
    }
1195

    
1196
    /**
1197
     * Test method for
1198
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerGatheringEvent()}
1199
     * .
1200
     */
1201
    @Test
1202
    public void testGetGatheringEvent() {
1203
        assertNotNull("Gathering event must not be null",
1204
                specimenFacade.innerGatheringEvent());
1205
        assertEquals(
1206
                "Gathering event must be field unit's gathering event",
1207
                specimenFacade.innerFieldUnit().getGatheringEvent(),
1208
                specimenFacade.innerGatheringEvent());
1209
    }
1210

    
1211
    /**
1212
     * Test method for
1213
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getIndividualCount()}
1214
     * .
1215
     */
1216
    @Test
1217
    public void testGetSetIndividualCount() {
1218
        assertEquals("Individual count must be same", individualCount,
1219
                specimenFacade.getIndividualCount());
1220
        specimenFacade.setIndividualCount(4);
1221
        assertEquals("New individual count must be '4'",
1222
                Integer.valueOf(4), specimenFacade.getIndividualCount());
1223

    
1224
    }
1225

    
1226
    /**
1227
     * Test method for
1228
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLifeStage()}.
1229
     */
1230
    @Test
1231
    public void testGetSetLifeStage() {
1232
        assertNotNull("Life stage must not be null", specimenFacade.getLifeStage());
1233
        assertEquals("Life stage must be same", lifeStage, specimenFacade.getLifeStage());
1234
        specimenFacade.setLifeStage(null);
1235
        assertNull("Life stage must be null", specimenFacade.getLifeStage());
1236
        //don't create if null
1237
        DerivedUnitFacade facade = emptyFacade();
1238
        emptyFacade().setLifeStage(null);
1239
        assertNull(facade.innerFieldUnit());
1240
    }
1241

    
1242
    /**
1243
     * Test method for
1244
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getSex()}.
1245
     */
1246
    @Test
1247
    public void testGetSetSex() {
1248
        assertNotNull("Sex must not be null", specimenFacade.getSex());
1249
        assertEquals("Sex must be same", sex, specimenFacade.getSex());
1250
        specimenFacade.setSex(null);
1251
        assertNull("Sex must be null", specimenFacade.getSex());
1252
        //don't create if null
1253
        DerivedUnitFacade facade = emptyFacade();
1254
        emptyFacade().setSex(null);
1255
        assertNull(facade.innerFieldUnit());
1256
    }
1257

    
1258

    
1259
    /**
1260
     * Test method for
1261
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getSex()}.
1262
     */
1263
    @Test
1264
    public void testGetSetKindOfUnit() {
1265
        assertNotNull("Kind-of-unit must not be null", specimenFacade.getKindOfUnit());
1266
        assertEquals("Kind-of-unit must be same", kindOfUnit, specimenFacade.getKindOfUnit());
1267
        specimenFacade.setKindOfUnit(null);
1268
        assertNull("Kind-of-unit must be null", specimenFacade.getKindOfUnit());
1269
        DerivedUnitFacade facade = emptyFacade();
1270
        facade.setKindOfUnit(null);
1271
        assertNull(facade.innerFieldUnit());
1272
    }
1273

    
1274
    /**
1275
     * Test method for
1276
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLocality()}.
1277
     */
1278
    @Test
1279
    public void testGetSetLocality() {
1280
        assertEquals("Locality must be same", locality,
1281
                specimenFacade.getLocality());
1282
        specimenFacade.setLocality("A completely new place", Language.FRENCH());
1283
        assertEquals("New locality must be 'A completely new place'",
1284
                "A completely new place", specimenFacade.getLocalityText());
1285
        assertEquals("New locality language must be French",
1286
                Language.FRENCH(), specimenFacade.getLocalityLanguage());
1287
        specimenFacade.setLocality("Another place");
1288
        assertEquals("New locality must be 'Another place'",
1289
                "Another place", specimenFacade.getLocalityText());
1290
        assertEquals("New locality language must be default",
1291
                Language.DEFAULT(), specimenFacade.getLocalityLanguage());
1292
    }
1293

    
1294
    /**
1295
     * Test method for
1296
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
1297
     * .
1298
     */
1299
    @Test
1300
    public void testAddGetRemoveSpecimenDefinition() {
1301
        assertEquals("There should be no definition yet", 0,
1302
                specimenFacade.getDerivedUnitDefinitions().size());
1303
        specimenFacade.addDerivedUnitDefinition("Tres interesant",
1304
                Language.FRENCH());
1305
        assertEquals("There should be exactly one definition", 1,
1306
                specimenFacade.getDerivedUnitDefinitions().size());
1307
        assertEquals(
1308
                "The French definition should be 'Tres interesant'",
1309
                "Tres interesant", specimenFacade.getDerivedUnitDefinitions()
1310
                        .get(Language.FRENCH()).getText());
1311
        assertEquals(
1312
                "The French definition should be 'Tres interesant'",
1313
                "Tres interesant",
1314
                specimenFacade.getDerivedUnitDefinition(Language.FRENCH()));
1315
        specimenFacade.addDerivedUnitDefinition("Sehr interessant",
1316
                Language.GERMAN());
1317
        assertEquals("There should be exactly 2 definition", 2,
1318
                specimenFacade.getDerivedUnitDefinitions().size());
1319
        specimenFacade.removeDerivedUnitDefinition(Language.FRENCH());
1320
		assertEquals("There should remain exactly 1 definition", 1,
1321
                specimenFacade.getDerivedUnitDefinitions().size());
1322
        assertEquals(
1323
                "The remaining German definition should be 'Sehr interessant'",
1324
                "Sehr interessant",
1325
		        specimenFacade.getDerivedUnitDefinition(Language.GERMAN()));
1326
		specimenFacade.removeDerivedUnitDefinition(Language.GERMAN());
1327
		assertEquals("There should remain no definition", 0,
1328
                specimenFacade.getDerivedUnitDefinitions().size());
1329
    }
1330

    
1331
    /**
1332
     * Test method for
1333
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1334
     * .
1335
     */
1336
    @Test
1337
    public void testAddGetRemoveDetermination() {
1338
        assertEquals("There should be no determination yet", 0,
1339
                specimenFacade.getDeterminations().size());
1340
        DeterminationEvent determinationEvent1 = DeterminationEvent
1341
                .NewInstance();
1342
        specimenFacade.addDetermination(determinationEvent1);
1343
        assertEquals("There should be exactly one determination", 1,
1344
                specimenFacade.getDeterminations().size());
1345
        assertEquals("The only determination should be determination 1",
1346
                determinationEvent1, specimenFacade.getDeterminations()
1347
                        .iterator().next());
1348

    
1349
        DeterminationEvent determinationEvent2 = DeterminationEvent
1350
                .NewInstance();
1351
        specimenFacade.addDetermination(determinationEvent2);
1352
        assertEquals("There should be exactly 2 determinations", 2,
1353
                specimenFacade.getDeterminations().size());
1354
        specimenFacade.removeDetermination(determinationEvent1);
1355

    
1356
        assertEquals("There should remain exactly 1 determination", 1,
1357
                specimenFacade.getDeterminations().size());
1358
        assertEquals(
1359
                "The remaining determinations should be determination 2",
1360
                determinationEvent2, specimenFacade.getDeterminations()
1361
                        .iterator().next());
1362

    
1363
        specimenFacade.removeDetermination(determinationEvent1);
1364
        assertEquals("There should remain exactly 1 determination", 1,
1365
                specimenFacade.getDeterminations().size());
1366

    
1367
        specimenFacade.removeDetermination(determinationEvent2);
1368
        assertEquals("There should remain no definition", 0,
1369
                specimenFacade.getDerivedUnitDefinitions().size());
1370

    
1371
    }
1372

    
1373

    
1374
    /**
1375
     * Test method for
1376
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1377
     * .
1378
     */
1379
    @Test
1380
    public void testPreferredOtherDeterminations() {
1381
        assertEquals("There should be no determination yet", 0,
1382
                specimenFacade.getDeterminations().size());
1383
        DeterminationEvent determinationEvent1 = DeterminationEvent.NewInstance();
1384
        specimenFacade.setPreferredDetermination(determinationEvent1);
1385

    
1386
        assertEquals("There should be exactly one determination", 1,
1387
                specimenFacade.getDeterminations().size());
1388
        assertEquals("The only determination should be determination 1",
1389
                determinationEvent1, specimenFacade.getDeterminations()
1390
                        .iterator().next());
1391
        assertEquals("determination 1 should be the preferred determination",
1392
                determinationEvent1, specimenFacade.getPreferredDetermination());
1393
        assertEquals("There should be no 'non preferred' determination", 0,
1394
                specimenFacade.getOtherDeterminations().size());
1395

    
1396

    
1397

    
1398
        DeterminationEvent determinationEvent2 = DeterminationEvent.NewInstance();
1399
        specimenFacade.addDetermination(determinationEvent2);
1400
        assertEquals("There should be exactly 2 determinations", 2,
1401
                specimenFacade.getDeterminations().size());
1402
        assertEquals("determination 1 should be the preferred determination",
1403
                determinationEvent1, specimenFacade.getPreferredDetermination());
1404
        assertEquals("There should be one 'non preferred' determination", 1,
1405
                specimenFacade.getOtherDeterminations().size());
1406
        assertEquals("The only 'non preferred' determination should be determination 2",
1407
                determinationEvent2, specimenFacade.getOtherDeterminations().iterator().next());
1408

    
1409

    
1410
        DeterminationEvent determinationEvent3 = DeterminationEvent.NewInstance();
1411
        specimenFacade.setPreferredDetermination(determinationEvent3);
1412
        assertEquals("There should be exactly 3 determinations", 3,
1413
                specimenFacade.getDeterminations().size());
1414
        assertEquals("determination 3 should be the preferred determination",
1415
                determinationEvent3, specimenFacade.getPreferredDetermination());
1416
        assertEquals("There should be 2 'non preferred' determination", 2,
1417
                specimenFacade.getOtherDeterminations().size());
1418
        assertTrue("determination 1 should be contained in the set of 'non preferred' determinations",
1419
                specimenFacade.getOtherDeterminations().contains(determinationEvent1));
1420
        assertTrue("determination 2 should be contained in the set of 'non preferred' determinations",
1421
                specimenFacade.getOtherDeterminations().contains(determinationEvent2));
1422

    
1423
    }
1424

    
1425
    /**
1426
     * Test method for
1427
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitMedia(eu.etaxonomy.cdm.model.media.Media)}
1428
     * .
1429
     */
1430
    @Test
1431
    public void testAddGetHasRemoveSpecimenMedia() {
1432
        assertFalse("There should be no image gallery yet",
1433
                specimenFacade.hasDerivedUnitImageGallery());
1434
        assertFalse(
1435
                "There should be also no field object image gallery yet",
1436
                specimenFacade.hasFieldObjectImageGallery());
1437

    
1438
        List<Media> media = specimenFacade.getDerivedUnitMedia();
1439
        assertFalse(
1440
                "There should still not be an empty image gallery now",
1441
                specimenFacade.hasDerivedUnitImageGallery());
1442
        assertEquals("There should be no media yet in the gallery", 0,
1443
                media.size());
1444

    
1445
        Media media1 = Media.NewInstance();
1446
        specimenFacade.addDerivedUnitMedia(media1);
1447
		assertEquals("There should be exactly one specimen media", 1,
1448
                specimenFacade.getDerivedUnitMedia().size());
1449
        assertEquals("The only media should be media 1", media1,
1450
                specimenFacade.getDerivedUnitMedia().get(0));
1451
        assertFalse(
1452
                "There should be still no field object image gallery",
1453
                specimenFacade.hasFieldObjectImageGallery());
1454

    
1455
        Media media2 = Media.NewInstance();
1456
        specimenFacade.addDerivedUnitMedia(media2);
1457
		assertEquals("There should be exactly 2 specimen media", 2,
1458
                specimenFacade.getDerivedUnitMedia().size());
1459
        assertEquals("The first media should be media1", media1,
1460
                specimenFacade.getDerivedUnitMedia().get(0));
1461
        assertEquals("The second media should be media2", media2,
1462
                specimenFacade.getDerivedUnitMedia().get(1));
1463

    
1464
        specimenFacade.removeDerivedUnitMedia(media1);
1465
        assertEquals("There should be exactly one specimen media", 1,
1466
                specimenFacade.getDerivedUnitMedia().size());
1467
        assertEquals("The only media should be media2", media2,
1468
                specimenFacade.getDerivedUnitMedia().get(0));
1469

    
1470
        specimenFacade.removeDerivedUnitMedia(media1);
1471
        assertEquals("There should still be exactly one specimen media",
1472
                1, specimenFacade.getDerivedUnitMedia().size());
1473

    
1474
        specimenFacade.removeDerivedUnitMedia(media2);
1475
        assertEquals("There should remain no media in the gallery", 0,
1476
                media.size());
1477
    }
1478

    
1479
    /**
1480
     * Test method for
1481
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAccessionNumber()}
1482
     * .
1483
     */
1484
    @Test
1485
    public void testGetSetAccessionNumber() {
1486
        assertEquals("Accession number must be same", accessionNumber,
1487
                specimenFacade.getAccessionNumber());
1488
        specimenFacade.setAccessionNumber("A12345693");
1489
		assertEquals("New accession number must be 'A12345693'",
1490
                "A12345693", specimenFacade.getAccessionNumber());
1491
    }
1492

    
1493

    
1494
    /**
1495
     * Test method for
1496
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCatalogNumber()}.
1497
     */
1498
    @Test
1499
    public void testGetCatalogNumber() {
1500
        assertEquals("Catalog number must be same", catalogNumber,
1501
                specimenFacade.getCatalogNumber());
1502
        specimenFacade.setCatalogNumber("B12345693");
1503
		assertEquals("New catalog number must be 'B12345693'",
1504
                "B12345693", specimenFacade.getCatalogNumber());
1505
    }
1506

    
1507
    /**
1508
     * Test method for
1509
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getPreservation()}.
1510
     */
1511
    @Test
1512
    public void testGetPreservation() {
1513
        try {
1514
            assertNotNull("Preservation method must not be null",
1515
                    specimenFacade.getPreservationMethod());
1516
            assertEquals("Preservation method must be same",
1517
                    preservationMethod, specimenFacade.getPreservationMethod());
1518
            specimenFacade.setPreservationMethod(null);
1519
            assertNull("Preservation method must be null",
1520
                    specimenFacade.getPreservationMethod());
1521
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1522
            fail("Method not supported should not be thrown for a specimen");
1523
        }
1524
        specimenFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.Observation);
1525
        try {
1526
            specimenFacade.setPreservationMethod(preservationMethod);
1527
            fail("Method not supported should be thrown for an observation on set preservation method");
1528

    
1529
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1530
            // ok
1531
        }
1532
        specimenFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.LivingSpecimen);
1533
        try {
1534
            specimenFacade.getPreservationMethod();
1535
            fail("Method not supported should be thrown for a living being on get preservation method");
1536
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1537
            // ok
1538
        }
1539

    
1540
    }
1541

    
1542
    @Test
1543
    public void testGetSetOriginalLabelInfo() {
1544
        assertEquals("Original label info must be same", originalLabelInfo,
1545
                specimenFacade.getOriginalLabelInfo());
1546
        specimenFacade.setOriginalLabelInfo("OrigLabel Info xxx");
1547
        assertEquals("New accession number must be 'OrigLabel Info xxx'",
1548
                "OrigLabel Info xxx", specimenFacade.getOriginalLabelInfo());
1549
    }
1550

    
1551
    @Test
1552
    public void testGetSetStableUri() {
1553
        assertEquals("Preferred stable URI must be same", stableUri,
1554
                specimenFacade.getPreferredStableUri());
1555
        URI newURI = URI.create("http://www.fgh.ij");
1556
        specimenFacade.setPreferredStableUri(newURI);
1557
        assertEquals("New preferred stable must be '" + newURI.toString() + "'",
1558
                newURI, specimenFacade.getPreferredStableUri());
1559
    }
1560

    
1561

    
1562

    
1563
    /**
1564
     * Test method for
1565
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getStoredUnder()}.
1566
     */
1567
    @Test
1568
    public void testGetStoredUnder() {
1569
        assertNotNull("Stored under name must not be null",
1570
                specimenFacade.getStoredUnder());
1571
        assertEquals("Stored under name must be same", taxonName,
1572
                specimenFacade.getStoredUnder());
1573
        specimenFacade.setStoredUnder(null);
1574
        assertNull("Stored under name must be null",
1575
                specimenFacade.getStoredUnder());
1576
    }
1577

    
1578
//	/**
1579
//	 * Test method for
1580
//	 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectorsNumber()}
1581
//	 * .
1582
//	 */
1583
//	@Test
1584
//	public void testGetSetCollectorsNumber() {
1585
//		assertEquals("Collectors number must be same", collectorsNumber,
1586
//				specimenFacade.getCollectorsNumber());
1587
//		specimenFacade.setCollectorsNumber("C12345693");
1588
//		assertEquals("New collectors number must be 'C12345693'",
1589
//				"C12345693", specimenFacade.getCollectorsNumber());
1590
//	}
1591

    
1592
    /**
1593
     * Test method for
1594
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTitleCache()}.
1595
     */
1596
    @Test
1597
    public void testGetTitleCache() {
1598
        assertNotNull(
1599
                "The title cache should never return null if not protected",
1600
                specimenFacade.getTitleCache());
1601
        specimenFacade.setTitleCache(null, false);
1602
        assertNotNull(
1603
                "The title cache should never return null if not protected",
1604
                specimenFacade.getTitleCache());
1605
    }
1606

    
1607
    /**
1608
     * Test method for
1609
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setTitleCache(java.lang.String)}
1610
     * .
1611
     */
1612
    @Test
1613
    public void testSetTitleCache() {
1614
        String testTitle = "Absdwk aksjlf";
1615
        specimenFacade.setTitleCache(testTitle, true);
1616
        assertEquals(
1617
                "Protected title cache should returns the test title",
1618
                testTitle, specimenFacade.getTitleCache());
1619
        specimenFacade.setTitleCache(testTitle, false);
1620
        assertFalse(
1621
                "Unprotected title cache should not return the test title",
1622
                testTitle.equals(specimenFacade.getTitleCache()));
1623
    }
1624

    
1625
    /**
1626
     * Test method for
1627
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerDerivedUnit()}.
1628
     */
1629
    @Test
1630
    public void testGetSpecimen() {
1631
        assertEquals("Specimen must be same", specimen,
1632
                specimenFacade.innerDerivedUnit());
1633
    }
1634

    
1635
    /**
1636
     * Test method for
1637
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollection()}.
1638
     */
1639
    @Test
1640
    public void testGetSetCollection() {
1641
        assertNotNull("Collection must not be null",
1642
                specimenFacade.getCollection());
1643
        assertEquals("Collection must be same", collection,
1644
                specimenFacade.getCollection());
1645
        specimenFacade.setCollection(null);
1646
        assertNull("Collection must be null",
1647
                specimenFacade.getCollection());
1648
    }
1649

    
1650
    @Test
1651
    public void testAddGetRemoveSource() {
1652
        assertEquals("No sources should exist yet", 0, specimenFacade.getSources().size());
1653

    
1654
        Reference reference = ReferenceFactory.newBook();
1655
        IdentifiableSource source1 = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference, "54", "myName");
1656
        assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1657
        IdentifiableSource source2 = IdentifiableSource.NewDataImportInstance("1", "myTable");
1658
        specimenFacade.addSource(source2);
1659
        assertEquals("One source should exist now", 2, specimenFacade.getSources().size());
1660
        specimenFacade.removeSource(source1);
1661
        assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1662
        Reference reference2 = ReferenceFactory.newJournal();
1663
        IdentifiableSource sourceNotUsed = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference2,null, null);
1664
        specimenFacade.removeSource(sourceNotUsed);
1665
        assertEquals("One source should still exist", 1, specimenFacade.getSources().size());
1666
        assertEquals("1", specimenFacade.getSources().iterator().next().getIdInSource());
1667
        specimenFacade.removeSource(source2);
1668
        assertEquals("No sources should exist anymore", 0,specimenFacade.getSources().size());
1669
    }
1670

    
1671
    @Test
1672
    public void testAddGetRemoveDuplicate() {
1673
        assertEquals("No duplicates should be available yet", 0,specimenFacade.getDuplicates().size());
1674
        DerivedUnit newSpecimen1 = DerivedUnit.NewPreservedSpecimenInstance();
1675
        specimenFacade.addDuplicate(newSpecimen1);
1676
        assertEquals("There should be 1 duplicate now", 1,specimenFacade.getDuplicates().size());
1677
        DerivedUnit newSpecimen2 = DerivedUnit.NewPreservedSpecimenInstance();
1678
        DerivationEvent newDerivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
1679
        newSpecimen2.setDerivedFrom(newDerivationEvent);
1680
        assertSame(
1681
                "The derivation event should be 'newDerivationEvent'",
1682
                newDerivationEvent, newSpecimen2.getDerivedFrom());
1683
       	specimenFacade.addDuplicate(newSpecimen2);
1684
        assertEquals("There should be 2 duplicates now", 2, specimenFacade.getDuplicates().size());
1685
        assertNotSame(
1686
                "The derivation event should not be 'newDerivationEvent' anymore",
1687
                newDerivationEvent, newSpecimen2.getDerivedFrom());
1688
        assertSame(
1689
                "The derivation event should not be the facades derivation event",
1690
                derivationEvent, newSpecimen2.getDerivedFrom());
1691
        specimenFacade.removeDuplicate(newSpecimen1);
1692
        assertEquals("There should be 1 duplicate now", 1,
1693
                specimenFacade.getDuplicates().size());
1694
        assertSame("The only duplicate should be 'newSpecimen2' now",
1695
                newSpecimen2, specimenFacade.getDuplicates().iterator().next());
1696
       	specimenFacade.addDuplicate(specimenFacade.innerDerivedUnit());
1697
        assertEquals(
1698
                "There should be still 1 duplicate because the facade specimen is not a duplicate",
1699
                1, specimenFacade.getDuplicates().size());
1700

    
1701
        Collection newCollection = Collection.NewInstance();
1702
        String catalogNumber = "1234890";
1703
        String accessionNumber = "345345";
1704
        TaxonName storedUnder = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1705
        PreservationMethod method = PreservationMethod.NewInstance();
1706
        DerivedUnit duplicateSpecimen = specimenFacade.addDuplicate(newCollection,
1707
				catalogNumber, accessionNumber, storedUnder,
1708
			    method);
1709
        assertEquals("There should be 2 duplicates now", 2,
1710
                specimenFacade.getDuplicates().size());
1711
        specimenFacade.removeDuplicate(newSpecimen2);
1712
		assertEquals("There should be 1 duplicates now", 1,
1713
                specimenFacade.getDuplicates().size());
1714
        Collection sameCollection = specimenFacade.getDuplicates().iterator().next().getCollection();
1715
        assertSame("Collections should be same", newCollection, sameCollection);
1716
    }
1717

    
1718
    // ************************** Existing Specimen with multiple derivation
1719
    // events in line **************/
1720

    
1721
    @Test
1722
    public void testExistingSpecimen() {
1723
        specimenFacade = null;
1724
        try {
1725
            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1726
        } catch (DerivedUnitFacadeNotSupportedException e) {
1727
            fail("Multiple derivation events in line should not throw a not supported exception");
1728
        }
1729
        assertSame(
1730
                "Gathering event should derive from the derivation line",
1731
                existingGatheringEvent, specimenFacade.innerGatheringEvent());
1732
        assertEquals(
1733
                "Mediasize should be 0. Only Imagegallery media are supported",
1734
                0, specimenFacade.getFieldObjectMedia().size());
1735
    }
1736

    
1737
    @Test
1738
    public void testMultipleFieldUnitNotSupported() {
1739
        specimenFacade = null;
1740
        FieldUnit secondFieldObject = FieldUnit.NewInstance();
1741
        firstDerivationEvent.addOriginal(secondFieldObject);
1742
        try {
1743
            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1744
            fail("Multiple field units for one specimen should no be supported by the facade");
1745
        } catch (DerivedUnitFacadeNotSupportedException e) {
1746
            // ok
1747
        }
1748
        assertNull("Specimen facade should not be initialized",
1749
                specimenFacade);
1750
    }
1751

    
1752
// 	not required anymore #3597
1753
//    @Test // #######DerivationEvent
1754
//    public void testOnlyImageGallerySupported() {
1755
//        specimenFacade = null;
1756
//        firstFieldObject.addMedia(media1);
1757
//        try {
1758
//            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1759
//            fail("Only image galleries are supported by the facade but not direct media");
1760
//        } catch (DerivedUnitFacadeNotSupportedException e) {
1761
//            // ok
1762
//        }
1763
//        assertNull("Specimen facade should not be initialized",
1764
//                specimenFacade);
1765
//    }
1766

    
1767
    @Test // #######DerivationEvent
1768
    public void testEventPropagation() {
1769
        specimenFacade.setDistanceToGround(24.0);
1770

    
1771
    }
1772

    
1773
    // @Ignore // set to ignore because I did not want to check knowingly
1774
    // failing tests. Remove @Ignore when this is fixed
1775
    @Test
1776
    public void testSetBarcode() {
1777
        String barcode = "barcode";
1778
        specimenFacade.setBarcode(barcode);
1779

    
1780
        assertEquals(barcode, specimenFacade.getBarcode());
1781
    }
1782

    
1783
    @Test
1784
    public void testIsEvenDistance() {
1785
        Integer minimum = 20;
1786
        Integer maximum = 1234;
1787

    
1788
        // this should not throw exceptions
1789
        specimenFacade.setAbsoluteElevationRange(minimum, maximum);
1790
    }
1791

    
1792
    /**
1793
     *
1794
     * See https://dev.e-taxonomy.eu/trac/ticket/2426
1795
     * This test doesn't handle the above issue yet as it doesn't fire events as
1796
     * expected (at least it does not reproduce the behaviour in the Taxonomic Editor).
1797
     * In the meanwhile the property change framework for the facade has been changed
1798
     * so the original problem may have disappeared.
1799
     *
1800
     */
1801
    @Test
1802
    @DataSets({
1803
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
1804
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml")
1805
    })
1806
   // @Ignore
1807
    public void testNoRecursiveChangeEvents(){
1808
        String username = "username";
1809
        String password = "password";
1810
        User user = User.NewInstance(username, password);
1811
        userService.save(user);
1812
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
1813
        SecurityContextHolder.getContext().setAuthentication(token);
1814

    
1815
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
1816
        facade.setLocality("testLocality");
1817
        facade.getTitleCache();
1818
	//	facade.innerGatheringEvent().firePropertyChange("createdBy", null, user);
1819
        this.service.save(facade.innerDerivedUnit());
1820
        commitAndStartNewTransaction(null);
1821
    }
1822

    
1823
    @Test
1824
    public void testBaseUnit() throws DerivedUnitFacadeNotSupportedException{
1825
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
1826
        assertEquals("baseUnit is incorrect", specimen, facade.baseUnit());
1827

    
1828
        facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, fieldUnit);
1829
        assertEquals("baseUnit is incorrect", fieldUnit, facade.baseUnit());
1830

    
1831
        facade = DerivedUnitFacade.NewInstance(specimen);
1832
        facade.getFieldUnit(true);
1833
        assertEquals("baseUnit is incorrect", specimen, facade.baseUnit());
1834
    }
1835

    
1836

    
1837
    /**
1838
     * Returns an empty DU facade
1839
     * @return
1840
     */
1841
    private DerivedUnitFacade emptyFacade() {
1842
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.DerivedUnit);
1843
        return facade;
1844
    }
1845

    
1846
    @Override
1847
    public void createTestDataSet() throws FileNotFoundException {
1848
        // TODO Auto-generated method stub
1849

    
1850
    }
1851

    
1852
}
(4-4/4)