Revision 485da8a9
Added by Andreas Kohlbecker over 3 years ago
cdmlib-model/src/test/java/eu/etaxonomy/cdm/model/occurrence/SpecimenTest.java | ||
---|---|---|
18 | 18 |
import static org.junit.Assert.assertTrue; |
19 | 19 |
|
20 | 20 |
import java.util.Calendar; |
21 |
import java.util.Comparator; |
|
22 |
import java.util.List; |
|
21 | 23 |
import java.util.UUID; |
24 |
import java.util.stream.Collectors; |
|
22 | 25 |
|
26 |
import org.apache.log4j.Level; |
|
23 | 27 |
import org.apache.log4j.Logger; |
24 | 28 |
import org.joda.time.DateTime; |
25 | 29 |
import org.junit.Assert; |
... | ... | |
32 | 36 |
import eu.etaxonomy.cdm.model.agent.Person; |
33 | 37 |
import eu.etaxonomy.cdm.model.common.Annotation; |
34 | 38 |
import eu.etaxonomy.cdm.model.common.Extension; |
39 |
import eu.etaxonomy.cdm.model.common.IdentifiableEntity; |
|
35 | 40 |
import eu.etaxonomy.cdm.model.common.IdentifiableSource; |
36 | 41 |
import eu.etaxonomy.cdm.model.common.LSID; |
37 | 42 |
import eu.etaxonomy.cdm.model.common.Language; |
... | ... | |
54 | 59 |
*/ |
55 | 60 |
public class SpecimenTest { |
56 | 61 |
|
62 |
/** |
|
63 |
* @author a.kohlbecker |
|
64 |
* @since Jan 18, 2021 |
|
65 |
*/ |
|
66 |
private final class ByTtitleCacheComparator implements Comparator<IdentifiableEntity> { |
|
67 |
@Override |
|
68 |
public int compare(IdentifiableEntity o1, IdentifiableEntity o2) { |
|
69 |
return o1.getTitleCache().compareTo(o2.getTitleCache()); |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
57 | 73 |
private static final Logger logger = Logger.getLogger(SpecimenTest.class); |
58 | 74 |
|
59 | 75 |
private DerivedUnit specimen; |
... | ... | |
203 | 219 |
|
204 | 220 |
//Null test is not full implemented, but an error is thrown if null throws |
205 | 221 |
//null pointer exception somewhere |
206 |
DerivedUnit specimenNullClone = (DerivedUnit)specimen.clone();
|
|
222 |
DerivedUnit specimenNullClone = specimen.clone(); |
|
207 | 223 |
|
208 | 224 |
String accessionNumber = "accNumber"; |
209 | 225 |
String catalogNumber = "catNumber"; |
... | ... | |
281 | 297 |
} catch (InterruptedException e) { |
282 | 298 |
//ignore |
283 | 299 |
} |
284 |
DerivedUnit specimenClone = (DerivedUnit)specimen.clone();
|
|
300 |
DerivedUnit specimenClone = specimen.clone(); |
|
285 | 301 |
|
286 | 302 |
assertFalse(id == specimenClone.getId()); |
287 | 303 |
assertFalse(created.equals(specimenClone.getCreated())); |
... | ... | |
347 | 363 |
assertEquals(1, specimenClone.getSources().size()); |
348 | 364 |
} |
349 | 365 |
|
350 |
|
|
351 | 366 |
@Test |
352 | 367 |
public void beanTests(){ |
353 | 368 |
// #5307 Test that BeanUtils does not fail |
... | ... | |
357 | 372 |
BeanUtils.getPropertyDescriptors(MediaSpecimen.class); |
358 | 373 |
BeanUtils.getPropertyDescriptors(DnaSample.class); |
359 | 374 |
} |
375 |
|
|
376 |
@Test |
|
377 |
public void testCollectRootUnits() { |
|
378 |
|
|
379 |
java.util.Collection<SpecimenOrObservationBase> rootSOBs; |
|
380 |
|
|
381 |
DerivedUnit theSpecimen = DerivedUnit.NewPreservedSpecimenInstance(); |
|
382 |
theSpecimen.setTitleCache("*SP*", true); |
|
383 |
|
|
384 |
rootSOBs = theSpecimen.collectRootUnits(null); |
|
385 |
assertEquals(1, rootSOBs.size()); |
|
386 |
assertEquals(theSpecimen, rootSOBs.iterator().next()); |
|
387 |
|
|
388 |
rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class); |
|
389 |
assertEquals(1, rootSOBs.size()); |
|
390 |
assertEquals(theSpecimen, rootSOBs.iterator().next()); |
|
391 |
|
|
392 |
java.util.Collection<DerivedUnit> rootDUs = theSpecimen.collectRootUnits(DerivedUnit.class); |
|
393 |
assertEquals(1, rootDUs.size()); |
|
394 |
assertEquals(theSpecimen, rootDUs.iterator().next()); |
|
395 |
|
|
396 |
java.util.Collection<FieldUnit> rootFUs = theSpecimen.collectRootUnits(FieldUnit.class); |
|
397 |
assertTrue(rootFUs.isEmpty()); |
|
398 |
|
|
399 |
// -- one FieldUnit as root ---------------------------------------- |
|
400 |
|
|
401 |
FieldUnit fieldUnit1 = FieldUnit.NewInstance(); |
|
402 |
fieldUnit1.setTitleCache("FU1", true); |
|
403 |
DerivationEvent.NewSimpleInstance(fieldUnit1, theSpecimen, DerivationEventType.GATHERING_IN_SITU()); |
|
404 |
|
|
405 |
rootSOBs = theSpecimen.collectRootUnits(null); |
|
406 |
assertEquals(1, rootSOBs.size()); |
|
407 |
assertEquals(fieldUnit1, rootSOBs.iterator().next()); |
|
408 |
|
|
409 |
rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class); |
|
410 |
assertEquals(1, rootSOBs.size()); |
|
411 |
assertEquals(fieldUnit1, rootSOBs.iterator().next()); |
|
412 |
|
|
413 |
rootDUs = theSpecimen.collectRootUnits(DerivedUnit.class); |
|
414 |
assertTrue(rootDUs.isEmpty()); |
|
415 |
|
|
416 |
rootFUs = theSpecimen.collectRootUnits(FieldUnit.class); |
|
417 |
assertEquals(1, rootFUs.size()); |
|
418 |
assertEquals(fieldUnit1, rootFUs.iterator().next()); |
|
419 |
|
|
420 |
// -- multiple roots ------------------------------------------ |
|
421 |
|
|
422 |
DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance(); |
|
423 |
specimen1.setTitleCache("SP1", true); |
|
424 |
theSpecimen.getDerivedFrom().addOriginal(specimen1); |
|
425 |
|
|
426 |
rootSOBs = theSpecimen.collectRootUnits(null); |
|
427 |
assertEquals(2, rootSOBs.size()); |
|
428 |
|
|
429 |
rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class); |
|
430 |
assertEquals(2, rootSOBs.size()); |
|
431 |
List<SpecimenOrObservationBase> rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList()); |
|
432 |
assertEquals(fieldUnit1, rootSOBsSorted.get(0)); |
|
433 |
assertEquals(specimen1, rootSOBsSorted.get(1)); |
|
434 |
|
|
435 |
FieldUnit fieldUnit2 = FieldUnit.NewInstance(); |
|
436 |
fieldUnit2.setTitleCache("FU2", true); |
|
437 |
DerivationEvent.NewSimpleInstance(fieldUnit2, specimen1, DerivationEventType.GATHERING_IN_SITU()); |
|
438 |
|
|
439 |
rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class); |
|
440 |
assertEquals(2, rootSOBs.size()); |
|
441 |
rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList()); |
|
442 |
assertEquals(fieldUnit1, rootSOBsSorted.get(0)); |
|
443 |
assertEquals(fieldUnit2, rootSOBsSorted.get(1)); |
|
444 |
|
|
445 |
// -- multiple roots & cycle detection ------------------------------------------ |
|
446 |
|
|
447 |
DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance(); |
|
448 |
specimen2.setTitleCache("SP2", true); |
|
449 |
|
|
450 |
DerivedUnit specimen3 = DerivedUnit.NewPreservedSpecimenInstance(); |
|
451 |
specimen3.setTitleCache("SP3", true); |
|
452 |
|
|
453 |
theSpecimen.getDerivedFrom().addOriginal(specimen2); |
|
454 |
DerivationEvent.NewSimpleInstance(specimen3, specimen2, DerivationEventType.GATHERING_IN_SITU()); |
|
455 |
DerivationEvent.NewSimpleInstance(theSpecimen, specimen3, DerivationEventType.GATHERING_IN_SITU()); |
|
456 |
|
|
457 |
Logger.getLogger(DerivedUnit.class).setLevel(Level.TRACE); |
|
458 |
rootSOBs = theSpecimen.collectRootUnits(SpecimenOrObservationBase.class); |
|
459 |
assertEquals(3, rootSOBs.size()); |
|
460 |
rootSOBsSorted = rootSOBs.stream().sorted(new ByTtitleCacheComparator()).collect(Collectors.toList()); |
|
461 |
assertEquals(fieldUnit1, rootSOBsSorted.get(0)); |
|
462 |
assertEquals(fieldUnit2, rootSOBsSorted.get(1)); |
|
463 |
assertEquals(specimen3, rootSOBsSorted.get(2)); |
|
464 |
|
|
465 |
|
|
466 |
} |
|
360 | 467 |
} |
Also available in: Unified diff
fix #9395 DerivedUnit.collectFieldUnits() as DerivedUnit.collectRootUnits() to return all types of root units