Project

General

Profile

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

    
11
package eu.etaxonomy.cdm.strategy.merge;
12

    
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.description.TaxonNameDescription;
37
import eu.etaxonomy.cdm.model.location.Point;
38
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
39
import eu.etaxonomy.cdm.model.location.Country;
40
import eu.etaxonomy.cdm.model.name.BotanicalName;
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.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.strategy.cache.reference.INomenclaturalReferenceCacheStrategy;
51

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

    
60
	private DefaultMergeStrategy bookMergeStrategy;
61
	private Reference<?> book1;
62
	private String editionString1 ="Ed.1";
63
	private String volumeString1 ="Vol.1";
64
	private Team team1;
65
	private Reference<?> printSeries1;
66
	private Annotation annotation1;
67
	private String title1 = "Title1";
68
	private TimePeriod datePublished1 = TimePeriod.NewInstance(2000);
69
	private int hasProblem1 = 1;
70
	private LSID lsid1;
71
	
72
	private Reference<?> book2;
73
	private String editionString2 ="Ed.2";
74
	private String volumeString2 ="Vol.2";
75
	private Team team2;
76
	private Reference<?> printSeries2;
77
	private Annotation annotation2;
78
	private String annotationString2;
79
	private String title2 = "Title2";
80
	private DateTime created2 = new DateTime(1999, 3, 1, 0, 0, 0, 0);
81
	private TimePeriod datePublished2 = TimePeriod.NewInstance(2002);
82
	private int hasProblem2 = 1;
83
	private LSID lsid2;
84
	
85
	
86
	private Reference<?> book3;
87
	
88
	/**
89
	 * @throws java.lang.Exception
90
	 */
91
	@BeforeClass
92
	public static void setUpBeforeClass() throws Exception {
93
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
94
		termInitializer.initialize();
95
	}
96

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

    
104
	/**
105
	 * @throws java.lang.Exception
106
	 */
107
	@Before
108
	public void setUp() throws Exception {
109
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
110
		team1 = Team.NewInstance();
111
		team1.setTitleCache("Team1", true);
112
		team2 = Team.NewInstance();
113
		team2.setTitleCache("Team2", true);
114
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
115
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
116
		annotation1 = Annotation.NewInstance("Annotation1", null);
117
		annotationString2 = "Annotation2";
118
		annotation2 = Annotation.NewInstance(annotationString2, null);
119
		
120
		book1 = ReferenceFactory.newBook();
121
		book1.setAuthorship(team1);
122
		book1.setTitle(title1);
123
		book1.setEdition(editionString1);
124
		book1.setVolume(volumeString1);
125
		book1.setInReference(printSeries1);
126
		book1.addAnnotation(annotation1);
127
		book1.setDatePublished(datePublished1);
128
		book1.setParsingProblem(hasProblem1);
129
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
130
		book1.setLsid(lsid1);
131
		book1.setNomenclaturallyRelevant(false);
132
		
133
		book2 = ReferenceFactory.newBook();
134
		book2.setAuthorship(team2);
135
		book2.setTitle(title2);
136
		book2.setEdition(editionString2);
137
		book2.setVolume(volumeString2);
138
		book2.setInReference(printSeries2);
139
		book2.addAnnotation(annotation2);
140
		book2.setCreated(created2);
141
		book2.setDatePublished(datePublished2);
142
		book2.setParsingProblem(hasProblem2);
143
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
144
		book2.setLsid(lsid2);
145
		book2.setNomenclaturallyRelevant(true);
146
		
147
	}
148

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

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

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

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

    
219
	
220
	/**
221
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
222
	 * @throws MergeException 
223
	 */
224
	@Test
225
	public void testInvokeReferences() throws MergeException {
226
		INomenclaturalReferenceCacheStrategy cacheStrategy1 = (INomenclaturalReferenceCacheStrategy)book1.getCacheStrategy();
227
		int id = book1.getId();
228
		UUID uuid = book1.getUuid();
229
		try {
230
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
231
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
232
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
233
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
234
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
235
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
236
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
237
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
238
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
239
			
240
			bookMergeStrategy.invoke(book1, book2);
241
		} catch (MergeException e) {
242
			throw e;
243
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
244
		}
245
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
246
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
247
		Assert.assertNull("Volume should be null", book1.getVolume());
248
		
249
		//Boolean
250
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
251
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
252
		
253
		
254
		//CdmBase
255
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
256
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
257
		
258
		//Transient
259
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.getCacheStrategy());
260
		
261
		
262
		//UserType
263
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
264
		//TODO updated should have the actual date if any value has changed
265
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
266
		Assert.assertSame("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
267
		//TODO this may not be correct
268
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
269
		
270

    
271
		//TODO
272
		//	book1.setProblemEnds(end);
273
		//	book1.setProtectedTitleCache(protectedTitleCache);
274
		
275
		//annotations -> ADD_CLONE
276
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
277
		boolean cloneExists = false;
278
		for (Annotation annotation : book1.getAnnotations()){
279
			if (annotation == this.annotation2){
280
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the 
281
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
282
				//This test can be changed once this bug does not exist anymore 
283
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
284
			}else if (annotationString2.equals(annotation.getText())){
285
				cloneExists = true;
286
			}
287
		}
288
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
289
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
290
		
291
		//identifier
292
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
293
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
294
		
295
		//Test Thesis
296
		Institution school1 = Institution.NewInstance();
297
		Institution school2 = Institution.NewInstance();
298
		
299
		Reference<?> thesis1 = ReferenceFactory.newThesis();
300
		thesis1.setSchool(school1);
301
		//Thesis thesis1 = Thesis.NewInstance(school1);
302
		Reference<?> thesis2 = ReferenceFactory.newThesis();
303
		thesis2.setSchool(school2);
304
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
305
		
306
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
307
		thesisStrategy.invoke(thesis1, thesis2);
308
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());	
309
	}
310
	
311
	
312
	/**
313
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
314
	 * @throws MergeException 
315
	 */
316
	@Test
317
	//@Ignore
318
	public void testInvokeTxonNames() throws MergeException {
319
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(BotanicalName.class);
320
		BotanicalName botName1 = BotanicalName.NewInstance(Rank.SPECIES());
321
		BotanicalName botName2 = BotanicalName.NewInstance(Rank.SPECIES());
322
		BotanicalName botName3 = BotanicalName.NewInstance(Rank.SPECIES());
323
		
324
		botName1.setGenusOrUninomial("Genus1");
325
		botName1.setSpecificEpithet("species1");
326
		botName1.setAnamorphic(true);
327
		
328
		botName2.setGenusOrUninomial("Genus2");
329
		botName2.setSpecificEpithet("species2");
330
		botName2.setAnamorphic(false);
331
		
332
		//name relations
333
		botName2.addBasionym(botName3, book1, "p.22", null);
334
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
335
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
336
		
337
		//descriptions
338
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
339
		botName1.addDescription(description1);
340
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
341
		botName1.addDescription(description2);
342
		
343
		//authors
344
		Team team1 = Team.NewInstance();
345
		Team team2 = Team.NewInstance();
346
		Person person1 = Person.NewInstance();
347
		botName1.setCombinationAuthorTeam(team1);
348
		botName2.setCombinationAuthorTeam(team2);
349
		
350
		//taxa
351
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
352
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
353
		
354
		try {
355
			botNameMergeStrategy.setMergeMode("combinationAuthorTeam", MergeMode.SECOND);
356
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
357
			
358
//			botNameMergeStrategy.invoke(botName1, botName2);
359
		} catch (MergeException e) {
360
			throw e;
361
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
362
		}
363

    
364
		//Boolean
365
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
366
		
367
		//NameRelations
368
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
369
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
370
		for (NameRelationship toRelation : toRelations){
371
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
372
				basionymRelations.add(toRelation);
373
			}
374
		}
375
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
376
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
377
		//TODO merge relation if matches() = true
378
		
379
		//Types
380
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
381
		//TODO add to all names etc.
382
		
383
		//Description
384
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
385
		
386
		//AuthorTeams
387
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorTeam());
388
		
389
		//Taxa
390
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
391
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
392
		
393
	}
394
	
395
	/**
396
	 * Test method for {@link eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy#invoke(eu.etaxonomy.cdm.strategy.merge.IMergable, eu.etaxonomy.cdm.strategy.merge.IMergable)}.
397
	 * @throws MergeException 
398
	 */
399
	@Test
400
	public void testInvokeAgents() throws MergeException {
401
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
402
		
403
		Team team1 = Team.NewInstance();
404
		Team team2 = Team.NewInstance();
405
		Team team3 = Team.NewInstance();
406
		
407
		Person person1 = Person.NewTitledInstance("person1");
408
		Person person2 = Person.NewTitledInstance("person2");
409
		Person person3 = Person.NewTitledInstance("person3");
410
		
411
		team1.setTitleCache("Team1", true);
412
		team1.setNomenclaturalTitle("T.1");
413
		String street1 = "Strasse1";
414
		team1.setContact(Contact.NewInstance(street1, "12345", "Berlin", Country.ARGENTINAARGENTINEREPUBLIC(),"pobox" , "Region", "a@b.de", "f12345", "+49-30-123456", "www.abc.de", Point.NewInstance(2.4, 3.2, ReferenceSystem.WGS84(), 3)));
415
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
416
		String street3 = "Street3";
417
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
418
		String emailAddress1 = "Email1";
419
		team1.addEmailAddress(emailAddress1);
420
		
421
		team2.addTeamMember(person1);
422
		team2.addTeamMember(person2);
423
		String emailAddress2 = "Email2";
424
		team2.addEmailAddress(emailAddress2);
425
		
426
		team3.addTeamMember(person3);
427
		team3.addEmailAddress("emailAddress3");
428
		
429
		teamMergeStrategy.invoke(team2, team3);
430
		
431
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
432
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
433
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
434
		
435
		//Contact 
436
		teamMergeStrategy.invoke(team2, team1);
437
		Contact team2Contact = team2.getContact();
438
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
439
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
440
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
441
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
442
		
443
		boolean street1Exists = false;
444
		boolean street3Exists = false;
445
		boolean country1Exists = false;
446
		for  (Address address : team2Contact.getAddresses()){
447
			if (street1.equals(address.getStreet())){
448
				street1Exists = true;
449
			}
450
			if (street3.equals(address.getStreet())){
451
				street3Exists = true;
452
			}
453
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
454
				country1Exists = true;
455
			}
456
		}
457
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
458
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
459
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
460
		
461
		//Person
462
		Institution institution1 = Institution.NewInstance();
463
		institution1.setTitleCache("inst1", true);
464
		Institution institution2 = Institution.NewInstance();
465
		institution2.setTitleCache("inst2", true);
466
		
467
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
468
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
469
		
470
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
471
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
472

    
473
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
474
		personMergeStrategy.invoke(person1, person2);
475
		
476
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
477
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
478
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
479
		}
480
		
481
	}
482
	
483
}
    (1-1/1)