sync trunk
[cdmlib.git] / cdmlib-services / src / test / java / eu / etaxonomy / cdm / api / facade / DerivedUnitFacadeTest.java
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 java.lang.reflect.Field;
13 import java.net.URI;
14 import java.text.ParseException;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.UUID;
18
19 import org.apache.log4j.Logger;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.BeforeClass;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
26 import org.springframework.security.core.context.SecurityContextHolder;
27 import org.unitils.dbunit.annotation.DataSet;
28 import org.unitils.dbunit.annotation.ExpectedDataSet;
29 import org.unitils.spring.annotation.SpringBeanByType;
30
31 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
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.model.agent.AgentBase;
36 import eu.etaxonomy.cdm.model.agent.Team;
37 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
38 import eu.etaxonomy.cdm.model.common.Language;
39 import eu.etaxonomy.cdm.model.common.LanguageString;
40 import eu.etaxonomy.cdm.model.common.TimePeriod;
41 import eu.etaxonomy.cdm.model.common.User;
42 import eu.etaxonomy.cdm.model.description.Feature;
43 import eu.etaxonomy.cdm.model.description.Sex;
44 import eu.etaxonomy.cdm.model.description.SpecimenDescription;
45 import eu.etaxonomy.cdm.model.description.Stage;
46 import eu.etaxonomy.cdm.model.description.TextData;
47 import eu.etaxonomy.cdm.model.location.NamedArea;
48 import eu.etaxonomy.cdm.model.location.Point;
49 import eu.etaxonomy.cdm.model.location.ReferenceSystem;
50 import eu.etaxonomy.cdm.model.location.TdwgArea;
51 import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
52 import eu.etaxonomy.cdm.model.media.Media;
53 import eu.etaxonomy.cdm.model.name.BotanicalName;
54 import eu.etaxonomy.cdm.model.name.Rank;
55 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
56 import eu.etaxonomy.cdm.model.occurrence.Collection;
57 import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
58 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
59 import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
60 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
61 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
62 import eu.etaxonomy.cdm.model.occurrence.Specimen;
63 import eu.etaxonomy.cdm.model.reference.Reference;
64 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
65 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
66
67 /**
68 * @author a.mueller
69 * @date 17.05.2010
70 *
71 */
72 public class DerivedUnitFacadeTest extends CdmTransactionalIntegrationTest {
73 private static final Logger logger = Logger.getLogger(DerivedUnitFacadeTest.class);
74
75 @SpringBeanByType
76 private IOccurrenceService service;
77
78 @SpringBeanByType
79 private ITermService termService;
80
81 Specimen specimen;
82 DerivationEvent derivationEvent;
83 FieldObservation fieldObservation;
84 GatheringEvent gatheringEvent;
85 Integer absoluteElevation = 10;
86 Integer absoluteElevationError = 2;
87 AgentBase collector = Team.NewInstance();
88 String collectingMethod = "Collection Method";
89 Integer distanceToGround = 22;
90 Integer distanceToSurface = 50;
91 ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
92 Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
93 String gatheringEventDescription = "A nice gathering description";
94 TimePeriod gatheringPeriod = TimePeriod.NewInstance(1888, 1889);
95
96 String fieldNumber = "15p23B";
97 String fieldNotes = "such a beautiful specimen";
98
99 Integer individualCount = 1;
100 Stage lifeStage = Stage.NewInstance("A wonderful stage", "stage", "st");
101 Sex sex = Sex.NewInstance("FemaleMale", "FM", "FM");
102 LanguageString locality = LanguageString.NewInstance("My locality",
103 Language.DEFAULT());
104
105 String accessionNumber = "888462535";
106 String catalogNumber = "UU879873590";
107 TaxonNameBase taxonName = BotanicalName.NewInstance(Rank.GENUS(), "Abies",
108 null, null, null, null, null, null, null);
109 String collectorsNumber = "234589913A34";
110 Collection collection = Collection.NewInstance();
111 PreservationMethod preservationMethod = PreservationMethod.NewInstance(
112 "my prservation", null, null);
113
114 DerivedUnitFacade specimenFacade;
115
116 Specimen collectionSpecimen;
117 GatheringEvent existingGatheringEvent;
118 DerivationEvent firstDerivationEvent;
119 FieldObservation firstFieldObject;
120 Media media1 = Media.NewInstance();
121
122 DerivedUnitFacade emptyFacade;
123
124 NamedArea country = WaterbodyOrCountry.GERMANY();
125
126 // ****************************** SET UP
127 // *****************************************/
128
129 /**
130 * @throws java.lang.Exception
131 */
132 @BeforeClass
133 public static void setUpBeforeClass() throws Exception {
134 // new DefaultTermInitializer().initialize();
135 }
136
137 /**
138 * @throws java.lang.Exception
139 */
140 @Before
141 public void setUp() throws Exception {
142 specimen = Specimen.NewInstance();
143
144 derivationEvent = DerivationEvent.NewInstance();
145 specimen.setDerivedFrom(derivationEvent);
146 fieldObservation = FieldObservation.NewInstance();
147 fieldObservation.addDerivationEvent(derivationEvent);
148 gatheringEvent = GatheringEvent.NewInstance();
149 fieldObservation.setGatheringEvent(gatheringEvent);
150 gatheringEvent.setAbsoluteElevation(absoluteElevation);
151 gatheringEvent.setAbsoluteElevationError(absoluteElevationError);
152 gatheringEvent.setActor(collector);
153 gatheringEvent.setCollectingMethod(collectingMethod);
154 gatheringEvent.setDistanceToGround(distanceToGround);
155 gatheringEvent.setDistanceToWaterSurface(distanceToSurface);
156 gatheringEvent.setExactLocation(exactLocation);
157 gatheringEvent.setDescription(gatheringEventDescription);
158 gatheringEvent.setCountry(country);
159
160 gatheringEvent.setTimeperiod(gatheringPeriod);
161 gatheringEvent.setLocality(locality);
162
163 fieldObservation.setFieldNumber(fieldNumber);
164 fieldObservation.setFieldNotes(fieldNotes);
165 fieldObservation.setIndividualCount(individualCount);
166 fieldObservation.setSex(sex);
167 fieldObservation.setLifeStage(lifeStage);
168
169 specimen.setAccessionNumber(accessionNumber);
170 specimen.setCatalogNumber(catalogNumber);
171 specimen.setStoredUnder(taxonName);
172 specimen.setCollection(collection);
173 specimen.setPreservation(preservationMethod);
174
175 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
176
177 // existing specimen with 2 derivation events in line
178 collectionSpecimen = Specimen.NewInstance();
179 Specimen middleSpecimen = Specimen.NewInstance();
180 firstFieldObject = FieldObservation.NewInstance();
181
182 DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance();
183 DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance();
184 firstDerivationEvent = DerivationEvent.NewInstance();
185
186 collectionSpecimen.setDerivedFrom(lastDerivationEvent);
187
188 lastDerivationEvent.addOriginal(middleSpecimen);
189 middleSpecimen.setDerivedFrom(firstDerivationEvent);
190 firstDerivationEvent.addOriginal(firstFieldObject);
191 existingGatheringEvent = GatheringEvent.NewInstance();
192 firstFieldObject.setGatheringEvent(existingGatheringEvent);
193
194 // empty facade
195 emptyFacade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
196
197 }
198
199 // ****************************** TESTS
200 // *****************************************/
201
202 @Test
203 @DataSet("DerivedUnitFacadeTest.testSetFieldObjectImageGallery.xml")
204 @ExpectedDataSet
205 public void testSetFieldObjectImageGallery() {
206 UUID imageFeatureUuid = Feature.IMAGE().getUuid();
207 Feature imageFeature = (Feature) termService.find(imageFeatureUuid);
208
209 DerivedUnitFacade facade = DerivedUnitFacade
210 .NewInstance(DerivedUnitType.Specimen);
211 facade.setFieldNumber("12345");
212 Media media = Media.NewInstance(URI.create("www.abc.de"), 200, null,
213 "jpeg");
214
215 try {
216 SpecimenDescription imageGallery = SpecimenDescription
217 .NewInstance();
218 imageGallery.addDescribedSpecimenOrObservation(facade
219 .innerFieldObservation());
220 imageGallery.setImageGallery(true);
221 TextData textData = TextData.NewInstance();
222 textData.setFeature(imageFeature);
223 imageGallery.addElement(textData);
224 textData.addMedia(media);
225 facade.setFieldObjectImageGallery(imageGallery);
226
227 } catch (DerivedUnitFacadeNotSupportedException e1) {
228 e1.printStackTrace();
229 Assert.fail(e1.getLocalizedMessage());
230 }
231 this.service.save(facade.innerDerivedUnit());
232
233 // setComplete(); endTransaction();
234 // try {if (true){printDataSet(System.out, new
235 // String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
236 // ,"DERIVATIONEVENT",
237 // "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA","DESCRIPTIONBASE_SPECIMENOROBSERVATIONBASE",
238 // "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
239 // } catch(Exception e) { logger.warn(e);}
240
241 }
242
243 @Test
244 @Ignore
245 // TODO generally works but has id problems when running together with above
246 // test ()setFieldObjectImageGallery. Therefore set to ignore.
247 @DataSet("DerivedUnitFacadeTest.testSetDerivedUnitImageGallery.xml")
248 @ExpectedDataSet
249 public void testSetDerivedUnitImageGallery() {
250 // UUID specimenUUID =
251 // UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
252 // Specimen specimen = (Specimen)service.find(specimenUUID);
253 DerivedUnitFacade facade = DerivedUnitFacade
254 .NewInstance(DerivedUnitType.Specimen);
255 Media media = Media.NewInstance(URI.create("www.derivedUnitImage.de"),
256 200, null, "png");
257
258 try {
259 SpecimenDescription imageGallery = SpecimenDescription
260 .NewInstance();
261 imageGallery.addDescribedSpecimenOrObservation(facade
262 .innerDerivedUnit());
263 imageGallery.setImageGallery(true);
264 TextData textData = TextData.NewInstance();
265 imageGallery.addElement(textData);
266 textData.addMedia(media);
267 facade.setDerivedUnitImageGallery(imageGallery);
268
269 } catch (DerivedUnitFacadeNotSupportedException e1) {
270 e1.printStackTrace();
271 Assert.fail(e1.getLocalizedMessage());
272 }
273 this.service.save(facade.innerDerivedUnit());
274
275 // setComplete(); endTransaction();
276 // try {if (true){printDataSet(System.out, new
277 // String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
278 // ,"DERIVATIONEVENT",
279 // "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA","DESCRIPTIONBASE_SPECIMENOROBSERVATIONBASE",
280 // "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
281 // } catch(Exception e) { logger.warn(e);}
282
283 }
284
285 @Test
286 @DataSet
287 public void testGetFieldObjectImageGalleryBooleanPersisted() {
288 UUID specimenUUID = UUID
289 .fromString("25383fc8-789b-4eff-92d3-a770d0622351");
290 Specimen specimen = (Specimen) service.find(specimenUUID);
291 Assert.assertNotNull("Specimen should exist (persisted)", specimen);
292 try {
293 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
294 SpecimenDescription imageGallery = facade
295 .getFieldObjectImageGallery(true);
296 Assert.assertNotNull("Image gallery should exist", imageGallery);
297 Assert.assertEquals("UUID should be equal to the persisted uuid",
298 UUID.fromString("8cb772e9-1577-45c6-91ab-dbec1413c060"),
299 imageGallery.getUuid());
300 Assert.assertEquals("The image gallery should be flagged as such",
301 true, imageGallery.isImageGallery());
302 Assert.assertEquals(
303 "There should be one TextData in image gallery", 1,
304 imageGallery.getElements().size());
305 List<Media> media = imageGallery.getElements().iterator().next()
306 .getMedia();
307 Assert.assertEquals("There should be 1 media", 1, media.size());
308 } catch (DerivedUnitFacadeNotSupportedException e) {
309 e.printStackTrace();
310 Assert.fail();
311 }
312 }
313
314 @Test
315 @DataSet
316 public void testGetDerivedUnitImageGalleryBooleanPersisted() {
317 UUID specimenUUID = UUID
318 .fromString("25383fc8-789b-4eff-92d3-a770d0622351");
319 Specimen specimen = (Specimen) service.find(specimenUUID);
320 Assert.assertNotNull("Specimen should exist (persisted)", specimen);
321 try {
322 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
323 SpecimenDescription imageGallery = facade
324 .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()
335 .getMedia();
336 Assert.assertEquals("There should be 1 media", 1, media.size());
337 } catch (DerivedUnitFacadeNotSupportedException e) {
338 e.printStackTrace();
339 Assert.fail();
340 }
341 }
342
343 @Test
344 public void testGetDerivedUnitImageGalleryBoolean() {
345 Specimen specimen = Specimen.NewInstance();
346 try {
347 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
348 SpecimenDescription imageGallery = facade
349 .getDerivedUnitImageGallery(true);
350 Assert.assertNotNull("Image Gallery should have been created",
351 imageGallery);
352 Assert.assertEquals("The image gallery should be flagged as such",
353 true, imageGallery.isImageGallery());
354 } catch (DerivedUnitFacadeNotSupportedException e) {
355 e.printStackTrace();
356 Assert.fail();
357 }
358
359 }
360
361 /**
362 * Test method for
363 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance()}.
364 */
365 @Test
366 public void testNewInstance() {
367 Assert.assertNotNull("The specimen should have been created",
368 specimenFacade.innerDerivedUnit());
369 // ???
370 // Assert.assertNotNull("The derivation event should have been created",
371 // specimenFacade.getSpecimen().getDerivedFrom());
372 // Assert.assertNotNull("The field observation should have been created",
373 // specimenFacade.getFieldObservation());
374 // Assert.assertNotNull("The gathering event should have been created",
375 // specimenFacade.getGatheringEvent());
376 }
377
378 /**
379 * Test method for
380 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance(eu.etaxonomy.cdm.model.occurrence.Specimen)}
381 * .
382 */
383 @Test
384 public void testNewInstanceSpecimen() {
385 Assert.assertSame("Specimen should be same", specimen,
386 specimenFacade.innerDerivedUnit());
387 Assert.assertSame("Derivation event should be same", derivationEvent,
388 specimenFacade.innerDerivedUnit().getDerivedFrom());
389 Assert.assertSame("Field observation should be same", fieldObservation,
390 specimenFacade.innerFieldObservation());
391 Assert.assertSame("Gathering event should be same", gatheringEvent,
392 specimenFacade.innerGatheringEvent());
393
394 }
395
396 @Test
397 public void testGatheringEventIsConnectedToDerivedUnit() {
398 Specimen specimen = Specimen.NewInstance();
399 DerivedUnitFacade specimenFacade;
400 try {
401 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
402 specimenFacade.setDistanceToGround(2);
403 FieldObservation specimenFieldObservation = (FieldObservation) specimen
404 .getDerivedFrom().getOriginals().iterator().next();
405 Assert.assertSame(
406 "Facade gathering event and specimen gathering event should be the same",
407 specimenFacade.innerGatheringEvent(),
408 specimenFieldObservation.getGatheringEvent());
409 } catch (DerivedUnitFacadeNotSupportedException e) {
410 Assert.fail("An error should not occur in NewInstance()");
411 }
412 }
413
414 @Test
415 public void testNoGatheringEventAndFieldObservation() {
416 Specimen specimen = Specimen.NewInstance();
417 DerivedUnitFacade specimenFacade;
418 try {
419 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
420 Assert.assertNull("No field observation should exists",
421 specimenFacade.innerFieldObservation());
422 } catch (DerivedUnitFacadeNotSupportedException e) {
423 Assert.fail("An error should not occur in NewInstance()");
424 }
425 }
426
427 @Test
428 public void testInititializeTextDataWithSupportTest() {
429 // TODO
430 Specimen specimen = Specimen.NewInstance();
431 DerivedUnitFacade specimenFacade;
432 try {
433 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
434 specimenFacade.setEcology("Ecology");
435 String plantDescription = specimenFacade.getPlantDescription();
436 Assert.assertNull(
437 "No plantDescription should exist yet and no NPE should be thrown until now",
438 plantDescription);
439 } catch (DerivedUnitFacadeNotSupportedException e) {
440 Assert.fail("An error should not occur in NewInstance()");
441 }
442 }
443
444 @Test
445 public void testGetSetCountry() {
446 logger.warn("Not yet implemented");
447 }
448
449 /**
450 * Test method for
451 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
452 * .
453 */
454 @Test
455 public void testAddGetRemoveCollectingArea() {
456 String tdwgLabel = "GER";
457 NamedArea tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwgLabel);
458 NamedArea newCollectingArea = NamedArea.NewInstance("A nice area",
459 "nice", "n");
460 specimenFacade.addCollectingArea(newCollectingArea);
461 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
462 .getCollectingAreas().size());
463 Assert.assertSame("Areas should be same", newCollectingArea,
464 specimenFacade.innerFieldObservation().getGatheringEvent()
465 .getCollectingAreas().iterator().next());
466 specimenFacade.addCollectingArea(tdwgArea);
467 Assert.assertEquals("Exactly 2 areas must exist", 2, specimenFacade
468 .getCollectingAreas().size());
469 specimenFacade.removeCollectingArea(newCollectingArea);
470 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
471 .getCollectingAreas().size());
472 NamedArea remainingArea = specimenFacade.getCollectingAreas()
473 .iterator().next();
474 Assert.assertEquals("Areas should be same", tdwgArea, remainingArea);
475 specimenFacade.removeCollectingArea(tdwgArea);
476 Assert.assertEquals("No area should remain", 0, specimenFacade
477 .getCollectingAreas().size());
478 }
479
480 /**
481 * Test method for
482 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
483 * .
484 */
485 @Test
486 public void testAddCollectingAreas() {
487 NamedArea firstArea = NamedArea.NewInstance("A nice area", "nice", "n");
488 Assert.assertEquals("No area must exist", 0, specimenFacade
489 .getCollectingAreas().size());
490 specimenFacade.addCollectingArea(firstArea);
491 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
492 .getCollectingAreas().size());
493
494 String tdwgLabel = "GER";
495 NamedArea tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwgLabel);
496 NamedArea secondArea = NamedArea
497 .NewInstance("A nice area", "nice", "n");
498
499 java.util.Collection<NamedArea> areaCollection = new HashSet<NamedArea>();
500 areaCollection.add(secondArea);
501 areaCollection.add(tdwgArea);
502 specimenFacade.addCollectingAreas(areaCollection);
503 Assert.assertEquals("Exactly 3 areas must exist", 3, specimenFacade
504 .getCollectingAreas().size());
505
506 }
507
508 /**
509 * Test method for
510 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevation()}
511 * .
512 */
513 @Test
514 public void testGetSetAbsoluteElevation() {
515 Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
516 specimenFacade.setAbsoluteElevation(400);
517 Assert.assertEquals("Absolute elevation must be 400",Integer.valueOf(400), specimenFacade.getAbsoluteElevation());
518 }
519
520 /**
521 * Test method for
522 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevationError()}
523 * .
524 */
525 @Test
526 public void testGetSetAbsoluteElevationError() {
527 Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
528 Assert.assertEquals("Absolute elevation error must be same",absoluteElevationError, specimenFacade.getAbsoluteElevationError());
529 specimenFacade.setAbsoluteElevationError(4);
530 Assert.assertEquals("Absolute elevation error must be 4",Integer.valueOf(4), specimenFacade.getAbsoluteElevationError());
531 Assert.assertEquals("Absolute elevation must be same",absoluteElevation, specimenFacade.getAbsoluteElevation());
532
533 }
534
535 @Test()
536 public void testGetSetAbsoluteElevationRange() {
537 Integer expected = absoluteElevation - 2;
538 Assert.assertEquals("", expected, specimenFacade.getAbsoluteElevationMinimum());
539 expected = absoluteElevation + 2;
540 Assert.assertEquals("", expected,specimenFacade.getAbsoluteElevationMaximum());
541
542 specimenFacade.setAbsoluteElevationRange(30, 36);
543 Assert.assertEquals("", Integer.valueOf(36),specimenFacade.getAbsoluteElevationMaximum());
544 Assert.assertEquals("", Integer.valueOf(30),specimenFacade.getAbsoluteElevationMinimum());
545 Assert.assertEquals("", Integer.valueOf(33),specimenFacade.getAbsoluteElevation());
546 Assert.assertEquals("", Integer.valueOf(3),specimenFacade.getAbsoluteElevationError());
547
548
549 try {
550 specimenFacade.setAbsoluteElevationRange(30, 35);
551 Assert.fail("Odd distance needs to throw IllegalArgumentException");
552 } catch (IllegalArgumentException e) {
553 Assert.assertTrue("Exception needs to be thrown", true);
554 }
555
556 specimenFacade.setAbsoluteElevationRange(41, null);
557 Assert.assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevationMaximum());
558 Assert.assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevationMinimum());
559 Assert.assertEquals("", Integer.valueOf(41),specimenFacade.getAbsoluteElevation());
560 Assert.assertNotNull("", specimenFacade.getAbsoluteElevationError());
561 Assert.assertEquals("", Integer.valueOf(0),specimenFacade.getAbsoluteElevationError());
562
563 specimenFacade.setAbsoluteElevationRange(null, null);
564 Assert.assertNull("", specimenFacade.getAbsoluteElevation());
565 Assert.assertNull("", specimenFacade.getAbsoluteElevationError());
566
567 }
568
569 /**
570 */
571 @Test
572 public void testGetSetCollector() {
573 Assert.assertNotNull("Collector must not be null",
574 specimenFacade.getCollector());
575 Assert.assertEquals("Collector must be same", collector,
576 specimenFacade.getCollector());
577 specimenFacade.setCollector(null);
578 Assert.assertNull("Collector must be null",
579 specimenFacade.getCollector());
580 }
581
582 /**
583 * Test method for
584 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectingMethod()}
585 * .
586 */
587 @Test
588 public void testGetSetCollectingMethod() {
589 Assert.assertEquals("Collecting method must be same", collectingMethod,
590 specimenFacade.getCollectingMethod());
591 specimenFacade.setCollectingMethod("new method");
592 Assert.assertEquals("Collecting method must be 'new method'",
593 "new method", specimenFacade.getCollectingMethod());
594 }
595
596 /**
597 * Test method for
598 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToGround()}
599 * .
600 */
601 @Test
602 public void testGetSetDistanceToGround() {
603 Assert.assertEquals("Distance to ground must be same",
604 distanceToGround, specimenFacade.getDistanceToGround());
605 specimenFacade.setDistanceToGround(5);
606 Assert.assertEquals("Distance to ground must be 5", Integer.valueOf(5),
607 specimenFacade.getDistanceToGround());
608 }
609
610 /**
611 * Test method for
612 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getDistanceToWaterSurface()}
613 * .
614 */
615 @Test
616 public void testGetDistanceToWaterSurface() {
617 Assert.assertEquals("Distance to surface must be same",
618 distanceToSurface, specimenFacade.getDistanceToWaterSurface());
619 specimenFacade.setDistanceToWaterSurface(6);
620 Assert.assertEquals("Distance to surface must be 6",
621 Integer.valueOf(6), specimenFacade.getDistanceToWaterSurface());
622 // empty facade tests
623 Assert.assertNull("Empty facace must not have any gathering values",
624 emptyFacade.getDistanceToWaterSurface());
625 emptyFacade.setDistanceToWaterSurface(13);
626 Assert.assertNotNull(
627 "Field observation must exist if distance to water exists",
628 emptyFacade.getFieldObservation(false));
629 Assert.assertNotNull(
630 "Gathering event must exist if distance to water exists",
631 emptyFacade.getGatheringEvent(false));
632 FieldObservation specimenFieldObservation = (FieldObservation) emptyFacade
633 .innerDerivedUnit().getDerivedFrom().getOriginals().iterator()
634 .next();
635 Assert.assertSame(
636 "Gathering event of facade and of specimen must be the same",
637 specimenFieldObservation.getGatheringEvent(),
638 emptyFacade.getGatheringEvent(false));
639 }
640
641 /**
642 * Test method for
643 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getExactLocation()}.
644 */
645 @Test
646 public void testGetSetExactLocation() {
647 Assert.assertNotNull("Exact location must not be null",
648 specimenFacade.getExactLocation());
649 Assert.assertEquals("Exact location must be same", exactLocation,
650 specimenFacade.getExactLocation());
651 specimenFacade.setExactLocation(null);
652 Assert.assertNull("Exact location must be null",
653 specimenFacade.getExactLocation());
654 }
655
656 @Test
657 public void testSetExactLocationByParsing() {
658 Point point1;
659 try {
660 specimenFacade.setExactLocationByParsing("112\u00B034'20\"W",
661 "34\u00B030,34'N", null, null);
662 point1 = specimenFacade.getExactLocation();
663 Assert.assertNotNull("", point1.getLatitude());
664 System.out.println(point1.getLatitude().toString());
665 Assert.assertTrue("",
666 point1.getLatitude().toString().startsWith("34.505"));
667 System.out.println(point1.getLongitude().toString());
668 Assert.assertTrue("",
669 point1.getLongitude().toString().startsWith("-112.5722"));
670
671 } catch (ParseException e) {
672 Assert.fail("No parsing error should occur");
673 }
674 }
675
676 /**
677 * Test method for
678 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getGatheringEventDescription()}
679 * .
680 */
681 @Test
682 public void testGetSetGatheringEventDescription() {
683 Assert.assertEquals("Gathering event description must be same",
684 gatheringEventDescription,
685 specimenFacade.getGatheringEventDescription());
686 specimenFacade.setGatheringEventDescription("new description");
687 Assert.assertEquals(
688 "Gathering event description must be 'new description' now",
689 "new description",
690 specimenFacade.getGatheringEventDescription());
691 }
692
693 /**
694 * Test method for
695 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTimeperiod()}.
696 */
697 @Test
698 public void testGetTimeperiod() {
699 Assert.assertNotNull("Gathering period must not be null",
700 specimenFacade.getGatheringPeriod());
701 Assert.assertEquals("Gathering period must be same", gatheringPeriod,
702 specimenFacade.getGatheringPeriod());
703 specimenFacade.setGatheringPeriod(null);
704 Assert.assertNull("Gathering period must be null",
705 specimenFacade.getGatheringPeriod());
706 }
707
708 @Test
709 public void testHasFieldObject() throws SecurityException,
710 NoSuchFieldException, IllegalArgumentException,
711 IllegalAccessException {
712 // this test depends on the current implementation of SpecimenFacade. In
713 // future
714 // field observation may not be initialized from the beginning. Than the
715 // following
716 // assert should be set to assertNull
717 Assert.assertTrue(
718 "field object should not be null (depends on specimen facade initialization !!)",
719 specimenFacade.hasFieldObject());
720
721 Field fieldObservationField = DerivedUnitFacade.class
722 .getDeclaredField("fieldObservation");
723 fieldObservationField.setAccessible(true);
724 fieldObservationField.set(specimenFacade, null);
725 Assert.assertFalse("The field observation should be null now",
726 specimenFacade.hasFieldObject());
727
728 specimenFacade.setDistanceToGround(33);
729 Assert.assertTrue(
730 "The field observation should have been created again",
731 specimenFacade.hasFieldObject());
732 }
733
734 /**
735 * Test method for
736 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
737 * .
738 */
739 @Test
740 public void testAddGetRemoveFieldObjectDefinition() {
741 Assert.assertEquals("There should be no definition yet", 0,
742 specimenFacade.getFieldObjectDefinition().size());
743 specimenFacade.addFieldObjectDefinition("Tres interesant",
744 Language.FRENCH());
745 Assert.assertEquals("There should be exactly one definition", 1,
746 specimenFacade.getFieldObjectDefinition().size());
747 Assert.assertEquals(
748 "The French definition should be 'Tres interesant'",
749 "Tres interesant", specimenFacade.getFieldObjectDefinition()
750 .get(Language.FRENCH()).getText());
751 Assert.assertEquals(
752 "The French definition should be 'Tres interesant'",
753 "Tres interesant",
754 specimenFacade.getFieldObjectDefinition(Language.FRENCH()));
755 specimenFacade.addFieldObjectDefinition("Sehr interessant",
756 Language.GERMAN());
757 Assert.assertEquals("There should be exactly 2 definition", 2,
758 specimenFacade.getFieldObjectDefinition().size());
759 specimenFacade.removeFieldObjectDefinition(Language.FRENCH());
760 Assert.assertEquals("There should remain exactly 1 definition", 1,
761 specimenFacade.getFieldObjectDefinition().size());
762 Assert.assertEquals(
763 "The remaining German definition should be 'Sehr interessant'",
764 "Sehr interessant",
765 specimenFacade.getFieldObjectDefinition(Language.GERMAN()));
766 specimenFacade.removeFieldObjectDefinition(Language.GERMAN());
767 Assert.assertEquals("There should remain no definition", 0,
768 specimenFacade.getFieldObjectDefinition().size());
769 }
770
771 /**
772 * Test method for
773 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
774 * .
775 */
776 @Test
777 public void testAddGetHasRemoveFieldObjectMedia() {
778 Assert.assertFalse("There should be no image gallery yet",
779 specimenFacade.hasFieldObjectImageGallery());
780 Assert.assertFalse("There should be no specimen image gallery either",
781 specimenFacade.hasDerivedUnitImageGallery());
782
783 List<Media> media = specimenFacade.getFieldObjectMedia();
784 Assert.assertFalse("There should still not be an image gallery now",
785 specimenFacade.hasFieldObjectImageGallery());
786 Assert.assertEquals("There should be no media yet in the gallery", 0,
787 media.size());
788
789 Media media1 = Media.NewInstance();
790 specimenFacade.addFieldObjectMedia(media1);
791 Assert.assertEquals("There should be exactly one specimen media", 1,
792 specimenFacade.getFieldObjectMedia().size());
793 Assert.assertEquals("The only media should be media 1", media1,
794 specimenFacade.getFieldObjectMedia().get(0));
795 Assert.assertFalse(
796 "There should still no specimen image gallery exist",
797 specimenFacade.hasDerivedUnitImageGallery());
798
799 Media media2 = Media.NewInstance();
800 specimenFacade.addFieldObjectMedia(media2);
801 Assert.assertEquals("There should be exactly 2 specimen media", 2,
802 specimenFacade.getFieldObjectMedia().size());
803 Assert.assertEquals("The first media should be media1", media1,
804 specimenFacade.getFieldObjectMedia().get(0));
805 Assert.assertEquals("The second media should be media2", media2,
806 specimenFacade.getFieldObjectMedia().get(1));
807
808 specimenFacade.removeFieldObjectMedia(media1);
809 Assert.assertEquals("There should be exactly one specimen media", 1,
810 specimenFacade.getFieldObjectMedia().size());
811 Assert.assertEquals("The only media should be media2", media2,
812 specimenFacade.getFieldObjectMedia().get(0));
813
814 specimenFacade.removeFieldObjectMedia(media1);
815 Assert.assertEquals("There should still be exactly one specimen media",
816 1, specimenFacade.getFieldObjectMedia().size());
817
818 specimenFacade.removeFieldObjectMedia(media2);
819 Assert.assertEquals("There should remain no media in the gallery", 0,
820 media.size());
821
822 }
823
824 /**
825 * Test method for
826 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
827 * .
828 */
829 @Test
830 public void testGetSetEcology() {
831 Assert.assertNotNull(
832 "An empty ecology data should be created when calling getEcology()",
833 specimenFacade.getEcologyAll());
834 Assert.assertEquals(
835 "An empty ecology data should be created when calling getEcology()",
836 0, specimenFacade.getEcologyAll().size());
837 specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
838 Assert.assertEquals("Ecology data should exist for 1 language", 1,
839 specimenFacade.getEcologyAll().size());
840 Assert.assertEquals(
841 "Ecology data should be 'Tres jolie ici' for French",
842 "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
843 Assert.assertNull(
844 "Ecology data should be null for the default language",
845 specimenFacade.getEcology());
846 specimenFacade.setEcology("Nice here");
847 Assert.assertEquals("Ecology data should exist for 2 languages", 2,
848 specimenFacade.getEcologyAll().size());
849 Assert.assertEquals("Ecology data should be 'Tres jolie ici'",
850 "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
851 Assert.assertEquals(
852 "Ecology data should be 'Nice here' for the default language",
853 "Nice here", specimenFacade.getEcology());
854 Assert.assertEquals("Ecology data should be 'Nice here' for english",
855 "Nice here", specimenFacade.getEcology());
856
857 specimenFacade.setEcology("Vert et rouge", Language.FRENCH());
858 Assert.assertEquals("Ecology data should exist for 2 languages", 2,
859 specimenFacade.getEcologyAll().size());
860 Assert.assertEquals("Ecology data should be 'Vert et rouge'",
861 "Vert et rouge", specimenFacade.getEcology(Language.FRENCH()));
862 Assert.assertEquals(
863 "Ecology data should be 'Nice here' for the default language",
864 "Nice here", specimenFacade.getEcology());
865
866 specimenFacade.setEcology(null, Language.FRENCH());
867 Assert.assertEquals("Ecology data should exist for 1 languages", 1,
868 specimenFacade.getEcologyAll().size());
869 Assert.assertEquals(
870 "Ecology data should be 'Nice here' for the default language",
871 "Nice here", specimenFacade.getEcology());
872 Assert.assertNull("Ecology data should be 'null' for French",
873 specimenFacade.getEcology(Language.FRENCH()));
874
875 specimenFacade.removeEcology(null);
876 Assert.assertEquals("There should be no ecology left", 0,
877 specimenFacade.getEcologyAll().size());
878 Assert.assertNull("Ecology data should be 'null' for default language",
879 specimenFacade.getEcology());
880
881 }
882
883 /**
884 * Test method for
885 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addFieldObjectMedia(eu.etaxonomy.cdm.model.media.Media)}
886 * .
887 */
888 @Test
889 public void testGetSetPlantDescription() {
890 Assert.assertNotNull(
891 "An empty plant description data should be created when calling getPlantDescriptionAll()",
892 specimenFacade.getPlantDescriptionAll());
893 Assert.assertEquals(
894 "An empty plant description data should be created when calling getPlantDescription()",
895 0, specimenFacade.getPlantDescriptionAll().size());
896 specimenFacade.setPlantDescription("bleu", Language.FRENCH());
897 Assert.assertEquals(
898 "Plant description data should exist for 1 language", 1,
899 specimenFacade.getPlantDescriptionAll().size());
900 Assert.assertEquals(
901 "Plant description data should be 'bleu' for French", "bleu",
902 specimenFacade.getPlantDescription(Language.FRENCH()));
903 Assert.assertNull(
904 "Plant description data should be null for the default language",
905 specimenFacade.getPlantDescription());
906 specimenFacade.setPlantDescription("Nice here");
907 Assert.assertEquals(
908 "Plant description data should exist for 2 languages", 2,
909 specimenFacade.getPlantDescriptionAll().size());
910 Assert.assertEquals("Plant description data should be 'bleu'", "bleu",
911 specimenFacade.getPlantDescription(Language.FRENCH()));
912 Assert.assertEquals(
913 "Plant description data should be 'Nice here' for the default language",
914 "Nice here", specimenFacade.getPlantDescription());
915 Assert.assertEquals(
916 "Plant description data should be 'Nice here' for english",
917 "Nice here", specimenFacade.getPlantDescription());
918
919 specimenFacade.setPlantDescription("Vert et rouge", Language.FRENCH());
920 Assert.assertEquals(
921 "Plant description data should exist for 2 languages", 2,
922 specimenFacade.getPlantDescriptionAll().size());
923 Assert.assertEquals("Plant description data should be 'Vert et rouge'",
924 "Vert et rouge",
925 specimenFacade.getPlantDescription(Language.FRENCH()));
926 Assert.assertEquals(
927 "Plant description data should be 'Nice here' for the default language",
928 "Nice here", specimenFacade.getPlantDescription());
929
930 specimenFacade.setPlantDescription(null, Language.FRENCH());
931 Assert.assertEquals(
932 "Plant description data should exist for 1 languages", 1,
933 specimenFacade.getPlantDescriptionAll().size());
934 Assert.assertEquals(
935 "Plant description data should be 'Nice here' for the default language",
936 "Nice here", specimenFacade.getPlantDescription());
937 Assert.assertNull("Plant description data should be 'null' for French",
938 specimenFacade.getPlantDescription(Language.FRENCH()));
939
940 // test interference with ecology
941 specimenFacade.setEcology("Tres jolie ici", Language.FRENCH());
942 Assert.assertEquals("Ecology data should exist for 1 language", 1,
943 specimenFacade.getEcologyAll().size());
944 Assert.assertEquals(
945 "Ecology data should be 'Tres jolie ici' for French",
946 "Tres jolie ici", specimenFacade.getEcology(Language.FRENCH()));
947 Assert.assertNull(
948 "Ecology data should be null for the default language",
949 specimenFacade.getEcology());
950
951 // repeat above test
952 Assert.assertEquals(
953 "Plant description data should exist for 1 languages", 1,
954 specimenFacade.getPlantDescriptionAll().size());
955 Assert.assertEquals(
956 "Plant description data should be 'Nice here' for the default language",
957 "Nice here", specimenFacade.getPlantDescription());
958 Assert.assertNull("Plant description data should be 'null' for French",
959 specimenFacade.getPlantDescription(Language.FRENCH()));
960
961 specimenFacade.removePlantDescription(null);
962 Assert.assertEquals("There should be no plant description left", 0,
963 specimenFacade.getPlantDescriptionAll().size());
964 Assert.assertNull(
965 "Plant description data should be 'null' for default language",
966 specimenFacade.getPlantDescription());
967
968 }
969
970 /**
971 * Test method for
972 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNumber()}.
973 */
974 @Test
975 public void testGetSetFieldNumber() {
976 Assert.assertEquals("Field number must be same", fieldNumber,
977 specimenFacade.getFieldNumber());
978 specimenFacade.setFieldNumber("564AB");
979 Assert.assertEquals("New field number must be '564AB'", "564AB",
980 specimenFacade.getFieldNumber());
981 // empty facade tests
982 Assert.assertNull("Empty facace must not have any field value",
983 emptyFacade.getFieldNumber());
984 emptyFacade.setFieldNumber("1256A");
985 Assert.assertNotNull(
986 "Field observation must exist if field number exists",
987 emptyFacade.getFieldObservation(false));
988 FieldObservation specimenFieldObservation = (FieldObservation) emptyFacade
989 .innerDerivedUnit().getDerivedFrom().getOriginals().iterator()
990 .next();
991 Assert.assertSame(
992 "Field observation of facade and of specimen must be the same",
993 specimenFieldObservation,
994 emptyFacade.getFieldObservation(false));
995 Assert.assertEquals("1256A", emptyFacade.getFieldNumber());
996 }
997
998 /**
999 * Test method for
1000 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getFieldNotes()}.
1001 */
1002 @Test
1003 public void testGetSetFieldNotes() {
1004 Assert.assertEquals("Field notes must be same", fieldNotes,
1005 specimenFacade.getFieldNotes());
1006 specimenFacade.setFieldNotes("A completely new info");
1007 Assert.assertEquals("New field note must be 'A completely new info'",
1008 "A completely new info", specimenFacade.getFieldNotes());
1009 }
1010
1011 /**
1012 * Test method for
1013 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setGatheringEvent(eu.etaxonomy.cdm.model.occurrence.GatheringEvent)}
1014 * .
1015 */
1016 @Test
1017 public void testSetGatheringEvent() {
1018 GatheringEvent newGatheringEvent = GatheringEvent.NewInstance();
1019 newGatheringEvent.setDistanceToGround(43);
1020 Assert.assertFalse("The initial distance to ground should not be 43",
1021 specimenFacade.getDistanceToGround() == 43);
1022 specimenFacade.setGatheringEvent(newGatheringEvent);
1023 Assert.assertTrue("The final distance to ground should be 43",
1024 specimenFacade.getDistanceToGround() == 43);
1025 Assert.assertSame(
1026 "The new gathering event should be 'newGatheringEvent'",
1027 newGatheringEvent, specimenFacade.innerGatheringEvent());
1028 }
1029
1030 /**
1031 * Test method for
1032 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerGatheringEvent()}
1033 * .
1034 */
1035 @Test
1036 public void testGetGatheringEvent() {
1037 Assert.assertNotNull("Gathering event must not be null",
1038 specimenFacade.innerGatheringEvent());
1039 Assert.assertEquals(
1040 "Gathering event must be field observations gathering event",
1041 specimenFacade.innerFieldObservation().getGatheringEvent(),
1042 specimenFacade.innerGatheringEvent());
1043 }
1044
1045 /**
1046 * Test method for
1047 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getIndividualCount()}
1048 * .
1049 */
1050 @Test
1051 public void testGetSetIndividualCount() {
1052 Assert.assertEquals("Individual count must be same", individualCount,
1053 specimenFacade.getIndividualCount());
1054 specimenFacade.setIndividualCount(4);
1055 Assert.assertEquals("New individual count must be '4'",
1056 Integer.valueOf(4), specimenFacade.getIndividualCount());
1057
1058 }
1059
1060 /**
1061 * Test method for
1062 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLifeStage()}.
1063 */
1064 @Test
1065 public void testGetSetLifeStage() {
1066 Assert.assertNotNull("Life stage must not be null",
1067 specimenFacade.getLifeStage());
1068 Assert.assertEquals("Life stage must be same", lifeStage,
1069 specimenFacade.getLifeStage());
1070 specimenFacade.setLifeStage(null);
1071 Assert.assertNull("Life stage must be null",
1072 specimenFacade.getLifeStage());
1073 }
1074
1075 /**
1076 * Test method for
1077 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getSex()}.
1078 */
1079 @Test
1080 public void testGetSetSex() {
1081 Assert.assertNotNull("Sex must not be null", specimenFacade.getSex());
1082 Assert.assertEquals("Sex must be same", sex, specimenFacade.getSex());
1083 specimenFacade.setSex(null);
1084 Assert.assertNull("Sex must be null", specimenFacade.getSex());
1085 }
1086
1087 /**
1088 * Test method for
1089 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLocality()}.
1090 */
1091 @Test
1092 public void testGetSetLocality() {
1093 Assert.assertEquals("Locality must be same", locality,
1094 specimenFacade.getLocality());
1095 specimenFacade.setLocality("A completely new place", Language.FRENCH());
1096 Assert.assertEquals("New locality must be 'A completely new place'",
1097 "A completely new place", specimenFacade.getLocalityText());
1098 Assert.assertEquals("New locality language must be French",
1099 Language.FRENCH(), specimenFacade.getLocalityLanguage());
1100 specimenFacade.setLocality("Another place");
1101 Assert.assertEquals("New locality must be 'Another place'",
1102 "Another place", specimenFacade.getLocalityText());
1103 Assert.assertEquals("New locality language must be default",
1104 Language.DEFAULT(), specimenFacade.getLocalityLanguage());
1105 }
1106
1107 /**
1108 * Test method for
1109 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
1110 * .
1111 */
1112 @Test
1113 public void testAddGetRemoveSpecimenDefinition() {
1114 Assert.assertEquals("There should be no definition yet", 0,
1115 specimenFacade.getDerivedUnitDefinitions().size());
1116 specimenFacade.addDerivedUnitDefinition("Tres interesant",
1117 Language.FRENCH());
1118 Assert.assertEquals("There should be exactly one definition", 1,
1119 specimenFacade.getDerivedUnitDefinitions().size());
1120 Assert.assertEquals(
1121 "The French definition should be 'Tres interesant'",
1122 "Tres interesant", specimenFacade.getDerivedUnitDefinitions()
1123 .get(Language.FRENCH()).getText());
1124 Assert.assertEquals(
1125 "The French definition should be 'Tres interesant'",
1126 "Tres interesant",
1127 specimenFacade.getDerivedUnitDefinition(Language.FRENCH()));
1128 specimenFacade.addDerivedUnitDefinition("Sehr interessant",
1129 Language.GERMAN());
1130 Assert.assertEquals("There should be exactly 2 definition", 2,
1131 specimenFacade.getDerivedUnitDefinitions().size());
1132 specimenFacade.removeDerivedUnitDefinition(Language.FRENCH());
1133 Assert.assertEquals("There should remain exactly 1 definition", 1,
1134 specimenFacade.getDerivedUnitDefinitions().size());
1135 Assert.assertEquals(
1136 "The remaining German definition should be 'Sehr interessant'",
1137 "Sehr interessant",
1138 specimenFacade.getDerivedUnitDefinition(Language.GERMAN()));
1139 specimenFacade.removeDerivedUnitDefinition(Language.GERMAN());
1140 Assert.assertEquals("There should remain no definition", 0,
1141 specimenFacade.getDerivedUnitDefinitions().size());
1142 }
1143
1144 /**
1145 * Test method for
1146 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1147 * .
1148 */
1149 @Test
1150 public void testAddGetRemoveDetermination() {
1151 Assert.assertEquals("There should be no determination yet", 0,
1152 specimenFacade.getDeterminations().size());
1153 DeterminationEvent determinationEvent1 = DeterminationEvent
1154 .NewInstance();
1155 specimenFacade.addDetermination(determinationEvent1);
1156 Assert.assertEquals("There should be exactly one determination", 1,
1157 specimenFacade.getDeterminations().size());
1158 Assert.assertEquals("The only determination should be determination 1",
1159 determinationEvent1, specimenFacade.getDeterminations()
1160 .iterator().next());
1161
1162 DeterminationEvent determinationEvent2 = DeterminationEvent
1163 .NewInstance();
1164 specimenFacade.addDetermination(determinationEvent2);
1165 Assert.assertEquals("There should be exactly 2 determinations", 2,
1166 specimenFacade.getDeterminations().size());
1167 specimenFacade.removeDetermination(determinationEvent1);
1168
1169 Assert.assertEquals("There should remain exactly 1 determination", 1,
1170 specimenFacade.getDeterminations().size());
1171 Assert.assertEquals(
1172 "The remaining determinations should be determination 2",
1173 determinationEvent2, specimenFacade.getDeterminations()
1174 .iterator().next());
1175
1176 specimenFacade.removeDetermination(determinationEvent1);
1177 Assert.assertEquals("There should remain exactly 1 determination", 1,
1178 specimenFacade.getDeterminations().size());
1179
1180 specimenFacade.removeDetermination(determinationEvent2);
1181 Assert.assertEquals("There should remain no definition", 0,
1182 specimenFacade.getDerivedUnitDefinitions().size());
1183
1184 }
1185
1186
1187 /**
1188 * Test method for
1189 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1190 * .
1191 */
1192 @Test
1193 public void testPreferredOtherDeterminations() {
1194 Assert.assertEquals("There should be no determination yet", 0,
1195 specimenFacade.getDeterminations().size());
1196 DeterminationEvent determinationEvent1 = DeterminationEvent.NewInstance();
1197 specimenFacade.setPreferredDetermination(determinationEvent1);
1198
1199 Assert.assertEquals("There should be exactly one determination", 1,
1200 specimenFacade.getDeterminations().size());
1201 Assert.assertEquals("The only determination should be determination 1",
1202 determinationEvent1, specimenFacade.getDeterminations()
1203 .iterator().next());
1204 Assert.assertEquals("determination 1 should be the preferred determination",
1205 determinationEvent1, specimenFacade.getPreferredDetermination());
1206 Assert.assertEquals("There should be no 'non preferred' determination", 0,
1207 specimenFacade.getOtherDeterminations().size());
1208
1209
1210
1211 DeterminationEvent determinationEvent2 = DeterminationEvent.NewInstance();
1212 specimenFacade.addDetermination(determinationEvent2);
1213 Assert.assertEquals("There should be exactly 2 determinations", 2,
1214 specimenFacade.getDeterminations().size());
1215 Assert.assertEquals("determination 1 should be the preferred determination",
1216 determinationEvent1, specimenFacade.getPreferredDetermination());
1217 Assert.assertEquals("There should be one 'non preferred' determination", 1,
1218 specimenFacade.getOtherDeterminations().size());
1219 Assert.assertEquals("The only 'non preferred' determination should be determination 2",
1220 determinationEvent2, specimenFacade.getOtherDeterminations().iterator().next());
1221
1222
1223 DeterminationEvent determinationEvent3 = DeterminationEvent.NewInstance();
1224 specimenFacade.setPreferredDetermination(determinationEvent3);
1225 Assert.assertEquals("There should be exactly 3 determinations", 3,
1226 specimenFacade.getDeterminations().size());
1227 Assert.assertEquals("determination 3 should be the preferred determination",
1228 determinationEvent3, specimenFacade.getPreferredDetermination());
1229 Assert.assertEquals("There should be 2 'non preferred' determination", 2,
1230 specimenFacade.getOtherDeterminations().size());
1231 Assert.assertTrue("determination 1 should be contained in the set of 'non preferred' determinations",
1232 specimenFacade.getOtherDeterminations().contains(determinationEvent1));
1233 Assert.assertTrue("determination 2 should be contained in the set of 'non preferred' determinations",
1234 specimenFacade.getOtherDeterminations().contains(determinationEvent2));
1235
1236 }
1237
1238 /**
1239 * Test method for
1240 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitMedia(eu.etaxonomy.cdm.model.media.Media)}
1241 * .
1242 */
1243 @Test
1244 public void testAddGetHasRemoveSpecimenMedia() {
1245 Assert.assertFalse("There should be no image gallery yet",
1246 specimenFacade.hasDerivedUnitImageGallery());
1247 Assert.assertFalse(
1248 "There should be also no field object image gallery yet",
1249 specimenFacade.hasFieldObjectImageGallery());
1250
1251 List<Media> media = specimenFacade.getDerivedUnitMedia();
1252 Assert.assertFalse(
1253 "There should still not be an empty image gallery now",
1254 specimenFacade.hasDerivedUnitImageGallery());
1255 Assert.assertEquals("There should be no media yet in the gallery", 0,
1256 media.size());
1257
1258 Media media1 = Media.NewInstance();
1259 specimenFacade.addDerivedUnitMedia(media1);
1260 Assert.assertEquals("There should be exactly one specimen media", 1,
1261 specimenFacade.getDerivedUnitMedia().size());
1262 Assert.assertEquals("The only media should be media 1", media1,
1263 specimenFacade.getDerivedUnitMedia().get(0));
1264 Assert.assertFalse(
1265 "There should be still no field object image gallery",
1266 specimenFacade.hasFieldObjectImageGallery());
1267
1268 Media media2 = Media.NewInstance();
1269 specimenFacade.addDerivedUnitMedia(media2);
1270 Assert.assertEquals("There should be exactly 2 specimen media", 2,
1271 specimenFacade.getDerivedUnitMedia().size());
1272 Assert.assertEquals("The first media should be media1", media1,
1273 specimenFacade.getDerivedUnitMedia().get(0));
1274 Assert.assertEquals("The second media should be media2", media2,
1275 specimenFacade.getDerivedUnitMedia().get(1));
1276
1277 specimenFacade.removeDerivedUnitMedia(media1);
1278 Assert.assertEquals("There should be exactly one specimen media", 1,
1279 specimenFacade.getDerivedUnitMedia().size());
1280 Assert.assertEquals("The only media should be media2", media2,
1281 specimenFacade.getDerivedUnitMedia().get(0));
1282
1283 specimenFacade.removeDerivedUnitMedia(media1);
1284 Assert.assertEquals("There should still be exactly one specimen media",
1285 1, specimenFacade.getDerivedUnitMedia().size());
1286
1287 specimenFacade.removeDerivedUnitMedia(media2);
1288 Assert.assertEquals("There should remain no media in the gallery", 0,
1289 media.size());
1290 }
1291
1292 /**
1293 * Test method for
1294 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAccessionNumber()}
1295 * .
1296 */
1297 @Test
1298 public void testGetSetAccessionNumber() {
1299 Assert.assertEquals("Accession number must be same", accessionNumber,
1300 specimenFacade.getAccessionNumber());
1301 specimenFacade.setAccessionNumber("A12345693");
1302 Assert.assertEquals("New accession number must be 'A12345693'",
1303 "A12345693", specimenFacade.getAccessionNumber());
1304 }
1305
1306 /**
1307 * Test method for
1308 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCatalogNumber()}.
1309 */
1310 @Test
1311 public void testGetCatalogNumber() {
1312 Assert.assertEquals("Catalog number must be same", catalogNumber,
1313 specimenFacade.getCatalogNumber());
1314 specimenFacade.setCatalogNumber("B12345693");
1315 Assert.assertEquals("New catalog number must be 'B12345693'",
1316 "B12345693", specimenFacade.getCatalogNumber());
1317 }
1318
1319 /**
1320 * Test method for
1321 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getPreservation()}.
1322 */
1323 @Test
1324 public void testGetPreservation() {
1325 try {
1326 Assert.assertNotNull("Preservation method must not be null",
1327 specimenFacade.getPreservationMethod());
1328 Assert.assertEquals("Preservation method must be same",
1329 preservationMethod, specimenFacade.getPreservationMethod());
1330 specimenFacade.setPreservationMethod(null);
1331 Assert.assertNull("Preservation method must be null",
1332 specimenFacade.getPreservationMethod());
1333 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1334 Assert.fail("Method not supported should not be thrown for a specimen");
1335 }
1336 specimenFacade = DerivedUnitFacade
1337 .NewInstance(DerivedUnitType.Observation);
1338 try {
1339 specimenFacade.setPreservationMethod(preservationMethod);
1340 Assert.fail("Method not supported should be thrown for an observation on set preservation method");
1341
1342 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1343 // ok
1344 }
1345 specimenFacade = DerivedUnitFacade
1346 .NewInstance(DerivedUnitType.LivingBeing);
1347 try {
1348 specimenFacade.getPreservationMethod();
1349 Assert.fail("Method not supported should be thrown for a living being on get preservation method");
1350 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1351 // ok
1352 }
1353
1354 }
1355
1356 /**
1357 * Test method for
1358 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getStoredUnder()}.
1359 */
1360 @Test
1361 public void testGetStoredUnder() {
1362 Assert.assertNotNull("Stored under name must not be null",
1363 specimenFacade.getStoredUnder());
1364 Assert.assertEquals("Stored under name must be same", taxonName,
1365 specimenFacade.getStoredUnder());
1366 specimenFacade.setStoredUnder(null);
1367 Assert.assertNull("Stored under name must be null",
1368 specimenFacade.getStoredUnder());
1369 }
1370
1371 // /**
1372 // * Test method for
1373 // * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectorsNumber()}
1374 // * .
1375 // */
1376 // @Test
1377 // public void testGetSetCollectorsNumber() {
1378 // Assert.assertEquals("Collectors number must be same", collectorsNumber,
1379 // specimenFacade.getCollectorsNumber());
1380 // specimenFacade.setCollectorsNumber("C12345693");
1381 // Assert.assertEquals("New collectors number must be 'C12345693'",
1382 // "C12345693", specimenFacade.getCollectorsNumber());
1383 // }
1384
1385 /**
1386 * Test method for
1387 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTitleCache()}.
1388 */
1389 @Test
1390 public void testGetTitleCache() {
1391 Assert.assertNotNull(
1392 "The title cache should never return null if not protected",
1393 specimenFacade.getTitleCache());
1394 specimenFacade.setTitleCache(null, false);
1395 Assert.assertNotNull(
1396 "The title cache should never return null if not protected",
1397 specimenFacade.getTitleCache());
1398 }
1399
1400 /**
1401 * Test method for
1402 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setTitleCache(java.lang.String)}
1403 * .
1404 */
1405 @Test
1406 public void testSetTitleCache() {
1407 String testTitle = "Absdwk aksjlf";
1408 specimenFacade.setTitleCache(testTitle, true);
1409 Assert.assertEquals(
1410 "Protected title cache should returns the test title",
1411 testTitle, specimenFacade.getTitleCache());
1412 specimenFacade.setTitleCache(testTitle, false);
1413 Assert.assertFalse(
1414 "Unprotected title cache should not return the test title",
1415 testTitle.equals(specimenFacade.getTitleCache()));
1416 }
1417
1418 /**
1419 * Test method for
1420 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerDerivedUnit()}.
1421 */
1422 @Test
1423 public void testGetSpecimen() {
1424 Assert.assertEquals("Specimen must be same", specimen,
1425 specimenFacade.innerDerivedUnit());
1426 }
1427
1428 /**
1429 * Test method for
1430 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollection()}.
1431 */
1432 @Test
1433 public void testGetSetCollection() {
1434 Assert.assertNotNull("Collection must not be null",
1435 specimenFacade.getCollection());
1436 Assert.assertEquals("Collection must be same", collection,
1437 specimenFacade.getCollection());
1438 specimenFacade.setCollection(null);
1439 Assert.assertNull("Collection must be null",
1440 specimenFacade.getCollection());
1441 }
1442
1443 @Test
1444 public void testAddGetRemoveSource() {
1445 Assert.assertEquals("No sources should exist yet", 0, specimenFacade
1446 .getSources().size());
1447 Reference reference = ReferenceFactory.newBook();
1448 IdentifiableSource source1 = specimenFacade.addSource(reference, "54",
1449 "myName");
1450 Assert.assertEquals("One source should exist now", 1, specimenFacade
1451 .getSources().size());
1452 IdentifiableSource source2 = IdentifiableSource.NewInstance("1",
1453 "myTable");
1454 specimenFacade.addSource(source2);
1455 Assert.assertEquals("One source should exist now", 2, specimenFacade
1456 .getSources().size());
1457 specimenFacade.removeSource(source1);
1458 Assert.assertEquals("One source should exist now", 1, specimenFacade
1459 .getSources().size());
1460 Reference reference2 = ReferenceFactory.newJournal();
1461 IdentifiableSource sourceNotUsed = specimenFacade.addSource(reference2,
1462 null, null);
1463 specimenFacade.removeSource(sourceNotUsed);
1464 Assert.assertEquals("One source should still exist", 1, specimenFacade
1465 .getSources().size());
1466 Assert.assertEquals("1", specimenFacade.getSources().iterator().next()
1467 .getIdInSource());
1468 specimenFacade.removeSource(source2);
1469 Assert.assertEquals("No sources should exist anymore", 0,
1470 specimenFacade.getSources().size());
1471 }
1472
1473 @Test
1474 public void testAddGetRemoveDuplicate() {
1475 Assert.assertEquals("No duplicates should be available yet", 0,
1476 specimenFacade.getDuplicates().size());
1477 Specimen newSpecimen1 = Specimen.NewInstance();
1478 specimenFacade.addDuplicate(newSpecimen1);
1479 Assert.assertEquals("There should be 1 duplicate now", 1,
1480 specimenFacade.getDuplicates().size());
1481 Specimen newSpecimen2 = Specimen.NewInstance();
1482 DerivationEvent newDerivationEvent = DerivationEvent.NewInstance();
1483 newSpecimen2.setDerivedFrom(newDerivationEvent);
1484 Assert.assertSame(
1485 "The derivation event should be 'newDerivationEvent'",
1486 newDerivationEvent, newSpecimen2.getDerivedFrom());
1487 specimenFacade.addDuplicate(newSpecimen2);
1488 Assert.assertEquals("There should be 2 duplicates now", 2,
1489 specimenFacade.getDuplicates().size());
1490 Assert.assertNotSame(
1491 "The derivation event should not be 'newDerivationEvent' anymore",
1492 newDerivationEvent, newSpecimen2.getDerivedFrom());
1493 Assert.assertSame(
1494 "The derivation event should not be the facades derivation event",
1495 derivationEvent, newSpecimen2.getDerivedFrom());
1496 specimenFacade.removeDuplicate(newSpecimen1);
1497 Assert.assertEquals("There should be 1 duplicate now", 1,
1498 specimenFacade.getDuplicates().size());
1499 Assert.assertSame("The only duplicate should be 'newSpecimen2' now",
1500 newSpecimen2, specimenFacade.getDuplicates().iterator().next());
1501 specimenFacade.addDuplicate(specimenFacade.innerDerivedUnit());
1502 Assert.assertEquals(
1503 "There should be still 1 duplicate because the facade specimen is not a duplicate",
1504 1, specimenFacade.getDuplicates().size());
1505
1506 Collection newCollection = Collection.NewInstance();
1507 String catalogNumber = "1234890";
1508 String accessionNumber = "345345";
1509 String collectorsNumber = "lkjewe";
1510 TaxonNameBase storedUnder = BotanicalName.NewInstance(Rank.SPECIES());
1511 PreservationMethod method = PreservationMethod.NewInstance();
1512 Specimen duplicateSpecimen = specimenFacade.addDuplicate(newCollection,
1513 catalogNumber, accessionNumber, storedUnder,
1514 method);
1515 Assert.assertEquals("There should be 2 duplicates now", 2,
1516 specimenFacade.getDuplicates().size());
1517 specimenFacade.removeDuplicate(newSpecimen2);
1518 Assert.assertEquals("There should be 1 duplicates now", 1,
1519 specimenFacade.getDuplicates().size());
1520 Collection sameCollection = specimenFacade.getDuplicates().iterator()
1521 .next().getCollection();
1522 Assert.assertSame("Collections should be same", newCollection,
1523 sameCollection);
1524 }
1525
1526 // ************************** Existing Specimen with multiple derivation
1527 // events in line **************/
1528
1529 @Test
1530 public void testExistingSpecimen() {
1531 specimenFacade = null;
1532 try {
1533 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1534 } catch (DerivedUnitFacadeNotSupportedException e) {
1535 Assert.fail("Multiple derivation events in line should not throw a not supported exception");
1536 }
1537 Assert.assertSame(
1538 "Gathering event should derive from the derivation line",
1539 existingGatheringEvent, specimenFacade.innerGatheringEvent());
1540 Assert.assertEquals(
1541 "Mediasize should be 0. Only Imagegallery media are supported",
1542 0, specimenFacade.getFieldObjectMedia().size());
1543 }
1544
1545 @Test
1546 public void testMultipleFieldObservationsNotSupported() {
1547 specimenFacade = null;
1548 FieldObservation secondFieldObject = FieldObservation.NewInstance();
1549 firstDerivationEvent.addOriginal(secondFieldObject);
1550 try {
1551 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1552 Assert.fail("Multiple field observations for one specimen should no be supported by the facade");
1553 } catch (DerivedUnitFacadeNotSupportedException e) {
1554 // ok
1555 }
1556 Assert.assertNull("Specimen facade should not be initialized",
1557 specimenFacade);
1558 }
1559
1560 @Test
1561 public void testOnlyImageGallerySupported() {
1562 specimenFacade = null;
1563 firstFieldObject.addMedia(media1);
1564 try {
1565 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1566 Assert.fail("Only image galleries are supported by the facade but not direct media");
1567 } catch (DerivedUnitFacadeNotSupportedException e) {
1568 // ok
1569 }
1570 Assert.assertNull("Specimen facade should not be initialized",
1571 specimenFacade);
1572 }
1573
1574 @Test
1575 public void testEventPropagation() {
1576 specimenFacade.setDistanceToGround(24);
1577
1578 }
1579
1580 // @Ignore // set to ignore because I did not want to check knowingly
1581 // failing tests. Remove @Ignore when this is fixed
1582 @Test
1583 public void testSetBarcode() {
1584 String barcode = "barcode";
1585 specimenFacade.setBarcode(barcode);
1586
1587 Assert.assertEquals(barcode, specimenFacade.getBarcode());
1588 }
1589
1590 @Test
1591 public void testIsEvenDistance() {
1592 Integer minimum = 20;
1593 Integer maximum = 1234;
1594
1595 // this should not throw exceptions
1596 specimenFacade.setAbsoluteElevationRange(minimum, maximum);
1597 }
1598
1599 @SpringBeanByType
1600 private IUserService userService;
1601
1602 /**
1603 *
1604 * See https://dev.e-taxonomy.eu/trac/ticket/2426
1605 * This test doesn't handle the above issue yet as it doesn't fire events as
1606 * expected (at least it does not reproduce the behaviour in the Taxonomic Editor).
1607 * In the meanwhile the property change framework for the facade has been changed
1608 * so the original problem may have disappeared.
1609 *
1610 */
1611 @Test
1612 public void testNoRecursiveChangeEvents(){
1613 String username = "username";
1614 String password = "password";
1615 User user = User.NewInstance(username, password);
1616 userService.save(user);
1617 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
1618 SecurityContextHolder.getContext().setAuthentication(token);
1619
1620 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
1621 facade.setLocality("testLocality");
1622 facade.getTitleCache();
1623 // facade.innerGatheringEvent().firePropertyChange("createdBy", null, user);
1624 this.service.save(facade.innerDerivedUnit());
1625
1626 }
1627
1628 }