Project

General

Profile

Download (9.59 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.BotanicalName;
34
import eu.etaxonomy.cdm.model.name.Rank;
35
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36
import eu.etaxonomy.cdm.model.reference.IArticle;
37
import eu.etaxonomy.cdm.model.reference.IBook;
38
import eu.etaxonomy.cdm.model.reference.IJournal;
39
import eu.etaxonomy.cdm.model.reference.Reference;
40
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
41
import eu.etaxonomy.cdm.model.taxon.Taxon;
42
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43
import eu.etaxonomy.cdm.persistence.dao.agent.IAgentDao;
44
import eu.etaxonomy.cdm.persistence.dao.common.IDefinedTermDao;
45
import eu.etaxonomy.cdm.persistence.dao.molecular.IAmplificationDao;
46
import eu.etaxonomy.cdm.persistence.dao.name.ITaxonNameDao;
47
import eu.etaxonomy.cdm.persistence.dao.reference.IReferenceDao;
48
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
49
import eu.etaxonomy.cdm.test.integration.CdmTransactionalIntegrationTest;
50

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

    
58
	private UUID uuid;
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
		uuid = UUID.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
83
		cdmBase = Taxon.NewInstance(null, null);
84
		cdmBase.setUuid(UUID.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
85
	}
86

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

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

    
123
		cdmEntityDaoBase.saveOrUpdate(name2);
124

    
125
		Assert.assertEquals(name, cdmEntityDaoBase.findByUuid(name.getUuid()));
126
		BotanicalName nameTest = (BotanicalName)cdmEntityDaoBase.findByUuid(name.getUuid());
127

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

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

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

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

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

    
159
		person1 = makePerson1();
160

    
161
		person2 = makePerson2();
162

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

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

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

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

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

    
189

    
190

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

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

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

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

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

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

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

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

    
245
		commit();
246
	}
247

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

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

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

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

    
269

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

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

    
(1-1/4)