Project

General

Profile

Download (18.3 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.net.URI;
13
import java.util.HashSet;
14
import java.util.Set;
15
import java.util.UUID;
16

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

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

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

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

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

    
87

    
88
	private Reference book3;
89

    
90
	/**
91
	 * @throws java.lang.Exception
92
	 */
93
	@BeforeClass
94
	public static void setUpBeforeClass() throws Exception {
95
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
96
		termInitializer.initialize();
97
	}
98

    
99
	/**
100
	 * @throws java.lang.Exception
101
	 */
102
	@AfterClass
103
	public static void tearDownAfterClass() throws Exception {
104
	}
105

    
106
	/**
107
	 * @throws java.lang.Exception
108
	 */
109
	@Before
110
	public void setUp() throws Exception {
111
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
112
		team1 = Team.NewInstance();
113
		team1.setTitleCache("Team1", true);
114
		team2 = Team.NewInstance();
115
		team2.setTitleCache("Team2", true);
116
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
117
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
118
		annotation1 = Annotation.NewInstance("Annotation1", null);
119
		annotationString2 = "Annotation2";
120
		annotation2 = Annotation.NewInstance(annotationString2, null);
121

    
122
		book1 = ReferenceFactory.newBook();
123
		book1.setAuthorship(team1);
124
		book1.setTitle(title1);
125
		book1.setEdition(editionString1);
126
		book1.setVolume(volumeString1);
127
		book1.setInReference(printSeries1);
128
		book1.addAnnotation(annotation1);
129
		book1.setDatePublished(datePublished1);
130
		book1.setParsingProblem(hasProblem1);
131
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
132
		book1.setLsid(lsid1);
133
		book1.setNomenclaturallyRelevant(false);
134

    
135
		book2 = ReferenceFactory.newBook();
136
		book2.setAuthorship(team2);
137
		book2.setTitle(title2);
138
		book2.setEdition(editionString2);
139
		book2.setVolume(volumeString2);
140
		book2.setInReference(printSeries2);
141
		book2.addAnnotation(annotation2);
142
		book2.setCreated(created2);
143
		book2.setDatePublished(datePublished2);
144
		book2.setParsingProblem(hasProblem2);
145
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
146
		book2.setLsid(lsid2);
147
		book2.setNomenclaturallyRelevant(true);
148

    
149
	}
150

    
151
	/**
152
	 * @throws java.lang.Exception
153
	 */
154
	@After
155
	public void tearDown() throws Exception {
156
	}
157

    
158
//********************* TEST *********************************************/
159

    
160
	/**
161
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#NewInstance(java.lang.Class)}.
162
	 */
163
	@Test
164
	public void testNewInstance() {
165
		Assert.assertNotNull(bookMergeStrategy);
166
		Assert.assertEquals(Reference.class, bookMergeStrategy.getMergeClass());
167
	}
168

    
169
	/**
170
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
171
	 */
172
	@Test
173
	public void testGetMergeMode() {
174
		Assert.assertEquals("Merge mode for title should be MergeMode.FIRST", MergeMode.FIRST, bookMergeStrategy.getMergeMode("title"));
175
	}
176

    
177
	/**
178
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#getMergeMode(java.lang.String)}.
179
	 */
180
	@Test
181
	public void testGetSetMergeMode() {
182
		//legal value
183
		try {
184
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
185
			Assert.assertEquals("Merge mode for edition should be", MergeMode.SECOND, bookMergeStrategy.getMergeMode("edition"));
186
		} catch (MergeException e1) {
187
			Assert.fail();
188
		}
189
		//illegalValue
190
		try {
191
			bookMergeStrategy.setMergeMode("xxx", MergeMode.SECOND);
192
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
193
		} catch (Exception e) {
194
			//ok
195
		}
196
		//illegalValue
197
		try {
198
			bookMergeStrategy.setMergeMode("cacheStrategy", MergeMode.SECOND);
199
			Assert.fail("CacheStrategy is transient and therefore not a legal merge parameter");
200
		} catch (Exception e) {
201
			//ok
202
		}
203
		//illegalValue
204
		try {
205
			bookMergeStrategy.setMergeMode("id", MergeMode.SECOND);
206
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
207
		} catch (Exception e) {
208
			//ok
209
		}
210
		//illegalValue
211
		try {
212
			bookMergeStrategy.setMergeMode("uuid", MergeMode.SECOND);
213
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
214
		} catch (Exception e) {
215
			//ok
216
		}
217

    
218

    
219
	}
220

    
221

    
222
	/**
223
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
224
	 * @throws MergeException
225
	 */
226
	@Test
227
	public void testInvokeReferences() throws MergeException {
228
		INomenclaturalReferenceCacheStrategy cacheStrategy1 = book1.getCacheStrategy();
229
		int id = book1.getId();
230
		UUID uuid = book1.getUuid();
231
		try {
232
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
233
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
234
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
235
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
236
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
237
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
238
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
239
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
240
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
241

    
242
			bookMergeStrategy.invoke(book1, book2);
243
		} catch (MergeException e) {
244
			throw e;
245
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
246
		}
247
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
248
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
249
		Assert.assertNull("Volume should be null", book1.getVolume());
250

    
251
		//Boolean
252
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
253
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
254

    
255

    
256
		//CdmBase
257
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
258
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
259

    
260
		//Transient
261
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.getCacheStrategy());
262

    
263

    
264
		//UserType
265
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
266
		//TODO updated should have the actual date if any value has changed
267
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
268
		Assert.assertEquals("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
269
		//TODO this may not be correct
270
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
271

    
272

    
273
		//TODO
274
		//	book1.setProblemEnds(end);
275
		//	book1.setProtectedTitleCache(protectedTitleCache);
276

    
277
		//annotations -> ADD_CLONE
278
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
279
		boolean cloneExists = false;
280
		for (Annotation annotation : book1.getAnnotations()){
281
			if (annotation == this.annotation2){
282
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the
283
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
284
				//This test can be changed once this bug does not exist anymore
285
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
286
			}else if (annotationString2.equals(annotation.getText())){
287
				cloneExists = true;
288
			}
289
		}
290
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
291
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
292

    
293
		//identifier
294
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
295
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
296

    
297
		//Test Thesis
298
		Institution school1 = Institution.NewInstance();
299
		Institution school2 = Institution.NewInstance();
300

    
301
		Reference thesis1 = ReferenceFactory.newThesis();
302
		thesis1.setSchool(school1);
303
		//Thesis thesis1 = Thesis.NewInstance(school1);
304
		Reference thesis2 = ReferenceFactory.newThesis();
305
		thesis2.setSchool(school2);
306
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
307

    
308
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
309
		thesisStrategy.invoke(thesis1, thesis2);
310
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());
311
	}
312

    
313

    
314
	/**
315
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
316
	 * @throws MergeException
317
	 */
318
	@Test
319
	//@Ignore
320
	public void testInvokeTxonNames() throws MergeException {
321
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
322
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
323
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
324
		TaxonName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
325

    
326
		botName1.setGenusOrUninomial("Genus1");
327
		botName1.setSpecificEpithet("species1");
328
		botName1.setAnamorphic(true);
329

    
330
		botName2.setGenusOrUninomial("Genus2");
331
		botName2.setSpecificEpithet("species2");
332
		botName2.setAnamorphic(false);
333

    
334
		//name relations
335
		botName2.addBasionym(botName3, book1, "p.22", null);
336
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
337
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
338

    
339
		//descriptions
340
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
341
		botName1.addDescription(description1);
342
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
343
		botName1.addDescription(description2);
344

    
345
		//authors
346
		Team team1 = Team.NewInstance();
347
		Team team2 = Team.NewInstance();
348
		Person person1 = Person.NewInstance();
349
		botName1.setCombinationAuthorship(team1);
350
		botName2.setCombinationAuthorship(team2);
351

    
352
		//taxa
353
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
354
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
355

    
356
		try {
357
			botNameMergeStrategy.setMergeMode("combinationAuthorship", MergeMode.SECOND);
358
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
359

    
360
//			botNameMergeStrategy.invoke(botName1, botName2);
361
		} catch (MergeException e) {
362
			throw e;
363
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
364
		}
365

    
366
		//Boolean
367
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
368

    
369
		//NameRelations
370
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
371
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
372
		for (NameRelationship toRelation : toRelations){
373
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
374
				basionymRelations.add(toRelation);
375
			}
376
		}
377
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
378
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
379
		//TODO merge relation if matches() = true
380

    
381
		//Types
382
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
383
		//TODO add to all names etc.
384

    
385
		//Description
386
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
387

    
388
		//Authorships
389
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorship());
390

    
391
		//Taxa
392
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
393
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
394

    
395
	}
396

    
397
	/**
398
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
399
	 * @throws MergeException
400
	 */
401
	@Test
402
	public void testInvokeAgents() throws MergeException {
403
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
404

    
405
		Team team1 = Team.NewInstance();
406
		Team team2 = Team.NewInstance();
407
		Team team3 = Team.NewInstance();
408

    
409
		Person person1 = Person.NewTitledInstance("person1");
410
		Person person2 = Person.NewTitledInstance("person2");
411
		Person person3 = Person.NewTitledInstance("person3");
412

    
413
		team1.setTitleCache("Team1", true);
414
		team1.setNomenclaturalTitle("T.1");
415
		String street1 = "Strasse1";
416
		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)));
417
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
418
		String street3 = "Street3";
419
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
420
		String emailAddress1 = "Email1";
421
		team1.addEmailAddress(emailAddress1);
422

    
423
		team2.addTeamMember(person1);
424
		team2.addTeamMember(person2);
425
		String emailAddress2 = "Email2";
426
		team2.addEmailAddress(emailAddress2);
427

    
428
		team3.addTeamMember(person3);
429
		team3.addEmailAddress("emailAddress3");
430

    
431
		teamMergeStrategy.invoke(team2, team3);
432

    
433
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
434
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
435
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
436

    
437
		//Contact
438
		teamMergeStrategy.invoke(team2, team1);
439
		Contact team2Contact = team2.getContact();
440
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
441
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
442
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
443
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
444

    
445
		boolean street1Exists = false;
446
		boolean street3Exists = false;
447
		boolean country1Exists = false;
448
		for  (Address address : team2Contact.getAddresses()){
449
			if (street1.equals(address.getStreet())){
450
				street1Exists = true;
451
			}
452
			if (street3.equals(address.getStreet())){
453
				street3Exists = true;
454
			}
455
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
456
				country1Exists = true;
457
			}
458
		}
459
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
460
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
461
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
462

    
463
		//Person
464
		Institution institution1 = Institution.NewInstance();
465
		institution1.setTitleCache("inst1", true);
466
		Institution institution2 = Institution.NewInstance();
467
		institution2.setTitleCache("inst2", true);
468

    
469
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
470
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
471

    
472
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
473
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
474

    
475
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
476
		personMergeStrategy.invoke(person1, person2);
477

    
478
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
479
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
480
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
481
		}
482

    
483
	}
484

    
485
}
    (1-1/1)