UserDaoTest.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* ===================================================================
  2. * UserDaoTest.java
  3. *
  4. * Created Sep 29, 2004 1:22:20 PM
  5. *
  6. * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: UserDaoTest.java 59 2009-05-11 09:16:24Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.dao;
  27. import magoffin.matt.ieat.dao.UserDao.UserIndexCallbackData;
  28. import magoffin.matt.ieat.domain.User;
  29. import org.apache.log4j.Logger;
  30. /**
  31. * Test case for the UserDao.
  32. *
  33. * <p>These tests can only work when the test data is loaded in the database!</p>
  34. *
  35. * @author Matt Magoffin (spamsqr@msqr.us)
  36. * @version $Revision: 59 $ $Date: 2009-05-11 21:16:24 +1200 (Mon, 11 May 2009) $
  37. */
  38. public class UserDaoTest extends AbstractDaoTest {
  39. private static final Logger LOG = Logger.getLogger(UserDaoTest.class);
  40. /** The UserDao to test. */
  41. protected UserDao userDao;
  42. /**
  43. * Test clean user registrations.
  44. */
  45. public void testCleanStaleUserRegistrations() {
  46. userDao.removeStaleUserRegistrations();
  47. }
  48. /**
  49. * Test create a new user.
  50. */
  51. public void testCreateAndDeleteNewUser() {
  52. User u = getDummyUser();
  53. User newUser = userDao.get(userDao.store(u));
  54. assertNotNull("Returned user should not be null on create",newUser);
  55. assertNotNull("New user should have user ID set", newUser.getUserId());
  56. // verify can get user from DAO
  57. newUser = userDao.get(newUser.getUserId());
  58. assertNotNull("New user should be available from DAO", newUser);
  59. assertTrue("New user's login should be same as dummy user's", u.getLogin().equals(newUser.getLogin()));
  60. // now clean up and remove test user
  61. userDao.delete(userDao.get(newUser.getUserId()));
  62. // verify not in DAO anymore
  63. newUser = userDao.getUser(u.getLogin());
  64. assertNull("After delete user should not be returned from DAO", newUser);
  65. }
  66. /**
  67. * Test get a user by login.
  68. */
  69. public void testGetUserByLogin() {
  70. // first throw user into DB
  71. User u = getDummyUser();
  72. userDao.store(u);
  73. // test that find by login works
  74. u = userDao.getUser(TEST_USER_LOGIN);
  75. assertNotNull("User should not be null for login '" +TEST_USER_LOGIN +"'", u);
  76. assertTrue("User should have login of '" +TEST_USER_LOGIN +"'", TEST_USER_LOGIN.equals(u.getLogin()));
  77. }
  78. /**
  79. * Test able to index.
  80. */
  81. public void testIndex() {
  82. userDao.index(new UserDao.UserIndexCallback() {
  83. public void handle(UserIndexCallbackData data) {
  84. if ( LOG.isDebugEnabled() ) {
  85. LOG.debug("Got user index callback data: " +data);
  86. }
  87. assertNotNull("User callback data should not be null", data);
  88. assertNotNull("User callback data 'name' should not be null", data.getName());
  89. assertNotNull("User callback data 'userId' should not be null", data.getUserId());
  90. assertNotNull("User callback data 'email' should not be null", data.getEmail());
  91. assertNotNull("User callback data 'createdDate' should not be null", data.getCreatedDate());
  92. }
  93. });
  94. }
  95. }