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.UUID;
|
22
|
|
23
|
import org.apache.log4j.Logger;
|
24
|
import org.joda.time.DateTime;
|
25
|
import org.junit.Assert;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.Test;
|
28
|
import org.springframework.beans.BeanUtils;
|
29
|
|
30
|
import com.ibm.lsid.MalformedLSIDException;
|
31
|
|
32
|
import eu.etaxonomy.cdm.model.agent.Person;
|
33
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
34
|
import eu.etaxonomy.cdm.model.common.Extension;
|
35
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
36
|
import eu.etaxonomy.cdm.model.common.LSID;
|
37
|
import eu.etaxonomy.cdm.model.common.Language;
|
38
|
import eu.etaxonomy.cdm.model.common.LanguageStringBase;
|
39
|
import eu.etaxonomy.cdm.model.common.Marker;
|
40
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
41
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
42
|
import eu.etaxonomy.cdm.model.media.Rights;
|
43
|
import eu.etaxonomy.cdm.model.molecular.DnaSample;
|
44
|
import eu.etaxonomy.cdm.model.name.Rank;
|
45
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
46
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
47
|
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
|
48
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
|
49
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
50
|
|
51
|
/**
|
52
|
* @author a.mueller
|
53
|
* @since 28.10.2008
|
54
|
*/
|
55
|
public class SpecimenTest {
|
56
|
private static final Logger logger = Logger.getLogger(SpecimenTest.class);
|
57
|
|
58
|
private DerivedUnit specimen;
|
59
|
|
60
|
@Before
|
61
|
public void setUp() throws Exception {
|
62
|
specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
63
|
}
|
64
|
|
65
|
/**
|
66
|
* Test method for {@link eu.etaxonomy.cdm.model.occurrence.Specimen#NewInstance()}.
|
67
|
*/
|
68
|
@Test
|
69
|
public void testNewInstance() {
|
70
|
DerivedUnit specimen = DerivedUnit.NewPreservedSpecimenInstance();
|
71
|
assertNotNull(specimen);
|
72
|
assertTrue(specimen.getRecordBasis().equals(SpecimenOrObservationType.PreservedSpecimen));
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Test method for {@link eu.etaxonomy.cdm.model.occurrence.Specimen#getPreservation()} and
|
77
|
* {@link eu.etaxonomy.cdm.model.occurrence.Specimen#setPreservation(eu.etaxonomy.cdm.model.occurrence.PreservationMethod)}.
|
78
|
*/
|
79
|
@Test
|
80
|
public void testGetSetPreservation() {
|
81
|
PreservationMethod preservation = PreservationMethod.NewInstance();
|
82
|
specimen.setPreservation(preservation);
|
83
|
assertSame(preservation, specimen.getPreservation());
|
84
|
specimen.setPreservation(null);
|
85
|
}
|
86
|
|
87
|
@Test
|
88
|
public void testBidirectionalSpecimenDescription(){
|
89
|
Assert.assertNotNull("Specimen should exist", specimen);
|
90
|
|
91
|
SpecimenDescription desc = SpecimenDescription.NewInstance(specimen);
|
92
|
Assert.assertNotNull("Description should exist.", desc);
|
93
|
Assert.assertSame("Descriptions specimen should be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
94
|
Assert.assertTrue("Specimen should contain description", specimen.getDescriptions().contains(desc));
|
95
|
|
96
|
SpecimenDescription desc2 = SpecimenDescription.NewInstance();
|
97
|
Assert.assertNotNull("Description should exist.", desc2);
|
98
|
specimen.addDescription(desc2);
|
99
|
Assert.assertSame("Description2 specimen should be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
|
100
|
Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
101
|
Assert.assertTrue("Specimen should contain description2", specimen.getDescriptions().contains(desc2));
|
102
|
Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
|
103
|
|
104
|
SpecimenDescription desc3 = SpecimenDescription.NewInstance();
|
105
|
Assert.assertNotNull("Description should exist.", desc3);
|
106
|
desc3.setDescribedSpecimenOrObservation(specimen);
|
107
|
Assert.assertSame("Description3 specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen);
|
108
|
Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
|
109
|
Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
110
|
Assert.assertTrue("Specimen should contain description3", specimen.getDescriptions().contains(desc3));
|
111
|
Assert.assertTrue("Specimen should still contain description2", specimen.getDescriptions().contains(desc2));
|
112
|
Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
|
113
|
|
114
|
|
115
|
//change specimen of a given description
|
116
|
DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
|
117
|
Assert.assertNotNull("Specimen should exist.", specimen2);
|
118
|
desc3.setDescribedSpecimenOrObservation(specimen2);
|
119
|
Assert.assertSame("Description3 new specimen should be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
|
120
|
Assert.assertSame("Descriptions2 specimen should still be set correctly", desc2.getDescribedSpecimenOrObservation(),specimen);
|
121
|
Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
122
|
Assert.assertTrue("Specimen2 should contain description3", specimen2.getDescriptions().contains(desc3));
|
123
|
Assert.assertEquals("Specimen2 should contain exactly 1 description", 1, specimen2.getDescriptions().size());
|
124
|
Assert.assertFalse("Specimen should no longer 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
|
//remove description which is not contained
|
129
|
specimen.removeDescription(desc3);
|
130
|
Assert.assertSame("Nothing should have changed", desc3.getDescribedSpecimenOrObservation(),specimen2);
|
131
|
Assert.assertSame("Nothing should have changed", desc2.getDescribedSpecimenOrObservation(),specimen);
|
132
|
Assert.assertSame("Nothing should have changed", desc.getDescribedSpecimenOrObservation(),specimen);
|
133
|
Assert.assertTrue("Nothing should have changed", specimen2.getDescriptions().contains(desc3));
|
134
|
Assert.assertEquals("Nothing should have changed", 1, specimen2.getDescriptions().size());
|
135
|
Assert.assertFalse("Nothing should have changed", specimen.getDescriptions().contains(desc3));
|
136
|
Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc2));
|
137
|
Assert.assertTrue("Nothing should have changed", specimen.getDescriptions().contains(desc));
|
138
|
|
139
|
//remove description
|
140
|
specimen.removeDescription(desc2);
|
141
|
Assert.assertNull("Descriptions2 specimen should not exist anymore", desc2.getDescribedSpecimenOrObservation());
|
142
|
Assert.assertSame("Description3 specimen should still be set correctly", desc3.getDescribedSpecimenOrObservation(),specimen2);
|
143
|
Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
144
|
Assert.assertTrue("Specimen2 should still contain description3", specimen2.getDescriptions().contains(desc3));
|
145
|
Assert.assertEquals("Specimen2 should still contain exactly 1 description", 1, specimen2.getDescriptions().size());
|
146
|
Assert.assertFalse("Specimen should not contain description2 anymore", specimen.getDescriptions().contains(desc2));
|
147
|
Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
|
148
|
Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
|
149
|
|
150
|
//remove description by setting null specimen
|
151
|
desc3.setDescribedSpecimenOrObservation(null);
|
152
|
Assert.assertNull("Description3 specimen should not exist anymore", desc3.getDescribedSpecimenOrObservation());
|
153
|
Assert.assertNull("Descriptions2 specimen should still not exist anymore", desc2.getDescribedSpecimenOrObservation());
|
154
|
Assert.assertSame("Descriptions specimen should still be set correctly", desc.getDescribedSpecimenOrObservation(),specimen);
|
155
|
Assert.assertFalse("Specimen2 should not contain description3 anymore", specimen2.getDescriptions().contains(desc3));
|
156
|
Assert.assertEquals("Specimen2 should contain no description now", 0, specimen2.getDescriptions().size());
|
157
|
Assert.assertFalse("Specimen should still no longer contain description2", specimen.getDescriptions().contains(desc2));
|
158
|
Assert.assertFalse("Specimen should still no longer contain description3", specimen.getDescriptions().contains(desc3));
|
159
|
Assert.assertTrue("Specimen should still contain description", specimen.getDescriptions().contains(desc));
|
160
|
|
161
|
|
162
|
}
|
163
|
|
164
|
|
165
|
@Test
|
166
|
public void testBidirectionalTypeDesignation(){
|
167
|
SpecimenTypeDesignation desig1 = SpecimenTypeDesignation.NewInstance();
|
168
|
SpecimenTypeDesignation desig2 = SpecimenTypeDesignation.NewInstance();
|
169
|
DerivedUnit specimen2 = DerivedUnit.NewPreservedSpecimenInstance();
|
170
|
|
171
|
specimen.addSpecimenTypeDesignation(desig1);
|
172
|
Assert.assertEquals("Specimen1 should be the designations specimen", specimen, desig1.getTypeSpecimen());
|
173
|
Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
|
174
|
Assert.assertEquals("specimen1's designation should be desig1", desig1, specimen.getSpecimenTypeDesignations().iterator().next());
|
175
|
|
176
|
specimen.addSpecimenTypeDesignation(desig2);
|
177
|
Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
|
178
|
Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
|
179
|
|
180
|
specimen2.addSpecimenTypeDesignation(desig2);
|
181
|
Assert.assertEquals("Specimen2 should have replaced specimen1 as desig2's specimen", specimen2, desig2.getTypeSpecimen());
|
182
|
Assert.assertEquals("Specimen2 should have exactly 1 designation", 1, specimen2.getSpecimenTypeDesignations().size());
|
183
|
Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen2.getSpecimenTypeDesignations().iterator().next());
|
184
|
Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
|
185
|
|
186
|
specimen2.removeSpecimenTypeDesignation(desig2);
|
187
|
Assert.assertEquals("Desig2 should not have a specimen anymore", null, desig2.getTypeSpecimen());
|
188
|
Assert.assertEquals("Specimen2 should have no designation", 0, specimen2.getSpecimenTypeDesignations().size());
|
189
|
Assert.assertEquals("specimen1 should have exactly 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
|
190
|
|
191
|
specimen.addSpecimenTypeDesignation(desig2);
|
192
|
Assert.assertEquals("Specimen1 should be the desig2's specimen", specimen, desig2.getTypeSpecimen());
|
193
|
Assert.assertEquals("specimen1 should have exactly 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
|
194
|
|
195
|
desig1.setTypeSpecimen(null);
|
196
|
Assert.assertEquals("Desig1 should not have a specimen anymore", null, desig1.getTypeSpecimen());
|
197
|
Assert.assertEquals("Specimen1 should have 1 designation", 1, specimen.getSpecimenTypeDesignations().size());
|
198
|
Assert.assertEquals("Specimen1's designation should be desig2", desig2, specimen.getSpecimenTypeDesignations().iterator().next());
|
199
|
|
200
|
desig1.setTypeSpecimen(specimen);
|
201
|
Assert.assertEquals("Desig1 should have specimen1 as specimen again", specimen, desig1.getTypeSpecimen());
|
202
|
Assert.assertEquals("Specimen1 should have 2 designation", 2, specimen.getSpecimenTypeDesignations().size());
|
203
|
|
204
|
}
|
205
|
|
206
|
@Test
|
207
|
public void testClone() {
|
208
|
logger.debug("Start testClone");
|
209
|
|
210
|
//Null test is not full implemented, but an error is thrown if null throws
|
211
|
//null pointer exception somewhere
|
212
|
DerivedUnit specimenNullClone = (DerivedUnit)specimen.clone();
|
213
|
|
214
|
String accessionNumber = "accNumber";
|
215
|
String catalogNumber = "catNumber";
|
216
|
Collection collection = Collection.NewInstance();
|
217
|
collection.setCode("code");
|
218
|
DateTime created = new DateTime();
|
219
|
Person createdBy = Person.NewTitledInstance("creator");
|
220
|
DerivationEvent derivedFrom = DerivationEvent.NewInstance(null);
|
221
|
int id = 22;
|
222
|
String individualCount = "25";
|
223
|
DefinedTerm lifeStage = DefinedTerm.NewStageInstance(null, null, null);
|
224
|
LSID lsid = null;
|
225
|
try {
|
226
|
lsid = new LSID("urn:lsid:example.com:foo:1");
|
227
|
} catch (MalformedLSIDException e) {
|
228
|
// TODO Auto-generated catch block
|
229
|
e.printStackTrace();
|
230
|
}
|
231
|
// DerivedUnit nextVersion = DerivedUnit.NewPreservedSpecimenInstance();
|
232
|
// DerivedUnit previousVersion = DerivedUnit.NewPreservedSpecimenInstance();
|
233
|
PreservationMethod preservation = PreservationMethod.NewInstance();
|
234
|
boolean protectedTitleCache = true;
|
235
|
DefinedTerm sex = DefinedTerm.SEX_FEMALE();
|
236
|
TaxonName storedUnder = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
|
237
|
String titleCache = "title";
|
238
|
Calendar updated = Calendar.getInstance();
|
239
|
Person updatedBy = Person.NewTitledInstance("updatedPerson");
|
240
|
UUID uuid = UUID.randomUUID();
|
241
|
|
242
|
Annotation annotation = Annotation.NewDefaultLanguageInstance("annotation");
|
243
|
String definition = "definition";
|
244
|
//TODO
|
245
|
DerivationEvent derivationEvent = DerivationEvent.NewInstance(null);
|
246
|
SpecimenDescription description = SpecimenDescription.NewInstance();
|
247
|
DeterminationEvent determination = DeterminationEvent.NewInstance();
|
248
|
Extension extension = Extension.NewInstance();
|
249
|
extension.setValue("extension");
|
250
|
Marker marker = Marker.NewInstance(MarkerType.COMPLETE(), false);
|
251
|
Rights right = Rights.NewInstance("right", Language.DEFAULT());
|
252
|
// Media media = Media.NewInstance();
|
253
|
IdentifiableSource source = IdentifiableSource.NewDataImportInstance("12", "idNamespace");
|
254
|
|
255
|
specimen.setAccessionNumber(accessionNumber);
|
256
|
specimen.setCatalogNumber(catalogNumber);
|
257
|
specimen.setCollection(collection);
|
258
|
specimen.setCreated(created);
|
259
|
// specimen.setCreatedBy(createdBy);
|
260
|
specimen.setDerivedFrom(derivedFrom);
|
261
|
specimen.setId(id);
|
262
|
specimen.setIndividualCount(individualCount);
|
263
|
specimen.setLifeStage(lifeStage);
|
264
|
specimen.setLsid(lsid);
|
265
|
specimen.setPreservation(preservation);
|
266
|
specimen.setProtectedTitleCache(protectedTitleCache);
|
267
|
specimen.setSex(sex);
|
268
|
specimen.setStoredUnder(storedUnder);
|
269
|
specimen.setTitleCache(titleCache, protectedTitleCache);
|
270
|
// specimen.setUpdated(updated);
|
271
|
// specimen.setUpdatedBy(updatedBy);
|
272
|
specimen.setUuid(uuid);
|
273
|
|
274
|
specimen.addAnnotation(annotation);
|
275
|
specimen.putDefinition(Language.DEFAULT(), definition);
|
276
|
specimen.addDerivationEvent(derivationEvent);
|
277
|
specimen.addDescription(description);
|
278
|
specimen.addDetermination(determination);
|
279
|
specimen.addExtension(extension);
|
280
|
specimen.addMarker(marker);
|
281
|
// specimen.addMedia(media); #3597
|
282
|
specimen.addRights(right);
|
283
|
specimen.addSource(source);
|
284
|
|
285
|
try {
|
286
|
Thread.sleep(200);
|
287
|
} catch (InterruptedException e) {
|
288
|
//ignore
|
289
|
}
|
290
|
DerivedUnit specimenClone = (DerivedUnit)specimen.clone();
|
291
|
|
292
|
assertFalse(id == specimenClone.getId());
|
293
|
assertFalse(created.equals(specimenClone.getCreated()));
|
294
|
assertFalse(createdBy.equals(specimenClone.getCreatedBy()));
|
295
|
assertFalse(updated.equals(specimenClone.getUpdated()));
|
296
|
assertFalse(updatedBy.equals(specimenClone.getUpdatedBy()));
|
297
|
assertNull(specimenClone.getUpdatedBy());
|
298
|
assertNull(specimenClone.getCreatedBy());
|
299
|
assertFalse(uuid.equals(specimenClone.getUuid()));
|
300
|
|
301
|
|
302
|
assertEquals(accessionNumber, specimenClone.getAccessionNumber());
|
303
|
assertEquals(catalogNumber, specimenClone.getCatalogNumber());
|
304
|
assertEquals(collection, specimenClone.getCollection());
|
305
|
assertEquals(derivedFrom, specimenClone.getDerivedFrom());
|
306
|
assertEquals(lifeStage, specimenClone.getLifeStage());
|
307
|
assertEquals(lsid, specimenClone.getLsid());
|
308
|
assertEquals(preservation, specimenClone.getPreservation());
|
309
|
assertEquals(protectedTitleCache, specimenClone.isProtectedTitleCache());
|
310
|
assertEquals(storedUnder, specimenClone.getStoredUnder());
|
311
|
assertEquals(sex, specimenClone.getSex());
|
312
|
assertEquals(titleCache, specimenClone.getTitleCache());
|
313
|
|
314
|
Annotation clonedAnnotation = specimenClone.getAnnotations().iterator().next();
|
315
|
assertFalse(annotation.equals(clonedAnnotation));
|
316
|
assertEquals(annotation.getText(), ((LanguageStringBase)specimenClone.getAnnotations().iterator().next()).getText() );
|
317
|
assertNotSame(annotation, specimenClone.getAnnotations().iterator().next() );
|
318
|
|
319
|
assertEquals(definition, specimenClone.getDefinition().get(Language.DEFAULT()).getText());
|
320
|
//TODO
|
321
|
// assertNotSame(definition, specimenClone.getDefinition().getText(Language.DEFAULT()));
|
322
|
|
323
|
assertEquals(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
|
324
|
assertSame(derivationEvent, specimenClone.getDerivationEvents().iterator().next());
|
325
|
|
326
|
assertEquals(description, specimenClone.getDescriptions().iterator().next());
|
327
|
// TODO ?
|
328
|
assertSame(description, specimenClone.getDescriptions().iterator().next());
|
329
|
|
330
|
assertEquals(determination, specimenClone.getDeterminations().iterator().next());
|
331
|
// TODO ?
|
332
|
assertSame(determination, specimenClone.getDeterminations().iterator().next());
|
333
|
|
334
|
assertFalse(extension.equals(specimenClone.getExtensions().iterator().next()));
|
335
|
assertEquals(extension.getValue(), specimenClone.getExtensions().iterator().next().getValue());
|
336
|
assertNotSame(extension, specimenClone.getExtensions().iterator().next());
|
337
|
assertEquals(1, specimen.getExtensions().size());
|
338
|
|
339
|
assertFalse(marker.equals(specimenClone.getMarkers().iterator().next()));
|
340
|
assertEquals(marker.getFlag(), specimenClone.getMarkers().iterator().next().getFlag());
|
341
|
assertNotSame(marker, specimenClone.getMarkers().iterator().next());
|
342
|
assertEquals(1, specimenClone.getMarkers().size());
|
343
|
|
344
|
// assertEquals(media, specimenClone.getMedia().iterator().next()); #3597
|
345
|
// assertEquals(right, specimenClone.getRights().iterator().next()); #5762
|
346
|
assertTrue("Rights must contain 1 rights object", specimenClone.getRights().size() == 1);
|
347
|
assertTrue("Rights must not be cloned", specimenClone.getRights().iterator().next().equals(right));
|
348
|
|
349
|
assertFalse(source.equals(specimenClone.getSources().iterator().next()));
|
350
|
assertEquals(source.getId(), ((OriginalSourceBase<?>)specimenClone.getSources().iterator().next()).getId());
|
351
|
assertNotSame(source, specimenClone.getSources().iterator().next());
|
352
|
assertEquals(1, specimenClone.getSources().size());
|
353
|
}
|
354
|
|
355
|
|
356
|
@Test
|
357
|
public void beanTests(){
|
358
|
// #5307 Test that BeanUtils does not fail
|
359
|
BeanUtils.getPropertyDescriptors(DerivedUnit.class);
|
360
|
BeanUtils.getPropertyDescriptors(SpecimenOrObservationBase.class);
|
361
|
BeanUtils.getPropertyDescriptors(FieldUnit.class);
|
362
|
BeanUtils.getPropertyDescriptors(MediaSpecimen.class);
|
363
|
BeanUtils.getPropertyDescriptors(DnaSample.class);
|
364
|
}
|
365
|
}
|