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