Project

General

Profile

Download (18.5 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

    
14
import org.apache.log4j.Logger;
15
import org.joda.time.DateTime;
16
import org.junit.After;
17
import org.junit.AfterClass;
18
import org.junit.Assert;
19
import org.junit.Before;
20
import org.junit.BeforeClass;
21
import org.junit.Test;
22

    
23
import eu.etaxonomy.cdm.model.agent.Contact;
24
import eu.etaxonomy.cdm.model.agent.Person;
25
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
27
import eu.etaxonomy.cdm.model.common.Annotation;
28
import eu.etaxonomy.cdm.model.common.DefaultTermInitializer;
29
import eu.etaxonomy.cdm.model.common.LSID;
30
import eu.etaxonomy.cdm.model.common.TimePeriod;
31
import eu.etaxonomy.cdm.model.location.Country;
32
import eu.etaxonomy.cdm.model.location.Point;
33
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
34
import eu.etaxonomy.cdm.model.name.BotanicalName;
35
import eu.etaxonomy.cdm.model.name.Rank;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.reference.IBook;
38
import eu.etaxonomy.cdm.model.reference.IBookSection;
39
import eu.etaxonomy.cdm.model.reference.IPrintSeries;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
42

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

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

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

    
77
	private IBook book3;
78

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

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

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

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

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

    
138
	}
139

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

    
147
//********************* TEST *********************************************/
148

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

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

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

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

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

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

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

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

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

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

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

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

    
235
	}
236

    
237

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

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

    
256

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

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

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

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

    
300

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

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

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

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

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

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

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

    
339

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

    
348
		BotanicalName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
349
		BotanicalName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
350
		BotanicalName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
351

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

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

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

    
361

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

    
368

    
369

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

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

    
389
	}
390

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

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

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

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

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

    
418

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

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

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

    
445
	}
446

    
447
	@Test
448
	public void testInvokeTeamOrPersonBase(){
449

    
450
	}
451

    
452
}
    (1-1/1)