update factory methods for original sources #1549
[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.Ignore;
23 import org.junit.Test;
24 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
25 import org.springframework.security.core.context.SecurityContextHolder;
26 import org.unitils.dbunit.annotation.DataSet;
27 import org.unitils.dbunit.annotation.ExpectedDataSet;
28 import org.unitils.spring.annotation.SpringBeanByType;
29
30 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade.DerivedUnitType;
31 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
32 import eu.etaxonomy.cdm.api.service.ITermService;
33 import eu.etaxonomy.cdm.api.service.IUserService;
34 import eu.etaxonomy.cdm.model.agent.AgentBase;
35 import eu.etaxonomy.cdm.model.agent.Team;
36 import eu.etaxonomy.cdm.model.common.IdentifiableSource;
37 import eu.etaxonomy.cdm.model.common.Language;
38 import eu.etaxonomy.cdm.model.common.LanguageString;
39 import eu.etaxonomy.cdm.model.common.OriginalSourceType;
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.DerivationEventType;
59 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
60 import eu.etaxonomy.cdm.model.occurrence.FieldObservation;
61 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
62 import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
63 import eu.etaxonomy.cdm.model.occurrence.Specimen;
64 import eu.etaxonomy.cdm.model.reference.Reference;
65 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
66 import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
67
68
69 /**
70 * @author a.mueller
71 * @date 17.05.2010
72 *
73 */
74
75 public class DerivedUnitFacadeTest extends CdmTransactionalIntegrationTest {
76 private static final Logger logger = Logger.getLogger(DerivedUnitFacadeTest.class);
77
78 @SpringBeanByType
79 private IOccurrenceService service;
80
81 @SpringBeanByType
82 private ITermService termService;
83
84 @SpringBeanByType
85 private IUserService userService;
86
87
88 Specimen specimen;
89 DerivationEvent derivationEvent;
90 FieldObservation fieldObservation;
91 GatheringEvent gatheringEvent;
92 Integer absoluteElevation = 10;
93 Integer absoluteElevationError = 2;
94 AgentBase collector = Team.NewInstance();
95 String collectingMethod = "Collection Method";
96 Integer distanceToGround = 22;
97 Integer distanceToSurface = 50;
98 ReferenceSystem referenceSystem = ReferenceSystem.WGS84();
99 Point exactLocation = Point.NewInstance(12.3, 10.567, referenceSystem, 22);
100 String gatheringEventDescription = "A nice gathering description";
101 TimePeriod gatheringPeriod = TimePeriod.NewInstance(1888, 1889);
102
103 String fieldNumber = "15p23B";
104 String fieldNotes = "such a beautiful specimen";
105
106 Integer individualCount = 1;
107 Stage lifeStage = Stage.NewInstance("A wonderful stage", "stage", "st");
108 Sex sex = Sex.NewInstance("FemaleMale", "FM", "FM");
109 LanguageString locality = LanguageString.NewInstance("My locality",
110 Language.DEFAULT());
111
112 String accessionNumber = "888462535";
113 String catalogNumber = "UU879873590";
114 TaxonNameBase taxonName = BotanicalName.NewInstance(Rank.GENUS(), "Abies",
115 null, null, null, null, null, null, null);
116 String collectorsNumber = "234589913A34";
117 Collection collection = Collection.NewInstance();
118 PreservationMethod preservationMethod = PreservationMethod.NewInstance(
119 "my prservation", null, null);
120
121 DerivedUnitFacade specimenFacade;
122
123 Specimen collectionSpecimen;
124 GatheringEvent existingGatheringEvent;
125 DerivationEvent firstDerivationEvent;
126 FieldObservation firstFieldObject;
127 Media media1 = Media.NewInstance();
128
129 DerivedUnitFacade emptyFacade;
130
131 NamedArea country = WaterbodyOrCountry.GERMANY();
132
133 // ****************************** SET UP **********************************/
134
135
136 /**
137 * @throws java.lang.Exception
138 */
139 @Before
140 public void setUp() throws Exception {
141
142 specimen = Specimen.NewInstance();
143
144 derivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
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 //TODO maybe we should define concrete event types here
183 DerivationEvent lastDerivationEvent = DerivationEvent.NewInstance();
184 DerivationEvent middleDerivationEvent = DerivationEvent.NewInstance();
185 firstDerivationEvent = DerivationEvent.NewInstance();
186
187 collectionSpecimen.setDerivedFrom(lastDerivationEvent);
188
189 lastDerivationEvent.addOriginal(middleSpecimen);
190 middleSpecimen.setDerivedFrom(firstDerivationEvent);
191 firstDerivationEvent.addOriginal(firstFieldObject);
192 existingGatheringEvent = GatheringEvent.NewInstance();
193 firstFieldObject.setGatheringEvent(existingGatheringEvent);
194
195 // empty facade
196 emptyFacade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
197
198 }
199
200 // ****************************** TESTS*****************************/
201
202 @Ignore //doesn't run in suite
203 @Test
204 @DataSet("DerivedUnitFacadeTest.testSetFieldObjectImageGallery.xml")
205 @ExpectedDataSet
206 public void testSetFieldObjectImageGallery() {
207 UUID imageFeatureUuid = Feature.IMAGE().getUuid();
208 Feature imageFeature = (Feature) termService.find(imageFeatureUuid);
209
210 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
211 facade.innerDerivedUnit().setUuid(UUID.fromString("77af784f-931b-4857-be9a-48ccf31ed3f1"));
212 facade.setFieldNumber("12345");
213 Media media = Media.NewInstance(URI.create("www.abc.de"), 200, null,"jpeg");
214
215 try {
216 SpecimenDescription imageGallery = SpecimenDescription.NewInstance();
217 imageGallery.addDescribedSpecimenOrObservation(facade.innerFieldObservation());
218 imageGallery.setImageGallery(true);
219 TextData textData = TextData.NewInstance();
220 textData.setFeature(imageFeature);
221 imageGallery.addElement(textData);
222 textData.addMedia(media);
223 facade.setFieldObjectImageGallery(imageGallery);
224
225 } catch (DerivedUnitFacadeNotSupportedException e1) {
226 e1.printStackTrace();
227 Assert.fail(e1.getLocalizedMessage());
228 }
229 this.service.save(facade.innerDerivedUnit());
230
231 setComplete(); endTransaction();
232 try {if (true){printDataSet(System.out, new
233 String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
234 ,"DERIVATIONEVENT",
235 "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA","DESCRIPTIONBASE_SPECIMENOROBSERVATIONBASE",
236 "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
237 } catch(Exception e) { logger.warn(e);}
238
239 }
240
241 @Test
242 @Ignore
243 // TODO generally works but has id problems when running together with above
244 // test ()setFieldObjectImageGallery. Therefore set to ignore.
245 @DataSet("DerivedUnitFacadeTest.testSetDerivedUnitImageGallery.xml")
246 @ExpectedDataSet
247 public void testSetDerivedUnitImageGallery() {
248 // UUID specimenUUID =
249 // UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
250 // Specimen specimen = (Specimen)service.find(specimenUUID);
251 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
252 Media media = Media.NewInstance(URI.create("www.derivedUnitImage.de"),200, null, "png");
253
254 try {
255 SpecimenDescription imageGallery = SpecimenDescription
256 .NewInstance();
257 imageGallery.addDescribedSpecimenOrObservation(facade
258 .innerDerivedUnit());
259 imageGallery.setImageGallery(true);
260 TextData textData = TextData.NewInstance();
261 imageGallery.addElement(textData);
262 textData.addMedia(media);
263 facade.setDerivedUnitImageGallery(imageGallery);
264
265 } catch (DerivedUnitFacadeNotSupportedException e1) {
266 e1.printStackTrace();
267 Assert.fail(e1.getLocalizedMessage());
268 }
269 this.service.save(facade.innerDerivedUnit());
270
271 // setComplete(); endTransaction();
272 // try {if (true){printDataSet(System.out, new
273 // String[]{"HIBERNATE_SEQUENCES","SPECIMENOROBSERVATIONBASE","SPECIMENOROBSERVATIONBASE_DERIVATIONEVENT"
274 // ,"DERIVATIONEVENT",
275 // "DESCRIPTIONBASE","DESCRIPTIONELEMENTBASE","DESCRIPTIONELEMENTBASE_MEDIA","DESCRIPTIONBASE_SPECIMENOROBSERVATIONBASE",
276 // "MEDIA", "MEDIAREPRESENTATION","MEDIAREPRESENTATIONPART"});}
277 // } catch(Exception e) { logger.warn(e);}
278
279
280 }
281
282 @Test
283 @DataSet
284 @Ignore // TODO generally works causes has id problems with following tests when running in suite
285 public void testGetFieldObjectImageGalleryBooleanPersisted() {
286 UUID specimenUUID = UUID
287 .fromString("25383fc8-789b-4eff-92d3-a770d0622351");
288 Specimen specimen = (Specimen) service.load(specimenUUID);
289 Assert.assertNotNull("Specimen should exist (persisted)", specimen);
290 try {
291 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
292 SpecimenDescription imageGallery = facade
293 .getFieldObjectImageGallery(true);
294 Assert.assertNotNull("Image gallery should exist", imageGallery);
295 Assert.assertEquals("UUID should be equal to the persisted uuid",
296 UUID.fromString("8cb772e9-1577-45c6-91ab-dbec1413c060"),
297 imageGallery.getUuid());
298 Assert.assertEquals("The image gallery should be flagged as such",
299 true, imageGallery.isImageGallery());
300 Assert.assertEquals(
301 "There should be one TextData in image gallery", 1,
302 imageGallery.getElements().size());
303 List<Media> media = imageGallery.getElements().iterator().next()
304 .getMedia();
305 Assert.assertEquals("There should be 1 media", 1, media.size());
306 } catch (DerivedUnitFacadeNotSupportedException e) {
307 e.printStackTrace();
308 Assert.fail();
309 }
310 }
311
312 @Test
313 @DataSet
314 // @Ignore // TODO generally works causes has id problems with following tests when running in suite
315 public void testGetDerivedUnitImageGalleryBooleanPersisted() {
316 UUID specimenUUID = UUID.fromString("25383fc8-789b-4eff-92d3-a770d0622351");
317 Specimen specimen = (Specimen) service.load(specimenUUID);
318 Assert.assertNotNull("Specimen should exist (persisted)", specimen);
319 try {
320 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
321 SpecimenDescription imageGallery = facade.getDerivedUnitImageGallery(true);
322 Assert.assertNotNull("Image gallery should exist", imageGallery);
323 Assert.assertEquals("UUID should be equal to the persisted uuid",
324 UUID.fromString("cb03acc4-8363-4020-aeef-ea8a8bcc0fe9"),
325 imageGallery.getUuid());
326 Assert.assertEquals("The image gallery should be flagged as such",
327 true, imageGallery.isImageGallery());
328 Assert.assertEquals(
329 "There should be one TextData in image gallery", 1,
330 imageGallery.getElements().size());
331 List<Media> media = imageGallery.getElements().iterator().next().getMedia();
332 Assert.assertEquals("There should be 1 media", 1, media.size());
333 } catch (DerivedUnitFacadeNotSupportedException e) {
334 e.printStackTrace();
335 Assert.fail();
336 }
337 }
338
339 @Test
340 public void testGetDerivedUnitImageGalleryBoolean() {
341 Specimen specimen = Specimen.NewInstance();
342 try {
343 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(specimen);
344 SpecimenDescription imageGallery = facade.getDerivedUnitImageGallery(true);
345 Assert.assertNotNull("Image Gallery should have been created",imageGallery);
346 Assert.assertEquals("The image gallery should be flagged as such",true, imageGallery.isImageGallery());
347 } catch (DerivedUnitFacadeNotSupportedException e) {
348 e.printStackTrace();
349 Assert.fail();
350 }
351
352 }
353
354 /**
355 * Test method for
356 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance()}.
357 */
358 @Test
359 public void testNewInstance() {
360 Assert.assertNotNull("The specimen should have been created",
361 specimenFacade.innerDerivedUnit());
362 // ???
363 // Assert.assertNotNull("The derivation event should have been created",
364 // specimenFacade.getSpecimen().getDerivedFrom());
365 // Assert.assertNotNull("The field observation should have been created",
366 // specimenFacade.getFieldObservation());
367 // Assert.assertNotNull("The gathering event should have been created",
368 // specimenFacade.getGatheringEvent());
369 }
370
371 /**
372 * Test method for
373 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#NewInstance(eu.etaxonomy.cdm.model.occurrence.Specimen)}
374 * .
375 */
376 @Test
377 public void testNewInstanceSpecimen() {
378 Assert.assertSame("Specimen should be same", specimen,
379 specimenFacade.innerDerivedUnit());
380 Assert.assertSame("Derivation event should be same", derivationEvent,
381 specimenFacade.innerDerivedUnit().getDerivedFrom());
382 Assert.assertSame("Field observation should be same", fieldObservation,
383 specimenFacade.innerFieldObservation());
384 Assert.assertSame("Gathering event should be same", gatheringEvent,
385 specimenFacade.innerGatheringEvent());
386
387 }
388
389 @Test
390 public void testGatheringEventIsConnectedToDerivedUnit() {
391 Specimen specimen = Specimen.NewInstance();
392 DerivedUnitFacade specimenFacade;
393 try {
394 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
395 specimenFacade.setDistanceToGround(2);
396 FieldObservation specimenFieldObservation = (FieldObservation) specimen
397 .getDerivedFrom().getOriginals().iterator().next();
398 Assert.assertSame(
399 "Facade gathering event and specimen gathering event should be the same",
400 specimenFacade.innerGatheringEvent(),
401 specimenFieldObservation.getGatheringEvent());
402 } catch (DerivedUnitFacadeNotSupportedException e) {
403 Assert.fail("An error should not occur in NewInstance()");
404 }
405 }
406
407 @Test
408 public void testNoGatheringEventAndFieldObservation() {
409 Specimen specimen = Specimen.NewInstance();
410 DerivedUnitFacade specimenFacade;
411 try {
412 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
413 Assert.assertNull("No field observation should exists",
414 specimenFacade.innerFieldObservation());
415 } catch (DerivedUnitFacadeNotSupportedException e) {
416 Assert.fail("An error should not occur in NewInstance()");
417 }
418 }
419
420 @Test
421 public void testInititializeTextDataWithSupportTest() {
422 // TODO
423 Specimen specimen = Specimen.NewInstance();
424 DerivedUnitFacade specimenFacade;
425 try {
426 specimenFacade = DerivedUnitFacade.NewInstance(specimen);
427 specimenFacade.setEcology("Ecology");
428 String plantDescription = specimenFacade.getPlantDescription();
429 Assert.assertNull(
430 "No plantDescription should exist yet and no NPE should be thrown until now",
431 plantDescription);
432 } catch (DerivedUnitFacadeNotSupportedException e) {
433 Assert.fail("An error should not occur in NewInstance()");
434 }
435 }
436
437 @Test
438 public void testGetSetCountry() {
439 logger.warn("Not yet implemented");
440 }
441
442 /**
443 * Test method for
444 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
445 * .
446 */
447 @Test
448 public void testAddGetRemoveCollectingArea() {
449 String tdwgLabel = "GER";
450 NamedArea tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwgLabel);
451 NamedArea newCollectingArea = NamedArea.NewInstance("A nice area",
452 "nice", "n");
453 specimenFacade.addCollectingArea(newCollectingArea);
454 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
455 .getCollectingAreas().size());
456 Assert.assertSame("Areas should be same", newCollectingArea,
457 specimenFacade.innerFieldObservation().getGatheringEvent()
458 .getCollectingAreas().iterator().next());
459 specimenFacade.addCollectingArea(tdwgArea);
460 Assert.assertEquals("Exactly 2 areas must exist", 2, specimenFacade
461 .getCollectingAreas().size());
462 specimenFacade.removeCollectingArea(newCollectingArea);
463 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
464 .getCollectingAreas().size());
465 NamedArea remainingArea = specimenFacade.getCollectingAreas()
466 .iterator().next();
467 Assert.assertEquals("Areas should be same", tdwgArea, remainingArea);
468 specimenFacade.removeCollectingArea(tdwgArea);
469 Assert.assertEquals("No area should remain", 0, specimenFacade
470 .getCollectingAreas().size());
471 }
472
473 /**
474 * Test method for
475 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addCollectingArea(eu.etaxonomy.cdm.model.location.NamedArea)}
476 * .
477 */
478 @Test
479 public void testAddCollectingAreas() {
480 NamedArea firstArea = NamedArea.NewInstance("A nice area", "nice", "n");
481 Assert.assertEquals("No area must exist", 0, specimenFacade
482 .getCollectingAreas().size());
483 specimenFacade.addCollectingArea(firstArea);
484 Assert.assertEquals("Exactly 1 area must exist", 1, specimenFacade
485 .getCollectingAreas().size());
486
487 String tdwgLabel = "GER";
488 NamedArea tdwgArea = TdwgArea.getAreaByTdwgAbbreviation(tdwgLabel);
489 NamedArea secondArea = NamedArea
490 .NewInstance("A nice area", "nice", "n");
491
492 java.util.Collection<NamedArea> areaCollection = new HashSet<NamedArea>();
493 areaCollection.add(secondArea);
494 areaCollection.add(tdwgArea);
495 specimenFacade.addCollectingAreas(areaCollection);
496 Assert.assertEquals("Exactly 3 areas must exist", 3, specimenFacade
497 .getCollectingAreas().size());
498
499 }
500
501 /**
502 * Test method for
503 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAbsoluteElevation()}
504 * .
505 */
506 @Test
507 @Ignore // #######DerivationEvent --------------------------- 2
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 @Ignore // #######DerivationEvent ---------------------------------------- 1
1012 public void testSetGatheringEvent() {
1013 GatheringEvent newGatheringEvent = GatheringEvent.NewInstance();
1014 newGatheringEvent.setDistanceToGround(43);
1015 Assert.assertFalse("The initial distance to ground should not be 43",
1016 specimenFacade.getDistanceToGround() == 43);
1017 specimenFacade.setGatheringEvent(newGatheringEvent);
1018 Assert.assertTrue("The final distance to ground should be 43",
1019 specimenFacade.getDistanceToGround() == 43);
1020 Assert.assertSame(
1021 "The new gathering event should be 'newGatheringEvent'",
1022 newGatheringEvent, specimenFacade.innerGatheringEvent());
1023 }
1024
1025 /**
1026 * Test method for
1027 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerGatheringEvent()}
1028 * .
1029 */
1030 @Test
1031 public void testGetGatheringEvent() {
1032 Assert.assertNotNull("Gathering event must not be null",
1033 specimenFacade.innerGatheringEvent());
1034 Assert.assertEquals(
1035 "Gathering event must be field observations gathering event",
1036 specimenFacade.innerFieldObservation().getGatheringEvent(),
1037 specimenFacade.innerGatheringEvent());
1038 }
1039
1040 /**
1041 * Test method for
1042 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getIndividualCount()}
1043 * .
1044 */
1045 @Test
1046 public void testGetSetIndividualCount() {
1047 Assert.assertEquals("Individual count must be same", individualCount,
1048 specimenFacade.getIndividualCount());
1049 specimenFacade.setIndividualCount(4);
1050 Assert.assertEquals("New individual count must be '4'",
1051 Integer.valueOf(4), specimenFacade.getIndividualCount());
1052
1053 }
1054
1055 /**
1056 * Test method for
1057 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLifeStage()}.
1058 */
1059 @Test
1060 public void testGetSetLifeStage() {
1061 Assert.assertNotNull("Life stage must not be null",
1062 specimenFacade.getLifeStage());
1063 Assert.assertEquals("Life stage must be same", lifeStage,
1064 specimenFacade.getLifeStage());
1065 specimenFacade.setLifeStage(null);
1066 Assert.assertNull("Life stage must be null",
1067 specimenFacade.getLifeStage());
1068 }
1069
1070 /**
1071 * Test method for
1072 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getSex()}.
1073 */
1074 @Test
1075 public void testGetSetSex() {
1076 Assert.assertNotNull("Sex must not be null", specimenFacade.getSex());
1077 Assert.assertEquals("Sex must be same", sex, specimenFacade.getSex());
1078 specimenFacade.setSex(null);
1079 Assert.assertNull("Sex must be null", specimenFacade.getSex());
1080 }
1081
1082 /**
1083 * Test method for
1084 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getLocality()}.
1085 */
1086 @Test
1087 public void testGetSetLocality() {
1088 Assert.assertEquals("Locality must be same", locality,
1089 specimenFacade.getLocality());
1090 specimenFacade.setLocality("A completely new place", Language.FRENCH());
1091 Assert.assertEquals("New locality must be 'A completely new place'",
1092 "A completely new place", specimenFacade.getLocalityText());
1093 Assert.assertEquals("New locality language must be French",
1094 Language.FRENCH(), specimenFacade.getLocalityLanguage());
1095 specimenFacade.setLocality("Another place");
1096 Assert.assertEquals("New locality must be 'Another place'",
1097 "Another place", specimenFacade.getLocalityText());
1098 Assert.assertEquals("New locality language must be default",
1099 Language.DEFAULT(), specimenFacade.getLocalityLanguage());
1100 }
1101
1102 /**
1103 * Test method for
1104 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitDefinition(java.lang.String, eu.etaxonomy.cdm.model.common.Language)}
1105 * .
1106 */
1107 @Test
1108 public void testAddGetRemoveSpecimenDefinition() {
1109 Assert.assertEquals("There should be no definition yet", 0,
1110 specimenFacade.getDerivedUnitDefinitions().size());
1111 specimenFacade.addDerivedUnitDefinition("Tres interesant",
1112 Language.FRENCH());
1113 Assert.assertEquals("There should be exactly one definition", 1,
1114 specimenFacade.getDerivedUnitDefinitions().size());
1115 Assert.assertEquals(
1116 "The French definition should be 'Tres interesant'",
1117 "Tres interesant", specimenFacade.getDerivedUnitDefinitions()
1118 .get(Language.FRENCH()).getText());
1119 Assert.assertEquals(
1120 "The French definition should be 'Tres interesant'",
1121 "Tres interesant",
1122 specimenFacade.getDerivedUnitDefinition(Language.FRENCH()));
1123 specimenFacade.addDerivedUnitDefinition("Sehr interessant",
1124 Language.GERMAN());
1125 Assert.assertEquals("There should be exactly 2 definition", 2,
1126 specimenFacade.getDerivedUnitDefinitions().size());
1127 specimenFacade.removeDerivedUnitDefinition(Language.FRENCH());
1128 Assert.assertEquals("There should remain exactly 1 definition", 1,
1129 specimenFacade.getDerivedUnitDefinitions().size());
1130 Assert.assertEquals(
1131 "The remaining German definition should be 'Sehr interessant'",
1132 "Sehr interessant",
1133 specimenFacade.getDerivedUnitDefinition(Language.GERMAN()));
1134 specimenFacade.removeDerivedUnitDefinition(Language.GERMAN());
1135 Assert.assertEquals("There should remain no definition", 0,
1136 specimenFacade.getDerivedUnitDefinitions().size());
1137 }
1138
1139 /**
1140 * Test method for
1141 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1142 * .
1143 */
1144 @Test
1145 public void testAddGetRemoveDetermination() {
1146 Assert.assertEquals("There should be no determination yet", 0,
1147 specimenFacade.getDeterminations().size());
1148 DeterminationEvent determinationEvent1 = DeterminationEvent
1149 .NewInstance();
1150 specimenFacade.addDetermination(determinationEvent1);
1151 Assert.assertEquals("There should be exactly one determination", 1,
1152 specimenFacade.getDeterminations().size());
1153 Assert.assertEquals("The only determination should be determination 1",
1154 determinationEvent1, specimenFacade.getDeterminations()
1155 .iterator().next());
1156
1157 DeterminationEvent determinationEvent2 = DeterminationEvent
1158 .NewInstance();
1159 specimenFacade.addDetermination(determinationEvent2);
1160 Assert.assertEquals("There should be exactly 2 determinations", 2,
1161 specimenFacade.getDeterminations().size());
1162 specimenFacade.removeDetermination(determinationEvent1);
1163
1164 Assert.assertEquals("There should remain exactly 1 determination", 1,
1165 specimenFacade.getDeterminations().size());
1166 Assert.assertEquals(
1167 "The remaining determinations should be determination 2",
1168 determinationEvent2, specimenFacade.getDeterminations()
1169 .iterator().next());
1170
1171 specimenFacade.removeDetermination(determinationEvent1);
1172 Assert.assertEquals("There should remain exactly 1 determination", 1,
1173 specimenFacade.getDeterminations().size());
1174
1175 specimenFacade.removeDetermination(determinationEvent2);
1176 Assert.assertEquals("There should remain no definition", 0,
1177 specimenFacade.getDerivedUnitDefinitions().size());
1178
1179 }
1180
1181
1182 /**
1183 * Test method for
1184 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDetermination(eu.etaxonomy.cdm.model.occurrence.DeterminationEvent)}
1185 * .
1186 */
1187 @Test
1188 public void testPreferredOtherDeterminations() {
1189 Assert.assertEquals("There should be no determination yet", 0,
1190 specimenFacade.getDeterminations().size());
1191 DeterminationEvent determinationEvent1 = DeterminationEvent.NewInstance();
1192 specimenFacade.setPreferredDetermination(determinationEvent1);
1193
1194 Assert.assertEquals("There should be exactly one determination", 1,
1195 specimenFacade.getDeterminations().size());
1196 Assert.assertEquals("The only determination should be determination 1",
1197 determinationEvent1, specimenFacade.getDeterminations()
1198 .iterator().next());
1199 Assert.assertEquals("determination 1 should be the preferred determination",
1200 determinationEvent1, specimenFacade.getPreferredDetermination());
1201 Assert.assertEquals("There should be no 'non preferred' determination", 0,
1202 specimenFacade.getOtherDeterminations().size());
1203
1204
1205
1206 DeterminationEvent determinationEvent2 = DeterminationEvent.NewInstance();
1207 specimenFacade.addDetermination(determinationEvent2);
1208 Assert.assertEquals("There should be exactly 2 determinations", 2,
1209 specimenFacade.getDeterminations().size());
1210 Assert.assertEquals("determination 1 should be the preferred determination",
1211 determinationEvent1, specimenFacade.getPreferredDetermination());
1212 Assert.assertEquals("There should be one 'non preferred' determination", 1,
1213 specimenFacade.getOtherDeterminations().size());
1214 Assert.assertEquals("The only 'non preferred' determination should be determination 2",
1215 determinationEvent2, specimenFacade.getOtherDeterminations().iterator().next());
1216
1217
1218 DeterminationEvent determinationEvent3 = DeterminationEvent.NewInstance();
1219 specimenFacade.setPreferredDetermination(determinationEvent3);
1220 Assert.assertEquals("There should be exactly 3 determinations", 3,
1221 specimenFacade.getDeterminations().size());
1222 Assert.assertEquals("determination 3 should be the preferred determination",
1223 determinationEvent3, specimenFacade.getPreferredDetermination());
1224 Assert.assertEquals("There should be 2 'non preferred' determination", 2,
1225 specimenFacade.getOtherDeterminations().size());
1226 Assert.assertTrue("determination 1 should be contained in the set of 'non preferred' determinations",
1227 specimenFacade.getOtherDeterminations().contains(determinationEvent1));
1228 Assert.assertTrue("determination 2 should be contained in the set of 'non preferred' determinations",
1229 specimenFacade.getOtherDeterminations().contains(determinationEvent2));
1230
1231 }
1232
1233 /**
1234 * Test method for
1235 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#addDerivedUnitMedia(eu.etaxonomy.cdm.model.media.Media)}
1236 * .
1237 */
1238 @Test
1239 public void testAddGetHasRemoveSpecimenMedia() {
1240 Assert.assertFalse("There should be no image gallery yet",
1241 specimenFacade.hasDerivedUnitImageGallery());
1242 Assert.assertFalse(
1243 "There should be also no field object image gallery yet",
1244 specimenFacade.hasFieldObjectImageGallery());
1245
1246 List<Media> media = specimenFacade.getDerivedUnitMedia();
1247 Assert.assertFalse(
1248 "There should still not be an empty image gallery now",
1249 specimenFacade.hasDerivedUnitImageGallery());
1250 Assert.assertEquals("There should be no media yet in the gallery", 0,
1251 media.size());
1252
1253 Media media1 = Media.NewInstance();
1254 specimenFacade.addDerivedUnitMedia(media1);
1255 Assert.assertEquals("There should be exactly one specimen media", 1,
1256 specimenFacade.getDerivedUnitMedia().size());
1257 Assert.assertEquals("The only media should be media 1", media1,
1258 specimenFacade.getDerivedUnitMedia().get(0));
1259 Assert.assertFalse(
1260 "There should be still no field object image gallery",
1261 specimenFacade.hasFieldObjectImageGallery());
1262
1263 Media media2 = Media.NewInstance();
1264 specimenFacade.addDerivedUnitMedia(media2);
1265 Assert.assertEquals("There should be exactly 2 specimen media", 2,
1266 specimenFacade.getDerivedUnitMedia().size());
1267 Assert.assertEquals("The first media should be media1", media1,
1268 specimenFacade.getDerivedUnitMedia().get(0));
1269 Assert.assertEquals("The second media should be media2", media2,
1270 specimenFacade.getDerivedUnitMedia().get(1));
1271
1272 specimenFacade.removeDerivedUnitMedia(media1);
1273 Assert.assertEquals("There should be exactly one specimen media", 1,
1274 specimenFacade.getDerivedUnitMedia().size());
1275 Assert.assertEquals("The only media should be media2", media2,
1276 specimenFacade.getDerivedUnitMedia().get(0));
1277
1278 specimenFacade.removeDerivedUnitMedia(media1);
1279 Assert.assertEquals("There should still be exactly one specimen media",
1280 1, specimenFacade.getDerivedUnitMedia().size());
1281
1282 specimenFacade.removeDerivedUnitMedia(media2);
1283 Assert.assertEquals("There should remain no media in the gallery", 0,
1284 media.size());
1285 }
1286
1287 /**
1288 * Test method for
1289 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getAccessionNumber()}
1290 * .
1291 */
1292 @Test
1293 public void testGetSetAccessionNumber() {
1294 Assert.assertEquals("Accession number must be same", accessionNumber,
1295 specimenFacade.getAccessionNumber());
1296 specimenFacade.setAccessionNumber("A12345693");
1297 Assert.assertEquals("New accession number must be 'A12345693'",
1298 "A12345693", specimenFacade.getAccessionNumber());
1299 }
1300
1301 /**
1302 * Test method for
1303 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCatalogNumber()}.
1304 */
1305 @Test
1306 public void testGetCatalogNumber() {
1307 Assert.assertEquals("Catalog number must be same", catalogNumber,
1308 specimenFacade.getCatalogNumber());
1309 specimenFacade.setCatalogNumber("B12345693");
1310 Assert.assertEquals("New catalog number must be 'B12345693'",
1311 "B12345693", specimenFacade.getCatalogNumber());
1312 }
1313
1314 /**
1315 * Test method for
1316 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getPreservation()}.
1317 */
1318 @Test
1319 public void testGetPreservation() {
1320 try {
1321 Assert.assertNotNull("Preservation method must not be null",
1322 specimenFacade.getPreservationMethod());
1323 Assert.assertEquals("Preservation method must be same",
1324 preservationMethod, specimenFacade.getPreservationMethod());
1325 specimenFacade.setPreservationMethod(null);
1326 Assert.assertNull("Preservation method must be null",
1327 specimenFacade.getPreservationMethod());
1328 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1329 Assert.fail("Method not supported should not be thrown for a specimen");
1330 }
1331 specimenFacade = DerivedUnitFacade
1332 .NewInstance(DerivedUnitType.Observation);
1333 try {
1334 specimenFacade.setPreservationMethod(preservationMethod);
1335 Assert.fail("Method not supported should be thrown for an observation on set preservation method");
1336
1337 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1338 // ok
1339 }
1340 specimenFacade = DerivedUnitFacade
1341 .NewInstance(DerivedUnitType.LivingBeing);
1342 try {
1343 specimenFacade.getPreservationMethod();
1344 Assert.fail("Method not supported should be thrown for a living being on get preservation method");
1345 } catch (MethodNotSupportedByDerivedUnitTypeException e) {
1346 // ok
1347 }
1348
1349 }
1350
1351 /**
1352 * Test method for
1353 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getStoredUnder()}.
1354 */
1355 @Test
1356 public void testGetStoredUnder() {
1357 Assert.assertNotNull("Stored under name must not be null",
1358 specimenFacade.getStoredUnder());
1359 Assert.assertEquals("Stored under name must be same", taxonName,
1360 specimenFacade.getStoredUnder());
1361 specimenFacade.setStoredUnder(null);
1362 Assert.assertNull("Stored under name must be null",
1363 specimenFacade.getStoredUnder());
1364 }
1365
1366 // /**
1367 // * Test method for
1368 // * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollectorsNumber()}
1369 // * .
1370 // */
1371 // @Test
1372 // public void testGetSetCollectorsNumber() {
1373 // Assert.assertEquals("Collectors number must be same", collectorsNumber,
1374 // specimenFacade.getCollectorsNumber());
1375 // specimenFacade.setCollectorsNumber("C12345693");
1376 // Assert.assertEquals("New collectors number must be 'C12345693'",
1377 // "C12345693", specimenFacade.getCollectorsNumber());
1378 // }
1379
1380 /**
1381 * Test method for
1382 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getTitleCache()}.
1383 */
1384 @Test
1385 public void testGetTitleCache() {
1386 Assert.assertNotNull(
1387 "The title cache should never return null if not protected",
1388 specimenFacade.getTitleCache());
1389 specimenFacade.setTitleCache(null, false);
1390 Assert.assertNotNull(
1391 "The title cache should never return null if not protected",
1392 specimenFacade.getTitleCache());
1393 }
1394
1395 /**
1396 * Test method for
1397 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#setTitleCache(java.lang.String)}
1398 * .
1399 */
1400 @Test
1401 public void testSetTitleCache() {
1402 String testTitle = "Absdwk aksjlf";
1403 specimenFacade.setTitleCache(testTitle, true);
1404 Assert.assertEquals(
1405 "Protected title cache should returns the test title",
1406 testTitle, specimenFacade.getTitleCache());
1407 specimenFacade.setTitleCache(testTitle, false);
1408 Assert.assertFalse(
1409 "Unprotected title cache should not return the test title",
1410 testTitle.equals(specimenFacade.getTitleCache()));
1411 }
1412
1413 /**
1414 * Test method for
1415 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#innerDerivedUnit()}.
1416 */
1417 @Test
1418 public void testGetSpecimen() {
1419 Assert.assertEquals("Specimen must be same", specimen,
1420 specimenFacade.innerDerivedUnit());
1421 }
1422
1423 /**
1424 * Test method for
1425 * {@link eu.etaxonomy.cdm.api.facade.DerivedUnitFacade#getCollection()}.
1426 */
1427 @Test
1428 public void testGetSetCollection() {
1429 Assert.assertNotNull("Collection must not be null",
1430 specimenFacade.getCollection());
1431 Assert.assertEquals("Collection must be same", collection,
1432 specimenFacade.getCollection());
1433 specimenFacade.setCollection(null);
1434 Assert.assertNull("Collection must be null",
1435 specimenFacade.getCollection());
1436 }
1437
1438 @Test
1439 public void testAddGetRemoveSource() {
1440 Assert.assertEquals("No sources should exist yet", 0, specimenFacade.getSources().size());
1441
1442 Reference<?> reference = ReferenceFactory.newBook();
1443 IdentifiableSource source1 = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference, "54", "myName");
1444 Assert.assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1445 IdentifiableSource source2 = IdentifiableSource.NewDataImportInstance("1", "myTable");
1446 specimenFacade.addSource(source2);
1447 Assert.assertEquals("One source should exist now", 2, specimenFacade.getSources().size());
1448 specimenFacade.removeSource(source1);
1449 Assert.assertEquals("One source should exist now", 1, specimenFacade.getSources().size());
1450 Reference reference2 = ReferenceFactory.newJournal();
1451 IdentifiableSource sourceNotUsed = specimenFacade.addSource(OriginalSourceType.PrimaryTaxonomicSource, reference2,null, null);
1452 specimenFacade.removeSource(sourceNotUsed);
1453 Assert.assertEquals("One source should still exist", 1, specimenFacade.getSources().size());
1454 Assert.assertEquals("1", specimenFacade.getSources().iterator().next().getIdInSource());
1455 specimenFacade.removeSource(source2);
1456 Assert.assertEquals("No sources should exist anymore", 0,specimenFacade.getSources().size());
1457 }
1458
1459 @Test
1460 public void testAddGetRemoveDuplicate() {
1461 Assert.assertEquals("No duplicates should be available yet", 0,specimenFacade.getDuplicates().size());
1462 Specimen newSpecimen1 = Specimen.NewInstance();
1463 specimenFacade.addDuplicate(newSpecimen1);
1464 Assert.assertEquals("There should be 1 duplicate now", 1,specimenFacade.getDuplicates().size());
1465 Specimen newSpecimen2 = Specimen.NewInstance();
1466 DerivationEvent newDerivationEvent = DerivationEvent.NewInstance(DerivationEventType.ACCESSIONING());
1467 newSpecimen2.setDerivedFrom(newDerivationEvent);
1468 Assert.assertSame(
1469 "The derivation event should be 'newDerivationEvent'",
1470 newDerivationEvent, newSpecimen2.getDerivedFrom());
1471 specimenFacade.addDuplicate(newSpecimen2);
1472 Assert.assertEquals("There should be 2 duplicates now", 2, specimenFacade.getDuplicates().size());
1473 Assert.assertNotSame(
1474 "The derivation event should not be 'newDerivationEvent' anymore",
1475 newDerivationEvent, newSpecimen2.getDerivedFrom());
1476 Assert.assertSame(
1477 "The derivation event should not be the facades derivation event",
1478 derivationEvent, newSpecimen2.getDerivedFrom());
1479 specimenFacade.removeDuplicate(newSpecimen1);
1480 Assert.assertEquals("There should be 1 duplicate now", 1,
1481 specimenFacade.getDuplicates().size());
1482 Assert.assertSame("The only duplicate should be 'newSpecimen2' now",
1483 newSpecimen2, specimenFacade.getDuplicates().iterator().next());
1484 specimenFacade.addDuplicate(specimenFacade.innerDerivedUnit());
1485 Assert.assertEquals(
1486 "There should be still 1 duplicate because the facade specimen is not a duplicate",
1487 1, specimenFacade.getDuplicates().size());
1488
1489 Collection newCollection = Collection.NewInstance();
1490 String catalogNumber = "1234890";
1491 String accessionNumber = "345345";
1492 String collectorsNumber = "lkjewe";
1493 TaxonNameBase<?,?> storedUnder = BotanicalName.NewInstance(Rank.SPECIES());
1494 PreservationMethod method = PreservationMethod.NewInstance();
1495 Specimen duplicateSpecimen = specimenFacade.addDuplicate(newCollection,
1496 catalogNumber, accessionNumber, storedUnder,
1497 method);
1498 Assert.assertEquals("There should be 2 duplicates now", 2,
1499 specimenFacade.getDuplicates().size());
1500 specimenFacade.removeDuplicate(newSpecimen2);
1501 Assert.assertEquals("There should be 1 duplicates now", 1,
1502 specimenFacade.getDuplicates().size());
1503 Collection sameCollection = specimenFacade.getDuplicates().iterator()
1504 .next().getCollection();
1505 Assert.assertSame("Collections should be same", newCollection,
1506 sameCollection);
1507 }
1508
1509 // ************************** Existing Specimen with multiple derivation
1510 // events in line **************/
1511
1512 @Test
1513 public void testExistingSpecimen() {
1514 specimenFacade = null;
1515 try {
1516 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1517 } catch (DerivedUnitFacadeNotSupportedException e) {
1518 Assert.fail("Multiple derivation events in line should not throw a not supported exception");
1519 }
1520 Assert.assertSame(
1521 "Gathering event should derive from the derivation line",
1522 existingGatheringEvent, specimenFacade.innerGatheringEvent());
1523 Assert.assertEquals(
1524 "Mediasize should be 0. Only Imagegallery media are supported",
1525 0, specimenFacade.getFieldObjectMedia().size());
1526 }
1527
1528 @Test
1529 public void testMultipleFieldObservationsNotSupported() {
1530 specimenFacade = null;
1531 FieldObservation secondFieldObject = FieldObservation.NewInstance();
1532 firstDerivationEvent.addOriginal(secondFieldObject);
1533 try {
1534 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1535 Assert.fail("Multiple field observations for one specimen should no be supported by the facade");
1536 } catch (DerivedUnitFacadeNotSupportedException e) {
1537 // ok
1538 }
1539 Assert.assertNull("Specimen facade should not be initialized",
1540 specimenFacade);
1541 }
1542
1543 @Test // #######DerivationEvent
1544 public void testOnlyImageGallerySupported() {
1545 specimenFacade = null;
1546 firstFieldObject.addMedia(media1);
1547 try {
1548 specimenFacade = DerivedUnitFacade.NewInstance(collectionSpecimen);
1549 Assert.fail("Only image galleries are supported by the facade but not direct media");
1550 } catch (DerivedUnitFacadeNotSupportedException e) {
1551 // ok
1552 }
1553 Assert.assertNull("Specimen facade should not be initialized",
1554 specimenFacade);
1555 }
1556
1557 @Test // #######DerivationEvent
1558 public void testEventPropagation() {
1559 specimenFacade.setDistanceToGround(24);
1560
1561 }
1562
1563 // @Ignore // set to ignore because I did not want to check knowingly
1564 // failing tests. Remove @Ignore when this is fixed
1565 @Test
1566 public void testSetBarcode() {
1567 String barcode = "barcode";
1568 specimenFacade.setBarcode(barcode);
1569
1570 Assert.assertEquals(barcode, specimenFacade.getBarcode());
1571 }
1572
1573 @Test
1574 public void testIsEvenDistance() {
1575 Integer minimum = 20;
1576 Integer maximum = 1234;
1577
1578 // this should not throw exceptions
1579 specimenFacade.setAbsoluteElevationRange(minimum, maximum);
1580 }
1581
1582 /**
1583 *
1584 * See https://dev.e-taxonomy.eu/trac/ticket/2426
1585 * This test doesn't handle the above issue yet as it doesn't fire events as
1586 * expected (at least it does not reproduce the behaviour in the Taxonomic Editor).
1587 * In the meanwhile the property change framework for the facade has been changed
1588 * so the original problem may have disappeared.
1589 *
1590 */
1591 @Test
1592 public void testNoRecursiveChangeEvents(){
1593 String username = "username";
1594 String password = "password";
1595 User user = User.NewInstance(username, password);
1596 userService.save(user);
1597 UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(user, password);
1598 SecurityContextHolder.getContext().setAuthentication(token);
1599
1600 DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(DerivedUnitType.Specimen);
1601 facade.setLocality("testLocality");
1602 facade.getTitleCache();
1603 // facade.innerGatheringEvent().firePropertyChange("createdBy", null, user);
1604 this.service.save(facade.innerDerivedUnit());
1605 commitAndStartNewTransaction(null);
1606 }
1607
1608 }