1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.businessobjects.security.persistence;
16  
17  import org.figure8.join.util.SpringTestCase;
18  import org.figure8.join.businessobjects.security.Role;
19  /**
20   * JUnit test case for testing RoleDao implementation.
21   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
22   * @version $Revision: 1.1 $
23   */
24  public class RoleDaoTest extends SpringTestCase{
25  
26     // Static -------------------------------------------------------------------
27  
28     /** Spring configuration files */
29     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/security/persistence/spring.xml"};
30  
31  
32     // Attributes ---------------------------------------------------------------
33  
34     /** RoleDao implementation to test */
35     protected RoleDao dao = null;
36  
37  
38     // Override of SpringTestCase -----------------------------------------------
39  
40     /** Retrieve RoleDao implementation after having initialized context */
41     public void setUp(){
42        super.setUp();
43        // Get dao.
44        dao = (RoleDao)context.getBean("roleDao");
45     }
46  
47     
48     // Public -------------------------------------------------------------------
49  
50     /** Test the creation of a role */
51     public void testRoleCreation(){
52        // Save a new role.
53        Role role = new Role("roleCreation", "Description");
54        int size = getRoleListSize();
55        dao.save(role);
56        // Assert there's one role.
57        assertEquals("Role is successfully created", size + 1, dao.findAll().size());
58     }
59  
60     /** Test the creation of the default roles */
61     public void testDefaultRolesCreation(){
62        int size = getRoleListSize();
63        // Save default roles.
64        dao.save(new Role(Role.DEFAULT_JOINER_ROLE, "Integration Team Member"));
65        dao.save(new Role(Role.DEFAULT_ARCHITECT_ROLE, "Software Project Architect"));
66        dao.save(new Role(Role.DEFAULT_ADMIN_ROLE, "Join Application Administrator"));
67        // Assert there's 3 more roles.
68        assertEquals("Defaults roles are successfully created",  size + 3, dao.findAll().size());
69     }
70  
71     /** Test the retrieve functions of dao */
72     public void testRoleRetrieval(){
73        // Save a new role.
74        Role role = new Role("roleRetrieval", "Retrieval description");
75        int size = getRoleListSize();
76        dao.save(role);
77        // Assert there's one role.
78        assertEquals("Role is successfully created", size + 1, dao.findAll().size());
79        // Retrieve it and assert.
80        Role foundRole = dao.getRole("roleRetrieval");
81        assertNotNull("Found a role", foundRole);
82        assertEquals("Found the correct role", "roleRetrieval", foundRole.getName());
83        assertEquals("Found the correct role", "Retrieval description", foundRole.getDescription());
84        // Retrieve it using id and assert.
85        foundRole = dao.getRole(role.getId());
86        assertNotNull("Found a role", foundRole);
87        assertEquals("Found the correct role", "roleRetrieval", foundRole.getName());
88        assertEquals("Found the correct role", "Retrieval description", foundRole.getDescription());
89     }
90  
91     /** Test the removal of a role */
92     public void testRoleRemoval(){
93        // Save a new role.
94        Role role = new Role("roleRemoval", "Description");
95        int size = getRoleListSize();
96        dao.save(role);
97        assertEquals("Role is successfully created", size + 1, dao.findAll().size());
98        // Remove it and asssert.
99        dao.remove(role);
100       assertEquals("Role is successfully removed", size, dao.findAll().size());
101    }
102 
103 
104    // Protected ----------------------------------------------------------------
105 
106    /** Test the findAll on roles */
107    protected int getRoleListSize(){
108       return dao.findAll().size();
109    }
110 
111 
112    // Override of SpringTestCase -----------------------------------------------
113 
114    /** @return configLocations inner field */
115    public String[] getConfigLocations(){
116       return configLocations;
117    }
118 }