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  import org.figure8.join.businessobjects.security.Permission;
20  /**
21   * JUnit test case for testing PermissionDao implementation.
22   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
23   * @version $Revision: 1.1 $
24   */
25  public class PermissionDaoTest extends SpringTestCase{
26  
27     // Static -------------------------------------------------------------------
28  
29     /** Spring configuration files */
30     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/security/persistence/spring.xml"};
31  
32  
33     // Attributes ---------------------------------------------------------------
34  
35     /** RoleDao implementation used for testing */
36     protected RoleDao roleDao = null;
37     /** PermissionDao implementation to test */
38     protected PermissionDao dao = null;
39  
40  
41     // Override of SpringTestCase -----------------------------------------------
42  
43     /** Retrieve PermissionDao and RoleDao after having initialized context */
44     public void setUp(){
45        super.setUp();
46        // Get daos.
47        roleDao = (RoleDao)context.getBean("roleDao");
48        dao = (PermissionDao)context.getBean("permissionDao");
49     }
50  
51  
52     // Public -------------------------------------------------------------------
53  
54     /** Test the creation of a permission */
55     public void testPermissionCreation(){
56        // Save new role and permission.
57        Role role = new Role("permissionCreation", "Description");
58        Permission permission = new Permission(role, "userId");
59        roleDao.save(role);
60        int size = getPermissionListSize();
61        dao.save(permission);
62        // Assert there's one permission.
63        assertEquals("Permission is successfully created", size + 1, dao.findAll().size());
64     }
65  
66     /** Test the removal of a permission */
67     public void testPermissionRemoval(){
68        // Save new role and permission.
69        Role role = new Role("permissionRemoval", "Description");
70        Permission permission = new Permission(role, "userId");
71        roleDao.save(role);
72        int size = getPermissionListSize();
73        dao.save(permission);
74        assertEquals("Permission is successfully created", size + 1, dao.findAll().size());
75        // Remove and assert.
76        dao.remove(permission);
77        assertEquals("Permission is successfully removed", size, dao.findAll().size());
78     }
79  
80  
81     // Protected ----------------------------------------------------------------
82  
83     /** Test the findAll on permission */
84     protected int getPermissionListSize(){
85        return dao.findAll().size();
86     }
87  
88  
89     // Override of SpringTestCase -----------------------------------------------
90  
91     /** @return configLocations inner field */
92     public String[] getConfigLocations(){
93        return configLocations;
94     }
95  }