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.User;
19  
20  import java.util.List;
21  /**
22   * JUnit test case for testing UserDao implementation.
23   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
24   * @version $Revision: 1.1 $
25   */
26  public class UserDaoTest extends SpringTestCase{
27  
28     // Static -------------------------------------------------------------------
29  
30     /** Spring configuration files */
31     private String[] configLocations = new String[]{"classpath:/org/figure8/join/businessobjects/security/persistence/spring.xml"};
32  
33  
34     // Attributes ---------------------------------------------------------------
35  
36     /** UserDao implementation to test */
37     protected UserDao dao = null;
38  
39  
40     // Override of SpringTestCase -----------------------------------------------
41  
42     /** Retrieve UserDao implementation after having initialized context */
43     public void setUp(){
44        super.setUp();
45        // Get dao.
46        dao = (UserDao)context.getBean("userDao");
47     }
48  
49  
50     // Public -------------------------------------------------------------------
51  
52     /** Test the creation of a user */
53     public void testUserCreation(){
54        // Save a new user.
55        User user = new User("userCreation", "password", "lastname", "firstname");
56        int size = getUserListSize();
57        dao.save(user);
58        // Assert there's one user.
59        assertEquals("User is successfully created", size + 1, dao.findAll().size());
60     }
61  
62     /** Test the retrieval of users */
63     public void testGetUsers(){
64        // Save a bunch of user.
65        User user1 = new User("userGet1", "password1", "get1", "firstname1");
66        User user2 = new User("userGet2", "password2", "get2", "firstname2");
67        User user3 = new User("userGet3", "password3", "get3", "firstname3");
68        int size = getUserListSize();
69        dao.save(user1);
70        dao.save(user2);
71        dao.save(user3);
72        // Assert there's 3 more users.
73        assertEquals("Users are sucessfully created", size + 3, dao.findAll().size());
74        // Retrieve using an id and assert.
75        User foundUser = dao.getUser("userGet2");
76        assertNotNull("Found a User", foundUser);
77        assertEquals("Found the correct user", "userGet2", foundUser.getLogin());
78        assertEquals("Found the correct user", "password2", foundUser.getPassword());
79        assertEquals("Found the correct user", "get2", foundUser.getLastname());
80        assertEquals("Found the correct user", "firstname2", foundUser.getFirstname());
81        // Retrieve lists using the lastname criterion.
82        List users = dao.getUsersWithLastnameLike("et");
83        assertEquals("Found correct user count", 3, users.size());
84        users = dao.getUsersWithLastnameLike("t1");
85        assertEquals("Found correct user count", 1, users.size());
86        users = dao.getUsersWithLastnameLike("get3");
87        assertEquals("Found correct user count", 1, users.size());
88     }
89  
90     /** Test the removal of a user */
91     public void testUserRemoval(){
92        // Save a new user.
93        User user = new User("userRemoval", "password", "lastname", "firstname");
94        int size = getUserListSize();
95        dao.save(user);
96        assertEquals("User is successfully created", size + 1, dao.findAll().size());
97        // Remove it and asssert.
98        dao.remove(user);
99        assertEquals("User is successfully removed", size, dao.findAll().size());
100    }
101 
102 
103    // Protected ----------------------------------------------------------------
104 
105    /** Test the findAll on users */
106    protected int getUserListSize(){
107       return dao.findAll().size();
108    }
109 
110 
111    // Override of SpringTestCase -----------------------------------------------
112 
113    /** @return configLocations inner field */
114    public String[] getConfigLocations(){
115       return configLocations;
116    }
117 }