Project

General

Profile

Download (18.2 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.description.TaxonNameDescription;
37
import eu.etaxonomy.cdm.model.location.Country;
38
import eu.etaxonomy.cdm.model.location.Point;
39
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
40
import eu.etaxonomy.cdm.model.name.NameRelationship;
41
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
42
import eu.etaxonomy.cdm.model.name.Rank;
43
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
44
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
45
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
46
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
47
import eu.etaxonomy.cdm.model.reference.Reference;
48
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
49
import eu.etaxonomy.cdm.model.taxon.Taxon;
50
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
51
import eu.etaxonomy.cdm.strategy.cache.reference.INomenclaturalReferenceCacheStrategy;
52

    
53
/**
54
 * @author a.mueller
55
 * @created 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 TimePeriod datePublished1 = TimePeriod.NewInstance(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 TimePeriod datePublished2 = TimePeriod.NewInstance(2002);
83
	private int hasProblem2 = 1;
84
	private LSID lsid2;
85

    
86

    
87
	private Reference book3;
88

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

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

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

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

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

    
148
	}
149

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

    
157
//********************* TEST *********************************************/
158

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

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

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

    
217

    
218
	}
219

    
220

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

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

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

    
254

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

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

    
262

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

    
271

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

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

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

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

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

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

    
312

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
394
	}
395

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

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

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

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

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

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

    
430
		teamMergeStrategy.invoke(team2, team3);
431

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

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

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

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

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

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

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

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

    
482
	}
483

    
484
}
    (1-1/1)