Project

General

Profile

Download (18.6 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.match;
11

    
12
import java.net.URI;
13
import java.time.ZoneId;
14
import java.time.ZonedDateTime;
15

    
16
import org.apache.log4j.Logger;
17
import org.junit.After;
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.model.agent.Contact;
25
import eu.etaxonomy.cdm.model.agent.Person;
26
import eu.etaxonomy.cdm.model.agent.Team;
27
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
28
import eu.etaxonomy.cdm.model.common.Annotation;
29
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
30
import eu.etaxonomy.cdm.model.common.LSID;
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.model.location.Country;
33
import eu.etaxonomy.cdm.model.location.Point;
34
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
35
import eu.etaxonomy.cdm.model.name.IBotanicalName;
36
import eu.etaxonomy.cdm.model.name.Rank;
37
import eu.etaxonomy.cdm.model.name.TaxonName;
38
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
39
import eu.etaxonomy.cdm.model.reference.IBook;
40
import eu.etaxonomy.cdm.model.reference.IBookSection;
41
import eu.etaxonomy.cdm.model.reference.IPrintSeries;
42
import eu.etaxonomy.cdm.model.reference.Reference;
43
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
44

    
45
/**
46
 * @author a.mueller
47
 * @created 03.08.2009
48
 */
49
public class DefaultMatchStrategyTest {
50
	@SuppressWarnings("unused")
51
	private static final Logger logger = Logger.getLogger(DefaultMatchStrategyTest.class);
52

    
53
	private DefaultMatchStrategy matchStrategy;
54
	private IBook book1;
55
	private String editionString1 ="Ed.1";
56
	private String volumeString1 ="Vol.1";
57
	private Team team1;
58
	private IPrintSeries printSeries1;
59
	private Annotation annotation1;
60
	private String title1 = "Title1";
61
	private TimePeriod datePublished1 = TimePeriod.NewInstance(2000);
62
	private int hasProblem1 = 1;
63
	private LSID lsid1;
64

    
65
	private IBook book2;
66
	private String editionString2 ="Ed.2";
67
	private String volumeString2 ="Vol.2";
68
	private Team team2;
69
	private IPrintSeries printSeries2;
70
	private Annotation annotation2;
71
	private String annotationString2;
72
	private String title2 = "Title2";
73
	private ZonedDateTime created2 = ZonedDateTime.of(1999, 3, 1, 0, 0, 0, 0, ZoneId.systemDefault());
74
	private TimePeriod datePublished2 = TimePeriod.NewInstance(2002);
75
	private int hasProblem2 = 1;
76
	private LSID lsid2;
77

    
78
	private IBook book3;
79

    
80
	/**
81
	 * @throws java.lang.Exception
82
	 */
83
	@BeforeClass
84
	public static void setUpBeforeClass() throws Exception {
85
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
86
		termInitializer.initialize();
87
	}
88

    
89
	/**
90
	 * @throws java.lang.Exception
91
	 */
92
	@AfterClass
93
	public static void tearDownAfterClass() throws Exception {
94
	}
95

    
96
	/**
97
	 * @throws java.lang.Exception
98
	 */
99
	@Before
100
	public void setUp() throws Exception {
101
		team1 = Team.NewInstance();
102
		team1.setTitleCache("Team1",true);
103
		team2 = Team.NewInstance();
104
		team2.setTitleCache("Team2",true);
105
		printSeries1 = ReferenceFactory.newPrintSeries("Series1");
106
		printSeries1.setTitle("print series");
107
		printSeries2 = ReferenceFactory.newPrintSeries("Series2");
108
		annotation1 = Annotation.NewInstance("Annotation1", null);
109
		annotationString2 = "Annotation2";
110
		annotation2 = Annotation.NewInstance(annotationString2, null);
111

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

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

    
139
	}
140

    
141
	/**
142
	 * @throws java.lang.Exception
143
	 */
144
	@After
145
	public void tearDown() throws Exception {
146
	}
147

    
148
//********************* TEST *********************************************/
149

    
150
	/**
151
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#NewInstance(java.lang.Class)}.
152
	 */
153
	@Test
154
	public void testNewInstance() {
155
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
156
		Assert.assertNotNull(matchStrategy);
157
		Assert.assertEquals(Reference.class, matchStrategy.getMatchClass());
158
	}
159

    
160
	/**
161
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#getMatchMode(java.lang.String)}.
162
	 */
163
	@Test
164
	public void testGetMatchMode() {
165
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
166
		Assert.assertEquals("Match mode for isbn should be MatchMode.EQUAL_", MatchMode.EQUAL, matchStrategy.getMatchMode("isbn"));
167
		Assert.assertEquals("Match mode for title should be MatchMode.EQUAL_REQUIRED", MatchMode.EQUAL_REQUIRED, matchStrategy.getMatchMode("title"));
168
	}
169

    
170
	/**
171
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#getMatchMode(java.lang.String)}.
172
	 */
173
	@Test
174
	public void testGetSetMatchMode() {
175
		//legal value
176
		try {
177
			matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
178
			matchStrategy.setMatchMode("edition", MatchMode.EQUAL_REQUIRED);
179
			Assert.assertEquals("Match mode for edition should be", MatchMode.EQUAL_REQUIRED, matchStrategy.getMatchMode("edition"));
180
		} catch (MatchException e1) {
181
			Assert.fail();
182
		}
183
		//illegalValue
184
		try {
185
			matchStrategy.setMatchMode("xxx", MatchMode.EQUAL_REQUIRED);
186
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
187
		} catch (Exception e) {
188
			//ok
189
		}
190
		//illegalValue
191
		try {
192
			matchStrategy.setMatchMode("cacheStrategy", MatchMode.EQUAL_REQUIRED);
193
			Assert.fail("CacheStrategy is transient and therefore not a legal match parameter");
194
		} catch (Exception e) {
195
			//ok
196
		}
197
	}
198

    
199
	@Test
200
	public void testInvokeCache() throws MatchException {
201
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
202
		Assert.assertTrue("Same object should always match", matchStrategy.invoke(book1, book1));
203

    
204
		IBook bookClone = (IBook) book1.clone();
205
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone));
206
		book1.setTitleCache("cache1",true);
207
		Assert.assertFalse("Cached book should not match", matchStrategy.invoke(book1, bookClone));
208

    
209
		bookClone.setTitleCache("cache1",true);
210
		Assert.assertTrue("Cached book with same cache should match", matchStrategy.invoke(book1, bookClone));
211

    
212
		bookClone.setTitleCache("cache2",true);
213
		Assert.assertFalse("Cached book with differings caches should not match", matchStrategy.invoke(book1, bookClone));
214
		bookClone.setTitleCache("cache1",true); //restore
215

    
216
		bookClone.setEdition(null);
217
		Assert.assertTrue("Cached book with a defined and a null edition should match", matchStrategy.invoke(book1, bookClone));
218

    
219
		matchStrategy = DefaultMatchStrategy.NewInstance(TaxonName.class);
220
		IBotanicalName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
221
		IBotanicalName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
222
		Assert.assertNotNull("Rank should not be null", botName1.getRank());
223

    
224
		botName1.setGenusOrUninomial("Genus1");
225
		botName2.setGenusOrUninomial("Genus1");
226
		Assert.assertTrue("Names with equal genus should match", matchStrategy.invoke(botName1, botName2));
227

    
228
		botName1.setCombinationAuthorship(team1);
229
		botName2.setCombinationAuthorship(null);
230
		Assert.assertFalse("Names one having an author the other one not should not match", matchStrategy.invoke(botName1, botName2));
231

    
232
		botName1.setAuthorshipCache("authorCache1");
233
		botName2.setAuthorshipCache("authorCache1");
234
		Assert.assertTrue("Names with cached authors should match", matchStrategy.invoke(botName1, botName2));
235

    
236
	}
237

    
238

    
239
	@Test
240
	public void testInvokeReferences() throws MatchException {
241
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
242
		Assert.assertTrue("Same object should always match", matchStrategy.invoke(book1, book1));
243

    
244
		IBook bookClone = (IBook) ((Reference) book1).clone();
245
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone));
246
		bookClone.setTitle("Any title");
247
		Assert.assertFalse("Books with differing titles should not match", matchStrategy.invoke(book1, bookClone));
248
		String originalTitle = book1.getTitle();
249
		bookClone.setTitle(originalTitle);
250
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone));
251
		book1.setTitle(null);
252
		bookClone.setTitle(null);
253
		Assert.assertFalse("Books with no title should not match", matchStrategy.invoke(book1, bookClone));
254
		book1.setTitle(originalTitle);
255
		bookClone.setTitle(originalTitle);
256

    
257

    
258
		bookClone.setInSeries(printSeries2);
259
		Assert.assertFalse("Cloned book with differing print series should not match", matchStrategy.invoke(book1, bookClone));
260
		IPrintSeries seriesClone = (IPrintSeries)((Reference)printSeries1).clone();
261
		bookClone.setInSeries(seriesClone);
262
		Assert.assertTrue("Cloned book with cloned bookSeries should match", matchStrategy.invoke(book1, bookClone));
263
		seriesClone.setTitle("Another title");
264
		Assert.assertFalse("Cloned book should not match with differing series title", matchStrategy.invoke(book1, bookClone));
265
		bookClone.setInSeries(printSeries1);
266
		Assert.assertTrue("Original printSeries should match", matchStrategy.invoke(book1, bookClone));
267

    
268
		IBook bookTitle1 = ReferenceFactory.newBook();
269
		IBook bookTitle2 = ReferenceFactory.newBook();
270
		Assert.assertFalse("Books without title should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
271
		String title = "Any title";
272
		bookTitle1.setTitle(title);
273
		bookTitle2.setTitle(title);
274
		Assert.assertTrue("Books with same title (not empty) should match", matchStrategy.invoke(bookTitle1, bookTitle2));
275
		bookTitle1.setTitle("");
276
		bookTitle2.setTitle("");
277
		Assert.assertFalse("Books with empty title should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
278

    
279
		//Time period
280
		bookTitle1.setTitle(title);
281
		bookTitle2.setTitle(title);
282
		bookTitle1.setDatePublished(TimePeriod.NewInstance(1999, 2002));
283
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
284
		bookTitle2.setDatePublished(TimePeriod.NewInstance(1998));
285
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
286
		bookTitle2.setDatePublished(TimePeriod.NewInstance(1999));
287
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
288
		bookTitle2.setDatePublished(TimePeriod.NewInstance(1999, 2002));
289
		Assert.assertTrue("Books with same publication dates should match", matchStrategy.invoke(bookTitle1, bookTitle2));
290

    
291
		//BookSection
292
		IBookSection section1 = ReferenceFactory.newBookSection();
293
		section1.setInBook(bookTitle1);
294
		section1.setTitle("SecTitle");
295
		section1.setPages("22-33");
296
		IBookSection section2 = ReferenceFactory.newBookSection();
297
		section2.setInBook(bookTitle1);
298
		section2.setTitle("SecTitle");
299
		section2.setPages("22-33");
300

    
301

    
302
		IMatchStrategy bookSectionMatchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
303
		Assert.assertTrue("Equal BookSections should match", bookSectionMatchStrategy.invoke(section1, section2));
304
		section2.setInBook(bookTitle2);
305
		Assert.assertTrue("Matching books should result in matching book sections", bookSectionMatchStrategy.invoke(section1, section2));
306
		bookTitle2.setPages("xx");
307
		Assert.assertFalse("Sections with differing books should not match", bookSectionMatchStrategy.invoke(section1, section2));
308
		//restore
309
		bookTitle2.setPages(null);
310
		Assert.assertTrue("Matching books should result in matching book sections", bookSectionMatchStrategy.invoke(section1, section2));
311
		printSeries2.setTitle("A new series title");
312
		IMatchStrategy printSeriesMatchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
313
		Assert.assertFalse("Print series with differing titles should not match", printSeriesMatchStrategy.invoke(printSeries1, printSeries2));
314
		bookTitle1.setInSeries(printSeries1);
315
		bookTitle2.setInSeries(printSeries2);
316
		Assert.assertFalse("Books with not matching in series should not match", matchStrategy.invoke(bookTitle1, bookTitle2));
317
		Assert.assertFalse("Sections with differing print series should not match", bookSectionMatchStrategy.invoke(section1, section2));
318

    
319
		//Authorship
320
		Person person1 = Person.NewTitledInstance("person");
321
		Person person2 = Person.NewTitledInstance("person");
322

    
323
		person1.setPrefix("pre1");
324
		person2.setPrefix("pre2");
325
		book2= (IBook) ((Reference) book1).clone();
326

    
327
		Assert.assertTrue("Equal books should match", matchStrategy.invoke(book1, book2));
328

    
329
		book1.setAuthorship(person1);
330
		book2.setAuthorship(person1);
331
		Assert.assertTrue("Books with same author should match", matchStrategy.invoke(book1, book2));
332

    
333
		book2.setAuthorship(person2);
334
		Assert.assertFalse("Books with different authors should not match", matchStrategy.invoke(book1, book2));
335

    
336
		person2.setPrefix("pre1");
337
		Assert.assertTrue("Books with matching authors should not match", matchStrategy.invoke(book1, book2));
338
	}
339

    
340

    
341
	/**
342
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#invoke(eu.etaxonomy.cdm.strategy.match.IMergable, eu.etaxonomy.cdm.strategy.match.IMergable)}.
343
	 * @throws MatchException
344
	 */
345
	@Test
346
	public void testInvokeTaxonNames() throws MatchException {
347
		matchStrategy = DefaultMatchStrategy.NewInstance(TaxonName.class);
348

    
349
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
350
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
351
		IBotanicalName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
352

    
353
		Assert.assertFalse("Names without title should not match", matchStrategy.invoke(botName1, botName2));
354

    
355
		botName1.setGenusOrUninomial("Genus1");
356
		botName1.setSpecificEpithet("species1");
357

    
358
		botName2.setGenusOrUninomial("Genus1");
359
		botName2.setSpecificEpithet("species1");
360
		Assert.assertTrue("Similar names with titles set should match", matchStrategy.invoke(botName1, botName2));
361

    
362

    
363
		botName1.setAnamorphic(true);
364
		botName2.setAnamorphic(false);
365
		Assert.assertFalse("Similar names with differing boolean marker values should not match", matchStrategy.invoke(botName1, botName2));
366
		botName2.setAnamorphic(true);
367
		Assert.assertTrue("Similar names with same boolean marker values should match", matchStrategy.invoke(botName1, botName2));
368

    
369

    
370

    
371
//		//name relations
372
//		botName2.addBasionym(botName3, book1, "p.22", null);
373
//		Specimen specimen1 = Specimen.NewInstance();
374
//		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
375
//
376
//		//descriptions
377
//		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
378
//		botName1.addDescription(description1);
379
//		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
380
//		botName2.addDescription(description2);
381

    
382
		//authors
383
		Team team1 = Team.NewInstance();
384
		Team team2 = Team.NewInstance();
385
		botName1.setCombinationAuthorship(team1);
386
		botName2.setCombinationAuthorship(team2);
387
		Assert.assertTrue("Similar teams should match", DefaultMatchStrategy.NewInstance(Team.class).invoke(team1, team2));
388
		Assert.assertTrue("Similar names with matching authors should match", matchStrategy.invoke(botName1, botName2));
389

    
390
	}
391

    
392
	/**
393
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#invoke(IMatchable, IMatchable), eu.etaxonomy.cdm.strategy.match.IMatchable)}.
394
	 * @throws MatchException
395
	 */
396
	@Test
397
	public void testInvokeAgents() throws MatchException {
398
		IMatchStrategy matchStrategy = DefaultMatchStrategy.NewInstance(Team.class);
399

    
400
		Team team1 = Team.NewInstance();
401
		Team team2 = Team.NewInstance();
402
		Team team3 = Team.NewInstance();
403

    
404
		Assert.assertTrue("Teams should match", matchStrategy.invoke(team1, team2));
405
		Assert.assertTrue("Teams should match", matchStrategy.invoke(team1, team3));
406

    
407
		String street1 = "Strasse1";
408
		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)));
409
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
410
		Assert.assertTrue("Contacts should be ignoredin default match strategy", matchStrategy.invoke(team1, team2));
411

    
412
		team1.setNomenclaturalTitle("nomTitle1");
413
		team2.setNomenclaturalTitle("nomTitle2");
414
		Assert.assertFalse("Agents with differing nomenclatural titles should not match", matchStrategy.invoke(team1, team2));
415
		//restore
416
		team2.setNomenclaturalTitle("nomTitle1");
417
		Assert.assertTrue("Agents with equal nomenclatural titles should match", matchStrategy.invoke(team1, team2));
418

    
419

    
420
		Person person1 = Person.NewTitledInstance("person1");
421
		person1.setProtectedTitleCache(true);
422
		Person person2 = Person.NewTitledInstance("person2");
423
		person2.setProtectedTitleCache(true);
424
		Person person3 = Person.NewTitledInstance("person3");
425
		person3.setProtectedTitleCache(true);
426

    
427
		team1.addTeamMember(person1);
428
		team2.addTeamMember(person1);
429
		Assert.assertTrue("Teams with same team members should match", matchStrategy.invoke(team1, team2));
430

    
431
		team1.addTeamMember(person2);
432
		Assert.assertFalse("Teams with differing team members should not match", matchStrategy.invoke(team1, team2));
433
		team2.addTeamMember(person2);
434
		Assert.assertTrue("Teams with same team members should match", matchStrategy.invoke(team1, team2));
435
		team2.removeTeamMember(person2);
436
		person3.setPrefix("pre3");
437
		team2.addTeamMember(person3);
438
		Assert.assertFalse("Teams with differing team members should not match", matchStrategy.invoke(team1, team2));
439
		person3.setTitleCache(person2.getTitleCache(),true);
440
		person2.setPrefix("pre3");
441
		Assert.assertTrue("Teams with matching members in right order should match", matchStrategy.invoke(team1, team2));
442
		team2.removeTeamMember(person1);
443
		team2.addTeamMember(person1);
444
		Assert.assertFalse("Teams with matching members in wrong order should not match", matchStrategy.invoke(team1, team2));
445

    
446
	}
447

    
448
	@Test
449
	public void testInvokeTeamOrPersonBase(){
450

    
451
	}
452

    
453
}
    (1-1/1)