Project

General

Profile

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

    
10
package eu.etaxonomy.cdm.model.occurrence;
11

    
12
import static org.junit.Assert.assertEquals;
13
import static org.junit.Assert.assertFalse;
14
import static org.junit.Assert.assertNotNull;
15
import static org.junit.Assert.assertNotSame;
16
import static org.junit.Assert.assertNull;
17
import static org.junit.Assert.assertSame;
18
import static org.junit.Assert.assertTrue;
19

    
20
import java.util.Calendar;
21
import java.util.Comparator;
22
import java.util.List;
23
import java.util.UUID;
24
import java.util.stream.Collectors;
25

    
26
import org.apache.log4j.Level;
27
import org.apache.log4j.Logger;
28
import org.joda.time.DateTime;
29
import org.junit.Assert;
30
import org.junit.Before;
31
import org.junit.Test;
32
import org.springframework.beans.BeanUtils;
33

    
34
import com.ibm.lsid.MalformedLSIDException;
35

    
36
import eu.etaxonomy.cdm.model.agent.Person;
37
import eu.etaxonomy.cdm.model.common.Annotation;
38
import eu.etaxonomy.cdm.model.common.Extension;
39
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
40
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
41
import eu.etaxonomy.cdm.model.common.LSID;
42
import eu.etaxonomy.cdm.model.common.Language;
43
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
44
import eu.etaxonomy.cdm.model.common.Marker;
45
import eu.etaxonomy.cdm.model.common.MarkerType;
46
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
47
import eu.etaxonomy.cdm.model.media.Rights;
48
import eu.etaxonomy.cdm.model.molecular.DnaSample;
49
import eu.etaxonomy.cdm.model.name.Rank;
50
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
51
import eu.etaxonomy.cdm.model.name.TaxonName;
52
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
53
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
54
import eu.etaxonomy.cdm.model.term.DefinedTerm;
55

    
56
/**
57
 * @author a.mueller
58
 * @since 28.10.2008
59
 */
60
public class SpecimenTest {
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

    
73
    private static final Logger logger = Logger.getLogger(SpecimenTest.class);
74

    
75
	private DerivedUnit specimen;
76

    
77
	@Before
78
	public void setUp() throws Exception {
79
		specimen = DerivedUnit.NewPreservedSpecimenInstance();
80
	}
81

    
82
	@Test
83
	public void testNewInstance() {
84
		DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
85
		assertNotNull(specimen);
86
		assertTrue(specimen.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen));
87
	}
88

    
89
	/**
90
	 * Test method for {@link eu.etaxonomy.cdm.model.occurrence.Specimen#getPreservation()} and
91
	 * {@link eu.etaxonomy.cdm.model.occurrence.Specimen#setPreservation(eu.etaxonomy.cdm.model.occurrence.PreservationMethod)}.
92
	 */
93
	@Test
94
	public void testGetSetPreservation() {
95
		PreservationMethod preservation = PreservationMethod.NewInstance();
96
		specimen.setPreservation(preservation);
97
		assertSame(preservation, specimen.getPreservation());
98
		specimen.setPreservation(null);
99
	}
100

    
101
	@Test
102
	public void testBidirectionalSpecimenDescription(){
103
		Assert.assertNotNull("Specimen should exist", specimen);
104

    
105
		SpecimenDescription desc = SpecimenDescription.NewInstance(specimen);
106
		Assert.assertNotNull("Description should exist.", desc);
107
		Assert.assertSame("Descriptions specimen should be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
108
		Assert.assertTrue("Specimen should contain description", specimen.getDescriptions().contains(desc));
109

    
110
		SpecimenDescription desc2 = SpecimenDescription.NewInstance();
111
		Assert.assertNotNull("Description should exist.", desc2);
112
		specimen.addDescription(desc2);
113
		Assert.assertSame("Description2 specimen should be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
114
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
115
		Assert.assertTrue("Specimen should contain description2", specimen.getDescriptions().contains(desc2));
116
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
117

    
118
		SpecimenDescription desc3 = SpecimenDescription.NewInstance();
119
		Assert.assertNotNull("Description should exist.", desc3);
120
		desc3.setDescribedSpecimenOrObservation(specimen);
121
		Assert.assertSame("Description3 specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen);
122
		Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
123
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
124
		Assert.assertTrue("Specimen should contain description3", specimen.getDescriptions().contains(desc3));
125
		Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
126
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
127

    
128
		//change specimen of a given description
129
		DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
130
		Assert.assertNotNull("Specimen should exist.", specimen2);
131
		desc3.setDescribedSpecimenOrObservation(specimen2);
132
		Assert.assertSame("Description3 new specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
133
		Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
134
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
135
		Assert.assertTrue("Specimen2 should contain description3", specimen2.getDescriptions().contains(desc3));
136
		Assert.assertEquals("Specimen2 should contain exactly 1 description", 1, specimen2.getDescriptions().size());
137
		Assert.assertFalse("Specimen should no longer contain description3", specimen.getDescriptions().contains(desc3));
138
		Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
139
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
140

    
141
		//remove description which is not contained
142
		specimen.removeDescription(desc3);
143
		Assert.assertSame("Nothing should have changed", desc3.getDescribedSpecimenOrObservation(),specimen2);
144
		Assert.assertSame("Nothing should have changed", desc2.getDescribedSpecimenOrObservation(),specimen);
145
		Assert.assertSame("Nothing should have changed", desc.getDescribedSpecimenOrObservation(),specimen);
146
		Assert.assertTrue("Nothing should have changed", specimen2.getDescriptions().contains(desc3));
147
		Assert.assertEquals("Nothing should have changed", 1, specimen2.getDescriptions().size());
148
		Assert.assertFalse("Nothing should have changed", specimen.getDescriptions().contains(desc3));
149
		Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc2));
150
		Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc));
151

    
152
		//remove description
153
		specimen.removeDescription(desc2);
154
		Assert.assertNull("Descriptions2 specimen should not exist anymore", desc2.getDescribedSpecimenOrObservation());
155
		Assert.assertSame("Description3 specimen should still be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
156
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
157
		Assert.assertTrue("Specimen2 should still contain description3", specimen2.getDescriptions().contains(desc3));
158
		Assert.assertEquals("Specimen2 should still contain exactly 1 description", 1, specimen2.getDescriptions().size());
159
		Assert.assertFalse("Specimen should not contain description2 anymore", specimen.getDescriptions().contains(desc2));
160
		Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
161
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
162

    
163
		//remove description by setting null specimen
164
		desc3.setDescribedSpecimenOrObservation(null);
165
		Assert.assertNull("Description3 specimen should not exist anymore", desc3.getDescribedSpecimenOrObservation());
166
		Assert.assertNull("Descriptions2 specimen should still not exist anymore", desc2.getDescribedSpecimenOrObservation());
167
		Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
168
		Assert.assertFalse("Specimen2 should not contain description3 anymore", specimen2.getDescriptions().contains(desc3));
169
		Assert.assertEquals("Specimen2 should contain no description now", 0, specimen2.getDescriptions().size());
170
		Assert.assertFalse("Specimen should still no longer contain description2", specimen.getDescriptions().contains(desc2));
171
		Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
172
		Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
173
	}
174

    
175
	@Test
176
	public void testBidirectionalTypeDesignation(){
177
		SpecimenTypeDesignation desig1 = SpecimenTypeDesignation.NewInstance();
178
		SpecimenTypeDesignation desig2 = SpecimenTypeDesignation.NewInstance();
179
		DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
180

    
181
		specimen.addSpecimenTypeDesignation(desig1);
182
		Assert.assertEquals("Specimen1 should be the designations specimen", specimen, desig1.getTypeSpecimen());
183
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
184
		Assert.assertEquals("specimen1's designation should be desig1", desig1, specimen.getSpecimenTypeDesignations().iterator().next());
185

    
186
		specimen.addSpecimenTypeDesignation(desig2);
187
		Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
188
		Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
189

    
190
		specimen2.addSpecimenTypeDesignation(desig2);
191
		Assert.assertEquals("Specimen2 should have replaced specimen1 as desig2's specimen", specimen2, desig2.getTypeSpecimen());
192
		Assert.assertEquals("Specimen2 should have exactly 1 designation", 1, specimen2.getSpecimenTypeDesignations().size());
193
		Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen2.getSpecimenTypeDesignations().iterator().next());
194
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
195

    
196
		specimen2.removeSpecimenTypeDesignation(desig2);
197
		Assert.assertEquals("Desig2 should not have a specimen anymore", null, desig2.getTypeSpecimen());
198
		Assert.assertEquals("Specimen2 should have no designation", 0, specimen2.getSpecimenTypeDesignations().size());
199
		Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
200

    
201
		specimen.addSpecimenTypeDesignation(desig2);
202
		Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
203
		Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
204

    
205
		desig1.setTypeSpecimen(null);
206
		Assert.assertEquals("Desig1 should not have a specimen anymore", null, desig1.getTypeSpecimen());
207
		Assert.assertEquals("Specimen1 should have 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
208
		Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen.getSpecimenTypeDesignations().iterator().next());
209

    
210
		desig1.setTypeSpecimen(specimen);
211
		Assert.assertEquals("Desig1 should have specimen1 as specimen again", specimen, desig1.getTypeSpecimen());
212
		Assert.assertEquals("Specimen1 should have 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
213

    
214
	}
215

    
216
	@Test
217
	public void testClone() {
218
		logger.debug("Start testClone");
219

    
220
		//Null test is not full implemented, but an error is thrown if null throws
221
		//null pointer exception somewhere
222
		DerivedUnit specimenNullClone = specimen.clone();
223

    
224
		String accessionNumber = "accNumber";
225
		String catalogNumber = "catNumber";
226
		Collection collection = Collection.NewInstance();
227
		collection.setCode("code");
228
		DateTime created = new DateTime();
229
		Person createdBy = Person.NewTitledInstance("creator");
230
		DerivationEvent derivedFrom = DerivationEvent.NewInstance(null);
231
		int id = 22;
232
		String individualCount = "25";
233
		DefinedTerm lifeStage = DefinedTerm.NewStageInstance(null, null, null);
234
		LSID lsid = null;
235
		try {
236
			lsid = new LSID("urn:lsid:example.com:foo:1");
237
		} catch (MalformedLSIDException e) {
238
			// TODO Auto-generated catch block
239
			e.printStackTrace();
240
		}
241
//		DerivedUnit nextVersion = DerivedUnit.NewPreservedSpecimenInstance();
242
//		DerivedUnit previousVersion = DerivedUnit.NewPreservedSpecimenInstance();
243
		PreservationMethod preservation = PreservationMethod.NewInstance();
244
		boolean protectedTitleCache = true;
245
		DefinedTerm sex = DefinedTerm.SEX_FEMALE();
246
		TaxonName storedUnder = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
247
		String titleCache = "title";
248
		Calendar updated = Calendar.getInstance();
249
		Person updatedBy = Person.NewTitledInstance("updatedPerson");
250
		UUID uuid = UUID.randomUUID();
251

    
252
		Annotation annotation = Annotation.NewDefaultLanguageInstance("annotation");
253
		String definition = "definition";
254
		//TODO
255
		DerivationEvent derivationEvent = DerivationEvent.NewInstance(null);
256
		SpecimenDescription description = SpecimenDescription.NewInstance();
257
		DeterminationEvent determination = DeterminationEvent.NewInstance();
258
		Extension extension = Extension.NewInstance();
259
		extension.setValue("extension");
260
		Marker marker = Marker.NewInstance(MarkerType.COMPLETE(), false);
261
		Rights right = Rights.NewInstance("right", Language.DEFAULT());
262
//		Media media = Media.NewInstance();
263
		IdentifiableSource source = IdentifiableSource.NewDataImportInstance("12", "idNamespace");
264

    
265
		specimen.setAccessionNumber(accessionNumber);
266
		specimen.setCatalogNumber(catalogNumber);
267
		specimen.setCollection(collection);
268
		specimen.setCreated(created);
269
//		specimen.setCreatedBy(createdBy);
270
		specimen.setDerivedFrom(derivedFrom);
271
		specimen.setId(id);
272
		specimen.setIndividualCount(individualCount);
273
		specimen.setLifeStage(lifeStage);
274
		specimen.setLsid(lsid);
275
		specimen.setPreservation(preservation);
276
		specimen.setProtectedTitleCache(protectedTitleCache);
277
		specimen.setSex(sex);
278
		specimen.setStoredUnder(storedUnder);
279
		specimen.setTitleCache(titleCache, protectedTitleCache);
280
//		specimen.setUpdated(updated);
281
//		specimen.setUpdatedBy(updatedBy);
282
		specimen.setUuid(uuid);
283

    
284
		specimen.addAnnotation(annotation);
285
		specimen.putDefinition(Language.DEFAULT(), definition);
286
		specimen.addDerivationEvent(derivationEvent);
287
		specimen.addDescription(description);
288
		specimen.addDetermination(determination);
289
		specimen.addExtension(extension);
290
		specimen.addMarker(marker);
291
//		specimen.addMedia(media);    #3597
292
		specimen.addRights(right);
293
		specimen.addSource(source);
294

    
295
		try {
296
			Thread.sleep(200);
297
		} catch (InterruptedException e) {
298
			//ignore
299
		}
300
		DerivedUnit specimenClone = specimen.clone();
301

    
302
		assertFalse(id == specimenClone.getId());
303
		assertFalse(created.equals(specimenClone.getCreated()));
304
		assertFalse(createdBy.equals(specimenClone.getCreatedBy()));
305
		assertFalse(updated.equals(specimenClone.getUpdated()));
306
		assertFalse(updatedBy.equals(specimenClone.getUpdatedBy()));
307
		assertNull(specimenClone.getUpdatedBy());
308
		assertNull(specimenClone.getCreatedBy());
309
		assertFalse(uuid.equals(specimenClone.getUuid()));
310

    
311

    
312
		assertEquals(accessionNumber, specimenClone.getAccessionNumber());
313
		assertEquals(catalogNumber, specimenClone.getCatalogNumber());
314
		assertEquals(collection, specimenClone.getCollection());
315
		assertEquals(derivedFrom, specimenClone.getDerivedFrom());
316
		assertEquals(lifeStage, specimenClone.getLifeStage());
317
		assertEquals(lsid, specimenClone.getLsid());
318
		assertEquals(preservation, specimenClone.getPreservation());
319
		assertEquals(protectedTitleCache, specimenClone.isProtectedTitleCache());
320
		assertEquals(storedUnder, specimenClone.getStoredUnder());
321
		assertEquals(sex, specimenClone.getSex());
322
		assertEquals(titleCache, specimenClone.getTitleCache());
323

    
324
		Annotation clonedAnnotation = specimenClone.getAnnotations().iterator().next();
325
		assertFalse(annotation.equals(clonedAnnotation));
326
		assertEquals(annotation.getText(), ((LanguageStringBase)specimenClone.getAnnotations().iterator().next()).getText() );
327
		assertNotSame(annotation, specimenClone.getAnnotations().iterator().next() );
328

    
329
		assertEquals(definition, specimenClone.getDefinition().get(Language.DEFAULT()).getText());
330
//TODO
331
//		assertNotSame(definition, specimenClone.getDefinition().getText(Language.DEFAULT()));
332

    
333
		assertEquals(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
334
		assertSame(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
335

    
336
		// FIXME check if descriptions and determinations were correctly cloned
337
//		assertEquals(description, specimenClone.getDescriptions().iterator().next());
338
//		// TODO ?
339
//		assertSame(description, specimenClone.getDescriptions().iterator().next());
340
//
341
//		assertEquals(determination, specimenClone.getDeterminations().iterator().next());
342
//		// TODO ?
343
//		assertSame(determination, specimenClone.getDeterminations().iterator().next());
344

    
345
		assertFalse(extension.equals(specimenClone.getExtensions().iterator().next()));
346
		assertEquals(extension.getValue(), specimenClone.getExtensions().iterator().next().getValue());
347
		assertNotSame(extension, specimenClone.getExtensions().iterator().next());
348
		assertEquals(1, specimen.getExtensions().size());
349

    
350
		assertFalse(marker.equals(specimenClone.getMarkers().iterator().next()));
351
		assertEquals(marker.getFlag(), specimenClone.getMarkers().iterator().next().getFlag());
352
		assertNotSame(marker, specimenClone.getMarkers().iterator().next());
353
		assertEquals(1, specimenClone.getMarkers().size());
354

    
355
//		assertEquals(media, specimenClone.getMedia().iterator().next());  #3597
356
//		assertEquals(right, specimenClone.getRights().iterator().next()); #5762
357
		assertTrue("Rights must contain 1 rights object", specimenClone.getRights().size() == 1);
358
  	    assertTrue("Rights must not be cloned", specimenClone.getRights().iterator().next().equals(right));
359

    
360
		assertFalse(source.equals(specimenClone.getSources().iterator().next()));
361
		assertEquals(source.getId(), ((OriginalSourceBase<?>)specimenClone.getSources().iterator().next()).getId());
362
		assertNotSame(source, specimenClone.getSources().iterator().next());
363
		assertEquals(1, specimenClone.getSources().size());
364
	}
365

    
366
    @Test
367
    public void beanTests(){
368
//      #5307 Test that BeanUtils does not fail
369
        BeanUtils.getPropertyDescriptors(DerivedUnit.class);
370
        BeanUtils.getPropertyDescriptors(SpecimenOrObservationBase.class);
371
        BeanUtils.getPropertyDescriptors(FieldUnit.class);
372
        BeanUtils.getPropertyDescriptors(MediaSpecimen.class);
373
        BeanUtils.getPropertyDescriptors(DnaSample.class);
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
    }
467
}
    (1-1/1)