Project

General

Profile

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

    
11
import java.io.FileNotFoundException;
12

    
13
import org.apache.log4j.Logger;
14
import org.junit.Assert;
15
import org.junit.Before;
16
import org.junit.Test;
17
import org.unitils.dbunit.annotation.DataSet;
18
import org.unitils.dbunit.annotation.DataSets;
19
import org.unitils.spring.annotation.SpringBeanByType;
20

    
21
import eu.etaxonomy.cdm.model.name.Rank;
22
import eu.etaxonomy.cdm.model.name.TaxonName;
23
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
24
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
25
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
26
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
27
import eu.etaxonomy.cdm.model.taxon.Taxon;
28
import eu.etaxonomy.cdm.persistence.dao.hibernate.occurrence.OccurrenceDaoHibernateImpl;
29
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
30
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
31

    
32
/**
33
 * @author a.mueller
34
 */
35
public class SaveOrUpdateEntityListenerTest extends CdmTransactionalIntegrationTest {
36
	@SuppressWarnings("unused")
37
	private static final Logger logger = Logger.getLogger(SaveOrUpdateEntityListenerTest.class);
38

    
39
	@SpringBeanByType
40
	private OccurrenceDaoHibernateImpl occurrenceDao;
41

    
42
	@Before
43
	public void setUp() throws Exception {
44
	}
45

    
46
	/**
47
	 * Test if save or update event correctly handleds
48
	 */
49
	@Test
50
    @DataSets({
51
        @DataSet(loadStrategy=CleanSweepInsertLoadStrategy.class, value="/eu/etaxonomy/cdm/database/ClearDB_with_Terms_DataSet.xml"),
52
        @DataSet(value="/eu/etaxonomy/cdm/database/TermsDataSet-with_auditing_info.xml"),
53

    
54
    })
55
	public void testOnSaveOrUpdateDeterminationTaxonName() {
56
		DerivedUnit unit = DerivedUnit.NewInstance(SpecimenOrObservationType.DerivedUnit);
57
		Taxon taxon = Taxon.NewInstance(null, null);
58
		DeterminationEvent detWithTaxonOnlyAndNoName = DeterminationEvent.NewInstance(taxon, unit);
59

    
60
		TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SPECIES());
61
		Taxon taxon2 = Taxon.NewInstance(name, null);
62
		DeterminationEvent detWithTaxonOnlyAndNameOnTaxon = DeterminationEvent.NewInstance(taxon2, unit);
63

    
64
		DeterminationEvent detWithNameOnly = DeterminationEvent.NewInstance(name, unit);
65

    
66
		occurrenceDao.save(unit);
67
		commitAndStartNewTransaction(null);
68

    
69
		unit = (DerivedUnit)occurrenceDao.findByUuid(unit.getUuid());
70
		for (DeterminationEvent persistedDetEvent : unit.getDeterminations()){
71
			if (persistedDetEvent.getUuid().equals(detWithTaxonOnlyAndNoName.getUuid())){
72
				Assert.assertNotNull("Taxon should not be null", persistedDetEvent.getTaxon());
73
				Assert.assertNull("TaxonName should be null, because taxon has no name", persistedDetEvent.getTaxonName());
74
			}else if (persistedDetEvent.getUuid().equals(detWithTaxonOnlyAndNameOnTaxon.getUuid())){
75
				Assert.assertNotNull("Taxon should not be null", persistedDetEvent.getTaxon());
76
				Assert.assertNotNull("TaxonName should not be null, injected by listener from taxon", persistedDetEvent.getTaxonName());
77
			}else if (persistedDetEvent.getUuid().equals(detWithNameOnly.getUuid())){
78
				Assert.assertNull("Taxon should be null as only name was attached", persistedDetEvent.getTaxon());
79
				Assert.assertNotNull("TaxonName should not be null as set manually", persistedDetEvent.getTaxonName());
80
			}else{
81
				Assert.fail("All cases should be handled");
82
			}
83
		}
84
	}
85

    
86
	@Override
87
	public void createTestDataSet() throws FileNotFoundException {}
88
}
(6-6/6)