1   /***
2    Copyright (C) 2005 The Java Community
3   
4    This program is free software; you can redistribute it and/or modify  it under
5    the terms of the GNU General Public License as published by  the Free Software
6    Foundation; either version 2 of the License, or  (at your option) any later
7    version.
8   
9    This program is distributed in the hope that it will be useful,  but WITHOUT
10   ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS
11   FOR A PARTICULAR PURPOSE. See the  GNU General Public License for more details.
12  
13   You should have received a copy of the GNU General Public License  along with
14   this program; if not, write to the Free Software  Foundation, Inc., 59 Temple
15   Place, Suite 330, Boston, MA 02111-1307 USA.
16   */
17  package org.bejug.javacareers.jobs.dao;
18  
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import org.bejug.javacareers.common.exception.DuplicateUserNameException;
23  import org.bejug.javacareers.common.exception.LastAdminException;
24  import org.bejug.javacareers.jobs.common.AbstractSpringContextDBUnitTests;
25  import org.bejug.javacareers.jobs.model.User;
26  import org.springframework.dao.DataAccessException;
27  
28  /***
29   * @author Sven Schauwvliege (Last modified by $Author: shally $)
30   * @version $Revision: 1.17 $ - $Date: 2005/12/20 15:36:47 $
31   */
32  
33  
34  public class UserDaoImplTests extends AbstractSpringContextDBUnitTests {
35  
36      /***
37       * the names of the tables need to be compaired.
38       */
39      private static final String[] TABLE_NAMES = {"Resume", "Parameter", "Country", "Region", "Address", "Contact", "Organisation", "Person", "User"};
40      
41      /***
42       * the columns to ignore.
43       */
44      private static final String[] IGNORE = {"id","modificationdate", "addressId", "organisationId", "contactId"};
45      /***
46       * the jobofferdao, injected through field-injection.
47       */
48      protected UserDao userDao;
49  
50      /***
51       * @return Returns the userDao.
52       */
53      public UserDao getUserDao() {
54          return userDao;
55      }
56  
57  
58      /***
59       * Test the Create, Read, Read all, Update and Delete functions for a User.
60       *
61       * @throws DataAccessException Thrown when a database exceptions occurs.
62       */
63      public void testCRUDUser() throws DataAccessException {
64          //
65          // (1) Read al users from the database to a list.
66          //
67          List users = getUserDao().getUsers();
68          assertTrue("read failed, amount of found objects not 5 but " + (users.size()), users.size() == 5);
69          assertTrue("read failed ", users.get(0) instanceof User);
70          
71          
72          //
73          // (2) delete al users by id and by object (change boolean to test both delete methods)
74          //
75          boolean change = true;
76          User lastAdmin = null;
77          for (Iterator i = users.iterator(); i.hasNext();) {
78              User user = (User) i.next();
79              if (change) {
80                  try {
81                      getUserDao().deleteUser(user.getId());
82                  } catch (LastAdminException e) {
83                      //remember last admin
84                      lastAdmin = user;
85                      e.printStackTrace();
86                  }
87              } else {
88                  try {
89                      getUserDao().deleteUser(user);
90                  } catch (LastAdminException e) {
91                      // remember last admin
92                      lastAdmin = user;
93                      e.printStackTrace();
94                  }
95              }
96              change = !change;
97          }
98          List emptyUsers = getUserDao().getUsers();
99          assertTrue("delete failed ", emptyUsers.size() == 1);
100       
101         //
102         // (3) Save all users (set version to null to save not update)
103         //
104         for (Iterator i = users.iterator(); i.hasNext();) {
105             
106             User user =(User)i.next();
107             if (! user.getId().equals(lastAdmin.getId())) {
108 	            user.setVersion(null);
109 	           
110 	            user.setOffers(null);
111 	        	user.setComments(null);
112 	            user.getAddress().setVersion(null);
113 	            user.getOrganisation().setVersion(null);
114 	            user.getOrganisation().getAddress().setVersion(null);
115                 if (user.getResume() != null) {
116                     user.getResume().setVersion(null);
117                 }
118 	            user.getContact().setVersion(null);
119 	            try {
120 	                getUserDao().store(user);
121 	            } catch (DuplicateUserNameException e1) {
122 	                assertTrue("duplicated username in save", false);
123 	                e1.printStackTrace();
124 	            } catch (LastAdminException e) {
125                     assertTrue("Last admin username in save", false);
126                 }
127             }
128         }
129         List foundUsers = getUserDao().getUsers();
130         assertTrue("save failed, amount of found objects not 5 but " + foundUsers.size(), foundUsers.size() == 5);
131         assertDBAsExpected(TABLE_NAMES, IGNORE);
132 
133     }
134 
135 
136     /***
137      * Test Duplicated username exception.
138      *
139      * @throws DataAccessException Thrown when a database exceptions occurs.
140      */
141     public void testDuplicatedUserName() throws DataAccessException {
142 
143         //  Get a user by id
144         User user = getUserDao().getUser(new Integer(100));
145         //create new identical user
146         User newUser = new User(user.getUserName(),user.getContact().getEmail(),user.getPassword(), user.getRole());
147         //save the new user
148         try {
149             getUserDao().store(newUser);
150             assertTrue("Duplicated username could be stored", false);
151         } catch (DuplicateUserNameException e1) {
152             assertTrue("no duplicated username in test duplicated username", true);
153             e1.printStackTrace();
154         } catch (LastAdminException e) {
155             assertTrue("Last admin username in save", false);
156         }
157     }
158 
159     /***
160      * Test delete last admin exception.
161      *
162      * @throws DataAccessException Thrown when a database exceptions occurs.
163      */
164     public void testDeleteLastAdmin() throws DataAccessException {
165 
166         try {
167             getUserDao().deleteUser(new Integer(101));
168         } catch (LastAdminException e) {
169         	fail("Last admin exception:"+e.getMessage());
170             e.printStackTrace();
171         }
172         boolean exception = false;
173         try {
174             getUserDao().deleteUser(new Integer(102));
175         } catch (LastAdminException e) {
176             exception = true;
177             e.printStackTrace();
178         }
179         assertTrue("no last admin exception occurred", exception);
180 
181     }
182 
183     /***
184      * Test update read.
185      *
186      * @throws DataAccessException Thrown when a database exceptions occurs.
187      */
188     public void testUpdateReadUser() throws DataAccessException {
189         // Get a user by id
190         // TODO if this fails when Comments are cached again check AbstractSpringContextDBUnitTests TODO
191         User foundUser = getUserDao().getUser(new Integer(100));
192         assertEquals("error in get by id", foundUser.getFirstName(), "Sven");
193         assertEquals("error in get by id", foundUser.getId(), new Integer(100));
194         // Update the User
195         foundUser.setFirstName("update");
196         try {
197             getUserDao().store(foundUser);
198         } catch (DuplicateUserNameException e1) {
199             assertTrue("duplicated username in update", false);
200             e1.printStackTrace();
201         } catch (LastAdminException e) {
202             assertTrue("Last admin username in save", false);
203         }
204         User newUser = getUserDao().getUser(new Integer(100));
205         assertEquals("error in update", newUser.getFirstName(), "update");
206         assertEquals("error in update", newUser.getId(), new Integer(100));
207     }
208 
209     /***
210      * {@inheritDoc}
211      */
212     public void onSetUpInTransaction() throws Exception {
213         init(TABLE_NAMES);
214 
215     }
216 }
217 
218 /*** 
219  * $Log: UserDaoImplTests.java,v $
220  * Revision 1.17  2005/12/20 15:36:47  shally
221  * CheckStyle and PMD changes.
222  *
223  * Revision 1.16  2005/09/13 08:11:06  schauwvliege
224  * organize imports
225  *
226  * Revision 1.15  2005/08/24 08:47:08  ge0ffrey
227  * fix for added resume fix
228  *
229  * Revision 1.14  2005/08/24 08:12:19  schauwvliege
230  * addded resume
231  *
232  * Revision 1.13  2005/08/17 09:09:30  schauwvliege
233  * Fixed all unit tests
234  *
235  * Revision 1.12  2005/08/12 14:15:20  ge0ffrey
236  * cache vs transactions in tests
237  *
238  * Revision 1.11  2005/08/11 13:31:35  bavo_jcs
239  * LastAdmin Fix
240  *
241  * Revision 1.10  2005/08/03 13:16:01  bme_jcs
242  * getDao's removed and storeObject renamed to store
243  *
244  * Revision 1.9  2005/08/03 09:10:59  ge0ffrey
245  * JAVACAREERS-247
246  *
247  * Revision 1.8  2005/07/06 12:10:10  schauwvliege
248  * added person/contact and location to model
249  *
250  * Revision 1.7  2005/07/05 15:09:29  schauwvliege
251  * added person/contact and location to model
252  *
253  * Revision 1.6  2005/06/29 09:00:26  psong09
254  * comment component updated
255  *
256  * Revision 1.5  2005/06/14 13:34:55  schauwvliege
257  * fixed tests
258  *
259  * Revision 1.4  2005/06/14 12:05:54  schauwvliege
260  * CheckStyle and fixing tests
261  *
262  * Revision 1.3  2005/06/14 07:46:48  schauwvliege
263  * fixed broken tests
264  *
265  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
266  * Fix initial import
267  *
268  * Revision 1.5  2005/06/07 12:23:43  ssc
269  * Delete last admin exception
270  *
271  * Revision 1.4  2005/06/07 12:12:56  ssc
272  * Delete last admin exception
273  *
274  * Revision 1.3  2005/06/03 13:47:12  ssc
275  * added UserTYpe
276  *
277  * Revision 1.2  2005/06/03 13:33:32  ssc
278  * added UserTYpe
279  *
280  * Revision 1.1  2005/05/25 11:06:53  ssc
281  * added DBUnit tests and fixed some errors
282  * 
283  **/