Project

General

Profile

Download (77.8 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

    
13
import java.io.FileNotFoundException;
14
import java.lang.reflect.Field;
15
import java.net.URI;
16
import java.text.ParseException;
17
import java.util.HashSet;
18
import java.util.List;
19
import java.util.UUID;
20

    
21
import org.apache.log4j.Logger;
22
import org.junit.Assert;
23
import org.junit.Before;
24
import org.junit.Ignore;
25
import org.junit.Test;
26
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
27
import org.springframework.security.core.context.SecurityContextHolder;
28
import org.unitils.dbunit.annotation.DataSet;
29
import org.unitils.dbunit.annotation.ExpectedDataSet;
30
import org.unitils.spring.annotation.SpringBeanByType;
31

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

    
69
/**
70
 * Test class for {@link DerivedUnitFacade}
71
 * @author a.mueller
72
 * @date 17.05.2010
73
 */
74

    
75
public class DerivedUnitFacadeTest extends CdmTransactionalIntegrationTest {
76
    @SuppressWarnings("unused")
77
	private static final Logger logger = Logger.getLogger(DerivedUnitFacadeTest.class);
78

    
79

    
80
    @SpringBeanByType
81
    private IOccurrenceService service;
82

    
83
    @SpringBeanByType
84
    private ITermService termService;
85

    
86
    @SpringBeanByType
87
    private IUserService userService;
88

    
89

    
90
    DerivedUnit specimen;
91
    DerivationEvent derivationEvent;
92
    FieldUnit fieldUnit;
93
    GatheringEvent gatheringEvent;
94
    Integer absoluteElevation = 10;
95
    Integer absoluteElevationMaximum = 14;
96
    AgentBase<?> collector = Team.NewInstance();
97
    String collectingMethod = "Collection Method";
98
    Double distanceToGround = 22.0;
99
    Double distanceToSurface = 0.3;
100
    Double distanceToSurfaceMax = 0.7;
101

    
102
    ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
103
    Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
104
    String gatheringEventDescription = "A nice gathering description";
105
    TimePeriod gatheringPeriod = TimePeriod.NewInstance(1888, 1889);
106

    
107
    String fieldNumber = "15p23B";
108
    String fieldNotes = "such a beautiful specimen";
109

    
110
    Integer individualCount = 1;
111
    DefinedTerm lifeStage = DefinedTerm.NewStageInstance("A wonderful stage", "stage", "st");
112
    DefinedTerm sex = DefinedTerm.NewSexInstance("FemaleMale", "FM", "FM");
113
    LanguageString locality = LanguageString.NewInstance("My locality",
114
            Language.DEFAULT());
115

    
116
    String accessionNumber = "888462535";
117
    String catalogNumber = "UU879873590";
118
    TaxonNameBase<?,?> taxonName = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS(), "Abies",
119
            null, null, null, null, null, null, null);
120
    String collectorsNumber = "234589913A34";
121
    Collection collection = Collection.NewInstance();
122
    PreservationMethod preservationMethod = PreservationMethod.NewInstance(null, "my prservation");
123
    String originalLabelInfo = "original label info";
124
    URI stableUri = URI.create("http://www.abc.de");
125

    
126

    
127
    DerivedUnitFacade specimenFacade;
128

    
129
    DerivedUnit collectionSpecimen;
130
    GatheringEvent existingGatheringEvent;
131
    DerivationEvent firstDerivationEvent;
132
    FieldUnit firstFieldObject;
133
    Media media1 = Media.NewInstance();
134

    
135
    DerivedUnitFacade emptyFacade;
136

    
137
    // ****************************** SET UP **********************************/
138

    
139

    
140
    /**
141
     * @throws java.lang.Exception
142
     */
143
    @Before
144
    public void setUp() throws Exception {
145

    
146
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
147

    
148
        derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
149
        specimen.setDerivedFrom(derivationEvent);
150
        fieldUnit = FieldUnit.NewInstance();
151
        fieldUnit.addDerivationEvent(derivationEvent);
152
        gatheringEvent = GatheringEvent.NewInstance();
153
        fieldUnit.setGatheringEvent(gatheringEvent);
154
        gatheringEvent.setAbsoluteElevation(absoluteElevation);
155
        gatheringEvent.setAbsoluteElevationMax(absoluteElevationMaximum);
156
        gatheringEvent.setActor(collector);
157
        gatheringEvent.setCollectingMethod(collectingMethod);
158
        gatheringEvent.setDistanceToGround(distanceToGround);
159
        gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
160
        gatheringEvent.setDistanceToWaterSurfaceMax(distanceToSurfaceMax);
161
        gatheringEvent.setExactLocation(exactLocation);
162
        gatheringEvent.setDescription(gatheringEventDescription);
163
        gatheringEvent.setCountry(Country.GERMANY());
164

    
165
        gatheringEvent.setTimeperiod(gatheringPeriod);
166
        gatheringEvent.setLocality(locality);
167

    
168
        fieldUnit.setFieldNumber(fieldNumber);
169
        fieldUnit.setFieldNotes(fieldNotes);
170
        fieldUnit.setIndividualCount(individualCount);
171
        fieldUnit.setSex(sex);
172
        fieldUnit.setLifeStage(lifeStage);
173

    
174
        specimen.setAccessionNumber(accessionNumber);
175
        specimen.setCatalogNumber(catalogNumber);
176
        specimen.setStoredUnder(taxonName);
177
        specimen.setCollection(collection);
178
        specimen.setPreservation(preservationMethod);
179
        specimen.setOriginalLabelInfo(originalLabelInfo);
180
        specimen.setPreferredStableUri(stableUri);
181

    
182
        specimenFacade = DerivedUnitFacade.NewInstance(specimen);
183

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

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

    
195
        collectionSpecimen.setDerivedFrom(lastDerivationEvent);
196

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

    
203
        // empty facade
204
        emptyFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
205

    
206
    }
207

    
208
    // ****************************** TESTS*****************************/
209

    
210
    @Ignore //doesn't run in suite
211
    @Test
212
    @DataSet("DerivedUnitFacadeTest.testSetFieldObjectImageGallery.xml")
213
    @ExpectedDataSet
214
    public void testSetFieldObjectImageGallery() {
215
        UUID imageFeatureUuid = Feature.IMAGE().getUuid();
216
        Feature imageFeature = (Feature) termService.find(imageFeatureUuid);
217

    
218
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
219
        facade.innerDerivedUnit().setUuid(UUID.fromString("77af784f-931b-4857-be9a-48ccf31ed3f1"));
220
        facade.setFieldNumber("12345");
221
        Media media = Media.NewInstance(URI.create("www.abc.de"), 200, null,"jpeg");
222

    
223
        try {
224
            SpecimenDescription imageGallery = SpecimenDescription.NewInstance();
225
            imageGallery.setDescribedSpecimenOrObservation(facade.innerFieldUnit());
226
            imageGallery.setImageGallery(true);
227
            TextData textData = TextData.NewInstance();
228
            textData.setFeature(imageFeature);
229
            imageGallery.addElement(textData);
230
            textData.addMedia(media);
231
            facade.setFieldObjectImageGallery(imageGallery);
232

    
233
        } catch (DerivedUnitFacadeNotSupportedException e1) {
234
            e1.printStackTrace();
235
            Assert.fail(e1.getLocalizedMessage());
236
        }
237
        this.service.save(facade.innerDerivedUnit());
238

    
239
         setComplete(); endTransaction();
240
//         try {if (true){printDataSet(System.out, new
241
//         String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
242
//         ,"DERIVATIONEVENT",
243
//         "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA",
244
//         "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
245
//         } catch(Exception e) { logger.warn(e);}
246

    
247
    }
248

    
249
    @Test
250
    @Ignore
251
    // TODO generally works but has id problems when running together with above
252
    // test ()setFieldObjectImageGallery. Therefore set to ignore.
253
    @DataSet("DerivedUnitFacadeTest.testSetDerivedUnitImageGallery.xml")
254
    @ExpectedDataSet
255
    public void testSetDerivedUnitImageGallery() {
256
        // UUID specimenUUID =
257
        // UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
258
        // Specimen specimen = (Specimen)service.find(specimenUUID);
259
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
260
        Media media = Media.NewInstance(URI.create("www.derivedUnitImage.de"),200, null, "png");
261

    
262
        try {
263
            SpecimenDescription imageGallery = SpecimenDescription
264
                    .NewInstance();
265
            imageGallery.setDescribedSpecimenOrObservation(facade.innerDerivedUnit());
266
            imageGallery.setImageGallery(true);
267
            TextData textData = TextData.NewInstance();
268
            imageGallery.addElement(textData);
269
            textData.addMedia(media);
270
            facade.setDerivedUnitImageGallery(imageGallery);
271

    
272
        } catch (DerivedUnitFacadeNotSupportedException e1) {
273
            e1.printStackTrace();
274
            Assert.fail(e1.getLocalizedMessage());
275
        }
276
        this.service.save(facade.innerDerivedUnit());
277

    
278
        // setComplete(); endTransaction();
279
        // try {if (true){printDataSet(System.out, new
280
        // String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
281
        // ,"DERIVATIONEVENT",
282
        // "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA",
283
        // "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
284
        // } catch(Exception e) { logger.warn(e);}
285

    
286

    
287
    }
288

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

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

    
341
	@Test
342
	public void testGetDerivedUnitImageGalleryBoolean() {
343
		DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
344
		try {
345
			DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
346
			SpecimenDescription imageGallery = facade.getDerivedUnitImageGallery(true);
347
			Assert.assertNotNull("Image Gallery should have been created",imageGallery);
348
			Assert.assertEquals("The image gallery should be flagged as such",true, imageGallery.isImageGallery());
349
		} catch (DerivedUnitFacadeNotSupportedException e) {
350
			e.printStackTrace();
351
			Assert.fail();
352
		}
353

    
354
    }
355

    
356
    /**
357
     * Test method for
358
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance()}.
359
     */
360
    @Test
361
    public void testNewInstanceGatheringEventCreated() {
362
        Assert.assertNotNull("The specimen should have been created",
363
                specimenFacade.innerDerivedUnit());
364
        // ???
365
         Assert.assertNotNull("The derivation event should have been created",
366
        		 specimenFacade.innerDerivedUnit().getDerivedFrom());
367
         Assert.assertNotNull("The field unit should have been created",
368
        		 specimenFacade.innerFieldUnit());
369
         Assert.assertNotNull("The gathering event should have been created",
370
        		 specimenFacade.innerGatheringEvent());
371
    }
372

    
373

    
374

    
375

    
376
    /**
377
     * Test method for
378
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance(eu.etaxonomy.cdm.model.occurrence.Specimen)}
379
     * .
380
     */
381
    @Test
382
    public void testNewInstanceSpecimen() {
383
        Assert.assertSame("Specimen should be same", specimen,
384
                specimenFacade.innerDerivedUnit());
385
        Assert.assertSame("Derivation event should be same", derivationEvent,
386
                specimenFacade.innerDerivedUnit().getDerivedFrom());
387
        Assert.assertSame("Field unit should be same", fieldUnit,
388
                specimenFacade.innerFieldUnit());
389
        Assert.assertSame("Gathering event should be same", gatheringEvent,
390
                specimenFacade.innerGatheringEvent());
391
    }
392

    
393
    @Test
394
    public void testNewInstanceSpecimenWithoutFieldUnit() {
395
        DerivedUnit parent = DerivedUnit.NewPreservedSpecimenInstance();
396
        DerivedUnit child = DerivedUnit.NewPreservedSpecimenInstance();
397
        DerivationEvent.NewSimpleInstance(parent, child, DerivationEventType.ACCESSIONING());
398

    
399
        DerivedUnitFacade facade;
400
		try {
401
			facade = DerivedUnitFacade.NewInstance(child);
402
		       	Assert.assertTrue(facade.innerDerivedUnit().getDerivedFrom().getOriginals().size() == 1);
403
		       	Assert.assertNull(facade.getFieldUnit(false));
404
		       	DerivationEvent.NewSimpleInstance(FieldUnit.NewInstance(), parent, DerivationEventType.ACCESSIONING());
405

    
406
		} catch (DerivedUnitFacadeNotSupportedException e) {
407
			e.printStackTrace();
408
			Assert.fail();
409
		}
410

    
411
    }
412

    
413
    @Test
414
    public void testGatheringEventIsConnectedToDerivedUnit() {
415
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
416
        DerivedUnitFacade specimenFacade;
417
        try {
418
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
419
            specimenFacade.setDistanceToGround(2.0);
420
            FieldUnit specimenFieldUnit = (FieldUnit) specimen.getDerivedFrom().getOriginals().iterator().next();
421
            Assert.assertSame(
422
                    "Facade gathering event and specimen gathering event should be the same",
423
                    specimenFacade.innerGatheringEvent(),
424
                    specimenFieldUnit.getGatheringEvent());
425
        } catch (DerivedUnitFacadeNotSupportedException e) {
426
            Assert.fail("An error should not occur in NewInstance()");
427
        }
428
    }
429

    
430
    @Test
431
    public void testNoGatheringEventAndFieldUnit() {
432
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
433
        DerivedUnitFacade specimenFacade;
434
        try {
435
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
436
            Assert.assertNull("No field unit should exists",
437
                    specimenFacade.innerFieldUnit());
438
        } catch (DerivedUnitFacadeNotSupportedException e) {
439
            Assert.fail("An error should not occur in NewInstance()");
440
        }
441
    }
442

    
443
    @Test
444
    public void testInititializeTextDataWithSupportTest() {
445
        // TODO
446
    	DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
447
        DerivedUnitFacade specimenFacade;
448
        try {
449
            specimenFacade = DerivedUnitFacade.NewInstance(specimen);
450
            specimenFacade.setEcology("Ecology");
451
            String plantDescription = specimenFacade.getPlantDescription();
452
            Assert.assertNull(
453
                    "No plantDescription should exist yet and no NPE should be thrown until now",
454
                    plantDescription);
455
        } catch (DerivedUnitFacadeNotSupportedException e) {
456
            Assert.fail("An error should not occur in NewInstance()");
457
        }
458
    }
459

    
460
    @Test
461
    public void testGetSetType() {
462
        Assert.assertEquals("Type must be same", SpecimenOrObservationType.PreservedSpecimen, specimenFacade.getType());
463
        SpecimenOrObservationType newType = SpecimenOrObservationType.Fossil;
464
        specimenFacade.setType(newType);
465
        Assert.assertEquals("New type must be Fossil", newType, specimenFacade.getType());
466
        Assert.assertEquals("DerivedUnit recordBasis must be set to Fossil", newType, specimenFacade.innerDerivedUnit().getRecordBasis());
467

    
468
    }
469

    
470
    @Test
471
    public void testGetSetCountry() {
472
        Assert.assertEquals("Country must be same", Country.GERMANY(), specimenFacade.getCountry());
473
        specimenFacade.setCountry(Country.FRANCEFRENCHREPUBLIC());
474
        Assert.assertEquals("New country must be France", Country.FRANCEFRENCHREPUBLIC(), specimenFacade.getCountry());
475
    }
476

    
477
    /**
478
     * Test method for
479
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
480
     * .
481
     */
482
    @Test
483
    public void testAddGetRemoveCollectingArea() {
484
        String tdwgLabel = "GER";
485
        NamedArea tdwgArea = termService.getAreaByTdwgAbbreviation(tdwgLabel);
486
        NamedArea newCollectingArea = NamedArea.NewInstance("A nice area",
487
                "nice", "n");
488
        specimenFacade.addCollectingArea(newCollectingArea);
489
        Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
490
                .getCollectingAreas().size());
491
        Assert.assertSame("Areas should be same", newCollectingArea,
492
                specimenFacade.innerFieldUnit().getGatheringEvent()
493
                        .getCollectingAreas().iterator().next());
494
        specimenFacade.addCollectingArea(tdwgArea);
495
        Assert.assertEquals("Exactly 2 areas must exist", 2, specimenFacade
496
                .getCollectingAreas().size());
497
        specimenFacade.removeCollectingArea(newCollectingArea);
498
        Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
499
                .getCollectingAreas().size());
500
        NamedArea remainingArea = specimenFacade.getCollectingAreas()
501
                .iterator().next();
502
        Assert.assertEquals("Areas should be same", tdwgArea, remainingArea);
503
        specimenFacade.removeCollectingArea(tdwgArea);
504
        Assert.assertEquals("No area should remain", 0, specimenFacade
505
                .getCollectingAreas().size());
506
    }
507

    
508
    /**
509
     * Test method for
510
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
511
     * .
512
     */
513
    @Test
514
    public void testAddCollectingAreas() {
515
        NamedArea firstArea = NamedArea.NewInstance("A nice area", "nice", "n");
516
        Assert.assertEquals("No area must exist", 0, specimenFacade.getCollectingAreas().size());
517
        specimenFacade.addCollectingArea(firstArea);
518
        Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade.getCollectingAreas().size());
519

    
520
        String tdwgLabel = "GER";
521
        NamedArea tdwgArea = termService.getAreaByTdwgAbbreviation(tdwgLabel);
522
        NamedArea secondArea = NamedArea.NewInstance("A nice area", "nice", "n");
523

    
524
        java.util.Collection<NamedArea> areaCollection = new HashSet<NamedArea>();
525
        areaCollection.add(secondArea);
526
        areaCollection.add(tdwgArea);
527
        specimenFacade.addCollectingAreas(areaCollection);
528
        Assert.assertEquals("Exactly 3 areas must exist", 3, specimenFacade
529
                .getCollectingAreas().size());
530

    
531
    }
532

    
533
    /**
534
     * Test method for getting and setting absolute elevation.
535
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevation()}
536
     * .
537
     */
538
    @Test
539
    public void testGetSetAbsoluteElevation() {
540
        Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
541
        specimenFacade.setAbsoluteElevation(400);
542
        Assert.assertEquals("Absolute elevation must be 400",Integer.valueOf(400), specimenFacade.getAbsoluteElevation());
543
    }
544

    
545
    /**
546
     * Test method for
547
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevationError()}
548
     * .
549
     */
550
    @Test
551
    public void testGetSetAbsoluteElevationMaximum() {
552
        Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
553
        Assert.assertEquals("Absolute elevation maximum must be same",absoluteElevationMaximum, specimenFacade.getAbsoluteElevationMaximum());
554
        specimenFacade.setAbsoluteElevationMax(4);
555
        Assert.assertEquals("Absolute elevation maximum must be 4", Integer.valueOf(4), specimenFacade.getAbsoluteElevationMaximum());
556
        Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
557

    
558
    }
559

    
560
    @Test
561
    public void testGetSetAbsoluteElevationRange() {
562
        Assert.assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
563
        Assert.assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
564

    
565
        specimenFacade.setAbsoluteElevationRange(30, 36);
566
        Assert.assertEquals("", Integer.valueOf(36),specimenFacade.getAbsoluteElevationMaximum());
567
        Assert.assertEquals("", Integer.valueOf(30),specimenFacade.getAbsoluteElevation());
568
        Assert.assertEquals("", "30" + UTF8.EN_DASH_SPATIUM + "36 m",specimenFacade.absoluteElevationToString());
569
        Assert.assertEquals("", null,specimenFacade.getAbsoluteElevationText());
570

    
571
        specimenFacade.setAbsoluteElevationRange(30, 35);
572
        Assert.assertEquals("Odd range should not throw an exception anymore",
573
        		String.format("30%s35 m", UTF8.EN_DASH_SPATIUM),specimenFacade.absoluteElevationToString());
574

    
575
        specimenFacade.setAbsoluteElevationRange(41, null);
576
        Assert.assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
577
        Assert.assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevation());
578
        Assert.assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevation());
579
        Assert.assertNull("", specimenFacade.getAbsoluteElevationText());
580
        Assert.assertEquals("", "41 m",specimenFacade.absoluteElevationToString());
581

    
582
        specimenFacade.setAbsoluteElevationRange(null, null);
583
        Assert.assertNull("", specimenFacade.getAbsoluteElevation());
584
        Assert.assertNull("", specimenFacade.getAbsoluteElevationMaximum());
585
        Assert.assertNull("", specimenFacade.absoluteElevationToString());
586

    
587
    }
588

    
589
    @Test
590
    public void testGetSetAbsoluteElevationText() {
591
        Assert.assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
592
        Assert.assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
593
        Assert.assertEquals("", null,specimenFacade.getAbsoluteElevationText());
594

    
595
        String elevText = "approx. 30 - 35";
596
        specimenFacade.setAbsoluteElevationText(elevText);
597
        Assert.assertEquals("", absoluteElevation, specimenFacade.getAbsoluteElevation());
598
        Assert.assertEquals("", absoluteElevationMaximum,specimenFacade.getAbsoluteElevationMaximum());
599
        Assert.assertEquals("", elevText,specimenFacade.absoluteElevationToString());
600

    
601
        specimenFacade.setAbsoluteElevationRange(30, 35);
602
        Assert.assertEquals("ToString should not change by setting range if text is set", elevText,specimenFacade.absoluteElevationToString());
603
        Assert.assertEquals("", Integer.valueOf(30), specimenFacade.getAbsoluteElevation());
604
        Assert.assertEquals("", Integer.valueOf(35),specimenFacade.getAbsoluteElevationMaximum());
605

    
606

    
607
        specimenFacade.setAbsoluteElevationRange(41, null);
608
        Assert.assertEquals("ToString should not change by setting range if text is set", elevText,specimenFacade.absoluteElevationToString());
609
        Assert.assertEquals("", Integer.valueOf(41), specimenFacade.getAbsoluteElevation());
610
        Assert.assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
611

    
612

    
613
        specimenFacade.setAbsoluteElevationText(null);
614
        Assert.assertNull("", specimenFacade.getAbsoluteElevationText());
615
        Assert.assertEquals("ToString should change by setting text to null", "41 m",specimenFacade.absoluteElevationToString());
616
        Assert.assertEquals("", Integer.valueOf(41), specimenFacade.getAbsoluteElevation());
617
        Assert.assertEquals("", null,specimenFacade.getAbsoluteElevationMaximum());
618
    }
619

    
620

    
621
    /**
622
     */
623
    @Test
624
    public void testGetSetCollector() {
625
        Assert.assertNotNull("Collector must not be null",
626
                specimenFacade.getCollector());
627
        Assert.assertEquals("Collector must be same", collector,
628
                specimenFacade.getCollector());
629
        specimenFacade.setCollector(null);
630
        Assert.assertNull("Collector must be null",
631
                specimenFacade.getCollector());
632
    }
633

    
634
    /**
635
     * Test method for
636
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectingMethod()}
637
     * .
638
     */
639
    @Test
640
    public void testGetSetCollectingMethod() {
641
        Assert.assertEquals("Collecting method must be same", collectingMethod,
642
                specimenFacade.getCollectingMethod());
643
        specimenFacade.setCollectingMethod("new method");
644
        Assert.assertEquals("Collecting method must be 'new method'",
645
                "new method", specimenFacade.getCollectingMethod());
646
    }
647

    
648
    /**
649
     * Test method for
650
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToGround()}
651
     * .
652
     */
653
    @Test
654
    public void testGetSetDistanceToGround() {
655
        Assert.assertEquals("Distance to ground must be same",distanceToGround, specimenFacade.getDistanceToGround());
656
        specimenFacade.setDistanceToGround(5.0);
657
        Assert.assertEquals("Distance to ground must be 5", Double.valueOf(5), specimenFacade.getDistanceToGround());
658
    }
659

    
660
    /**
661
     * Test method for
662
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToWaterSurface()}
663
     * .
664
     */
665
    @Test
666
    public void testGetDistanceToWaterSurface() {
667
        Assert.assertEquals("Distance to surface must be same", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
668
        specimenFacade.setDistanceToWaterSurface(6.0);
669
        Assert.assertEquals("Distance to surface must be 6", Double.valueOf(6), specimenFacade.getDistanceToWaterSurface());
670
        // empty facade tests
671
        Assert.assertNull("Empty facace must not have any gathering values", emptyFacade.getDistanceToWaterSurface());
672
        emptyFacade.setDistanceToWaterSurface(13.0);
673
        Assert.assertNotNull("Field unit must exist if distance to water exists", emptyFacade.getFieldUnit(false));
674
        Assert.assertNotNull("Gathering event must exist if distance to water exists", emptyFacade.getGatheringEvent(false));
675
        FieldUnit specimenFieldUnit = (FieldUnit) emptyFacade
676
                .innerDerivedUnit().getDerivedFrom().getOriginals().iterator().next();
677
        Assert.assertSame("Gathering event of facade and of specimen must be the same",
678
                specimenFieldUnit.getGatheringEvent(), emptyFacade.getGatheringEvent(false));
679
    }
680

    
681
    @Test
682
    public void testGetSetDistanceToWaterText() {
683
        Assert.assertEquals("", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
684
        Assert.assertEquals("", distanceToSurfaceMax ,specimenFacade.getDistanceToWaterSurfaceMax());
685
        Assert.assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceText());
686

    
687
        String distText = "approx. 0.3 - 0.6";
688
        specimenFacade.setDistanceToWaterSurfaceText(distText);
689
        Assert.assertEquals("", distanceToSurface, specimenFacade.getDistanceToWaterSurface());
690
        Assert.assertEquals("", distanceToSurfaceMax,specimenFacade.getDistanceToWaterSurfaceMax());
691
        Assert.assertEquals("", distText,specimenFacade.distanceToWaterSurfaceToString());
692

    
693
        specimenFacade.setDistanceToWaterSurfaceRange(0.6, 1.4);
694
        Assert.assertEquals("ToString should not change by setting range if text is set", distText,specimenFacade.distanceToWaterSurfaceToString());
695
        Assert.assertEquals("", Double.valueOf(0.6), specimenFacade.getDistanceToWaterSurface());
696
        Assert.assertEquals("", Double.valueOf(1.4),specimenFacade.getDistanceToWaterSurfaceMax());
697

    
698
        specimenFacade.setDistanceToWaterSurfaceRange(41.2, null);
699
        Assert.assertEquals("ToString should not change by setting range if text is set", distText,specimenFacade.distanceToWaterSurfaceToString());
700
        Assert.assertEquals("", Double.valueOf(41.2), specimenFacade.getDistanceToWaterSurface());
701
        Assert.assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceMax());
702

    
703
        specimenFacade.setDistanceToWaterSurfaceText(null);
704
        Assert.assertNull("", specimenFacade.getDistanceToWaterSurfaceText());
705
        Assert.assertEquals("ToString should change by setting text to null", "41.2 m",specimenFacade.distanceToWaterSurfaceToString());
706
        Assert.assertEquals("", Double.valueOf(41.2), specimenFacade.getDistanceToWaterSurface());
707
        Assert.assertEquals("", null,specimenFacade.getDistanceToWaterSurfaceMax());
708
    }
709

    
710

    
711
    /**
712
     * Test method for
713
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getExactLocation()}.
714
     */
715
    @Test
716
    public void testGetSetExactLocation() {
717
        Assert.assertNotNull("Exact location must not be null",
718
                specimenFacade.getExactLocation());
719
        Assert.assertEquals("Exact location must be same", exactLocation,
720
                specimenFacade.getExactLocation());
721
        specimenFacade.setExactLocation(null);
722
        Assert.assertNull("Exact location must be null",
723
                specimenFacade.getExactLocation());
724
    }
725

    
726
    @Test
727
    public void testSetExactLocationByParsing() {
728
        Point point1;
729
        try {
730
            specimenFacade.setExactLocationByParsing("112\u00B034'20\"W",
731
                    "34\u00B030,34'N", null, null);
732
            point1 = specimenFacade.getExactLocation();
733
            Assert.assertNotNull("", point1.getLatitude());
734
            System.out.println(point1.getLatitude().toString());
735
            Assert.assertTrue("",
736
                    point1.getLatitude().toString().startsWith("34.505"));
737
            System.out.println(point1.getLongitude().toString());
738
            Assert.assertTrue("",
739
                    point1.getLongitude().toString().startsWith("-112.5722"));
740

    
741
        } catch (ParseException e) {
742
            Assert.fail("No parsing error should occur");
743
        }
744
    }
745

    
746
    /**
747
     * Test method for
748
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getGatheringEventDescription()}
749
     * .
750
     */
751
    @Test
752
    public void testGetSetGatheringEventDescription() {
753
        Assert.assertEquals("Gathering event description must be same",
754
                gatheringEventDescription,
755
                specimenFacade.getGatheringEventDescription());
756
        specimenFacade.setGatheringEventDescription("new description");
757
        Assert.assertEquals(
758
                "Gathering event description must be 'new description' now",
759
                "new description",
760
                specimenFacade.getGatheringEventDescription());
761
    }
762

    
763
    /**
764
     * Test method for
765
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTimeperiod()}.
766
     */
767
    @Test
768
    public void testGetTimeperiod() {
769
        Assert.assertNotNull("Gathering period must not be null", specimenFacade.getGatheringPeriod());
770
        Assert.assertFalse("Gathering period must not be empty", specimenFacade.getGatheringPeriod().isEmpty());
771
        Assert.assertEquals("Gathering period must be same", gatheringPeriod, specimenFacade.getGatheringPeriod());
772
        specimenFacade.setGatheringPeriod(null);
773
        Assert.assertTrue("Gathering period must be null", specimenFacade.getGatheringPeriod().isEmpty());
774
    }
775

    
776
    @Test
777
    public void testHasFieldObject() throws SecurityException,
778
            NoSuchFieldException, IllegalArgumentException,
779
            IllegalAccessException {
780
        // this test depends on the current implementation of SpecimenFacade. In
781
        // future
782
        // field unit may not be initialized from the beginning. Than the
783
        // following
784
        // assert should be set to assertNull
785
        Assert.assertTrue("field object should not be null (depends on specimen facade initialization !!)",
786
                specimenFacade.hasFieldObject());
787

    
788
        Field fieldUnitField = DerivedUnitFacade.class.getDeclaredField("fieldUnit");
789
        fieldUnitField.setAccessible(true);
790
        fieldUnitField.set(specimenFacade, null);
791
        Assert.assertFalse("The field unit should be null now",
792
                specimenFacade.hasFieldObject());
793

    
794
        specimenFacade.setDistanceToGround(33.0);
795
        Assert.assertTrue(
796
                "The field unit should have been created again",
797
                specimenFacade.hasFieldObject());
798
    }
799

    
800
    /**
801
     * Test method for
802
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
803
     * .
804
     */
805
    @Test
806
    public void testAddGetRemoveFieldObjectDefinition() {
807
        Assert.assertEquals("There should be no definition yet", 0,
808
                specimenFacade.getFieldObjectDefinition().size());
809
        specimenFacade.addFieldObjectDefinition("Tres interesant",
810
                Language.FRENCH());
811
        Assert.assertEquals("There should be exactly one definition", 1,
812
                specimenFacade.getFieldObjectDefinition().size());
813
        Assert.assertEquals(
814
                "The French definition should be 'Tres interesant'",
815
                "Tres interesant", specimenFacade.getFieldObjectDefinition()
816
                        .get(Language.FRENCH()).getText());
817
        Assert.assertEquals(
818
                "The French definition should be 'Tres interesant'",
819
                "Tres interesant",
820
                specimenFacade.getFieldObjectDefinition(Language.FRENCH()));
821
        specimenFacade.addFieldObjectDefinition("Sehr interessant",
822
                Language.GERMAN());
823
        Assert.assertEquals("There should be exactly 2 definition", 2,
824
                specimenFacade.getFieldObjectDefinition().size());
825
        specimenFacade.removeFieldObjectDefinition(Language.FRENCH());
826
        Assert.assertEquals("There should remain exactly 1 definition", 1,
827
                specimenFacade.getFieldObjectDefinition().size());
828
        Assert.assertEquals(
829
                "The remaining German definition should be 'Sehr interessant'",
830
                "Sehr interessant",
831
                specimenFacade.getFieldObjectDefinition(Language.GERMAN()));
832
        specimenFacade.removeFieldObjectDefinition(Language.GERMAN());
833
        Assert.assertEquals("There should remain no definition", 0,
834
                specimenFacade.getFieldObjectDefinition().size());
835
    }
836

    
837
    /**
838
     * Test method for
839
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
840
     * .
841
     */
842
    @Test
843
    public void testAddGetHasRemoveFieldObjectMedia() {
844
        Assert.assertFalse("There should be no image gallery yet",
845
                specimenFacade.hasFieldObjectImageGallery());
846
        Assert.assertFalse("There should be no specimen image gallery either",
847
                specimenFacade.hasDerivedUnitImageGallery());
848

    
849
        List<Media> media = specimenFacade.getFieldObjectMedia();
850
        Assert.assertFalse("There should still not be an image gallery now",
851
                specimenFacade.hasFieldObjectImageGallery());
852
        Assert.assertEquals("There should be no media yet in the gallery", 0,
853
                media.size());
854

    
855
        Media media1 = Media.NewInstance();
856
        specimenFacade.addFieldObjectMedia(media1);
857
        Assert.assertEquals("There should be exactly one specimen media", 1,
858
                specimenFacade.getFieldObjectMedia().size());
859
        Assert.assertEquals("The only media should be media 1", media1,
860
                specimenFacade.getFieldObjectMedia().get(0));
861
        Assert.assertFalse(
862
                "There should still no specimen image gallery exist",
863
                specimenFacade.hasDerivedUnitImageGallery());
864

    
865
        Media media2 = Media.NewInstance();
866
        specimenFacade.addFieldObjectMedia(media2);
867
        Assert.assertEquals("There should be exactly 2 specimen media", 2,
868
                specimenFacade.getFieldObjectMedia().size());
869
        Assert.assertEquals("The first media should be media1", media1,
870
                specimenFacade.getFieldObjectMedia().get(0));
871
        Assert.assertEquals("The second media should be media2", media2,
872
                specimenFacade.getFieldObjectMedia().get(1));
873

    
874
        specimenFacade.removeFieldObjectMedia(media1);
875
        Assert.assertEquals("There should be exactly one specimen media", 1,
876
                specimenFacade.getFieldObjectMedia().size());
877
        Assert.assertEquals("The only media should be media2", media2,
878
                specimenFacade.getFieldObjectMedia().get(0));
879

    
880
        specimenFacade.removeFieldObjectMedia(media1);
881
        Assert.assertEquals("There should still be exactly one specimen media",
882
                1, specimenFacade.getFieldObjectMedia().size());
883

    
884
        specimenFacade.removeFieldObjectMedia(media2);
885
        Assert.assertEquals("There should remain no media in the gallery", 0,
886
                media.size());
887

    
888
    }
889

    
890
    /**
891
     * Test method for
892
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
893
     * .
894
     */
895
    @Test
896
    public void testGetSetEcology() {
897
        Assert.assertNotNull(
898
                "An empty ecology data should be created when calling getEcology()",
899
                specimenFacade.getEcologyAll());
900
        Assert.assertEquals(
901
                "An empty ecology data should be created when calling getEcology()",
902
                0, specimenFacade.getEcologyAll().size());
903
        specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
904
        Assert.assertEquals("Ecology data should exist for 1 language", 1,
905
                specimenFacade.getEcologyAll().size());
906
        Assert.assertEquals(
907
                "Ecology data should be 'Tres jolie ici' for French",
908
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
909
        Assert.assertNull(
910
                "Ecology data should be null for the default language",
911
                specimenFacade.getEcology());
912
        specimenFacade.setEcology("Nice here");
913
        Assert.assertEquals("Ecology data should exist for 2 languages", 2,
914
                specimenFacade.getEcologyAll().size());
915
        Assert.assertEquals("Ecology data should be 'Tres jolie ici'",
916
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
917
        Assert.assertEquals(
918
                "Ecology data should be 'Nice here' for the default language",
919
                "Nice here", specimenFacade.getEcology());
920
        Assert.assertEquals("Ecology data should be 'Nice here' for english",
921
                "Nice here", specimenFacade.getEcology());
922

    
923
        specimenFacade.setEcology("Vert et rouge", Language.FRENCH());
924
        Assert.assertEquals("Ecology data should exist for 2 languages", 2,
925
                specimenFacade.getEcologyAll().size());
926
        Assert.assertEquals("Ecology data should be 'Vert et rouge'",
927
                "Vert et rouge", specimenFacade.getEcology(Language.FRENCH()));
928
        Assert.assertEquals(
929
                "Ecology data should be 'Nice here' for the default language",
930
                "Nice here", specimenFacade.getEcology());
931

    
932
        specimenFacade.setEcology(null, Language.FRENCH());
933
        Assert.assertEquals("Ecology data should exist for 1 languages", 1,
934
                specimenFacade.getEcologyAll().size());
935
        Assert.assertEquals(
936
                "Ecology data should be 'Nice here' for the default language",
937
                "Nice here", specimenFacade.getEcology());
938
        Assert.assertNull("Ecology data should be 'null' for French",
939
                specimenFacade.getEcology(Language.FRENCH()));
940

    
941
        specimenFacade.removeEcology(null);
942
        Assert.assertEquals("There should be no ecology left", 0,
943
                specimenFacade.getEcologyAll().size());
944
        Assert.assertNull("Ecology data should be 'null' for default language",
945
                specimenFacade.getEcology());
946

    
947
    }
948

    
949
    /**
950
     * Test method for
951
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
952
     * .
953
     */
954
    @Test
955
    public void testGetSetPlantDescription() {
956
        Assert.assertNotNull(
957
                "An empty plant description data should be created when calling getPlantDescriptionAll()",
958
                specimenFacade.getPlantDescriptionAll());
959
        Assert.assertEquals(
960
                "An empty plant description data should be created when calling getPlantDescription()",
961
                0, specimenFacade.getPlantDescriptionAll().size());
962
        specimenFacade.setPlantDescription("bleu", Language.FRENCH());
963
        Assert.assertEquals(
964
                "Plant description data should exist for 1 language", 1,
965
                specimenFacade.getPlantDescriptionAll().size());
966
        Assert.assertEquals(
967
                "Plant description data should be 'bleu' for French", "bleu",
968
                specimenFacade.getPlantDescription(Language.FRENCH()));
969
        Assert.assertNull(
970
                "Plant description data should be null for the default language",
971
                specimenFacade.getPlantDescription());
972
        specimenFacade.setPlantDescription("Nice here");
973
        Assert.assertEquals(
974
                "Plant description data should exist for 2 languages", 2,
975
                specimenFacade.getPlantDescriptionAll().size());
976
        Assert.assertEquals("Plant description data should be 'bleu'", "bleu",
977
                specimenFacade.getPlantDescription(Language.FRENCH()));
978
        Assert.assertEquals(
979
                "Plant description data should be 'Nice here' for the default language",
980
                "Nice here", specimenFacade.getPlantDescription());
981
        Assert.assertEquals(
982
                "Plant description data should be 'Nice here' for english",
983
                "Nice here", specimenFacade.getPlantDescription());
984

    
985
        specimenFacade.setPlantDescription("Vert et rouge", Language.FRENCH());
986
        Assert.assertEquals(
987
                "Plant description data should exist for 2 languages", 2,
988
                specimenFacade.getPlantDescriptionAll().size());
989
        Assert.assertEquals("Plant description data should be 'Vert et rouge'",
990
                "Vert et rouge",
991
                specimenFacade.getPlantDescription(Language.FRENCH()));
992
        Assert.assertEquals(
993
                "Plant description data should be 'Nice here' for the default language",
994
                "Nice here", specimenFacade.getPlantDescription());
995

    
996
        specimenFacade.setPlantDescription(null, Language.FRENCH());
997
        Assert.assertEquals(
998
                "Plant description data should exist for 1 languages", 1,
999
                specimenFacade.getPlantDescriptionAll().size());
1000
        Assert.assertEquals(
1001
                "Plant description data should be 'Nice here' for the default language",
1002
                "Nice here", specimenFacade.getPlantDescription());
1003
        Assert.assertNull("Plant description data should be 'null' for French",
1004
                specimenFacade.getPlantDescription(Language.FRENCH()));
1005

    
1006
        // test interference with ecology
1007
        specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
1008
        Assert.assertEquals("Ecology data should exist for 1 language", 1,
1009
                specimenFacade.getEcologyAll().size());
1010
        Assert.assertEquals(
1011
                "Ecology data should be 'Tres jolie ici' for French",
1012
                "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
1013
        Assert.assertNull(
1014
                "Ecology data should be null for the default language",
1015
                specimenFacade.getEcology());
1016

    
1017
        // repeat above test
1018
        Assert.assertEquals(
1019
                "Plant description data should exist for 1 languages", 1,
1020
                specimenFacade.getPlantDescriptionAll().size());
1021
        Assert.assertEquals(
1022
                "Plant description data should be 'Nice here' for the default language",
1023
                "Nice here", specimenFacade.getPlantDescription());
1024
        Assert.assertNull("Plant description data should be 'null' for French",
1025
                specimenFacade.getPlantDescription(Language.FRENCH()));
1026

    
1027
        specimenFacade.removePlantDescription(null);
1028
        Assert.assertEquals("There should be no plant description left", 0,
1029
                specimenFacade.getPlantDescriptionAll().size());
1030
        Assert.assertNull(
1031
                "Plant description data should be 'null' for default language",
1032
                specimenFacade.getPlantDescription());
1033

    
1034
    }
1035

    
1036
    /**
1037
     * Test method for
1038
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNumber()}.
1039
     */
1040
    @Test
1041
    public void testGetSetFieldNumber() {
1042
        Assert.assertEquals("Field number must be same", fieldNumber,
1043
                specimenFacade.getFieldNumber());
1044
        specimenFacade.setFieldNumber("564AB");
1045
        Assert.assertEquals("New field number must be '564AB'", "564AB",
1046
                specimenFacade.getFieldNumber());
1047
        // empty facade tests
1048
        Assert.assertNull("Empty facace must not have any field value",
1049
                emptyFacade.getFieldNumber());
1050
        emptyFacade.setFieldNumber("1256A");
1051
        Assert.assertNotNull(
1052
                "Field unit must exist if field number exists",
1053
                emptyFacade.getFieldUnit(false));
1054
        FieldUnit specimenFieldUnit = (FieldUnit) emptyFacade
1055
                .innerDerivedUnit().getDerivedFrom().getOriginals().iterator()
1056
                .next();
1057
        Assert.assertSame(
1058
                "Field unit of facade and of specimen must be the same",
1059
                specimenFieldUnit,
1060
                emptyFacade.getFieldUnit(false));
1061
        Assert.assertEquals("1256A", emptyFacade.getFieldNumber());
1062
    }
1063

    
1064
    /**
1065
     * Test method for
1066
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNotes()}.
1067
     */
1068
    @Test
1069
    public void testGetSetFieldNotes() {
1070
        Assert.assertEquals("Field notes must be same", fieldNotes,
1071
                specimenFacade.getFieldNotes());
1072
        specimenFacade.setFieldNotes("A completely new info");
1073
        Assert.assertEquals("New field note must be 'A completely new info'",
1074
                "A completely new info", specimenFacade.getFieldNotes());
1075
    }
1076

    
1077
    /**
1078
     * Test method for
1079
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setGatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent)}
1080
     * .
1081
     */
1082
    @Test
1083
    @Ignore // #######DerivationEvent ---------------------------------------- 1
1084
    public void testSetGatheringEvent() {
1085
        GatheringEvent newGatheringEvent = GatheringEvent.NewInstance();
1086
        newGatheringEvent.setDistanceToGround(43.0);
1087
        Assert.assertFalse("The initial distance to ground should not be 43",
1088
                specimenFacade.getDistanceToGround() == 43.0);
1089
        specimenFacade.setGatheringEvent(newGatheringEvent);
1090
        Assert.assertTrue("The final distance to ground should be 43",
1091
                specimenFacade.getDistanceToGround() == 43.0);
1092
        Assert.assertSame(
1093
                "The new gathering event should be 'newGatheringEvent'",
1094
                newGatheringEvent, specimenFacade.innerGatheringEvent());
1095
    }
1096

    
1097
    /**
1098
     * Test method for
1099
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerGatheringEvent()}
1100
     * .
1101
     */
1102
    @Test
1103
    public void testGetGatheringEvent() {
1104
        Assert.assertNotNull("Gathering event must not be null",
1105
                specimenFacade.innerGatheringEvent());
1106
        Assert.assertEquals(
1107
                "Gathering event must be field unit's gathering event",
1108
                specimenFacade.innerFieldUnit().getGatheringEvent(),
1109
                specimenFacade.innerGatheringEvent());
1110
    }
1111

    
1112
    /**
1113
     * Test method for
1114
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getIndividualCount()}
1115
     * .
1116
     */
1117
    @Test
1118
    public void testGetSetIndividualCount() {
1119
        Assert.assertEquals("Individual count must be same", individualCount,
1120
                specimenFacade.getIndividualCount());
1121
        specimenFacade.setIndividualCount(4);
1122
        Assert.assertEquals("New individual count must be '4'",
1123
                Integer.valueOf(4), specimenFacade.getIndividualCount());
1124

    
1125
    }
1126

    
1127
    /**
1128
     * Test method for
1129
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLifeStage()}.
1130
     */
1131
    @Test
1132
    public void testGetSetLifeStage() {
1133
        Assert.assertNotNull("Life stage must not be null",
1134
                specimenFacade.getLifeStage());
1135
        Assert.assertEquals("Life stage must be same", lifeStage,
1136
                specimenFacade.getLifeStage());
1137
        specimenFacade.setLifeStage(null);
1138
        Assert.assertNull("Life stage must be null",
1139
                specimenFacade.getLifeStage());
1140
    }
1141

    
1142
    /**
1143
     * Test method for
1144
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getSex()}.
1145
     */
1146
    @Test
1147
    public void testGetSetSex() {
1148
        Assert.assertNotNull("Sex must not be null", specimenFacade.getSex());
1149
        Assert.assertEquals("Sex must be same", sex, specimenFacade.getSex());
1150
        specimenFacade.setSex(null);
1151
        Assert.assertNull("Sex must be null", specimenFacade.getSex());
1152
    }
1153

    
1154
    /**
1155
     * Test method for
1156
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLocality()}.
1157
     */
1158
    @Test
1159
    public void testGetSetLocality() {
1160
        Assert.assertEquals("Locality must be same", locality,
1161
                specimenFacade.getLocality());
1162
        specimenFacade.setLocality("A completely new place", Language.FRENCH());
1163
        Assert.assertEquals("New locality must be 'A completely new place'",
1164
                "A completely new place", specimenFacade.getLocalityText());
1165
        Assert.assertEquals("New locality language must be French",
1166
                Language.FRENCH(), specimenFacade.getLocalityLanguage());
1167
        specimenFacade.setLocality("Another place");
1168
        Assert.assertEquals("New locality must be 'Another place'",
1169
                "Another place", specimenFacade.getLocalityText());
1170
        Assert.assertEquals("New locality language must be default",
1171
                Language.DEFAULT(), specimenFacade.getLocalityLanguage());
1172
    }
1173

    
1174
    /**
1175
     * Test method for
1176
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
1177
     * .
1178
     */
1179
    @Test
1180
    public void testAddGetRemoveSpecimenDefinition() {
1181
        Assert.assertEquals("There should be no definition yet", 0,
1182
                specimenFacade.getDerivedUnitDefinitions().size());
1183
        specimenFacade.addDerivedUnitDefinition("Tres interesant",
1184
                Language.FRENCH());
1185
        Assert.assertEquals("There should be exactly one definition", 1,
1186
                specimenFacade.getDerivedUnitDefinitions().size());
1187
        Assert.assertEquals(
1188
                "The French definition should be 'Tres interesant'",
1189
                "Tres interesant", specimenFacade.getDerivedUnitDefinitions()
1190
                        .get(Language.FRENCH()).getText());
1191
        Assert.assertEquals(
1192
                "The French definition should be 'Tres interesant'",
1193
                "Tres interesant",
1194
                specimenFacade.getDerivedUnitDefinition(Language.FRENCH()));
1195
        specimenFacade.addDerivedUnitDefinition("Sehr interessant",
1196
                Language.GERMAN());
1197
        Assert.assertEquals("There should be exactly 2 definition", 2,
1198
                specimenFacade.getDerivedUnitDefinitions().size());
1199
        specimenFacade.removeDerivedUnitDefinition(Language.FRENCH());
1200
		Assert.assertEquals("There should remain exactly 1 definition", 1,
1201
                specimenFacade.getDerivedUnitDefinitions().size());
1202
        Assert.assertEquals(
1203
                "The remaining German definition should be 'Sehr interessant'",
1204
                "Sehr interessant",
1205
		        specimenFacade.getDerivedUnitDefinition(Language.GERMAN()));
1206
		specimenFacade.removeDerivedUnitDefinition(Language.GERMAN());
1207
		Assert.assertEquals("There should remain no definition", 0,
1208
                specimenFacade.getDerivedUnitDefinitions().size());
1209
    }
1210

    
1211
    /**
1212
     * Test method for
1213
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1214
     * .
1215
     */
1216
    @Test
1217
    public void testAddGetRemoveDetermination() {
1218
        Assert.assertEquals("There should be no determination yet", 0,
1219
                specimenFacade.getDeterminations().size());
1220
        DeterminationEvent determinationEvent1 = DeterminationEvent
1221
                .NewInstance();
1222
        specimenFacade.addDetermination(determinationEvent1);
1223
        Assert.assertEquals("There should be exactly one determination", 1,
1224
                specimenFacade.getDeterminations().size());
1225
        Assert.assertEquals("The only determination should be determination 1",
1226
                determinationEvent1, specimenFacade.getDeterminations()
1227
                        .iterator().next());
1228

    
1229
        DeterminationEvent determinationEvent2 = DeterminationEvent
1230
                .NewInstance();
1231
        specimenFacade.addDetermination(determinationEvent2);
1232
        Assert.assertEquals("There should be exactly 2 determinations", 2,
1233
                specimenFacade.getDeterminations().size());
1234
        specimenFacade.removeDetermination(determinationEvent1);
1235

    
1236
        Assert.assertEquals("There should remain exactly 1 determination", 1,
1237
                specimenFacade.getDeterminations().size());
1238
        Assert.assertEquals(
1239
                "The remaining determinations should be determination 2",
1240
                determinationEvent2, specimenFacade.getDeterminations()
1241
                        .iterator().next());
1242

    
1243
        specimenFacade.removeDetermination(determinationEvent1);
1244
        Assert.assertEquals("There should remain exactly 1 determination", 1,
1245
                specimenFacade.getDeterminations().size());
1246

    
1247
        specimenFacade.removeDetermination(determinationEvent2);
1248
        Assert.assertEquals("There should remain no definition", 0,
1249
                specimenFacade.getDerivedUnitDefinitions().size());
1250

    
1251
    }
1252

    
1253

    
1254
    /**
1255
     * Test method for
1256
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1257
     * .
1258
     */
1259
    @Test
1260
    public void testPreferredOtherDeterminations() {
1261
        Assert.assertEquals("There should be no determination yet", 0,
1262
                specimenFacade.getDeterminations().size());
1263
        DeterminationEvent determinationEvent1 = DeterminationEvent.NewInstance();
1264
        specimenFacade.setPreferredDetermination(determinationEvent1);
1265

    
1266
        Assert.assertEquals("There should be exactly one determination", 1,
1267
                specimenFacade.getDeterminations().size());
1268
        Assert.assertEquals("The only determination should be determination 1",
1269
                determinationEvent1, specimenFacade.getDeterminations()
1270
                        .iterator().next());
1271
        Assert.assertEquals("determination 1 should be the preferred determination",
1272
                determinationEvent1, specimenFacade.getPreferredDetermination());
1273
        Assert.assertEquals("There should be no 'non preferred' determination", 0,
1274
                specimenFacade.getOtherDeterminations().size());
1275

    
1276

    
1277

    
1278
        DeterminationEvent determinationEvent2 = DeterminationEvent.NewInstance();
1279
        specimenFacade.addDetermination(determinationEvent2);
1280
        Assert.assertEquals("There should be exactly 2 determinations", 2,
1281
                specimenFacade.getDeterminations().size());
1282
        Assert.assertEquals("determination 1 should be the preferred determination",
1283
                determinationEvent1, specimenFacade.getPreferredDetermination());
1284
        Assert.assertEquals("There should be one 'non preferred' determination", 1,
1285
                specimenFacade.getOtherDeterminations().size());
1286
        Assert.assertEquals("The only 'non preferred' determination should be determination 2",
1287
                determinationEvent2, specimenFacade.getOtherDeterminations().iterator().next());
1288

    
1289

    
1290
        DeterminationEvent determinationEvent3 = DeterminationEvent.NewInstance();
1291
        specimenFacade.setPreferredDetermination(determinationEvent3);
1292
        Assert.assertEquals("There should be exactly 3 determinations", 3,
1293
                specimenFacade.getDeterminations().size());
1294
        Assert.assertEquals("determination 3 should be the preferred determination",
1295
                determinationEvent3, specimenFacade.getPreferredDetermination());
1296
        Assert.assertEquals("There should be 2 'non preferred' determination", 2,
1297
                specimenFacade.getOtherDeterminations().size());
1298
        Assert.assertTrue("determination 1 should be contained in the set of 'non preferred' determinations",
1299
                specimenFacade.getOtherDeterminations().contains(determinationEvent1));
1300
        Assert.assertTrue("determination 2 should be contained in the set of 'non preferred' determinations",
1301
                specimenFacade.getOtherDeterminations().contains(determinationEvent2));
1302

    
1303
    }
1304

    
1305
    /**
1306
     * Test method for
1307
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitMedia(eu.etaxonomy.cdm.model.media.Media)}
1308
     * .
1309
     */
1310
    @Test
1311
    public void testAddGetHasRemoveSpecimenMedia() {
1312
        Assert.assertFalse("There should be no image gallery yet",
1313
                specimenFacade.hasDerivedUnitImageGallery());
1314
        Assert.assertFalse(
1315
                "There should be also no field object image gallery yet",
1316
                specimenFacade.hasFieldObjectImageGallery());
1317

    
1318
        List<Media> media = specimenFacade.getDerivedUnitMedia();
1319
        Assert.assertFalse(
1320
                "There should still not be an empty image gallery now",
1321
                specimenFacade.hasDerivedUnitImageGallery());
1322
        Assert.assertEquals("There should be no media yet in the gallery", 0,
1323
                media.size());
1324

    
1325
        Media media1 = Media.NewInstance();
1326
        specimenFacade.addDerivedUnitMedia(media1);
1327
		Assert.assertEquals("There should be exactly one specimen media", 1,
1328
                specimenFacade.getDerivedUnitMedia().size());
1329
        Assert.assertEquals("The only media should be media 1", media1,
1330
                specimenFacade.getDerivedUnitMedia().get(0));
1331
        Assert.assertFalse(
1332
                "There should be still no field object image gallery",
1333
                specimenFacade.hasFieldObjectImageGallery());
1334

    
1335
        Media media2 = Media.NewInstance();
1336
        specimenFacade.addDerivedUnitMedia(media2);
1337
		Assert.assertEquals("There should be exactly 2 specimen media", 2,
1338
                specimenFacade.getDerivedUnitMedia().size());
1339
        Assert.assertEquals("The first media should be media1", media1,
1340
                specimenFacade.getDerivedUnitMedia().get(0));
1341
        Assert.assertEquals("The second media should be media2", media2,
1342
                specimenFacade.getDerivedUnitMedia().get(1));
1343

    
1344
        specimenFacade.removeDerivedUnitMedia(media1);
1345
        Assert.assertEquals("There should be exactly one specimen media", 1,
1346
                specimenFacade.getDerivedUnitMedia().size());
1347
        Assert.assertEquals("The only media should be media2", media2,
1348
                specimenFacade.getDerivedUnitMedia().get(0));
1349

    
1350
        specimenFacade.removeDerivedUnitMedia(media1);
1351
        Assert.assertEquals("There should still be exactly one specimen media",
1352
                1, specimenFacade.getDerivedUnitMedia().size());
1353

    
1354
        specimenFacade.removeDerivedUnitMedia(media2);
1355
        Assert.assertEquals("There should remain no media in the gallery", 0,
1356
                media.size());
1357
    }
1358

    
1359
    /**
1360
     * Test method for
1361
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAccessionNumber()}
1362
     * .
1363
     */
1364
    @Test
1365
    public void testGetSetAccessionNumber() {
1366
        Assert.assertEquals("Accession number must be same", accessionNumber,
1367
                specimenFacade.getAccessionNumber());
1368
        specimenFacade.setAccessionNumber("A12345693");
1369
		Assert.assertEquals("New accession number must be 'A12345693'",
1370
                "A12345693", specimenFacade.getAccessionNumber());
1371
    }
1372

    
1373

    
1374
    /**
1375
     * Test method for
1376
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCatalogNumber()}.
1377
     */
1378
    @Test
1379
    public void testGetCatalogNumber() {
1380
        Assert.assertEquals("Catalog number must be same", catalogNumber,
1381
                specimenFacade.getCatalogNumber());
1382
        specimenFacade.setCatalogNumber("B12345693");
1383
		Assert.assertEquals("New catalog number must be 'B12345693'",
1384
                "B12345693", specimenFacade.getCatalogNumber());
1385
    }
1386

    
1387
    /**
1388
     * Test method for
1389
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getPreservation()}.
1390
     */
1391
    @Test
1392
    public void testGetPreservation() {
1393
        try {
1394
            Assert.assertNotNull("Preservation method must not be null",
1395
                    specimenFacade.getPreservationMethod());
1396
            Assert.assertEquals("Preservation method must be same",
1397
                    preservationMethod, specimenFacade.getPreservationMethod());
1398
            specimenFacade.setPreservationMethod(null);
1399
            Assert.assertNull("Preservation method must be null",
1400
                    specimenFacade.getPreservationMethod());
1401
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1402
            Assert.fail("Method not supported should not be thrown for a specimen");
1403
        }
1404
        specimenFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.Observation);
1405
        try {
1406
            specimenFacade.setPreservationMethod(preservationMethod);
1407
            Assert.fail("Method not supported should be thrown for an observation on set preservation method");
1408

    
1409
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1410
            // ok
1411
        }
1412
        specimenFacade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.LivingSpecimen);
1413
        try {
1414
            specimenFacade.getPreservationMethod();
1415
            Assert.fail("Method not supported should be thrown for a living being on get preservation method");
1416
        } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1417
            // ok
1418
        }
1419

    
1420
    }
1421

    
1422
    @Test
1423
    public void testGetSetOriginalLabelInfo() {
1424
        Assert.assertEquals("Original label info must be same", originalLabelInfo,
1425
                specimenFacade.getOriginalLabelInfo());
1426
        specimenFacade.setOriginalLabelInfo("OrigLabel Info xxx");
1427
        Assert.assertEquals("New accession number must be 'OrigLabel Info xxx'",
1428
                "OrigLabel Info xxx", specimenFacade.getOriginalLabelInfo());
1429
    }
1430

    
1431
    @Test
1432
    public void testGetSetStableUri() {
1433
        Assert.assertEquals("Preferred stable URI must be same", stableUri,
1434
                specimenFacade.getPreferredStableUri());
1435
        URI newURI = URI.create("http://www.fgh.ij");
1436
        specimenFacade.setPreferredStableUri(newURI);
1437
        Assert.assertEquals("New preferred stable must be '" + newURI.toString() + "'",
1438
                newURI, specimenFacade.getPreferredStableUri());
1439
    }
1440

    
1441

    
1442

    
1443
    /**
1444
     * Test method for
1445
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getStoredUnder()}.
1446
     */
1447
    @Test
1448
    public void testGetStoredUnder() {
1449
        Assert.assertNotNull("Stored under name must not be null",
1450
                specimenFacade.getStoredUnder());
1451
        Assert.assertEquals("Stored under name must be same", taxonName,
1452
                specimenFacade.getStoredUnder());
1453
        specimenFacade.setStoredUnder(null);
1454
        Assert.assertNull("Stored under name must be null",
1455
                specimenFacade.getStoredUnder());
1456
    }
1457

    
1458
//	/**
1459
//	 * Test method for
1460
//	 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectorsNumber()}
1461
//	 * .
1462
//	 */
1463
//	@Test
1464
//	public void testGetSetCollectorsNumber() {
1465
//		Assert.assertEquals("Collectors number must be same", collectorsNumber,
1466
//				specimenFacade.getCollectorsNumber());
1467
//		specimenFacade.setCollectorsNumber("C12345693");
1468
//		Assert.assertEquals("New collectors number must be 'C12345693'",
1469
//				"C12345693", specimenFacade.getCollectorsNumber());
1470
//	}
1471

    
1472
    /**
1473
     * Test method for
1474
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTitleCache()}.
1475
     */
1476
    @Test
1477
    public void testGetTitleCache() {
1478
        Assert.assertNotNull(
1479
                "The title cache should never return null if not protected",
1480
                specimenFacade.getTitleCache());
1481
        specimenFacade.setTitleCache(null, false);
1482
        Assert.assertNotNull(
1483
                "The title cache should never return null if not protected",
1484
                specimenFacade.getTitleCache());
1485
    }
1486

    
1487
    /**
1488
     * Test method for
1489
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setTitleCache(java.lang.String)}
1490
     * .
1491
     */
1492
    @Test
1493
    public void testSetTitleCache() {
1494
        String testTitle = "Absdwk aksjlf";
1495
        specimenFacade.setTitleCache(testTitle, true);
1496
        Assert.assertEquals(
1497
                "Protected title cache should returns the test title",
1498
                testTitle, specimenFacade.getTitleCache());
1499
        specimenFacade.setTitleCache(testTitle, false);
1500
        Assert.assertFalse(
1501
                "Unprotected title cache should not return the test title",
1502
                testTitle.equals(specimenFacade.getTitleCache()));
1503
    }
1504

    
1505
    /**
1506
     * Test method for
1507
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerDerivedUnit()}.
1508
     */
1509
    @Test
1510
    public void testGetSpecimen() {
1511
        Assert.assertEquals("Specimen must be same", specimen,
1512
                specimenFacade.innerDerivedUnit());
1513
    }
1514

    
1515
    /**
1516
     * Test method for
1517
     * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollection()}.
1518
     */
1519
    @Test
1520
    public void testGetSetCollection() {
1521
        Assert.assertNotNull("Collection must not be null",
1522
                specimenFacade.getCollection());
1523
        Assert.assertEquals("Collection must be same", collection,
1524
                specimenFacade.getCollection());
1525
        specimenFacade.setCollection(null);
1526
        Assert.assertNull("Collection must be null",
1527
                specimenFacade.getCollection());
1528
    }
1529

    
1530
    @Test
1531
    public void testAddGetRemoveSource() {
1532
        Assert.assertEquals("No sources should exist yet", 0, specimenFacade.getSources().size());
1533

    
1534
        Reference reference = ReferenceFactory.newBook();
1535
        IdentifiableSource source1 = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference, "54", "myName");
1536
        Assert.assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1537
        IdentifiableSource source2 = IdentifiableSource.NewDataImportInstance("1", "myTable");
1538
        specimenFacade.addSource(source2);
1539
        Assert.assertEquals("One source should exist now", 2, specimenFacade.getSources().size());
1540
        specimenFacade.removeSource(source1);
1541
        Assert.assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1542
        Reference reference2 = ReferenceFactory.newJournal();
1543
        IdentifiableSource sourceNotUsed = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference2,null, null);
1544
        specimenFacade.removeSource(sourceNotUsed);
1545
        Assert.assertEquals("One source should still exist", 1, specimenFacade.getSources().size());
1546
        Assert.assertEquals("1", specimenFacade.getSources().iterator().next().getIdInSource());
1547
        specimenFacade.removeSource(source2);
1548
        Assert.assertEquals("No sources should exist anymore", 0,specimenFacade.getSources().size());
1549
    }
1550

    
1551
    @Test
1552
    public void testAddGetRemoveDuplicate() {
1553
        Assert.assertEquals("No duplicates should be available yet", 0,specimenFacade.getDuplicates().size());
1554
        DerivedUnit newSpecimen1 = DerivedUnit.NewPreservedSpecimenInstance();
1555
        specimenFacade.addDuplicate(newSpecimen1);
1556
        Assert.assertEquals("There should be 1 duplicate now", 1,specimenFacade.getDuplicates().size());
1557
        DerivedUnit newSpecimen2 = DerivedUnit.NewPreservedSpecimenInstance();
1558
        DerivationEvent newDerivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
1559
        newSpecimen2.setDerivedFrom(newDerivationEvent);
1560
        Assert.assertSame(
1561
                "The derivation event should be 'newDerivationEvent'",
1562
                newDerivationEvent, newSpecimen2.getDerivedFrom());
1563
       	specimenFacade.addDuplicate(newSpecimen2);
1564
        Assert.assertEquals("There should be 2 duplicates now", 2, specimenFacade.getDuplicates().size());
1565
        Assert.assertNotSame(
1566
                "The derivation event should not be 'newDerivationEvent' anymore",
1567
                newDerivationEvent, newSpecimen2.getDerivedFrom());
1568
        Assert.assertSame(
1569
                "The derivation event should not be the facades derivation event",
1570
                derivationEvent, newSpecimen2.getDerivedFrom());
1571
        specimenFacade.removeDuplicate(newSpecimen1);
1572
        Assert.assertEquals("There should be 1 duplicate now", 1,
1573
                specimenFacade.getDuplicates().size());
1574
        Assert.assertSame("The only duplicate should be 'newSpecimen2' now",
1575
                newSpecimen2, specimenFacade.getDuplicates().iterator().next());
1576
       	specimenFacade.addDuplicate(specimenFacade.innerDerivedUnit());
1577
        Assert.assertEquals(
1578
                "There should be still 1 duplicate because the facade specimen is not a duplicate",
1579
                1, specimenFacade.getDuplicates().size());
1580

    
1581
        Collection newCollection = Collection.NewInstance();
1582
        String catalogNumber = "1234890";
1583
        String accessionNumber = "345345";
1584
        TaxonNameBase<?,?> storedUnder = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
1585
        PreservationMethod method = PreservationMethod.NewInstance();
1586
        DerivedUnit duplicateSpecimen = specimenFacade.addDuplicate(newCollection,
1587
				catalogNumber, accessionNumber, storedUnder,
1588
			    method);
1589
        Assert.assertEquals("There should be 2 duplicates now", 2,
1590
                specimenFacade.getDuplicates().size());
1591
        specimenFacade.removeDuplicate(newSpecimen2);
1592
		Assert.assertEquals("There should be 1 duplicates now", 1,
1593
                specimenFacade.getDuplicates().size());
1594
        Collection sameCollection = specimenFacade.getDuplicates().iterator().next().getCollection();
1595
        Assert.assertSame("Collections should be same", newCollection, sameCollection);
1596
    }
1597

    
1598
    // ************************** Existing Specimen with multiple derivation
1599
    // events in line **************/
1600

    
1601
    @Test
1602
    public void testExistingSpecimen() {
1603
        specimenFacade = null;
1604
        try {
1605
            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1606
        } catch (DerivedUnitFacadeNotSupportedException e) {
1607
            Assert.fail("Multiple derivation events in line should not throw a not supported exception");
1608
        }
1609
        Assert.assertSame(
1610
                "Gathering event should derive from the derivation line",
1611
                existingGatheringEvent, specimenFacade.innerGatheringEvent());
1612
        Assert.assertEquals(
1613
                "Mediasize should be 0. Only Imagegallery media are supported",
1614
                0, specimenFacade.getFieldObjectMedia().size());
1615
    }
1616

    
1617
    @Test
1618
    public void testMultipleFieldUnitNotSupported() {
1619
        specimenFacade = null;
1620
        FieldUnit secondFieldObject = FieldUnit.NewInstance();
1621
        firstDerivationEvent.addOriginal(secondFieldObject);
1622
        try {
1623
            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1624
            Assert.fail("Multiple field units for one specimen should no be supported by the facade");
1625
        } catch (DerivedUnitFacadeNotSupportedException e) {
1626
            // ok
1627
        }
1628
        Assert.assertNull("Specimen facade should not be initialized",
1629
                specimenFacade);
1630
    }
1631

    
1632
// 	not required anymore #3597
1633
//    @Test // #######DerivationEvent
1634
//    public void testOnlyImageGallerySupported() {
1635
//        specimenFacade = null;
1636
//        firstFieldObject.addMedia(media1);
1637
//        try {
1638
//            specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1639
//            Assert.fail("Only image galleries are supported by the facade but not direct media");
1640
//        } catch (DerivedUnitFacadeNotSupportedException e) {
1641
//            // ok
1642
//        }
1643
//        Assert.assertNull("Specimen facade should not be initialized",
1644
//                specimenFacade);
1645
//    }
1646

    
1647
    @Test // #######DerivationEvent
1648
    public void testEventPropagation() {
1649
        specimenFacade.setDistanceToGround(24.0);
1650

    
1651
    }
1652

    
1653
    // @Ignore // set to ignore because I did not want to check knowingly
1654
    // failing tests. Remove @Ignore when this is fixed
1655
    @Test
1656
    public void testSetBarcode() {
1657
        String barcode = "barcode";
1658
        specimenFacade.setBarcode(barcode);
1659

    
1660
        Assert.assertEquals(barcode, specimenFacade.getBarcode());
1661
    }
1662

    
1663
    @Test
1664
    public void testIsEvenDistance() {
1665
        Integer minimum = 20;
1666
        Integer maximum = 1234;
1667

    
1668
        // this should not throw exceptions
1669
        specimenFacade.setAbsoluteElevationRange(minimum, maximum);
1670
    }
1671

    
1672
    /**
1673
     *
1674
     * See https://dev.e-taxonomy.eu/trac/ticket/2426
1675
     * This test doesn't handle the above issue yet as it doesn't fire events as
1676
     * expected (at least it does not reproduce the behaviour in the Taxonomic Editor).
1677
     * In the meanwhile the property change framework for the facade has been changed
1678
     * so the original problem may have disappeared.
1679
     *
1680
     */
1681
    @Test
1682
    @Ignore
1683
    public void testNoRecursiveChangeEvents(){
1684
        String username = "username";
1685
        String password = "password";
1686
        User user = User.NewInstance(username, password);
1687
        userService.save(user);
1688
        UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
1689
        SecurityContextHolder.getContext().setAuthentication(token);
1690

    
1691
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
1692
        facade.setLocality("testLocality");
1693
        facade.getTitleCache();
1694
//		facade.innerGatheringEvent().firePropertyChange("createdBy", null, user);
1695
        this.service.save(facade.innerDerivedUnit());
1696
        commitAndStartNewTransaction(null);
1697
    }
1698

    
1699
    @Test
1700
    public void testBaseUnit() throws DerivedUnitFacadeNotSupportedException{
1701
        DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
1702
        assertEquals("baseUnit is incorrect", specimen, facade.baseUnit());
1703

    
1704
        facade = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.FieldUnit, fieldUnit);
1705
        assertEquals("baseUnit is incorrect", fieldUnit, facade.baseUnit());
1706

    
1707
        facade = DerivedUnitFacade.NewInstance(specimen);
1708
        facade.getFieldUnit(true);
1709
        assertEquals("baseUnit is incorrect", specimen, facade.baseUnit());
1710
    }
1711

    
1712
    @Override
1713
    public void createTestDataSet() throws FileNotFoundException {
1714
        // TODO Auto-generated method stub
1715

    
1716
    }
1717

    
1718
}
(4-4/4)