2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
;
12 import static org
.junit
.Assert
.assertEquals
;
13 import static org
.junit
.Assert
.assertFalse
;
14 import static org
.junit
.Assert
.assertNotNull
;
15 import static org
.junit
.Assert
.assertTrue
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.junit
.Before
;
22 import org
.junit
.Test
;
23 import org
.springframework
.security
.authentication
.AuthenticationManager
;
24 import org
.springframework
.security
.authentication
.TestingAuthenticationToken
;
25 import org
.springframework
.security
.core
.Authentication
;
26 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
27 import org
.springframework
.security
.core
.context
.SecurityContextImpl
;
28 import org
.unitils
.database
.annotations
.Transactional
;
29 import org
.unitils
.database
.util
.TransactionMode
;
30 import org
.unitils
.dbunit
.annotation
.DataSet
;
31 import org
.unitils
.dbunit
.annotation
.ExpectedDataSet
;
32 import org
.unitils
.spring
.annotation
.SpringBeanByType
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.GrantedAuthorityImpl
;
36 import eu
.etaxonomy
.cdm
.model
.common
.User
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
38 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
39 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.IUserDao
;
40 import eu
.etaxonomy
.cdm
.persistence
.dao
.taxon
.ITaxonDao
;
41 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
42 import eu
.etaxonomy
.cdm
.persistence
.query
.RandomOrder
;
43 import eu
.etaxonomy
.cdm
.test
.integration
.CdmTransactionalIntegrationTest
;
49 public class CdmEntityDaoBaseTest
extends CdmTransactionalIntegrationTest
{
52 private TaxonBase cdmBase
;
55 private ITaxonDao cdmEntityDaoBase
;
58 private AuthenticationManager authenticationManager
;
61 private IUserDao userDao
;
64 * @throws java.lang.Exception
67 public void setUp() throws Exception
{
68 uuid
= UUID
.fromString("8d77c380-c76a-11dd-ad8b-0800200c9a66");
69 cdmBase
= Taxon
.NewInstance(null, null);
70 cdmBase
.setUuid(UUID
.fromString("e463b270-c76b-11dd-ad8b-0800200c9a66"));
72 // Clear the context prior to each test
73 SecurityContextHolder
.clearContext();
76 private void setAuthentication(User user
) {
77 TestingAuthenticationToken token
= new TestingAuthenticationToken(user
, "password", new GrantedAuthorityImpl
[0]);
78 Authentication authentication
= authenticationManager
.authenticate(token
);
80 SecurityContextImpl secureContext
= new SecurityContextImpl();
81 secureContext
.setAuthentication(authentication
);
82 SecurityContextHolder
.setContext(secureContext
);
85 /************ TESTS ********************************/
89 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#CdmEntityDaoBase(java.lang.Class)}.
93 public void testCdmEntityDaoBase() throws Exception
{
94 assertNotNull("cdmEntityDaoBase should exist",cdmEntityDaoBase
);
98 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
101 @DataSet("CdmEntityDaoBaseTest.xml")
103 public void testSaveOrUpdate() {
104 TaxonBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
105 cdmBase
.setDoubtful(true);
106 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
110 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
113 @DataSet("CdmEntityDaoBaseTest.xml")
115 public void testSaveOrUpdateWithAuthentication() {
116 User user
= userDao
.findByUuid(UUID
.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
117 assert user
!= null : "User cannot be null";
118 setAuthentication(user
);
119 TaxonBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
120 cdmBase
.setDoubtful(true);
121 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
125 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#saveOrUpdate(eu.etaxonomy.cdm.model.common.CdmBase)}.
128 @DataSet("CdmEntityDaoBaseTest.xml")
130 public void testSaveOrUpdateNewObjectWithAuthentication() {
131 // printDataSet(System.err, new String[]{"TAXONBASE", "HIBERNATE_SEQUENCES"});
132 User user
= userDao
.findByUuid(UUID
.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
133 assert user
!= null : "User cannot be null";
134 setAuthentication(user
);
135 cdmEntityDaoBase
.saveOrUpdate(cdmBase
);
138 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
141 @DataSet("CdmEntityDaoBaseTest.xml")
143 public void testSave() throws Exception
{
144 cdmEntityDaoBase
.save(cdmBase
);
148 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#save(eu.etaxonomy.cdm.model.common.CdmBase)}.
151 @DataSet("CdmEntityDaoBaseTest.xml")
153 public void testSaveWithAuthentication() throws Exception
{
154 User user
= userDao
.findByUuid(UUID
.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
155 assert user
!= null : "User cannot be null";
156 setAuthentication(user
);
157 cdmEntityDaoBase
.save(cdmBase
);
161 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#update(eu.etaxonomy.cdm.model.common.CdmBase)}.
164 @DataSet("CdmEntityDaoBaseTest.xml")
166 public void testUpdate() {
167 TaxonBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
168 cdmBase
.setDoubtful(true);
169 cdmEntityDaoBase
.update(cdmBase
);
173 @DataSet("CdmEntityDaoBaseTest.xml")
175 public void testUpdateWithAuthentication() {
176 User user
= userDao
.findByUuid(UUID
.fromString("dbac0f20-07f2-11de-8c30-0800200c9a66"));
177 assert user
!= null : "User cannot be null";
179 setAuthentication(user
);
180 TaxonBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
181 cdmBase
.setDoubtful(true);
182 cdmEntityDaoBase
.update(cdmBase
);
186 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findById(int)}.
189 @DataSet("CdmEntityDaoBaseTest.xml")
190 public void testFindById() {
191 CdmBase cdmBase
= cdmEntityDaoBase
.findById(1);
192 assertNotNull("There should be an entity with an id of 1",cdmBase
);
196 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#findByUuid(java.util.UUID)}.
199 @DataSet("CdmEntityDaoBaseTest.xml")
200 public void testFindByUuid() {
201 CdmBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
202 assertNotNull("testFindByUuid() an entity with a uuid of " + uuid
.toString(),cdmBase
);
206 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#exists(java.util.UUID)}.
209 @DataSet("CdmEntityDaoBaseTest.xml")
210 public void testExists() {
211 boolean exists
= cdmEntityDaoBase
.exists(uuid
);
212 assertTrue("exists() should return true for uuid " + uuid
.toString(), exists
);
213 boolean existsRandom
= cdmEntityDaoBase
.exists(UUID
.randomUUID());
214 assertFalse("exists() should return false for any other uuid", existsRandom
);
218 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
221 @DataSet("CdmEntityDaoBaseTest.xml")
222 public void testList() {
223 List
<TaxonBase
> list
= cdmEntityDaoBase
.list(1000, 0);
224 assertNotNull("list() should not return null",list
);
225 assertEquals("list() should return a list with two entities in it",list
.size(),2);
229 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#list(int, int)}.
232 @DataSet("CdmEntityDaoBaseTest.xml")
233 public void testRandomOrder() {
234 List
<OrderHint
> orderHints
= new ArrayList
<OrderHint
>();
235 orderHints
.add(new RandomOrder());
236 List
<TaxonBase
> list
= cdmEntityDaoBase
.list(null,1000, 0,orderHints
,null);
237 assertNotNull("list() should not return null",list
);
238 assertEquals("list() should return a list with two entities in it",list
.size(),2);
242 * Test method for {@link eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase#delete(eu.etaxonomy.cdm.model.common.CdmBase)}.
245 @DataSet("CdmEntityDaoBaseTest.xml")
247 public void testDelete() {
248 TaxonBase cdmBase
= cdmEntityDaoBase
.findByUuid(uuid
);
249 assertNotNull(cdmBase
);
250 cdmEntityDaoBase
.delete(cdmBase
);