Project

General

Profile

Download (45 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10

    
11
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
12

    
13
import static org.junit.Assert.assertEquals;
14

    
15
import java.io.FileNotFoundException;
16
import java.net.URI;
17
import java.util.ArrayList;
18
import java.util.Arrays;
19
import java.util.List;
20
import java.util.Map;
21
import java.util.Set;
22
import java.util.UUID;
23

    
24
import org.apache.log4j.Logger;
25
import org.hibernate.MappingException;
26
import org.hibernate.internal.SessionFactoryImpl;
27
import org.hibernate.persister.collection.CollectionPersister;
28
import org.hibernate.stat.Statistics;
29
import org.hibernate.type.Type;
30
import org.junit.Assert;
31
import org.junit.Before;
32
import org.junit.Test;
33
import org.unitils.dbunit.annotation.DataSet;
34
import org.unitils.spring.annotation.SpringBeanByType;
35

    
36
import eu.etaxonomy.cdm.model.agent.Address;
37
import eu.etaxonomy.cdm.model.agent.AgentBase;
38
import eu.etaxonomy.cdm.model.agent.Contact;
39
import eu.etaxonomy.cdm.model.agent.Institution;
40
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
41
import eu.etaxonomy.cdm.model.agent.Person;
42
import eu.etaxonomy.cdm.model.agent.Team;
43
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
44
import eu.etaxonomy.cdm.model.common.Annotation;
45
import eu.etaxonomy.cdm.model.common.AnnotationType;
46
import eu.etaxonomy.cdm.model.common.CdmBase;
47
import eu.etaxonomy.cdm.model.common.Credit;
48
import eu.etaxonomy.cdm.model.common.DefinedTerm;
49
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
50
import eu.etaxonomy.cdm.model.common.Extension;
51
import eu.etaxonomy.cdm.model.common.ExtensionType;
52
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
53
import eu.etaxonomy.cdm.model.common.Group;
54
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
55
import eu.etaxonomy.cdm.model.common.Identifier;
56
import eu.etaxonomy.cdm.model.common.IntextReference;
57
import eu.etaxonomy.cdm.model.common.LSIDAuthority;
58
import eu.etaxonomy.cdm.model.common.Language;
59
import eu.etaxonomy.cdm.model.common.LanguageString;
60
import eu.etaxonomy.cdm.model.common.Marker;
61
import eu.etaxonomy.cdm.model.common.MarkerType;
62
import eu.etaxonomy.cdm.model.common.OrderedTermBase;
63
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
64
import eu.etaxonomy.cdm.model.common.OriginalSourceBase;
65
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
66
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
67
import eu.etaxonomy.cdm.model.common.Representation;
68
import eu.etaxonomy.cdm.model.common.TermVocabulary;
69
import eu.etaxonomy.cdm.model.common.TimePeriod;
70
import eu.etaxonomy.cdm.model.common.User;
71
import eu.etaxonomy.cdm.model.description.CategoricalData;
72
import eu.etaxonomy.cdm.model.description.CommonTaxonName;
73
import eu.etaxonomy.cdm.model.description.DescriptionBase;
74
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
75
import eu.etaxonomy.cdm.model.description.Distribution;
76
import eu.etaxonomy.cdm.model.description.Feature;
77
import eu.etaxonomy.cdm.model.description.FeatureNode;
78
import eu.etaxonomy.cdm.model.description.FeatureTree;
79
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
80
import eu.etaxonomy.cdm.model.description.MeasurementUnit;
81
import eu.etaxonomy.cdm.model.description.MediaKey;
82
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
83
import eu.etaxonomy.cdm.model.description.QuantitativeData;
84
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
85
import eu.etaxonomy.cdm.model.description.State;
86
import eu.etaxonomy.cdm.model.description.StateData;
87
import eu.etaxonomy.cdm.model.description.StatisticalMeasure;
88
import eu.etaxonomy.cdm.model.description.StatisticalMeasurementValue;
89
import eu.etaxonomy.cdm.model.description.TaxonDescription;
90
import eu.etaxonomy.cdm.model.description.TaxonInteraction;
91
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
92
import eu.etaxonomy.cdm.model.description.TextData;
93
import eu.etaxonomy.cdm.model.description.TextFormat;
94
import eu.etaxonomy.cdm.model.location.Country;
95
import eu.etaxonomy.cdm.model.location.NamedArea;
96
import eu.etaxonomy.cdm.model.location.NamedAreaLevel;
97
import eu.etaxonomy.cdm.model.location.NamedAreaType;
98
import eu.etaxonomy.cdm.model.location.Point;
99
import eu.etaxonomy.cdm.model.location.ReferenceSystem;
100
import eu.etaxonomy.cdm.model.media.AudioFile;
101
import eu.etaxonomy.cdm.model.media.ImageFile;
102
import eu.etaxonomy.cdm.model.media.Media;
103
import eu.etaxonomy.cdm.model.media.MediaRepresentation;
104
import eu.etaxonomy.cdm.model.media.MediaRepresentationPart;
105
import eu.etaxonomy.cdm.model.media.MovieFile;
106
import eu.etaxonomy.cdm.model.media.Rights;
107
import eu.etaxonomy.cdm.model.media.RightsType;
108
import eu.etaxonomy.cdm.model.metadata.CdmPreference;
109
import eu.etaxonomy.cdm.model.molecular.Amplification;
110
import eu.etaxonomy.cdm.model.molecular.DnaSample;
111
import eu.etaxonomy.cdm.model.molecular.PhylogeneticTree;
112
import eu.etaxonomy.cdm.model.molecular.Primer;
113
import eu.etaxonomy.cdm.model.molecular.Sequence;
114
import eu.etaxonomy.cdm.model.molecular.SingleRead;
115
import eu.etaxonomy.cdm.model.molecular.SingleReadAlignment;
116
import eu.etaxonomy.cdm.model.name.BacterialName;
117
import eu.etaxonomy.cdm.model.name.BotanicalName;
118
import eu.etaxonomy.cdm.model.name.CultivarPlantName;
119
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
120
import eu.etaxonomy.cdm.model.name.HybridRelationship;
121
import eu.etaxonomy.cdm.model.name.HybridRelationshipType;
122
import eu.etaxonomy.cdm.model.name.NameRelationship;
123
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
124
import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
125
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
126
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
127
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
128
import eu.etaxonomy.cdm.model.name.NonViralName;
129
import eu.etaxonomy.cdm.model.name.Rank;
130
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
131
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
132
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
133
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
134
import eu.etaxonomy.cdm.model.name.ViralName;
135
import eu.etaxonomy.cdm.model.name.ZoologicalName;
136
import eu.etaxonomy.cdm.model.occurrence.Collection;
137
import eu.etaxonomy.cdm.model.occurrence.DerivationEvent;
138
import eu.etaxonomy.cdm.model.occurrence.DerivationEventType;
139
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
140
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
141
import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
142
import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
143
import eu.etaxonomy.cdm.model.occurrence.PreservationMethod;
144
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
145
import eu.etaxonomy.cdm.model.reference.IBook;
146
import eu.etaxonomy.cdm.model.reference.IBookSection;
147
import eu.etaxonomy.cdm.model.reference.Reference;
148
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
149
import eu.etaxonomy.cdm.model.taxon.Classification;
150
import eu.etaxonomy.cdm.model.taxon.Synonym;
151
import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
152
import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
153
import eu.etaxonomy.cdm.model.taxon.Taxon;
154
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
155
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
156
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
157
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
158
import eu.etaxonomy.cdm.model.view.AuditEvent;
159
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
160
import eu.etaxonomy.cdm.persistence.dao.common.ICdmGenericDao;
161
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
162
import eu.etaxonomy.cdm.persistence.dao.occurrence.IOccurrenceDao;
163
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonDao;
164
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
165
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
166
import eu.etaxonomy.cdm.strategy.match.MatchException;
167
import eu.etaxonomy.cdm.strategy.merge.DefaultMergeStrategy;
168
import eu.etaxonomy.cdm.strategy.merge.IMergeStrategy;
169
import eu.etaxonomy.cdm.strategy.merge.MergeException;
170
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
171

    
172
/**
173
 * @author a.mueller
174
 * @created 27.07.2009
175
 */
176
public class CdmGenericDaoImplTest extends CdmTransactionalIntegrationTest {
177
	private static final Logger logger = Logger.getLogger(CdmGenericDaoImplTest.class);
178

    
179
	@SpringBeanByType
180
	private ICdmGenericDao cdmGenericDao;
181

    
182
	@SpringBeanByType
183
	private ITaxonDao taxonDao;
184

    
185
	@SpringBeanByType
186
	private IOccurrenceDao occurrenceDao;
187

    
188
	@SpringBeanByType
189
	private ITaxonNameDao nameDao;
190

    
191
	@SpringBeanByType
192
	private IAgentDao agentDao;
193

    
194
	/**
195
	 * @throws java.lang.Exception
196
	 */
197
	@Before
198
	public void setUp() throws Exception {}
199

    
200
// ***************** TESTS **************************************************
201

    
202
	@Test
203
	public void testDelete(){
204
		Reference<?> ref1 = ReferenceFactory.newBook();
205
		Reference<?> ref2 = ReferenceFactory.newBook();
206
		Annotation annotation = Annotation.NewInstance("Anno1", null);
207
		ref1.addAnnotation(annotation);
208
		cdmGenericDao.saveOrUpdate(ref1);
209
		cdmGenericDao.saveOrUpdate(ref2);
210
		taxonDao.flush();
211
		try {
212
			cdmGenericDao.merge(ref2, ref1, null);
213
			taxonDao.flush();
214
		} catch (MergeException e) {
215
			Assert.fail();
216
		}
217
		cdmGenericDao.delete(ref1);
218
		taxonDao.flush();
219
	}
220

    
221

    
222
	/**
223
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getCdmBasesByFieldAndClass(java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.common.CdmBase)}.
224
	 */
225
	@Test
226
	public void testGetCdmBasesByFieldAndClass() {
227
		logger.warn("Not yet implemented");
228
	}
229

    
230
	/**
231
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getCdmBasesWithItemInCollection(java.lang.Class, java.lang.Class, java.lang.String, eu.etaxonomy.cdm.model.common.CdmBase)}.
232
	 */
233
	@Test
234
	public void testGetCdmBasesWithItemInCollection() {
235
		logger.warn("Not yet implemented");
236
	}
237

    
238
	/**
239
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getAllPersistedClasses(boolean)}.
240
	 */
241
	@Test
242
	public void testGetAllPersistedClasses() {
243
		Class<?>[] existingClassesArray = {
244
				Address.class,
245
				AgentBase.class,
246
				Institution.class,
247
				InstitutionalMembership.class,
248
				Person.class,
249
				Team.class,
250
				TeamOrPersonBase.class,
251
				Annotation.class,
252
				AnnotationType.class,
253
				Credit.class,
254
				DefinedTermBase.class,
255
				Extension.class,
256
				ExtensionType.class,
257
				GrantedAuthorityImpl.class,
258
				Group.class,
259
				IdentifiableSource.class,
260
				Identifier.class,
261
				IntextReference.class,
262
				Language.class,
263
				LanguageString.class,
264
				LSIDAuthority.class,
265
				Marker.class,
266
				MarkerType.class,
267
				OrderedTermBase.class,
268
				OrderedTermVocabulary.class,
269
				OriginalSourceBase.class,
270
				RelationshipTermBase.class,
271
				Representation.class,
272
				TermVocabulary.class,
273
				User.class,
274
				DefinedTerm.class,
275

    
276
				CategoricalData.class,
277
				CommonTaxonName.class,
278
				DescriptionBase.class,
279
				DescriptionElementBase.class,
280
				Distribution.class,
281
				Feature.class,
282
				FeatureNode.class,
283
				FeatureTree.class,
284
				MediaKey.class,
285
				IndividualsAssociation.class,
286
				MeasurementUnit.class,
287
				PresenceAbsenceTerm.class,
288
				QuantitativeData.class,
289
				SpecimenDescription.class,
290
				State.class,
291
				StateData.class,
292
				StatisticalMeasure.class,
293
				StatisticalMeasurementValue.class,
294
				TaxonDescription.class,
295
				TaxonInteraction.class,
296
				TaxonNameDescription.class,
297
				TextData.class,
298
				TextFormat.class,
299
				NamedArea.class,
300
				NamedAreaLevel.class,
301
				NamedAreaType.class,
302
				ReferenceSystem.class,
303
				Country.class,
304
				AudioFile.class,
305
				ImageFile.class,
306
				Media.class,
307
				MediaRepresentation.class,
308
				MediaRepresentationPart.class,
309
				MovieFile.class,
310
				Rights.class,
311
				RightsType.class,
312
				Amplification.class,
313
				DnaSample.class,
314
				SingleRead.class,
315
				SingleReadAlignment.class,
316
				Primer.class,
317
				Sequence.class,
318
				PhylogeneticTree.class,
319
				Sequence.class,
320
				BacterialName.class,
321
				BotanicalName.class,
322
				CultivarPlantName.class,
323
				HomotypicalGroup.class,
324
				HybridRelationship.class,
325
				HybridRelationshipType.class,
326
				NameRelationship.class,
327
				NameRelationshipType.class,
328
				NameTypeDesignation.class,
329
				NameTypeDesignationStatus.class,
330
				NomenclaturalStatus.class,
331
				NomenclaturalStatusType.class,
332
				NonViralName.class,
333
				Rank.class,
334
				SpecimenTypeDesignation.class,
335
				SpecimenTypeDesignationStatus.class,
336
				TaxonNameBase.class,
337
				TypeDesignationBase.class,
338
				ViralName.class,
339
				ZoologicalName.class,
340
				Collection.class,
341
				DerivationEvent.class,
342
				DerivationEventType.class,
343
				DerivedUnit.class,
344
				DeterminationEvent.class,
345
				FieldUnit.class,
346
				GatheringEvent.class,
347
				PreservationMethod.class,
348
				SpecimenOrObservationBase.class,
349
				Reference.class,
350
				Synonym.class,
351
				SynonymRelationship.class,
352
				SynonymRelationshipType.class,
353
				Taxon.class,
354
				TaxonBase.class,
355
				TaxonNode.class,
356
				Classification.class,
357
				TaxonRelationship.class,
358
				TaxonRelationshipType.class ,
359
				//Contact.class,  //these are embedabble classes
360
				//LSID.class,
361
				//Point.class,
362
				//NomenclaturalCode.class,
363
		}	;
364
		List<Class<?>> existingClassesList = new ArrayList<Class<?>>();
365
		existingClassesList.addAll(Arrays.asList(existingClassesArray));
366
		boolean includeAbstractClasses = true;
367
		Set<Class<? extends CdmBase>> foundClasses = cdmGenericDao.getAllPersistedClasses(includeAbstractClasses);
368

    
369
		//for debugging only
370
		//		for (Class existingClass : existingClassesList){
371
		//			if (! foundClasses.contains(existingClass)){
372
		//				logger.warn("Class not found: " + existingClass.getCanonicalName());
373
		//			}
374
		//		}
375

    
376
		//All classes must be found
377
		Assert.assertTrue("all classes must be found by getAllCdmClasses() method", foundClasses.containsAll(existingClassesList));
378

    
379

    
380
		//No extra classes must be found
381
		for (Class<?> clazz : foundClasses){
382
			if (! CdmBase.class.isAssignableFrom(clazz)&& !( AuditEvent.class == clazz) && !( CdmPreference.class == clazz)  ){ //OLD: && !( LSID.class == clazz)&& !( NomenclaturalCode.class == clazz) && !( Point.class == clazz) && !( Modifier.class == clazz) && !( Contact.class == clazz)
383
				Assert.fail("Class " + clazz.getName() + " is not assignable from CdmBase");
384
			}
385
		}
386

    
387
		includeAbstractClasses = false;
388
		Set<Class<? extends CdmBase>> noAbstractClasses = cdmGenericDao.getAllPersistedClasses(includeAbstractClasses);
389
		Class<?> abstractClassToTest = TaxonNameBase.class;
390
		Assert.assertFalse("Abstract class " + abstractClassToTest.getName() + " may not be in set ", noAbstractClasses.contains(abstractClassToTest));
391
	}
392

    
393
	/**
394
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getReferencingObjects(CdmBase)}.
395
	 */
396
	@Test
397
	public void testGetReferencingObjectsCdmBase() {
398
		BotanicalName name = BotanicalName.NewInstance(Rank.SPECIES());
399
		name.setTitleCache("A name", true);
400
		Reference<?> ref1 = ReferenceFactory.newArticle();
401
		Taxon taxon = Taxon.NewInstance(name, ref1);
402
		Person author = Person.NewInstance();
403
		author.setTitleCache("Author", true);
404
		ref1.addAnnotation(Annotation.NewInstance("A1", Language.DEFAULT()));
405
		ref1.setAuthorship(author);
406
		name.setBasionymAuthorship(author);
407

    
408
		name.setNomenclaturalReference(ref1);
409

    
410
		taxonDao.save(taxon);
411
//		UUID uuid = UUID.fromString("613980ac-9bd5-43b9-a374-d71e1794688f");
412
//		Reference ref1 = referenceService.findByUuid(uuid);
413

    
414

    
415
		Set<CdmBase> referencedObjects = cdmGenericDao.getReferencingObjects(ref1);
416
		System.out.println("############## RESULT ###################");
417
		for (CdmBase obj: referencedObjects){
418
			System.out.println("Object1: " + obj.getClass().getSimpleName() + " - " + obj);
419
		}
420
		assertEquals(3, referencedObjects.size());
421
		System.out.println("############## ENDE ###################");
422

    
423
//		UUID uuidAuthor = UUID.fromString("4ce66544-a5a3-4601-ab0b-1f0a1338327b");
424
//		AgentBase author = agentService.findByUuid(uuidAuthor);
425

    
426
		referencedObjects = cdmGenericDao.getReferencingObjects(author);
427
		System.out.println("############## RESULT ###################");
428
		for (CdmBase obj: referencedObjects){
429
			System.out.println("Object2: " + obj.getClass().getSimpleName() + " - " + obj);
430
		}
431
		assertEquals(2, referencedObjects.size());
432
		System.out.println("############## ENDE ###################");
433
	}
434

    
435
	/**
436
	 * 2nd test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getReferencingObjects(CdmBase)}.
437
	 *
438
	 */
439
	@Test
440
	@DataSet
441
	public final void testGetReferencingObjects2() {
442
//		SpecimenDescription desc1 = SpecimenDescription.NewInstance();
443
//		desc1.setTitleCache("desc1");
444
//		SpecimenDescription desc2 = SpecimenDescription.NewInstance();
445
//		desc2.setTitleCache("desc2");
446
//
447
//		SpecimenOrObservationBase spec1 = Specimen.NewInstance();
448
//
449
//		desc1.addDescribedSpecimenOrObservation(spec1);
450
//		//Taxon taxon = Taxon.NewInstance(taxonNameBase, sec)
451
//		spec1.addDescription(desc2);
452
//
453
//		occurrenceService.save(spec1);
454

    
455
		UUID uuidSpec = UUID.fromString("41539e9c-3764-4f14-9712-2d07d00c8e4c");
456
		SpecimenOrObservationBase<?> spec1 = occurrenceDao.findByUuid(uuidSpec);
457

    
458

    
459
		Set<CdmBase> referencingObjects = cdmGenericDao.getReferencingObjects(spec1);
460
//		System.out.println("############## RESULT ###################");
461
//		for (CdmBase obj: referencingObjects){
462
//			System.out.println("Object: " + obj.getClass().getSimpleName() + " - " + obj);
463
//		}
464
//		System.out.println("############## ENDE ###################");
465
		assertEquals("Number of referencing objects must be 2.", 2, referencingObjects.size());
466

    
467
	}
468

    
469
	/**
470
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
471
	 * @throws MergeException
472
	 */
473
	@Test
474
//	@Ignore
475
	public void testMergeCdmBaseReferenceAndIdentifiable() throws MergeException {
476

    
477
		BotanicalName name1 = BotanicalName.NewInstance(Rank.SPECIES());
478
		name1.setTitleCache("BotanicalName1", true);
479

    
480
		BotanicalName name2 = BotanicalName.NewInstance(Rank.SPECIES());
481
		name2.setTitleCache("BotanicalName2", true);
482

    
483
		ZoologicalName zooName1 = ZoologicalName.NewInstance(Rank.SPECIES());
484
		name1.setTitleCache("ZoologicalName1", true);
485

    
486
		Reference<?> article1 = ReferenceFactory.newArticle();
487
		Reference<?> article2 = ReferenceFactory.newArticle();
488

    
489

    
490
		name1.setNomenclaturalReference(article1);
491
		name2.setNomenclaturalReference(article2);
492

    
493

    
494
		Taxon taxon1 = Taxon.NewInstance(name1, article1);
495
		Taxon taxon2 = Taxon.NewInstance(name2, article2);
496

    
497

    
498
//		Person author = Person.NewInstance();
499
//		author.setTitleCache("Author");
500
		Annotation annotation1 = Annotation.NewInstance("A1", Language.DEFAULT());
501
		Annotation annotation2 = Annotation.NewInstance("A2", Language.DEFAULT());
502

    
503
		article1.addAnnotation(annotation1);
504
		article2.addAnnotation(annotation2);
505

    
506
		Marker marker1 = Marker.NewInstance(MarkerType.COMPLETE(), false);
507
		Marker marker2 = Marker.NewInstance(MarkerType.IMPORTED(), false);
508

    
509
		article1.addMarker(marker1);
510
		article2.addMarker(marker2);
511

    
512
		Rights rights1 = Rights.NewInstance();
513
		Rights rights2 = Rights.NewInstance();
514

    
515
		article1.addRights(rights1);
516
		article2.addRights(rights2);
517

    
518
		Credit credit1 = Credit.NewInstance(Team.NewInstance(), "credit1");
519
		Credit credit2 = Credit.NewInstance(Team.NewInstance(), "credit2");
520

    
521
		article1.addCredit(credit1);
522
		article2.addCredit(credit2);
523

    
524
		Extension extension1 = Extension.NewInstance();
525
		Extension extension2 = Extension.NewInstance();
526

    
527
		article1.addExtension(extension1);
528
		article2.addExtension(extension2);
529

    
530
		IdentifiableSource source1 = IdentifiableSource.NewInstance(OriginalSourceType.Unknown);
531
		IdentifiableSource source2 = IdentifiableSource.NewInstance(OriginalSourceType.Unknown);
532

    
533
		article1.addSource(source1);
534
		article2.addSource(source2);
535

    
536
		Media media1 = Media.NewInstance();
537
		Media media2 = Media.NewInstance();
538

    
539
		article1.addMedia(media1);
540
		article2.addMedia(media2);
541

    
542
//		ref1.setAuthorship(author);
543
//		name1.setBasionymAuthorship(author);
544

    
545
		name1.setNomenclaturalReference(article1);
546

    
547
		nameDao.save(name1);
548
		nameDao.save(name2);
549
		nameDao.save(zooName1);
550

    
551
		TaxonDescription taxDesc = TaxonDescription.NewInstance(taxon1);
552
		taxDesc.setTitleCache("taxDesc", true);
553
		taxDesc.addSource(OriginalSourceType.Unknown, null, null, article2, null);
554

    
555
		taxonDao.save(taxon1);
556

    
557
		//unidircetional reference to the merged object should be redirected
558
		cdmGenericDao.merge(article1, article2, null);
559
		Assert.assertEquals("Name2 must have article 1 as new nomRef", article1 ,name2.getNomenclaturalReference());
560
		//TODO microCitations!! -> warning
561

    
562
		//Annotations
563
		Assert.assertEquals("Annotation number should be 2 (1 from each of the merged objects)", 2, article1.getAnnotations().size());
564

    
565
		//Marker
566
		Assert.assertEquals("Marker number should be 2 (1 from each of the merged objects)", 2, article1.getMarkers().size());
567

    
568
		//Rights
569
		Assert.assertEquals("Rights number should be 2 (1 from each of the merged objects)", 2, article1.getRights().size());
570

    
571
		//Credits
572
		Assert.assertEquals("Credits number should be 2 (1 from each of the merged objects)", 2, article1.getCredits().size());
573

    
574
		//Extensions
575
		Assert.assertEquals("Extensions number should be 2 (1 from each of the merged objects)", 2, article1.getExtensions().size());
576

    
577
		//Sources
578
		Assert.assertEquals("Sources number should be 2 (1 from each of the merged objects)", 2, article1.getSources().size());
579

    
580
		//Media
581
		Assert.assertEquals("Media number should be 2 (1 from each of the merged objects)", 2, article1.getMedia().size());
582

    
583
		//Description sources
584
		Assert.assertEquals("Number of sources for taxon description must be 1", 1, taxDesc.getSources().size());
585
		Assert.assertEquals("Taxon description must have article1 as source", taxDesc.getSources().iterator().next().getCitation(),article1);
586

    
587
		//test exceptions
588
		testMergeExceptions(name1, name2, taxon1, zooName1);
589

    
590

    
591
		//FIXME TO BE IMPLEMENTED
592
		//current defalt implementation for rights, credits and media is ADD_CLONE and therefore the below tests don't work
593
		//TODO is this the wanted default behaviour?
594
//		Assert.assertTrue("Rights2 must be contained in the rights", article1.getRights().contains(rights2));
595
//		Assert.assertTrue("Credits2 must be contained in the credits", article1.getCredits().contains(credit2));
596
//		Assert.assertTrue("Media2 must be contained in the media", article1.getMedia().contains(media2));
597

    
598
	}
599

    
600
	/**
601
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
602
	 * @throws MergeException
603
	 */
604
	@Test
605
//	@Ignore
606
	public void testMergeTaxonNameAndTaxon() throws MergeException {
607
		BotanicalName name1 = BotanicalName.NewInstance(Rank.SPECIES());
608
		name1.setTitleCache("BotanicalName1", true);
609

    
610
		BotanicalName name2 = BotanicalName.NewInstance(Rank.SPECIES());
611
		name2.setTitleCache("BotanicalName2", true);
612

    
613
		BotanicalName name3 = BotanicalName.NewInstance(Rank.SPECIES());
614
		name3.setTitleCache("BotanicalName3", true);
615

    
616
		Reference<?> database = ReferenceFactory.newDatabase();
617

    
618
		Taxon taxon1 = Taxon.NewInstance(name1, database);
619
		Taxon taxon2 = Taxon.NewInstance(name2, database);
620
		Taxon taxon3 = Taxon.NewInstance(name3, database);
621

    
622
		taxonDao.save(taxon1);
623
		taxonDao.save(taxon2);
624
		taxonDao.save(taxon3);
625

    
626
		cdmGenericDao.merge(name1, name2, null);
627
		Assert.assertEquals("Name1 must have 2 taxa attached now.", 2 ,name1.getTaxonBases().size());
628
		Assert.assertEquals("Taxon2 must have name1 as new name.", name1 ,taxon2.getName());
629

    
630
//TODO
631
//		cdmGenericDao.merge(taxon1, taxon3, null);
632
//		Assert.assertEquals("Name1 must have 3 taxa attached now.", 3 ,name1.getTaxonBases().size());
633

    
634

    
635
	}
636

    
637
	/**
638
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
639
	 * @throws MergeException
640
	 */
641
	@Test
642
	public void testMergeAuthors() throws MergeException {
643

    
644
		BotanicalName name1 = BotanicalName.NewInstance(Rank.SPECIES());
645
		name1.setTitleCache("BotanicalName1", true);
646

    
647
		BotanicalName name2 = BotanicalName.NewInstance(Rank.SPECIES());
648
		name2.setTitleCache("BotanicalName2", true);
649

    
650
		IBook book1 = ReferenceFactory.newBook();
651
		IBook book2 = ReferenceFactory.newBook();
652

    
653
		Team team1 = Team.NewInstance();
654
		Team team2 = Team.NewInstance();
655
		Team team3 = Team.NewInstance();
656
		team1.setTitleCache("team1", true);
657
		team2.setTitleCache("team2", true);
658
		team3.setTitleCache("team3", true);
659

    
660
		Person person1 = Person.NewTitledInstance("person1");
661
		Person person2 = Person.NewTitledInstance("person2");
662
		Person person3 = Person.NewTitledInstance("person3");
663

    
664
		team1.setNomenclaturalTitle("T.1");
665
		String street1 = "Strasse1";
666
		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)));
667
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
668
		String street3 = "Street3";
669
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
670
		String emailAddress1 = "Email1";
671
		team1.addEmailAddress(emailAddress1);
672

    
673
		team2.addTeamMember(person1);
674
		team2.addTeamMember(person2);
675
		String emailAddress2 = "Email2";
676
		team2.addEmailAddress(emailAddress2);
677

    
678
		team3.addTeamMember(person3);
679
		team3.addEmailAddress("emailAddress3");
680

    
681
		book1.setAuthorship(team2);
682
		book2.setAuthorship(team3);
683

    
684
		Credit credit1 = Credit.NewInstance(team3, "credit1");
685
		book2.addCredit(credit1);
686

    
687
		agentDao.save(team1);
688
		agentDao.save(team2);
689
		agentDao.save(team3);
690
		cdmGenericDao.save((Reference<?>)book1);
691
		cdmGenericDao.save((Reference<?>)book2);
692

    
693
		cdmGenericDao.merge(team2, team3, null);
694

    
695
		Assert.assertSame("Author of book1 must be team2.", team2, book1.getAuthorship());
696
		Assert.assertSame("Author of book2 must be team2.", team2, book2.getAuthorship());
697
		Assert.assertSame("Agent of credit1 must be team2.", team2, credit1.getAgent());
698

    
699
		Assert.assertEquals("Team2 must have 3 persons as members.",3, team2.getTeamMembers().size());
700
		Assert.assertTrue("Team2 must have person3 as new member.", team2.getTeamMembers().contains(person3));
701
		Assert.assertSame("Team2 must have person3 as third member.",person3, team2.getTeamMembers().get(2));
702

    
703

    
704
		//Contact
705
		cdmGenericDao.merge(team2, team1, null);
706
		Contact team2Contact = team2.getContact();
707
		Assert.assertNotNull("team2Contact must not be null", team2Contact);
708
		Assert.assertNotNull("Addresses must not be null", team2Contact.getAddresses());
709
		Assert.assertEquals("Number of addresses must be 3", 3, team2Contact.getAddresses().size());
710
		Assert.assertEquals("Number of email addresses must be 4", 4, team2Contact.getEmailAddresses().size());
711

    
712
		boolean street1Exists = false;
713
		boolean street3Exists = false;
714
		boolean country1Exists = false;
715
		for  (Address address : team2Contact.getAddresses()){
716
			if (street1.equals(address.getStreet())){
717
				street1Exists = true;
718
			}
719
			if (street3.equals(address.getStreet())){
720
				street3Exists = true;
721
			}
722
			if (Country.ARGENTINAARGENTINEREPUBLIC() == address.getCountry()){
723
				country1Exists = true;
724
			}
725
		}
726
		Assert.assertTrue("Street1 must be one of the streets in team2's addresses", street1Exists);
727
		Assert.assertTrue("Street3 must be one of the streets in team2's addressesss", street3Exists);
728
		Assert.assertTrue("Argentina must be one of the countries in team2's addresses", country1Exists);
729

    
730
		//Person
731
		Institution institution1 = Institution.NewInstance();
732
		institution1.setTitleCache("inst1", true);
733
		Institution institution2 = Institution.NewInstance();
734
		institution2.setTitleCache("inst2", true);
735

    
736
		TimePeriod period1 = TimePeriod.NewInstance(2002, 2004);
737
		TimePeriod period2 = TimePeriod.NewInstance(2004, 2006);
738

    
739
		person1.addInstitutionalMembership(institution1, period1, "departement1", "role1");
740
		person2.addInstitutionalMembership(institution2, period2, "departement2", "role2");
741

    
742
		IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
743
		personMergeStrategy.invoke(person1, person2);
744

    
745
		Assert.assertEquals("Number of institutional memberships must be 2", 2, person1.getInstitutionalMemberships().size());
746
		for (InstitutionalMembership institutionalMembership : person1.getInstitutionalMemberships()){
747
			Assert.assertSame("Person of institutional memebership must be person1", person1, institutionalMembership.getPerson());
748
		}
749
	}
750

    
751
	/**
752
     * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
753
     *
754
     * Test for  http://dev.e-taxonomy.eu/trac/ticket/5652
755
     *
756
     * @throws MergeException
757
     */
758
    @Test
759
    public void testMergePersons() throws MergeException {
760
        Team team1 = Team.NewInstance();
761
        Team team2 = Team.NewInstance();
762
        Team team3 = Team.NewInstance();
763
        team1.setTitleCache("team1", true);
764
        team2.setTitleCache("team2", true);
765
        team3.setTitleCache("team3", true);
766

    
767
        Person person1a = Person.NewTitledInstance("person1a");
768
        Person person1b = Person.NewTitledInstance("person1b");
769
        Person person2 = Person.NewTitledInstance("person2");
770
        Person person3 = Person.NewTitledInstance("person3");
771

    
772
        team1.addTeamMember(person1a);
773
        team1.addTeamMember(person2);
774

    
775

    
776
        team2.addTeamMember(person2);
777
        team2.addTeamMember(person1a);
778
        team2.addTeamMember(person3);
779

    
780
        team3.addTeamMember(person3);
781

    
782
        agentDao.save(team1);
783
        agentDao.save(team2);
784
        agentDao.save(team3);
785
        agentDao.save(person1b);
786
        commitAndStartNewTransaction(null);
787

    
788
        IMergeStrategy personMergeStrategy = DefaultMergeStrategy.NewInstance(Person.class);
789
        cdmGenericDao.merge(person1b, person1a, personMergeStrategy);
790

    
791
        team1 = (Team)agentDao.load(team1.getUuid());
792
        team2 = (Team)agentDao.load(team2.getUuid());
793

    
794
        //order should not change and 1a should be replaced by 1b
795
        Assert.assertEquals("person1b", team1.getTeamMembers().get(0).getTitleCache());
796
        Assert.assertEquals("person2", team1.getTeamMembers().get(1).getTitleCache());
797

    
798
        Assert.assertEquals("person2", team2.getTeamMembers().get(0).getTitleCache());
799
        Assert.assertEquals("person1b", team2.getTeamMembers().get(1).getTitleCache());
800
        Assert.assertEquals("person3", team2.getTeamMembers().get(2).getTitleCache());
801

    
802
    }
803

    
804

    
805

    
806
	/**
807
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#merge(CdmBase, CdmBase)}.
808
	 * @throws MergeException
809
	 */
810
	@Test
811
	public void testReallocatePersonTeam() throws MergeException {
812

    
813
		BotanicalName name1 = BotanicalName.NewInstance(Rank.SPECIES());
814
		name1.setTitleCache("BotanicalName1", true);
815

    
816
		IBook book1 = ReferenceFactory.newBook();
817

    
818
		Team team1 = Team.NewInstance();
819
		Team team2 = Team.NewInstance();
820
		team1.setTitleCache("team1", true);
821
		team2.setTitleCache("team2", true);
822

    
823
		Person person1 = Person.NewTitledInstance("person1");
824
		Person person2 = Person.NewTitledInstance("person2");
825

    
826
		team1.setNomenclaturalTitle("T.1");
827
		String street1 = "Strasse1";
828
		person1.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)));
829
		team2.setContact(Contact.NewInstance("Street2", null, "London", null, null, null, null, "874599873", null, null, null));
830
		String street3 = "Street3";
831
		team2.addAddress(street3, null, null, null, null, null, Point.NewInstance(1.1, 2.2, null, 4));
832
		String emailAddress1 = "Email1";
833
		team1.addEmailAddress(emailAddress1);
834

    
835

    
836
		//FIXME
837
//		team2.addTeamMember(person1);
838
		team2.addTeamMember(person2);
839
		String emailAddress2 = "Email2";
840
		team2.addEmailAddress(emailAddress2);
841

    
842
		Credit credit1 = Credit.NewInstance(team2, "credit1");
843
		book1.addCredit(credit1);
844

    
845
		agentDao.save(team1);
846
		agentDao.save(team2);
847
		agentDao.save(person1);
848
		agentDao.save(person2);
849

    
850
		cdmGenericDao.save((Reference<?>)book1);
851

    
852
		//starting condition
853
		name1.setCombinationAuthorship(person1);
854
		Assert.assertEquals("Name1 should have person1 as combination author", person1, name1.getCombinationAuthorship());
855

    
856
		DefaultMergeStrategy strategy = DefaultMergeStrategy.NewInstance(TeamOrPersonBase.class);
857
//		strategy.setOnlyReallocateLinks(true);
858

    
859
		FieldUnit fieldUnit1 = FieldUnit.NewInstance();
860
		fieldUnit1.setPrimaryCollector(person1);
861
		cdmGenericDao.save(fieldUnit1);
862
		try {
863
			cdmGenericDao.merge(team2, person1, strategy);
864
			Assert.fail("We expect exception because fieldunit.primaryCollector is of type person");
865
		} catch (MergeException e) {
866
			if (! e.getMessage().contains("Object can not be merged into new object as it is referenced in a way that does not allow merging")){
867
				Assert.fail("The exception should be the one thrown by DeduplicationHelper.reallocateByHolder(...)");
868
			}
869
			fieldUnit1.setPrimaryCollector(null);  //clean up for next test
870
		} catch (Exception e) {
871
			Assert.fail("Unhandled exception during merge");
872
		}
873
		Assert.assertEquals("Name1 should still have person1 as combination author", person1, name1.getCombinationAuthorship());
874

    
875
		//test collections
876
		team1.addTeamMember(person1);
877
		try {
878
			cdmGenericDao.merge(team2, person1, strategy);
879
			Assert.fail("We expect exception because fieldunit.primaryCollector is of type person");
880
		} catch (MergeException e) {
881
			if (! e.getMessage().contains("Object can not be merged into new object as it is referenced in a way that does not allow merging")){
882
				Assert.fail("The exception should be the one thrown by DeduplicationHelper.reallocateByHolder(...)");
883
			}
884
			team1.removeTeamMember(person1); //clean up for next test
885
		} catch (Exception e) {
886
			Assert.fail("Unhandled exception during merge");
887
		}
888
		Assert.assertEquals("Name1 should still have person1 as combination author", person1, name1.getCombinationAuthorship());
889

    
890
		//test successful merge
891
		cdmGenericDao.save(name1);
892
		cdmGenericDao.merge(team2, person1, strategy);
893
		Assert.assertEquals("Name1 should have team2 as combination author now", team2, name1.getCombinationAuthorship());
894

    
895
	}
896

    
897

    
898
	private void testMergeExceptions(CdmBase name1, CdmBase name2, CdmBase taxon,CdmBase zooName1) throws MergeException{
899
		//
900
		try {
901
			cdmGenericDao.merge(name1, null, null);
902
			Assert.fail("Merging of 2 objects one or both of them null must throw an exception");
903
		} catch (NullPointerException e) {
904
			Assert.assertTrue("Merging of 2 objects of different types must throw an exception", true);
905
		}
906
		//
907
		try {
908
			cdmGenericDao.merge(null, name1, null);
909
			Assert.fail("Merging of 2 objects one or both of them null must throw an exception");
910
		} catch (NullPointerException e) {
911
			Assert.assertTrue("Merging of 2 objects of different types must throw an exception", true);
912
		}
913
		//exceptions to be thrown
914
		try {
915
			cdmGenericDao.merge(name1, taxon, null);
916
			//this is not fully true anymore !! In certain cases merging of objects of different classes is allowed
917
			Assert.fail("Merging of 2 objects of different types must throw an exception");
918
		} catch (MergeException e) {
919
			Assert.assertTrue("Merging of 2 objects of different types must throw an exception", true);
920
		}
921
		//next exception
922
		//for names this is not the case anymore
923
//		try {
924
//			cdmGenericDao.merge(name1, zooName1, null);
925
//			Assert.fail("Merging of 2 objects of different types must throw an exception");
926
//		} catch (MergeException e) {
927
//			Assert.assertTrue("Merging of 2 objects of different types must throw an exception", true);
928
//		}
929
	}
930

    
931
	@Test
932
	public void findMatching(){
933
		IBook book1 = ReferenceFactory.newBook();
934
		IBook book2 = ReferenceFactory.newBook();
935
		IBook book3 = ReferenceFactory.newBook();
936

    
937
		String title1 = "title1";
938
		String title2 = "title2";
939
		book1.setTitle(title1);
940
		book2.setTitle(title2);
941
		book3.setTitle(title1);
942

    
943
		cdmGenericDao.saveOrUpdate((Reference<?>)book1);
944
		cdmGenericDao.saveOrUpdate((Reference<?>)book2);
945
		cdmGenericDao.saveOrUpdate((Reference<?>)book3);
946

    
947
		IMatchStrategy matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
948

    
949
		try {
950
			List<IBook> matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
951
			Assert.assertNotNull("Resultlist must not be null", matchResult);
952
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
953
			Assert.assertSame("Resultlist entry must be book 1", book1, matchResult.get(0));
954

    
955
			book1.setDatePublished(TimePeriod.NewInstance(1999, 2002));
956
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
957
			Assert.assertTrue("Resultlist must have no entries", matchResult.isEmpty());
958

    
959
			book3.setDatePublished(TimePeriod.NewInstance(1999));
960
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
961
			Assert.assertTrue("Resultlist must have no entries", matchResult.isEmpty());
962

    
963
			book3.setDatePublished(TimePeriod.NewInstance(1999,2002));
964
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
965
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
966
			Assert.assertSame("Resultlist entry must be book 1", book1, matchResult.get(0));
967

    
968
			//BookSection
969
			IBookSection section1 = ReferenceFactory.newBookSection();
970
			section1.setInBook(book1);
971
			section1.setTitle("SecTitle");
972
			section1.setPages("22-33");
973
			IBookSection section2 = ReferenceFactory.newBookSection();
974
			section2.setInBook(book2);
975
			section2.setTitle("SecTitle");
976
			section2.setPages("22-33");
977
			IBookSection section3 = ReferenceFactory.newBookSection();
978
			section3.setInBook(book1);
979
			section3.setTitle("SecTitle");
980
			section3.setPages("22-33");
981
			cdmGenericDao.saveOrUpdate((Reference<?>)section1);
982
			cdmGenericDao.saveOrUpdate((Reference<?>)section2);
983
			cdmGenericDao.saveOrUpdate((Reference<?>)section3);
984

    
985
			List<IBookSection> sectionResult = cdmGenericDao.findMatching(section3, null);
986
			Assert.assertEquals("Resultlist must have 1 entries", 1, sectionResult.size());
987
			Assert.assertSame("Resultlist entry must be section1", section1, sectionResult.get(0));
988

    
989
			section2.setInBook(book2 = (IBook)book1.clone());
990
			cdmGenericDao.saveOrUpdate((Reference<?>)book2);
991
			cdmGenericDao.saveOrUpdate((Reference<?>)book1);
992
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
993
			Assert.assertEquals("Resultlist must have 2 entries", 2, matchResult.size());
994
			sectionResult = cdmGenericDao.findMatching(section3, null);
995
			Assert.assertEquals("Resultlist must have 1 entries", 2, sectionResult.size());
996

    
997

    
998
			Person person1 = Person.NewTitledInstance("person");
999
			Person person2 = Person.NewTitledInstance("person");
1000
			Person person3 = Person.NewTitledInstance("person");
1001

    
1002
			person1.setPrefix("pre1");
1003
			person2.setPrefix("pre2");
1004
			person3.setPrefix("pre3");
1005

    
1006
//			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1007
//			Assert.assertEquals("Resultlist must have 2 entries", 2, matchResult.size());
1008

    
1009
			book1.setAuthorship(person1);
1010
			book2.setAuthorship(person1);
1011
			book3.setAuthorship(person1);
1012

    
1013
			boolean m = matchStrategy.invoke(book1, book3);
1014
			boolean m2 = matchStrategy.invoke(book2, book3);
1015

    
1016
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1017
			Assert.assertEquals("Resultlist must have 2 entries", 2, matchResult.size());
1018

    
1019
			book2.setAuthorship(person2);
1020
			book3.setAuthorship(person3);
1021
			matchResult = cdmGenericDao.findMatching(book3, null);
1022
			Assert.assertEquals("Resultlist must have no entries", 0, matchResult.size());
1023

    
1024
			person3.setPrefix("pre1");
1025
			matchResult = cdmGenericDao.findMatching(book3, null);
1026
			Assert.assertEquals("Resultlist must have 1 entry", 1, matchResult.size());
1027
			Assert.assertSame("Resultlist entry must be book 1", book1, matchResult.get(0));
1028

    
1029
		} catch (MatchException e) {
1030
			Assert.fail("Find match must not throw Exception: " + e.getMessage());
1031
			e.printStackTrace();
1032
		}
1033

    
1034
	}
1035

    
1036

    
1037
	@Test
1038
	public void findMatchingCache(){
1039
		IBook book1 = ReferenceFactory.newBook();
1040
		Team team1 = Team.NewInstance();
1041
		Team team2 = Team.NewInstance();
1042
		team1.setTitleCache("Team1", true);
1043
		team2.setTitleCache("Team1", true);
1044

    
1045
		book1.setTitle("Title1");
1046
		book1.setEdition("Edition1");
1047
		book1.setAuthorship(team1);
1048

    
1049

    
1050
		IBook book2 = (IBook) ((Reference<?>)book1).clone();
1051
		IBook book3 = (IBook) ((Reference<?>)book1).clone();
1052

    
1053
//		Assert.assertTrue("Cloned book should match", matchStrategy.invoke(book1, bookClone));
1054
//		book1.setTitleCache("cache1");
1055
//		Assert.assertFalse("Cached book should not match", matchStrategy.invoke(book1, bookClone));
1056
//
1057
//		bookClone.setTitleCache("cache1");
1058
//		Assert.assertTrue("Cached book with same cache should match", matchStrategy.invoke(book1, bookClone));
1059
//
1060
//		bookClone.setTitleCache("cache2");
1061
//		Assert.assertFalse("Cached book with differings caches should not match", matchStrategy.invoke(book1, bookClone));
1062
//		bookClone.setTitleCache("cache1"); //restore
1063
//
1064
//		bookClone.setEdition(null);
1065
//		Assert.assertTrue("Cached book with a defined and a null edition should match", matchStrategy.invoke(book1, bookClone));
1066

    
1067
		cdmGenericDao.saveOrUpdate((Reference<?>)book1);
1068
		cdmGenericDao.saveOrUpdate((Reference<?>)book2);
1069
		cdmGenericDao.saveOrUpdate((Reference<?>)book3);
1070
		cdmGenericDao.saveOrUpdate(team1);
1071
		cdmGenericDao.saveOrUpdate(team2);
1072

    
1073

    
1074
		IMatchStrategy matchStrategy = DefaultMatchStrategy.NewInstance(Reference.class);
1075

    
1076
		try {
1077
			List<IBook> matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1078
			Assert.assertNotNull("Resultlist must not be null", matchResult);
1079
			Assert.assertEquals("Resultlist must have 2 entries", 2, matchResult.size());
1080
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1081
			Assert.assertTrue("Resultlist must contain book 2", matchResult.contains(book2));
1082

    
1083
			book1.setTitleCache("cache1", true);
1084
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1085
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1086
			Assert.assertTrue("Resultlist must contain book 2", matchResult.contains(book2));
1087

    
1088
			book2.setTitleCache("cache2", false);
1089
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1090
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1091
			Assert.assertTrue("Resultlist must contain book 2", matchResult.contains(book2));
1092

    
1093
			book2.setEdition(null);
1094
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1095
			Assert.assertEquals("Resultlist must have 0 entries", 0, matchResult.size());
1096

    
1097
			book3.setTitleCache("cache1", true);
1098
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1099
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1100
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1101

    
1102
			IMatchStrategy teamMatcher = DefaultMatchStrategy.NewInstance(Team.class);
1103
			boolean teamsMatch = teamMatcher.invoke(team1, team2);
1104
			Assert.assertTrue("Team1 and team2 should match" ,teamsMatch);
1105

    
1106
			book3.setAuthorship(team2);
1107
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1108
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1109
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1110

    
1111
			book3.setAuthorship(null);
1112
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1113
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1114
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1115

    
1116
			book2.setTitleCache(book3.getTitleCache(), true);
1117
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1118
			Assert.assertEquals("Resultlist must have 2 entries", 2, matchResult.size());
1119
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1120
			Assert.assertTrue("Resultlist must contain book 2", matchResult.contains(book2));
1121

    
1122
			team2.setTitleCache("team2", true);
1123
			teamsMatch = teamMatcher.invoke(team1, team2);
1124
			Assert.assertFalse("Team1 and team2 should not match" ,teamsMatch);
1125

    
1126
			book3.setAuthorship(team1);
1127
			book2.setAuthorship(team2);
1128
			matchResult = cdmGenericDao.findMatching(book3, matchStrategy);
1129
			Assert.assertEquals("Resultlist must have 1 entries", 1, matchResult.size());
1130
			Assert.assertTrue("Resultlist must contain book 1", matchResult.contains(book1));
1131

    
1132

    
1133
		} catch (MatchException e) {
1134
			Assert.fail("Find match must not throw Exception: " + e.getMessage());
1135
			e.printStackTrace();
1136
		}
1137
	}
1138

    
1139
	//from original testing within class, can be removed if not needed anymore
1140
	private void test() {
1141
		SessionFactoryImpl factory = (SessionFactoryImpl)((CdmGenericDaoImpl)cdmGenericDao).getSession().getSessionFactory();
1142
		Type propType = factory.getReferencedPropertyType(BotanicalName.class.getCanonicalName(), "titleCache");
1143
		Map<?,?> collMetadata = factory.getAllCollectionMetadata();
1144
		Object roles = factory.getCollectionRolesByEntityParticipant("eu.etaxonomy.cdm.model.name.BotanicalName");
1145
		CollectionPersister collPersister;
1146
		try {
1147
			collPersister = factory.getCollectionPersister(TaxonNameBase.class.getCanonicalName()+".annotations");
1148
		} catch (MappingException e) {
1149
			// TODO Auto-generated catch block
1150
			e.printStackTrace();
1151
		}
1152
		Statistics statistics = factory.getStatistics();
1153
		Map<?,?> allClassMetadata = factory.getAllClassMetadata();
1154
		logger.debug("");
1155

    
1156
	}
1157

    
1158
	/**
1159
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmGenericDaoImpl#getHqlResult(java.lang.String)}.
1160
	 */
1161
	@Test
1162
	public void testGetHqlResult() {
1163
		logger.warn("Not yet implemented");
1164
	}
1165

    
1166
    @Override
1167
    public void createTestDataSet() throws FileNotFoundException {}
1168
}
(4-4/11)