Project

General

Profile

Download (18.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.strategy.merge;
11

    
12
import java.util.HashSet;
13
import java.util.Set;
14
import java.util.UUID;
15

    
16
import org.apache.log4j.Logger;
17
import org.joda.time.DateTime;
18
import org.junit.AfterClass;
19
import org.junit.Assert;
20
import org.junit.Before;
21
import org.junit.BeforeClass;
22
import org.junit.Test;
23

    
24
import eu.etaxonomy.cdm.common.URI;
25
import eu.etaxonomy.cdm.model.agent.Address;
26
import eu.etaxonomy.cdm.model.agent.Contact;
27
import eu.etaxonomy.cdm.model.agent.Institution;
28
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
29
import eu.etaxonomy.cdm.model.agent.Person;
30
import eu.etaxonomy.cdm.model.agent.Team;
31
import eu.etaxonomy.cdm.model.common.Annotation;
32
import eu.etaxonomy.cdm.model.common.LSID;
33
import eu.etaxonomy.cdm.model.common.TimePeriod;
34
import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
35
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
36
import eu.etaxonomy.cdm.model.location.Country;
37
import eu.etaxonomy.cdm.model.location.Point;
38
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
39
import eu.etaxonomy.cdm.model.name.NameRelationship;
40
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
41
import eu.etaxonomy.cdm.model.name.Rank;
42
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
43
import eu.etaxonomy.cdm.model.name.TaxonName;
44
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
45
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
46
import eu.etaxonomy.cdm.model.reference.Reference;
47
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
48
import eu.etaxonomy.cdm.model.taxon.Taxon;
49
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
50
import eu.etaxonomy.cdm.model.term.DefaultTermInitializer;
51
import eu.etaxonomy.cdm.strategy.cache.reference.INomenclaturalReferenceCacheStrategy;
52

    
53
/**
54
 * @author a.mueller
55
 * @since 03.08.2009
56
 */
57
public class DefaultMergeStrategyTest {
58
	@SuppressWarnings("unused")
59
	private static final Logger logger = Logger.getLogger(DefaultMergeStrategyTest.class);
60

    
61
	private DefaultMergeStrategy bookMergeStrategy;
62
	private Reference book1;
63
	private String editionString1 ="Ed.1";
64
	private String volumeString1 ="Vol.1";
65
	private Team team1;
66
	private Reference printSeries1;
67
	private Annotation annotation1;
68
	private String title1 = "Title1";
69
	private VerbatimTimePeriod datePublished1 = VerbatimTimePeriod.NewVerbatimInstance(2000);
70
	private int hasProblem1 = 1;
71
	private LSID lsid1;
72

    
73
	private Reference book2;
74
	private String editionString2 ="Ed.2";
75
	private String volumeString2 ="Vol.2";
76
	private Team team2;
77
	private Reference printSeries2;
78
	private Annotation annotation2;
79
	private String annotationString2;
80
	private String title2 = "Title2";
81
	private DateTime created2 = new DateTime(1999, 3, 1, 0, 0, 0, 0);
82
	private VerbatimTimePeriod datePublished2 = VerbatimTimePeriod.NewVerbatimInstance(2002);
83
	private int hasProblem2 = 1;
84
	private LSID lsid2;
85

    
86
	@BeforeClass
87
	public static void setUpBeforeClass() throws Exception {
88
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
89
		termInitializer.initialize();
90
	}
91

    
92
	@AfterClass
93
	public static void tearDownAfterClass() throws Exception {
94
	}
95

    
96
	@Before
97
	public void setUp() throws Exception {
98
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
99
		team1 = Team.NewInstance();
100
		team1.setTitleCache("Team1", true);
101
		team2 = Team.NewInstance();
102
		team2.setTitleCache("Team2", true);
103
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
104
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
105
		annotation1 = Annotation.NewInstance("Annotation1", null);
106
		annotationString2 = "Annotation2";
107
		annotation2 = Annotation.NewInstance(annotationString2, null);
108

    
109
		book1 = ReferenceFactory.newBook();
110
		book1.setAuthorship(team1);
111
		book1.setTitle(title1);
112
		book1.setEdition(editionString1);
113
		book1.setVolume(volumeString1);
114
		book1.setInReference(printSeries1);
115
		book1.addAnnotation(annotation1);
116
		book1.setDatePublished(datePublished1);
117
		book1.setParsingProblem(hasProblem1);
118
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
119
		book1.setLsid(lsid1);
120
		book1.setNomenclaturallyRelevant(false);
121

    
122
		book2 = ReferenceFactory.newBook();
123
		book2.setAuthorship(team2);
124
		book2.setTitle(title2);
125
		book2.setEdition(editionString2);
126
		book2.setVolume(volumeString2);
127
		book2.setInReference(printSeries2);
128
		book2.addAnnotation(annotation2);
129
		book2.setCreated(created2);
130
		book2.setDatePublished(datePublished2);
131
		book2.setParsingProblem(hasProblem2);
132
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
133
		book2.setLsid(lsid2);
134
		book2.setNomenclaturallyRelevant(true);
135
	}
136

    
137
//********************* TEST *********************************************/
138

    
139
	/**
140
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#NewInstance(java.lang.Class)}.
141
	 */
142
	@Test
143
	public void testNewInstance() {
144
		Assert.assertNotNull(bookMergeStrategy);
145
		Assert.assertEquals(Reference.class, bookMergeStrategy.getMergeClass());
146
	}
147

    
148
	/**
149
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
150
	 */
151
	@Test
152
	public void testGetMergeMode() {
153
		Assert.assertEquals("Merge mode for title should be MergeMode.FIRST", MergeMode.FIRST, bookMergeStrategy.getMergeMode("title"));
154
	}
155

    
156
	/**
157
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
158
	 */
159
	@Test
160
	public void testGetSetMergeMode() {
161
		//legal value
162
		try {
163
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
164
			Assert.assertEquals("Merge mode for edition should be", MergeMode.SECOND, bookMergeStrategy.getMergeMode("edition"));
165
		} catch (MergeException e1) {
166
			Assert.fail();
167
		}
168
		//illegalValue
169
		try {
170
			bookMergeStrategy.setMergeMode("xxx", MergeMode.SECOND);
171
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
172
		} catch (Exception e) {
173
			//ok
174
		}
175
		//illegalValue
176
		try {
177
			bookMergeStrategy.setMergeMode("cacheStrategy", MergeMode.SECOND);
178
			Assert.fail("CacheStrategy is transient and therefore not a legal merge parameter");
179
		} catch (Exception e) {
180
			//ok
181
		}
182
		//illegalValue
183
		try {
184
			bookMergeStrategy.setMergeMode("id", MergeMode.SECOND);
185
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
186
		} catch (Exception e) {
187
			//ok
188
		}
189
		//illegalValue
190
		try {
191
			bookMergeStrategy.setMergeMode("uuid", MergeMode.SECOND);
192
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
193
		} catch (Exception e) {
194
			//ok
195
		}
196

    
197

    
198
	}
199

    
200

    
201
	/**
202
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
203
	 * @throws MergeException
204
	 */
205
	@Test
206
	public void testInvokeReferences() throws MergeException {
207
		INomenclaturalReferenceCacheStrategy cacheStrategy1 = book1.getCacheStrategy();
208
		int id = book1.getId();
209
		UUID uuid = book1.getUuid();
210
		try {
211
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
212
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
213
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
214
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
215
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
216
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
217
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
218
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
219
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
220

    
221
			bookMergeStrategy.invoke(book1, book2);
222
		} catch (MergeException e) {
223
			throw e;
224
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
225
		}
226
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
227
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
228
		Assert.assertNull("Volume should be null", book1.getVolume());
229

    
230
		//Boolean
231
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
232
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
233

    
234

    
235
		//CdmBase
236
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
237
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
238

    
239
		//Transient
240
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.getCacheStrategy());
241

    
242

    
243
		//UserType
244
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
245
		//TODO updated should have the actual date if any value has changed
246
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
247
		Assert.assertEquals("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
248
		//TODO this may not be correct
249
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
250

    
251

    
252
		//TODO
253
		//	book1.setProblemEnds(end);
254
		//	book1.setProtectedTitleCache(protectedTitleCache);
255

    
256
		//annotations -> ADD_CLONE
257
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
258
		boolean cloneExists = false;
259
		for (Annotation annotation : book1.getAnnotations()){
260
			if (annotation == this.annotation2){
261
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the
262
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
263
				//This test can be changed once this bug does not exist anymore
264
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
265
			}else if (annotationString2.equals(annotation.getText())){
266
				cloneExists = true;
267
			}
268
		}
269
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
270
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
271

    
272
		//identifier
273
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
274
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
275

    
276
		//Test Thesis
277
		Institution school1 = Institution.NewInstance();
278
		Institution school2 = Institution.NewInstance();
279

    
280
		Reference thesis1 = ReferenceFactory.newThesis();
281
		thesis1.setSchool(school1);
282
		//Thesis thesis1 = Thesis.NewInstance(school1);
283
		Reference thesis2 = ReferenceFactory.newThesis();
284
		thesis2.setSchool(school2);
285
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
286

    
287
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
288
		thesisStrategy.invoke(thesis1, thesis2);
289
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());
290
	}
291

    
292

    
293
	/**
294
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
295
	 * @throws MergeException
296
	 */
297
	@Test
298
	//@Ignore
299
	public void testInvokeTxonNames() throws MergeException {
300
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
301
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
302
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
303
		TaxonName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
304

    
305
		botName1.setGenusOrUninomial("Genus1");
306
		botName1.setSpecificEpithet("species1");
307
		botName1.setAnamorphic(true);
308

    
309
		botName2.setGenusOrUninomial("Genus2");
310
		botName2.setSpecificEpithet("species2");
311
		botName2.setAnamorphic(false);
312

    
313
		//name relations
314
		botName2.addBasionym(botName3, book1, "p.22", null, null);
315
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
316
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
317

    
318
		//descriptions
319
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
320
		botName1.addDescription(description1);
321
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
322
		botName1.addDescription(description2);
323

    
324
		//authors
325
		Team team1 = Team.NewInstance();
326
		Team team2 = Team.NewInstance();
327
		Person person1 = Person.NewInstance();
328
		botName1.setCombinationAuthorship(team1);
329
		botName2.setCombinationAuthorship(team2);
330

    
331
		//taxa
332
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
333
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
334

    
335
		try {
336
			botNameMergeStrategy.setMergeMode("combinationAuthorship", MergeMode.SECOND);
337
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
338

    
339
//			botNameMergeStrategy.invoke(botName1, botName2);
340
		} catch (MergeException e) {
341
			throw e;
342
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
343
		}
344

    
345
		//Boolean
346
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
347

    
348
		//NameRelations
349
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
350
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
351
		for (NameRelationship toRelation : toRelations){
352
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
353
				basionymRelations.add(toRelation);
354
			}
355
		}
356
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
357
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
358
		//TODO merge relation if matches() = true
359

    
360
		//Types
361
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
362
		//TODO add to all names etc.
363

    
364
		//Description
365
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
366

    
367
		//Authorships
368
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorship());
369

    
370
		//Taxa
371
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
372
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
373

    
374
	}
375

    
376
	/**
377
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
378
	 * @throws MergeException
379
	 */
380
	@Test
381
	public void testInvokeAgents() throws MergeException {
382
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
383

    
384
		Team team1 = Team.NewInstance();
385
		Team team2 = Team.NewInstance();
386
		Team team3 = Team.NewInstance();
387

    
388
		Person person1 = Person.NewTitledInstance("person1");
389
		Person person2 = Person.NewTitledInstance("person2");
390
		Person person3 = Person.NewTitledInstance("person3");
391

    
392
		team1.setTitleCache("Team1", true);
393
		team1.setNomenclaturalTitle("T.1");
394
		String street1 = "Strasse1";
395
		team1.setContact(Contact.NewInstance(street1, "12345", "Berlin", Country.ARGENTINAARGENTINEREPUBLIC(),"pobox" , "Region", "a@b.de", "f12345", "+49-30-123456", URI.create("www.abc.de"), Point.NewInstance(2.4, 3.2, ReferenceSystem.WGS84(), 3)));
396
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
397
		String street3 = "Street3";
398
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
399
		String emailAddress1 = "Email1";
400
		team1.addEmailAddress(emailAddress1);
401

    
402
		team2.addTeamMember(person1);
403
		team2.addTeamMember(person2);
404
		String emailAddress2 = "Email2";
405
		team2.addEmailAddress(emailAddress2);
406

    
407
		team3.addTeamMember(person3);
408
		team3.addEmailAddress("emailAddress3");
409

    
410
		teamMergeStrategy.invoke(team2, team3);
411

    
412
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
413
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
414
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
415

    
416
		//Contact
417
		teamMergeStrategy.invoke(team2, team1);
418
		Contact team2Contact = team2.getContact();
419
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
420
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
421
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
422
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
423

    
424
		boolean street1Exists = false;
425
		boolean street3Exists = false;
426
		boolean country1Exists = false;
427
		for  (Address address : team2Contact.getAddresses()){
428
			if (street1.equals(address.getStreet())){
429
				street1Exists = true;
430
			}
431
			if (street3.equals(address.getStreet())){
432
				street3Exists = true;
433
			}
434
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
435
				country1Exists = true;
436
			}
437
		}
438
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
439
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
440
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
441

    
442
		//Person
443
		Institution institution1 = Institution.NewInstance();
444
		institution1.setTitleCache("inst1", true);
445
		Institution institution2 = Institution.NewInstance();
446
		institution2.setTitleCache("inst2", true);
447

    
448
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
449
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
450

    
451
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
452
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
453

    
454
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
455
		personMergeStrategy.invoke(person1, person2);
456

    
457
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
458
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
459
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
460
		}
461

    
462
	}
463

    
464
}
    (1-1/1)