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.jobs.common.AbstractSpringContextDBUnitTests;
23  import org.bejug.javacareers.jobs.model.OfferType;
24  import org.bejug.javacareers.jobs.model.OrganisationType;
25  import org.bejug.javacareers.jobs.model.Profile;
26  import org.springframework.dao.DataAccessException;
27  
28  /***
29   * @author Sven Schauwvliege (Last modified by $Author: bme_jcs $)
30   * @version $Revision: 1.6 $ - $Date: 2005/08/03 13:16:02 $
31   */
32  
33  
34  public class ParameterDaoImplTests extends AbstractSpringContextDBUnitTests {
35  
36      /***
37       * the names of the tables need to be compaired
38       */
39      private static final String[] TABLE_NAMES = {"Parameter"};
40      private static final String[] IGNORE = {"id", "modificationdate"};
41  
42      /***
43       * the offerTypeDao. Is been injected through field-injection.
44       */
45      protected ParameterDao offerTypeDao;
46  
47      /***
48       * the profileDao. Is been injected through field-injection.
49       */
50      protected ParameterDao profileDao;
51  
52      /***
53       * the organisationTypeDao. Is been injected through field-injection.
54       */
55      protected ParameterDao organisationTypeDao;
56  
57      /***
58       * @return the wanted offerTypeDao
59       */
60      public ParameterDao getOfferTypeDao() {
61          return this.offerTypeDao;
62      }
63  
64      /***
65       * @return the wanted organisationTypeDao
66       */
67      public ParameterDao getOrganisationTypeDao() {
68          return this.organisationTypeDao;
69      }
70  
71      /***
72       * @return the wanted profileDao
73       */
74      public ParameterDao getProfileDao() {
75          return this.profileDao;
76      }
77  
78      /***
79       * Test the Create, Read all and Delete functions for a OfferType.
80       *
81       * @throws DataAccessException Thrown when a database exceptions occurs.
82       */
83      public void testCRDOfferType() throws DataAccessException {
84  
85          //
86          // (1) Read al offers from the database to a list.
87          //
88          List offers = offerTypeDao.getParameters();
89          assertTrue("read failed, amount of found objects not 5 but "
90                  + (offers.size()), offers.size() == 6);
91          assertTrue("read failed ", offers.get(0).getClass() == OfferType.class);
92   
93          //
94          // (2) delete al offers 
95          //
96          boolean change = true;
97          for (Iterator i = offers.iterator(); i.hasNext();) {
98              OfferType offerType = (OfferType) i.next();
99              if (change) {
100                 getOfferTypeDao().deleteParameter(offerType.getId());
101             } else {
102                 getOfferTypeDao().deleteParameter(offerType);
103             }
104             change = !change;
105         }
106         List emptyOfferTypes = getOfferTypeDao().getParameters();
107         assertTrue("delete faled ", emptyOfferTypes.size() == 0);
108       
109         //
110         // (3) Save all offers (set version to null to save not update)
111         //
112         for (Iterator i = offers.iterator(); i.hasNext();) {
113             OfferType offerType = (OfferType) i.next();
114             offerType.setVersion(null);
115             offerType.setId(null);
116             getOfferTypeDao().store(offerType);
117         }
118         List foundOfferTypes = getOfferTypeDao().getParameters();
119         assertTrue("save failed, amount of found objects not 5 but " + foundOfferTypes.size(), foundOfferTypes.size() == 6);
120 
121         assertDBAsExpected(TABLE_NAMES, IGNORE);
122 
123     }
124 
125     /***
126      * Test the update/ get by id.
127      *
128      * @throws DataAccessException Thrown when a database exceptions occurs.
129      */
130     public void testUpdateRead() throws DataAccessException {
131 
132         // Get a offerType by id
133         OfferType foundOfferType = (OfferType) getOfferTypeDao().getParameter(new Integer(1000));
134         assertEquals("error in get by id", foundOfferType.getName(), "Architect");
135         assertEquals("error in get by id", foundOfferType.getId(), new Integer(1000));
136 
137         // Update the OfferType
138         foundOfferType.setName("update");
139         getOfferTypeDao().store(foundOfferType);
140         OfferType newOfferType = (OfferType) getOfferTypeDao().getParameter(new Integer(1000));
141         assertEquals("error in update", newOfferType.getName(), "update");
142         assertEquals("error in update", newOfferType.getId(), new Integer(1000));
143         foundOfferType.setName("Architect");
144     }
145 
146 
147     /***
148      * Test the Create, Read all and Delete functions for a OrganisationType.
149      *
150      * @throws DataAccessException Thrown when a database exceptions occurs.
151      */
152     public void testCRDOrganisationType() throws DataAccessException {
153 
154         //
155         // (1) Read al offers from the database to a list.
156         //
157         List offers = organisationTypeDao.getParameters();
158         assertTrue("read failed, amount of found objects not 5 but " + (offers.size() + 1), offers.size() == 6);
159         assertTrue("read failed ", offers.get(0).getClass() == OrganisationType.class);
160            
161         //
162         // (2) delete al offers 
163         //
164         boolean change = true;
165         for (Iterator i = offers.iterator(); i.hasNext();) {
166             OrganisationType organisationType = (OrganisationType) i.next();
167             if (change) {
168                 getOrganisationTypeDao().deleteParameter(organisationType.getId());
169             } else {
170                 getOrganisationTypeDao().deleteParameter(organisationType);
171             }
172             change = !change;
173         }
174         List emptyOrganisationTypes = getOrganisationTypeDao().getParameters();
175         assertTrue("delete faled ", emptyOrganisationTypes.size() == 0);
176       
177         //
178         // (3) Save all offers (set version to null to save not update)
179         //
180         for (Iterator i = offers.iterator(); i.hasNext();) {
181             OrganisationType organisationType = (OrganisationType) i.next();
182             organisationType.setVersion(null);
183             organisationType.setId(null);
184             getOrganisationTypeDao().store(organisationType);
185         }
186         List foundOrganisationTypes = getOrganisationTypeDao().getParameters();
187         assertTrue("save failed, amount of found objects not 5 but " + foundOrganisationTypes.size() + 1, foundOrganisationTypes.size() == 6);
188 
189         assertDBAsExpected(TABLE_NAMES, IGNORE);
190 
191     }
192 
193     /***
194      * Test the update/ get by id.
195      *
196      * @throws DataAccessException Thrown when a database exceptions occurs.
197      */
198     public void testUpdateReadOrganisationType() throws DataAccessException {
199 
200         // Get a organisationType by id
201         OrganisationType foundOrganisationType = (OrganisationType) getOrganisationTypeDao().getParameter(new Integer(2000));
202         assertEquals("error in get by id", foundOrganisationType.getName(), "Universiteit");
203         assertEquals("error in get by id", foundOrganisationType.getId(), new Integer(2000));
204 
205         // Update the OrganisationType
206         foundOrganisationType.setName("update");
207         getOrganisationTypeDao().store(foundOrganisationType);
208         OrganisationType newOrganisationType = (OrganisationType) getOrganisationTypeDao().getParameter(new Integer(2000));
209         assertEquals("error in update", newOrganisationType.getName(), "update");
210         assertEquals("error in update", newOrganisationType.getId(), new Integer(2000));
211         foundOrganisationType.setName("Architect");
212     }
213 
214     /***
215      * Test the Create, Read all and Delete functions for a Profile.
216      *
217      * @throws DataAccessException Thrown when a database exceptions occurs.
218      */
219     public void testCRDProfile() throws DataAccessException {
220 
221         //
222         // (1) Read al offers from the database to a list.
223         //
224         List offers = profileDao.getParameters();
225         assertTrue("read failed, amount of found objects not 5 but " + (offers.size() + 1), offers.size() == 6);
226         assertTrue("read failed ", offers.get(0).getClass() == Profile.class);
227    
228         //
229         // (2) delete al offers 
230         //
231         boolean change = true;
232         for (Iterator i = offers.iterator(); i.hasNext();) {
233             Profile profile = (Profile) i.next();
234             if (change) {
235                 getProfileDao().deleteParameter(profile.getId());
236             } else {
237                 getProfileDao().deleteParameter(profile);
238             }
239             change = !change;
240         }
241         List emptyProfiles = getProfileDao().getParameters();
242         assertTrue("delete faled ", emptyProfiles.size() == 0);
243       
244         //
245         // (3) Save all offers (set version to null to save not update)
246         //
247         for (Iterator i = offers.iterator(); i.hasNext();) {
248             Profile profile = (Profile) i.next();
249             profile.setVersion(null);
250             profile.setId(null);
251             getProfileDao().store(profile);
252         }
253         List foundProfiles = getProfileDao().getParameters();
254         assertTrue("save failed, amount of found objects not 5 but " + foundProfiles.size() + 1, foundProfiles.size() == 6);
255         assertDBAsExpected(TABLE_NAMES, IGNORE);
256     }
257 
258     /***
259      * Test the update/ get by id.
260      *
261      * @throws DataAccessException Thrown when a database exceptions occurs.
262      */
263     public void testUpdateReadProfile() throws DataAccessException {
264 
265         // Get a profile by id
266         Profile foundProfile = (Profile) getProfileDao().getParameter(new Integer(3000));
267         assertEquals("error in get by id", foundProfile.getName(), "Senior");
268         assertEquals("error in get by id", foundProfile.getId(), new Integer(3000));
269 
270         // Update the Profile
271         foundProfile.setName("update");
272         getProfileDao().store(foundProfile);
273         Profile newProfile = (Profile) getProfileDao().getParameter(new Integer(3000));
274         assertEquals("error in update", newProfile.getName(), "update");
275         assertEquals("error in update", newProfile.getId(), new Integer(3000));
276     }
277 
278 
279     /***
280      * {@inheritDoc}
281      */
282     public void onSetUpInTransaction() throws Exception {
283         init(TABLE_NAMES);
284     }
285 }
286 
287 /***
288  * $Log: ParameterDaoImplTests.java,v $
289  * Revision 1.6  2005/08/03 13:16:02  bme_jcs
290  * getDao's removed and storeObject renamed to store
291  *
292  * Revision 1.5  2005/08/03 09:10:59  ge0ffrey
293  * JAVACAREERS-247
294  *
295  * Revision 1.4  2005/06/29 09:00:26  psong09
296  * comment component updated
297  *
298  * Revision 1.3  2005/06/14 13:34:56  schauwvliege
299  * fixed tests
300  *
301  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
302  * Fix initial import
303  *
304  * Revision 1.3  2005/05/30 09:37:03  bme
305  * updated for the introduction of HQL in the hbm-files
306  *
307  * Revision 1.2  2005/05/26 15:52:19  ssc
308  * update
309  *
310  * Revision 1.1  2005/05/25 11:06:53  ssc
311  * added DBUnit tests and fixed some errors
312  * 
313  **/