Project

General

Profile

Download (77.8 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2009 EDIT
4
 * European Distributed Institute of Taxonomy
5
 * http://www.e-taxonomy.eu
6
 *
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.cdm.api.facade;
11

    
12
import static org.junit.Assert.assertEquals;
13

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

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

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

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

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

    
80

    
81
    @SpringBeanByType
82
    private IOccurrenceService service;
83

    
84
    @SpringBeanByType
85
    private ITermService termService;
86

    
87
    @SpringBeanByType
88
    private IUserService userService;
89

    
90

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

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

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

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

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

    
127

    
128
    DerivedUnitFacade specimenFacade;
129

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

    
136
    DerivedUnitFacade emptyFacade;
137

    
138
    // ****************************** SET UP **********************************/
139

    
140

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

    
147
        specimen = DerivedUnit.NewPreservedSpecimenInstance();
148

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

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

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

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

    
183
        specimenFacade = DerivedUnitFacade.NewInstance(specimen);
184

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

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

    
196
        collectionSpecimen.setDerivedFrom(lastDerivationEvent);
197

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

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

    
207
    }
208

    
209
    // ****************************** TESTS*****************************/
210

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

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

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

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

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

    
248
    }
249

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

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

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

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

    
287

    
288
    }
289

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

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

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

    
355
    }
356

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

    
374

    
375

    
376

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

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

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

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

    
412
    }
413

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

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

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

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

    
469
    }
470

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

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

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

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

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

    
532
    }
533

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

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

    
559
    }
560

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

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

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

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

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

    
588
    }
589

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

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

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

    
607

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

    
613

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

    
621

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

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

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

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

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

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

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

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

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

    
711

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
889
    }
890

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

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

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

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

    
948
    }
949

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

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

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

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

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

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

    
1035
    }
1036

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

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

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

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

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

    
1126
    }
1127

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

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

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

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

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

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

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

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

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

    
1252
    }
1253

    
1254

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

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

    
1277

    
1278

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

    
1290

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

    
1304
    }
1305

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

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

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

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

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

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

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

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

    
1374

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

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

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

    
1421
    }
1422

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

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

    
1442

    
1443

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1652
    }
1653

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

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

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

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

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

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

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

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

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

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

    
1717
    }
1718

    
1719
}
(4-4/4)