Project

General

Profile

Download (19.1 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.match;
10

    
11
import java.net.URI;
12

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

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

    
41
/**
42
 * @author a.mueller
43
 * @since 03.08.2009
44
 */
45
public class DefaultMatchStrategyTest {
46
	@SuppressWarnings("unused")
47
	private static final Logger logger = Logger.getLogger(DefaultMatchStrategyTest.class);
48

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

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

    
74
	private IBook book3;
75

    
76
	@BeforeClass
77
	public static void setUpBeforeClass() throws Exception {
78
		DefaultTermInitializer termInitializer = new DefaultTermInitializer();
79
		termInitializer.initialize();
80
	}
81

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

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

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

    
122
	}
123

    
124
//********************* TEST *********************************************/
125

    
126
	/**
127
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#NewInstance(java.lang.Class)}.
128
	 */
129
	@Test
130
	public void testNewInstance() {
131
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
132
		Assert.assertNotNull(matchStrategy);
133
//		Assert.assertEquals(Reference.class, matchStrategy.getMatchClass());
134
	}
135

    
136
	/**
137
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#getMatchMode(java.lang.String)}.
138
	 */
139
	@Test
140
	public void testGetMatchMode() {
141
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
142
//		Assert.assertEquals("Match mode for isbn should be MatchMode.EQUAL_", MatchMode.EQUAL, matchStrategy.getMatchMode("isbn"));
143
//		Assert.assertEquals("Match mode for title should be MatchMode.EQUAL_REQUIRED", MatchMode.EQUAL_REQUIRED, matchStrategy.getMatchMode("title"));
144
	}
145

    
146
	/**
147
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#getMatchMode(java.lang.String)}.
148
	 */
149
	@Test
150
	public void testGetSetMatchMode() {
151
		//legal value
152
		try {
153
			matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
154
			matchStrategy.setMatchMode("edition", MatchMode.EQUAL_REQUIRED);
155

    
156
			matchStrategy.getMatching().getFieldMatcher("edition");
157
//			Assert.assertEquals("Match mode for edition should be", MatchMode.EQUAL_REQUIRED, matchStrategy.getMatchMode("edition"));
158
		} catch (MatchException e1) {
159
			Assert.fail();
160
		}
161
		//illegalValue
162
		try {
163
			matchStrategy.setMatchMode("xxx", MatchMode.EQUAL_REQUIRED);
164
			Assert.fail("A property name must exist, otherwise an exception must be thrown");
165
		} catch (Exception e) {
166
			//ok
167
		}
168
		//illegalValue
169
		try {
170
			matchStrategy.setMatchMode("cacheStrategy", MatchMode.EQUAL_REQUIRED);
171
			Assert.fail("CacheStrategy is transient and therefore not a legal match parameter");
172
		} catch (Exception e) {
173
			//ok
174
		}
175
	}
176

    
177
	@Test
178
	public void testInvokeCache() throws MatchException {
179
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
180
		Assert.assertTrue("Same object should always match", matchStrategy.invoke(book1, book1).isSuccessful());
181

    
182
		IBook bookClone = (IBook) book1.clone();
183
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
184
		book1.setTitleCache("cache1",true);
185
		Assert.assertFalse("Cached book should not match", matchStrategy.invoke(book1, bookClone).isSuccessful());
186

    
187
		bookClone.setTitleCache("cache1",true);
188
		Assert.assertTrue("Cached book with same cache should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
189

    
190
		bookClone.setTitleCache("cache2",true);
191
		Assert.assertFalse("Cached book with differings caches should not match", matchStrategy.invoke(book1, bookClone).isSuccessful());
192
		bookClone.setTitleCache("cache1",true); //restore
193

    
194
		bookClone.setEdition(null);
195
		Assert.assertTrue("Cached book with a defined and a null edition should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
196

    
197
		matchStrategy = DefaultMatchStrategy.NewInstance(TaxonName.class);
198
		IBotanicalName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
199
		IBotanicalName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
200
		Assert.assertNotNull("Rank should not be null", botName1.getRank());
201

    
202
		botName1.setGenusOrUninomial("Genus1");
203
		botName2.setGenusOrUninomial("Genus1");
204
		Assert.assertTrue("Names with equal genus should match", matchStrategy.invoke(botName1, botName2).isSuccessful());
205

    
206
		botName1.setCombinationAuthorship(team1);
207
		botName2.setCombinationAuthorship(null);
208
		Assert.assertFalse("Names one having an author the other one not should not match", matchStrategy.invoke(botName1, botName2).isSuccessful());
209

    
210
		botName1.setAuthorshipCache("authorCache1");
211
		botName2.setAuthorshipCache("authorCache1");
212
		Assert.assertTrue("Names with cached authors should match", matchStrategy.invoke(botName1, botName2).isSuccessful());
213

    
214
	}
215

    
216

    
217
	@Test
218
	public void testInvokeReferences() throws MatchException {
219
		matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
220
		Assert.assertTrue("Same object should always match", matchStrategy.invoke(book1, book1).isSuccessful());
221

    
222
		IBook bookClone = ((Reference) book1).clone();
223
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
224
		bookClone.setTitle("Any title");
225
		Assert.assertFalse("Books with differing titles should not match", matchStrategy.invoke(book1, bookClone).isSuccessful());
226
		String originalTitle = book1.getTitle();
227
		bookClone.setTitle(originalTitle);
228
		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
229
		book1.setTitle(null);
230
		bookClone.setTitle(null);
231
		Assert.assertFalse("Books with no title should not match", matchStrategy.invoke(book1, bookClone).isSuccessful());
232
		book1.setTitle(originalTitle);
233
		bookClone.setTitle(originalTitle);
234

    
235

    
236
		bookClone.setInSeries(printSeries2);
237
		Assert.assertFalse("Cloned book with differing print series should not match", matchStrategy.invoke(book1, bookClone).isSuccessful());
238
		IPrintSeries seriesClone = ((Reference)printSeries1).clone();
239
		bookClone.setInSeries(seriesClone);
240
		Assert.assertTrue("Cloned book with cloned bookSeries should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
241
		seriesClone.setTitle("Another title");
242
		Assert.assertFalse("Cloned book should not match with differing series title", matchStrategy.invoke(book1, bookClone).isSuccessful());
243
		bookClone.setInSeries(printSeries1);
244
		Assert.assertTrue("Original printSeries should match", matchStrategy.invoke(book1, bookClone).isSuccessful());
245

    
246
		IBook bookTitle1 = ReferenceFactory.newBook();
247
		IBook bookTitle2 = ReferenceFactory.newBook();
248
		Assert.assertFalse("Books without title should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
249
		String title = "Any title";
250
		bookTitle1.setTitle(title);
251
		bookTitle2.setTitle(title);
252
		Assert.assertTrue("Books with same title (not empty) should match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
253
		bookTitle1.setTitle("");
254
		bookTitle2.setTitle("");
255
		Assert.assertFalse("Books with empty title should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
256

    
257
		//Time period
258
		bookTitle1.setTitle(title);
259
		bookTitle2.setTitle(title);
260
		bookTitle1.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1999, 2002));
261
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
262
		bookTitle2.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1998));
263
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
264
		bookTitle2.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1999));
265
		Assert.assertFalse("Books with differing publication dates should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
266
		bookTitle2.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(1999, 2002));
267
		Assert.assertTrue("Books with same publication dates should match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
268

    
269
		//BookSection
270
		IBookSection section1 = ReferenceFactory.newBookSection();
271
		section1.setInBook(bookTitle1);
272
		section1.setTitle("SecTitle");
273
		section1.setPages("22-33");
274
		IBookSection section2 = ReferenceFactory.newBookSection();
275
		section2.setInBook(bookTitle1);
276
		section2.setTitle("SecTitle");
277
		section2.setPages("22-33");
278

    
279

    
280
		IMatchStrategyEqual bookSectionMatchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
281
		Assert.assertTrue("Equal BookSections should match", bookSectionMatchStrategy.invoke(section1, section2).isSuccessful());
282
		section2.setInBook(bookTitle2);
283
		Assert.assertTrue("Matching books should result in matching book sections", bookSectionMatchStrategy.invoke(section1, section2).isSuccessful());
284
		bookTitle2.setPages("xx");
285
		Assert.assertFalse("Sections with differing books should not match", bookSectionMatchStrategy.invoke(section1, section2).isSuccessful());
286
		//restore
287
		bookTitle2.setPages(null);
288
		Assert.assertTrue("Matching books should result in matching book sections", bookSectionMatchStrategy.invoke(section1, section2).isSuccessful());
289
		printSeries2.setTitle("A new series title");
290
		IMatchStrategyEqual printSeriesMatchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
291
		Assert.assertFalse("Print series with differing titles should not match", printSeriesMatchStrategy.invoke(printSeries1, printSeries2).isSuccessful());
292
		bookTitle1.setInSeries(printSeries1);
293
		bookTitle2.setInSeries(printSeries2);
294
		Assert.assertFalse("Books with not matching in series should not match", matchStrategy.invoke(bookTitle1, bookTitle2).isSuccessful());
295
		Assert.assertFalse("Sections with differing print series should not match", bookSectionMatchStrategy.invoke(section1, section2).isSuccessful());
296

    
297
		//Authorship
298
		Person person1 = Person.NewTitledInstance("person");
299
		Person person2 = Person.NewTitledInstance("person");
300

    
301
		person1.setPrefix("pre1");
302
		person2.setPrefix("pre2");
303
		book2= ((Reference) book1).clone();
304

    
305
		Assert.assertTrue("Equal books should match", matchStrategy.invoke(book1, book2).isSuccessful());
306

    
307
		book1.setAuthorship(person1);
308
		book2.setAuthorship(person1);
309
		Assert.assertTrue("Books with same author should match", matchStrategy.invoke(book1, book2).isSuccessful());
310

    
311
		book2.setAuthorship(person2);
312
		Assert.assertFalse("Books with different authors should not match", matchStrategy.invoke(book1, book2).isSuccessful());
313

    
314
		person2.setPrefix("pre1");
315
		Assert.assertTrue("Books with matching authors should not match", matchStrategy.invoke(book1, book2).isSuccessful());
316
	}
317

    
318

    
319
	/**
320
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#invoke(eu.etaxonomy.cdm.strategy.match.IMergable, eu.etaxonomy.cdm.strategy.match.IMergable)}.
321
	 * @throws MatchException
322
	 */
323
	@Test
324
	public void testInvokeTaxonNames() throws MatchException {
325
		matchStrategy = DefaultMatchStrategy.NewInstance(TaxonName.class);
326

    
327
		TaxonName botName1 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
328
		TaxonName botName2 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
329
		IBotanicalName botName3 = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
330

    
331
		Assert.assertFalse("Names without title should not match", matchStrategy.invoke(botName1, botName2).isSuccessful());
332

    
333
		botName1.setGenusOrUninomial("Genus1");
334
		botName1.setSpecificEpithet("species1");
335

    
336
		botName2.setGenusOrUninomial("Genus1");
337
		botName2.setSpecificEpithet("species1");
338
		Assert.assertTrue("Similar names with titles set should match", matchStrategy.invoke(botName1, botName2).isSuccessful());
339

    
340

    
341
		botName1.setAnamorphic(true);
342
		botName2.setAnamorphic(false);
343
		Assert.assertFalse("Similar names with differing boolean marker values should not match", matchStrategy.invoke(botName1, botName2).isSuccessful());
344
		botName2.setAnamorphic(true);
345
		Assert.assertTrue("Similar names with same boolean marker values should match", matchStrategy.invoke(botName1, botName2).isSuccessful());
346

    
347

    
348

    
349
//		//name relations
350
//		botName2.addBasionym(botName3, book1, "p.22", null);
351
//		Specimen specimen1 = Specimen.NewInstance();
352
//		botName2.addSpecimenTypeDesignation(specimen1, SpecimenTypeDesignationStatus.HOLOTYPE(), book2, "p.56", "originalNameString", false, true);
353
//
354
//		//descriptions
355
//		TaxonNameDescription description1 = TaxonNameDescription.NewInstance();
356
//		botName1.addDescription(description1);
357
//		TaxonNameDescription description2 = TaxonNameDescription.NewInstance();
358
//		botName2.addDescription(description2);
359

    
360
		//authors
361
		Team team1 = Team.NewInstance();
362
		Team team2 = Team.NewInstance();
363
		botName1.setCombinationAuthorship(team1);
364
		botName2.setCombinationAuthorship(team2);
365
		Assert.assertTrue("Similar teams should match", DefaultMatchStrategy.NewInstance(Team.class).invoke(team1, team2).isSuccessful());
366
		Assert.assertTrue("Similar names with matching authors should match", matchStrategy.invoke(botName1, botName2).isSuccessful());
367

    
368
	}
369

    
370
	/**
371
	 * Test method for {@link eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy#invoke(IMatchable, IMatchable), eu.etaxonomy.cdm.strategy.match.IMatchable)}.
372
	 * @throws MatchException
373
	 */
374
	@Test
375
	public void testInvokeAgents() throws MatchException {
376
		IMatchStrategyEqual matchStrategy = DefaultMatchStrategy.NewInstance(Team.class);
377

    
378
		Team team1 = Team.NewInstance();
379
		Team team2 = Team.NewInstance();
380
		Team team3 = Team.NewInstance();
381

    
382
		Assert.assertTrue("Teams should match", matchStrategy.invoke(team1, team2).isSuccessful());
383
		Assert.assertTrue("Teams should match", matchStrategy.invoke(team1, team3).isSuccessful());
384

    
385
		String street1 = "Strasse1";
386
		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)));
387
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
388
		Assert.assertTrue("Contacts should be ignoredin default match strategy", matchStrategy.invoke(team1, team2).isSuccessful());
389

    
390
		team1.setNomenclaturalTitle("nomTitle1");
391
		team2.setNomenclaturalTitle("nomTitle2");
392
		Assert.assertFalse("Agents with differing nomenclatural titles should not match", matchStrategy.invoke(team1, team2).isSuccessful());
393
		//restore
394
		team2.setNomenclaturalTitle("nomTitle1");
395
		Assert.assertTrue("Agents with equal nomenclatural titles should match", matchStrategy.invoke(team1, team2).isSuccessful());
396

    
397

    
398
		Person person1 = Person.NewTitledInstance("person1");
399
		person1.setProtectedTitleCache(true);
400
		Person person2 = Person.NewTitledInstance("person2");
401
		person2.setProtectedTitleCache(true);
402
		Person person3 = Person.NewTitledInstance("person3");
403
		person3.setProtectedTitleCache(true);
404

    
405
		team1.addTeamMember(person1);
406
		team2.addTeamMember(person1);
407
		Assert.assertTrue("Teams with same team members should match", matchStrategy.invoke(team1, team2).isSuccessful());
408

    
409
		team1.addTeamMember(person2);
410
		Assert.assertFalse("Teams with differing team members should not match", matchStrategy.invoke(team1, team2).isSuccessful());
411
		team2.addTeamMember(person2);
412
		Assert.assertTrue("Teams with same team members should match", matchStrategy.invoke(team1, team2).isSuccessful());
413
		team2.removeTeamMember(person2);
414
		person3.setPrefix("pre3");
415
		team2.addTeamMember(person3);
416
		Assert.assertFalse("Teams with differing team members should not match", matchStrategy.invoke(team1, team2).isSuccessful());
417
		person3.setTitleCache(person2.getTitleCache(),true);
418
		person2.setPrefix("pre3");
419
		Assert.assertTrue("Teams with matching members in right order should match", matchStrategy.invoke(team1, team2).isSuccessful());
420
		team2.removeTeamMember(person1);
421
		team2.addTeamMember(person1);
422
		Assert.assertFalse("Teams with matching members in wrong order should not match", matchStrategy.invoke(team1, team2).isSuccessful());
423

    
424
	}
425

    
426
	@Test
427
	public void testInvokeTeamOrPersonBase(){
428

    
429
	}
430

    
431
}
(1-1/2)