Project

General

Profile

Download (9.54 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.persistence.hibernate;
11

    
12

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

    
15
import java.io.FileNotFoundException;
16
import java.util.List;
17
import java.util.UUID;
18

    
19
import org.apache.log4j.Logger;
20
import org.junit.Assert;
21
import org.junit.Before;
22
import org.junit.Test;
23
import org.unitils.dbunit.annotation.DataSet;
24
import org.unitils.dbunit.annotation.ExpectedDataSet;
25
import org.unitils.spring.annotation.SpringBeanByType;
26

    
27
import eu.etaxonomy.cdm.model.agent.Institution;
28
import eu.etaxonomy.cdm.model.agent.Person;
29
import eu.etaxonomy.cdm.model.agent.Team;
30
import eu.etaxonomy.cdm.model.common.DefinedTerm;
31
import eu.etaxonomy.cdm.model.common.TimePeriod;
32
import eu.etaxonomy.cdm.model.molecular.Amplification;
33
import eu.etaxonomy.cdm.model.name.IBotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
37
import eu.etaxonomy.cdm.model.reference.IArticle;
38
import eu.etaxonomy.cdm.model.reference.IBook;
39
import eu.etaxonomy.cdm.model.reference.IJournal;
40
import eu.etaxonomy.cdm.model.reference.Reference;
41
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
42
import eu.etaxonomy.cdm.model.taxon.Taxon;
43
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
45
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
46
import eu.etaxonomy.cdm.persistence.dao.molecular.IAmplificationDao;
47
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
48
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
49
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
50
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
51

    
52
/**
53
 * @author a.mueller
54
 * @created 18.03.2009
55
 */
56
public class CacheStrategyGeneratorTest extends CdmTransactionalIntegrationTest {
57
	private static Logger logger = Logger.getLogger(CacheStrategyGeneratorTest.class);
58

    
59
	private TaxonBase<?> cdmBase;
60

    
61
	@SpringBeanByType
62
	private ITaxonNameDao cdmEntityDaoBase;
63

    
64
	@SpringBeanByType
65
	private IAgentDao agentDao;
66

    
67
	@SpringBeanByType
68
	private IReferenceDao referenceDao;
69

    
70
	@SpringBeanByType
71
	private IAmplificationDao amplificationDao;
72

    
73
	@SpringBeanByType
74
	private IDefinedTermDao termDao;
75

    
76

    
77
	/**
78
	 * @throws java.lang.Exception
79
	 */
80
	@Before
81
	public void setUp() throws Exception {
82
		cdmBase = Taxon.NewInstance(null, null);
83
		cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
84
	}
85

    
86
	/**
87
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
88
	 * @throws Exception
89
	 */
90
	@Test
91
	public void testDaos() throws Exception {
92
		assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase);
93
		assertNotNull("agentDao should exist",agentDao);
94
	}
95

    
96
	/**
97
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
98
	 */
99
	@Test
100
	@DataSet("CacheStrategyGeneratorTest.xml")
101
	@ExpectedDataSet
102
	public void testOnSaveOrUpdateNames() {
103
		//names
104
	    TaxonNameBase<?,?> name =  cdmEntityDaoBase.findByUuid(UUID.fromString("a49a3963-c4ea-4047-8588-2f8f15352730"));
105
		name.setTitleCache(null, false);
106
		name.setNameCache(null, false);
107
		name.setGenusOrUninomial("Abies");
108
		name.setAuthorshipCache("Mill.", true);
109
		cdmEntityDaoBase.saveOrUpdate(name);
110
		TaxonNameBase<?,?> name2 =  cdmEntityDaoBase.findByUuid(UUID.fromString("05a438d6-065f-49ef-84db-c7dc2c259975"));
111
		name2.setProtectedFullTitleCache(false);
112
		name2.setProtectedTitleCache(false);
113
		name2.setProtectedNameCache(false);
114
		name2.setGenusOrUninomial("Abies");
115
		name2.setSpecificEpithet("alba");
116
		name2.setAuthorshipCache("Mill.", true);
117
		IBook ref = ReferenceFactory.newBook();
118
		ref.setTitle("My Book");
119
		name2.setNomenclaturalReference(ref);
120
		name2.setNomenclaturalMicroReference("44");
121

    
122
		cdmEntityDaoBase.saveOrUpdate(name2);
123

    
124
		Assert.assertEquals(name, cdmEntityDaoBase.findByUuid(name.getUuid()));
125
		IBotanicalName nameTest = cdmEntityDaoBase.findByUuid(name.getUuid());
126

    
127
		Assert.assertEquals(name2, cdmEntityDaoBase.findByUuid(name2.getUuid()));
128
		logger.debug("FulltitleCache: "+ cdmEntityDaoBase.findByUuid(name2.getUuid()).getFullTitleCache());
129
		logger.debug("updated: " + cdmEntityDaoBase.findByUuid(name2.getUuid()).getUpdated());
130
		TaxonNameBase<?,?> name3 = TaxonNameFactory.NewBotanicalInstance(Rank.GENUS());
131
		name3.setFullTitleCache("Test: MyBook");
132
		name3.setTitleCache("Test", true);
133
		cdmEntityDaoBase.saveOrUpdate(name3);
134
		List<TaxonNameBase> taxa = cdmEntityDaoBase.findByTitle("Test");
135

    
136
		TaxonNameBase<?,?> nameBase = taxa.get (0);
137
		IBotanicalName botName = nameBase;
138

    
139
		logger.debug("created "+botName.getCreated());
140
		logger.debug("updated: " +botName.getUpdated());
141
//		IBotanicalName name3 =  (IBotanicalName)cdmEntityDaoBase.findByUuid(UUID.fromString("049a3963-c4ea-4047-8588-2f8f15352730"));
142
//		printDataSet(System.err, new String[]{"TaxonNameBase", "Reference"});
143
	}
144

    
145
	/**
146
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
147
	 */
148
	@Test
149
	@DataSet("CacheStrategyGeneratorTest.xml")
150
	@ExpectedDataSet
151
	public void testOnSaveOrUpdateAgents() {
152

    
153
		//person
154
		Person person1;
155
		Person person2;
156
		Person person3;
157

    
158
		person1 = makePerson1();
159

    
160
		person2 = makePerson2();
161

    
162
		person3 = Person.NewInstance(); //empty person
163
		person3.setUuid(UUID.fromString("4c4e15e3-3a4f-4505-900a-fae2555ac9e4"));
164

    
165
//		System.out.println(person1.getTitleCache());
166
//		System.out.println(person1.getNomenclaturalTitle());
167
//		System.out.println(person2.getTitleCache());
168
//		System.out.println(person2.getNomenclaturalTitle());
169
//		System.out.println(person3.getTitleCache());
170
//		System.out.println(person3.getNomenclaturalTitle());
171

    
172
		agentDao.saveOrUpdate(person1);
173
		agentDao.saveOrUpdate(person2);
174
		agentDao.saveOrUpdate(person3);
175

    
176
		//Teams
177
		Team team1 = Team.NewInstance();
178
		team1.addTeamMember(person1);
179
		team1.setUuid(UUID.fromString("db957a0a-1494-49bb-8d17-d3eaa2076573"));
180
		agentDao.saveOrUpdate(team1);
181

    
182
		Person person4 = (Person)agentDao.findByUuid(UUID.fromString("4c4e15e3-3a4f-4505-900a-fae2555ac9e4"));
183
		Assert.assertEquals(person3, person4);
184
		Team team2 = (Team) agentDao.findByUuid(UUID.fromString("db957a0a-1494-49bb-8d17-d3eaa2076573"));
185
		Assert.assertEquals(team1, team2);
186
	}
187

    
188

    
189

    
190
	private Person makePerson1() {
191
		Person person1;
192
		person1 = Person.NewInstance();
193

    
194
		person1.setUuid(UUID.fromString("646dad4b-0f0e-4f5a-b059-8099ad9a6125"));
195
		person1.setFirstname("P1FN");
196
		person1.setLastname("P1LN");
197
		person1.setPrefix("Dr1.");
198
		person1.setSuffix("Suff1");
199
		return person1;
200
	}
201

    
202
	private Person makePerson2() {
203
		Person person2;
204
		person2 = Person.NewInstance();
205
		person2.setUuid(UUID.fromString("ca904533-2a70-49f3-9a0e-5e4bcc12c154"));
206
		person2.setNomenclaturalTitle("P2NomT");
207
		person2.setLastname("P2LN");
208
		person2.setFirstname("P2FN");
209
		person2.setSuffix("P2Suff");
210
		return person2;
211
	}
212

    
213
	/**
214
	 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
215
	 */
216
	@Test
217
	@DataSet("CacheStrategyGeneratorTest.xml")
218
	@ExpectedDataSet
219
	//TODO correct abbrevTitleCache for article still unclear (open question: with or without article title ?)
220
	public void testOnSaveOrUpdateReferences() {
221
		//References
222
		IJournal journal1 = ReferenceFactory.newJournal();
223
		Person journalAuthor = makePerson1();
224

    
225
		journal1.setTitle("My journal");
226
		journal1.setUuid(UUID.fromString("a7fdf3b8-acd8-410a-afcd-1768d29d67e9"));
227
		journal1.setAbbrevTitle("M. Journ.");
228
		journal1.setAuthorship(journalAuthor);
229

    
230
		referenceDao.save((Reference)journal1);
231

    
232
		Person articleAuthor = makePerson2();
233
		IArticle article1 = ReferenceFactory.newArticle();
234
		article1.setUuid(UUID.fromString("eb090fbc-5895-405c-aba5-cac287efb128"));
235
		article1.setAbbrevTitle("M. Art.");
236
		article1.setVolume("1");
237
		article1.setDatePublished(TimePeriod.NewInstance(1972));
238
		article1.setInJournal(journal1);
239
		article1.setAuthorship(articleAuthor);
240
		article1.getAbbrevTitleCache();
241

    
242
		referenceDao.saveOrUpdate((Reference)article1);
243

    
244
		commit();
245
	}
246

    
247
	@Test
248
//	@DataSet("CacheStrategyGeneratorTest.xml")
249
//	@ExpectedDataSet
250
	public void testOnSaveOrUpdateAmplification() {
251
		Amplification amplification = Amplification.NewInstance();
252
		UUID amplUuid = UUID.fromString("11e6b2d5-3eb5-4434-9c56-5bb4c1102147");
253
		amplification.setUuid(amplUuid);
254

    
255
		amplificationDao.save(amplification);
256
		Assert.assertEquals("<Amplification:11e6b2d5-3eb5-4434-9c56-5bb4c1102147>", amplification.getLabelCache());
257

    
258
		Person author = Person.NewTitledInstance("Person");
259
		Institution institution = Institution.NewInstance();
260
		institution.setName("My institute");
261
		DefinedTerm marker = DefinedTerm.NewDnaMarkerInstance("marker", "marker", "dm");
262

    
263
		amplification.setActor(author);
264
		amplification.setTimeperiod(TimePeriodParser.parseString("2008"));
265
		amplification.setDnaMarker(marker);
266
		amplification.setInstitution(institution);
267

    
268

    
269
		termDao.save(marker);
270
		amplificationDao.saveOrUpdate(amplification);
271
		Assert.assertEquals("My institute_Person_marker_2008", amplification.getLabelCache());
272
	}
273

    
274
	/* (non-Javadoc)
275
	 * @see eu.etaxonomy.cdm.test.integration.CdmIntegrationTest#createTestData()
276
	 */
277
	@Override
278
	public void createTestDataSet() throws FileNotFoundException {
279
		// TODO Auto-generated method stub
280
	}
281
}
282

    
(1-1/5)