Project

General

Profile

Download (3.42 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.cdm.persistence.hibernate;
5

    
6
import java.io.FileNotFoundException;
7

    
8
import org.apache.log4j.Logger;
9
import org.junit.Assert;
10
import org.junit.Before;
11
import org.junit.Test;
12
import org.unitils.dbunit.annotation.DataSet;
13
import org.unitils.dbunit.annotation.DataSets;
14
import org.unitils.spring.annotation.SpringBeanByType;
15

    
16
import eu.etaxonomy.cdm.model.name.BotanicalName;
17
import eu.etaxonomy.cdm.model.name.Rank;
18
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
19
import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
20
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
21
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
22
import eu.etaxonomy.cdm.model.taxon.Taxon;
23
import eu.etaxonomy.cdm.persistence.dao.hibernate.occurrence.OccurrenceDaoHibernateImpl;
24
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
25
import eu.etaxonomy.cdm.test.unitils.CleanSweepInsertLoadStrategy;
26

    
27
/**
28
 * @author a.mueller
29
 *
30
 */
31
public class SaveOrUpdateEntityListenerTest extends CdmTransactionalIntegrationTest {
32
	@SuppressWarnings("unused")
33
	private static final Logger logger = Logger.getLogger(SaveOrUpdateEntityListenerTest.class);
34

    
35
	@SpringBeanByType
36
	private OccurrenceDaoHibernateImpl occurrenceDao;
37

    
38

    
39
	/**
40
	 * @throws java.lang.Exception
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
		BotanicalName 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

    
87

    
88
	@Override
89
	public void createTestDataSet() throws FileNotFoundException {}
90

    
91
}
(5-5/5)