Project

General

Profile

Download (16.8 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
package eu.etaxonomy.cdm.strategy.merge;
10

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

    
15
import org.apache.log4j.Logger;
16
import org.joda.time.DateTime;
17
import org.junit.Assert;
18
import org.junit.Before;
19
import org.junit.Test;
20

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

    
50
/**
51
 * @author a.mueller
52
 * @since 03.08.2009
53
 */
54
public class DefaultMergeStrategyTest extends TermTestBase {
55

    
56
	@SuppressWarnings("unused")
57
	private static final Logger logger = Logger.getLogger(DefaultMergeStrategyTest.class);
58

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

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

    
84
	@Before
85
	public void setUp() throws Exception {
86
		bookMergeStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
87
		team1 = Team.NewInstance();
88
		team1.setTitleCache("Team1", true);
89
		team2 = Team.NewInstance();
90
		team2.setTitleCache("Team2", true);
91
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
92
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
93
		annotation1 = Annotation.NewInstance("Annotation1", null);
94
		annotationString2 = "Annotation2";
95
		annotation2 = Annotation.NewInstance(annotationString2, null);
96

    
97
		book1 = ReferenceFactory.newBook();
98
		book1.setAuthorship(team1);
99
		book1.setTitle(title1);
100
		book1.setEdition(editionString1);
101
		book1.setVolume(volumeString1);
102
		book1.setInReference(printSeries1);
103
		book1.addAnnotation(annotation1);
104
		book1.setDatePublished(datePublished1);
105
		book1.setParsingProblem(hasProblem1);
106
		lsid1 = new LSID("authority1", "namespace1", "object1", "revision1");
107
		book1.setLsid(lsid1);
108
		book1.setNomenclaturallyRelevant(false);
109

    
110
		book2 = ReferenceFactory.newBook();
111
		book2.setAuthorship(team2);
112
		book2.setTitle(title2);
113
		book2.setEdition(editionString2);
114
		book2.setVolume(volumeString2);
115
		book2.setInReference(printSeries2);
116
		book2.addAnnotation(annotation2);
117
		book2.setCreated(created2);
118
		book2.setDatePublished(datePublished2);
119
		book2.setParsingProblem(hasProblem2);
120
		lsid2 = new LSID("authority2", "namespace2", "object2", "revision2");
121
		book2.setLsid(lsid2);
122
		book2.setNomenclaturallyRelevant(true);
123
	}
124

    
125
//********************* TEST *********************************************/
126

    
127
	@Test
128
	public void testNewInstance() {
129
		Assert.assertNotNull(bookMergeStrategy);
130
		Assert.assertEquals(Reference.class, bookMergeStrategy.getMergeClass());
131
	}
132

    
133
	@Test
134
	public void testGetMergeMode() {
135
		Assert.assertEquals("Merge mode for title should be MergeMode.FIRST", MergeMode.FIRST, bookMergeStrategy.getMergeMode("title"));
136
	}
137

    
138
	@Test
139
	public void testGetSetMergeMode() {
140
		//legal value
141
		try {
142
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
143
			Assert.assertEquals("Merge mode for edition should be", MergeMode.SECOND, bookMergeStrategy.getMergeMode("edition"));
144
		} catch (MergeException e1) {
145
			Assert.fail();
146
		}
147
		//illegalValue
148
		try {
149
			bookMergeStrategy.setMergeMode("xxx", MergeMode.SECOND);
150
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
151
		} catch (Exception e) {
152
			//ok
153
		}
154
		//illegalValue
155
		try {
156
			bookMergeStrategy.setMergeMode("cacheStrategy", MergeMode.SECOND);
157
			Assert.fail("CacheStrategy is transient and therefore not a legal merge parameter");
158
		} catch (Exception e) {
159
			//ok
160
		}
161
		//illegalValue
162
		try {
163
			bookMergeStrategy.setMergeMode("id", MergeMode.SECOND);
164
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
165
		} catch (Exception e) {
166
			//ok
167
		}
168
		//illegalValue
169
		try {
170
			bookMergeStrategy.setMergeMode("uuid", MergeMode.SECOND);
171
			Assert.fail("Identifier merge mode must always be MergeMode.FIRST");
172
		} catch (Exception e) {
173
			//ok
174
		}
175
	}
176

    
177
	@Test
178
	public void testInvokeReferences() throws MergeException {
179
		INomenclaturalReferenceCacheStrategy cacheStrategy1 = book1.getCacheStrategy();
180
		int id = book1.getId();
181
		UUID uuid = book1.getUuid();
182
		try {
183
			bookMergeStrategy.setMergeMode("edition", MergeMode.SECOND);
184
			bookMergeStrategy.setMergeMode("volume", MergeMode.NULL);
185
			bookMergeStrategy.setMergeMode("authorship", MergeMode.SECOND);
186
			bookMergeStrategy.setMergeMode("created", MergeMode.SECOND);
187
			bookMergeStrategy.setMergeMode("updated",MergeMode.NULL);
188
			bookMergeStrategy.setMergeMode("datePublished", MergeMode.SECOND);
189
			bookMergeStrategy.setMergeMode("parsingProblem", MergeMode.SECOND);
190
			bookMergeStrategy.setMergeMode("inReference", MergeMode.SECOND);
191
			bookMergeStrategy.setMergeMode("lsid", MergeMode.SECOND);
192

    
193
			bookMergeStrategy.invoke(book1, book2);
194
		} catch (MergeException e) {
195
			throw e;
196
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
197
		}
198
		Assert.assertEquals("Title should stay the same", title1, book1.getTitle());
199
		Assert.assertEquals("Edition should become edition 2", editionString2, book1.getEdition());
200
		Assert.assertNull("Volume should be null", book1.getVolume());
201

    
202
		//Boolean
203
		Assert.assertEquals("Has problem must be hasProblem2", hasProblem2, book1.getParsingProblem());
204
		Assert.assertEquals("nomenclaturally relevant must have value true (AND semantics)", true, book1.isNomenclaturallyRelevant() );
205

    
206
		//CdmBase
207
		Assert.assertSame("Authorship must be the one of book2", team2, book1.getAuthorship());
208
		Assert.assertSame("In Series must be the one of book2", printSeries2, book1.getInReference());
209

    
210
		//Transient
211
		Assert.assertSame("Cache strategy is transient and shouldn't change therefore", cacheStrategy1, book1.getCacheStrategy());
212

    
213
		//UserType
214
		Assert.assertSame("Created must be created2", created2, book1.getCreated());
215
		//TODO updated should have the actual date if any value has changed
216
		Assert.assertSame("Created must be created2", null, book1.getUpdated());
217
		Assert.assertEquals("Created must be datePublsihed2", datePublished2, book1.getDatePublished());
218
		//TODO this may not be correct
219
		Assert.assertSame("LSID must be LSID2", lsid2, book1.getLsid());
220

    
221
		//TODO
222
		//	book1.setProblemEnds(end);
223
		//	book1.setProtectedTitleCache(protectedTitleCache);
224

    
225
		//annotations -> ADD_CLONE
226
		Assert.assertEquals("Annotations should contain annotations of both books", 2, book1.getAnnotations().size());
227
		boolean cloneExists = false;
228
		for (Annotation annotation : book1.getAnnotations()){
229
			if (annotation == this.annotation2){
230
				//Hibernate will not persist the exact same object. Probably this is a bug (the according row in the
231
				//M:M table is not deleted and a unique constraints does not allow adding 2 rows with the same annotation_id
232
				//This test can be changed once this bug does not exist anymore
233
				Assert.fail("Book1 should contain a clone of annotation2 but contains annotation2 itself");
234
			}else if (annotationString2.equals(annotation.getText())){
235
				cloneExists = true;
236
			}
237
		}
238
		Assert.assertTrue("Book1 should contain a clone of annotation2", cloneExists);
239
	//	Assert.assertEquals("Annotations from book2 should be deleted", 0, book2.getAnnotations().size());
240

    
241
		//identifier
242
		Assert.assertSame("Identifier must never be changed", id, book1.getId());
243
		Assert.assertSame("Identifier must never be changed", uuid, book1.getUuid());
244

    
245
		//Test Thesis
246
		Institution school1 = Institution.NewInstance();
247
		Institution school2 = Institution.NewInstance();
248

    
249
		Reference thesis1 = ReferenceFactory.newThesis();
250
		thesis1.setSchool(school1);
251
		//Thesis thesis1 = Thesis.NewInstance(school1);
252
		Reference thesis2 = ReferenceFactory.newThesis();
253
		thesis2.setSchool(school2);
254
		DefaultMergeStrategy thesisStrategy = DefaultMergeStrategy.NewInstance(Reference.class);
255

    
256
		thesisStrategy.setMergeMode("school", MergeMode.SECOND);
257
		thesisStrategy.invoke(thesis1, thesis2);
258
		Assert.assertSame("school must be school2", school2, thesis1.getSchool());
259
	}
260

    
261
	@Test
262
	public void testInvokeTxonNames() throws MergeException {
263
		IMergeStrategy botNameMergeStrategy = DefaultMergeStrategy.NewInstance(TaxonName.class);
264
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
265
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
266
		TaxonName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
267

    
268
		botName1.setGenusOrUninomial("Genus1");
269
		botName1.setSpecificEpithet("species1");
270
		botName1.setAnamorphic(true);
271

    
272
		botName2.setGenusOrUninomial("Genus2");
273
		botName2.setSpecificEpithet("species2");
274
		botName2.setAnamorphic(false);
275

    
276
		//name relations
277
		botName2.addBasionym(botName3, book1, "p.22", null, null);
278
		DerivedUnit specimen1 = DerivedUnit.NewPreservedSpecimenInstance();
279
		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
280

    
281
		//descriptions
282
		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
283
		botName1.addDescription(description1);
284
		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
285
		botName1.addDescription(description2);
286

    
287
		//authors
288
		Team team1 = Team.NewInstance();
289
		Team team2 = Team.NewInstance();
290
		Person person1 = Person.NewInstance();
291
		botName1.setCombinationAuthorship(team1);
292
		botName2.setCombinationAuthorship(team2);
293

    
294
		//taxa
295
		TaxonBase<?> taxon1= Taxon.NewInstance(botName1, book1);
296
		TaxonBase<?> taxon2= Taxon.NewInstance(botName2, book2);
297

    
298
		try {
299
			botNameMergeStrategy.setMergeMode("combinationAuthorship", MergeMode.SECOND);
300
			botNameMergeStrategy.setMergeMode("anamorphic", MergeMode.AND);
301

    
302
//			botNameMergeStrategy.invoke(botName1, botName2);
303
		} catch (MergeException e) {
304
			throw e;
305
			//Assert.fail("An unexpected merge exception occurred: " + e.getMessage() + ";" + e.getCause().getMessage());
306
		}
307

    
308
		//Boolean
309
		Assert.assertEquals("Is anamorphic must be false", true && false, botName2.isAnamorphic());
310

    
311
		//NameRelations
312
		Set<NameRelationship> toRelations = botName2.getRelationsToThisName();
313
		Set<NameRelationship> basionymRelations = new HashSet<NameRelationship>();
314
		for (NameRelationship toRelation : toRelations){
315
			if (toRelation.getType().equals(NameRelationshipType.BASIONYM())){
316
				basionymRelations.add(toRelation);
317
			}
318
		}
319
		Assert.assertEquals("Number of basionyms must be 1", 1, basionymRelations.size());
320
		Assert.assertEquals("Basionym must have same reference", book1, basionymRelations.iterator().next().getCitation());
321
		//TODO merge relation if matches() = true
322

    
323
		//Types
324
		Assert.assertEquals("Number of specimen type designations must be 1", 1, botName2.getSpecimenTypeDesignations().size());
325
		//TODO add to all names etc.
326

    
327
		//Description
328
		Assert.assertEquals("Number of descriptions must be 2", 2, botName1.getDescriptions().size());
329

    
330
		//Authorships
331
		Assert.assertEquals("Combination author must be combination author 1", team1, botName1.getCombinationAuthorship());
332

    
333
		//Taxa
334
		Assert.assertEquals("TaxonName of taxon1 must be name1", botName1, taxon1.getName());
335
		Assert.assertEquals("TaxonName of taxon2 must be name2", botName2, taxon2.getName());
336
	}
337

    
338
	@Test
339
	public void testInvokeAgents() throws MergeException {
340
		IMergeStrategy teamMergeStrategy = DefaultMergeStrategy.NewInstance(Team.class);
341

    
342
		Team team1 = Team.NewInstance();
343
		Team team2 = Team.NewInstance();
344
		Team team3 = Team.NewInstance();
345

    
346
		Person person1 = Person.NewTitledInstance("person1");
347
		Person person2 = Person.NewTitledInstance("person2");
348
		Person person3 = Person.NewTitledInstance("person3");
349

    
350
		team1.setTitleCache("Team1", true);
351
		team1.setNomenclaturalTitle("T.1");
352
		String street1 = "Strasse1";
353
		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)));
354
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
355
		String street3 = "Street3";
356
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
357
		String emailAddress1 = "Email1";
358
		team1.addEmailAddress(emailAddress1);
359

    
360
		team2.addTeamMember(person1);
361
		team2.addTeamMember(person2);
362
		String emailAddress2 = "Email2";
363
		team2.addEmailAddress(emailAddress2);
364

    
365
		team3.addTeamMember(person3);
366
		team3.addEmailAddress("emailAddress3");
367

    
368
		teamMergeStrategy.invoke(team2, team3);
369

    
370
		Assert.assertEquals("Team2 must have 3 persons as members",3, team2.getTeamMembers().size());
371
		Assert.assertTrue("Team2 must have person3 as new member", team2.getTeamMembers().contains(person3));
372
		Assert.assertSame("Team2 must have person3 as third member",person3, team2.getTeamMembers().get(2));
373

    
374
		//Contact
375
		teamMergeStrategy.invoke(team2, team1);
376
		Contact team2Contact = team2.getContact();
377
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
378
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
379
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
380
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
381

    
382
		boolean street1Exists = false;
383
		boolean street3Exists = false;
384
		boolean country1Exists = false;
385
		for  (Address address : team2Contact.getAddresses()){
386
			if (street1.equals(address.getStreet())){
387
				street1Exists = true;
388
			}
389
			if (street3.equals(address.getStreet())){
390
				street3Exists = true;
391
			}
392
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
393
				country1Exists = true;
394
			}
395
		}
396
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
397
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
398
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
399

    
400
		//Person
401
		Institution institution1 = Institution.NewInstance();
402
		institution1.setTitleCache("inst1", true);
403
		Institution institution2 = Institution.NewInstance();
404
		institution2.setTitleCache("inst2", true);
405

    
406
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
407
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
408

    
409
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
410
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
411

    
412
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
413
		personMergeStrategy.invoke(person1, person2);
414

    
415
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
416
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
417
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
418
		}
419
	}
420
}
    (1-1/1)