Project

General

Profile

Download (77.7 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.occurrence.Collection;
56
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
57
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
58
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
59
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
60
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
61
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
62
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
63
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
64
import eu.etaxonomy.cdm.model.reference.Reference;
65
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
66
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
67

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

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

    
78

    
79
    @SpringBeanByType
80
    private IOccurrenceService service;
81

    
82
    @SpringBeanByType
83
    private ITermService termService;
84

    
85
    @SpringBeanByType
86
    private IUserService userService;
87

    
88

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

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

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

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

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

    
125

    
126
    DerivedUnitFacade specimenFacade;
127

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

    
134
    DerivedUnitFacade emptyFacade;
135

    
136
    // ****************************** SET UP **********************************/
137

    
138

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

    
145
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
146

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

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

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

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

    
181
        specimenFacade = DerivedUnitFacade.NewInstance(specimen);
182

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

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

    
194
        collectionSpecimen.setDerivedFrom(lastDerivationEvent);
195

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

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

    
205
    }
206

    
207
    // ****************************** TESTS*****************************/
208

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

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

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

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

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

    
246
    }
247

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

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

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

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

    
285

    
286
    }
287

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

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

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

    
353
    }
354

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

    
372

    
373

    
374

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

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

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

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

    
410
    }
411

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

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

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

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

    
467
    }
468

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

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

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

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

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

    
530
    }
531

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

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

    
557
    }
558

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

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

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

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

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

    
586
    }
587

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

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

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

    
605

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

    
611

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

    
619

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

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

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

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

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

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

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

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

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

    
709

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
887
    }
888

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

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

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

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

    
946
    }
947

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

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

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

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

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

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

    
1033
    }
1034

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

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

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

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

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

    
1124
    }
1125

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

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

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

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

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

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

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

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

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

    
1250
    }
1251

    
1252

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

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

    
1275

    
1276

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

    
1288

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

    
1302
    }
1303

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

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

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

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

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

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

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

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

    
1372

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

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

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

    
1419
    }
1420

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

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

    
1440

    
1441

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1650
    }
1651

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

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

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

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

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

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

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

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

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

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

    
1715
    }
1716

    
1717
}
(4-4/4)