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.AcademicEducationOffer;
24  import org.springframework.dao.DataAccessException;
25  
26  /***
27   *
28   * @author Sven Schauwvliege (Last modified by $Author: shally $)
29   * @version $Revision: 1.11 $ - $Date: 2005/12/09 10:46:57 $
30   */
31  
32  
33  public class AcademicEducationOfferDaoImplTests extends AbstractSpringContextDBUnitTests {
34      
35      /***
36       * the names of the tables need to be compaired
37       */
38      private static final String[] TABLE_NAMES = {"Resume", "Parameter", "Country", "Region", "Address","Contact", "Organisation", "Person", "User", "Offer",
39               "EducationOffer", "AcademicEducationOffer", "Offer_Profile","Offer_OfferType"};
40      /***
41       * the columns to ignore.
42       */
43      private static final String[] IGNORE = {"id","modificationdate", "addressid", "contactId"};
44      /***
45       * the jobofferdao, injected through field-injection.
46       */
47      protected OfferDao academicEducationOfferDao;
48      
49      /***
50       * @return Returns the academicEducationOfferDao.
51       */
52      public OfferDao getAcademicEducationOfferDao() {
53          return academicEducationOfferDao;
54      }
55      
56      /***
57       * Test the Create, Read, Read all, Update and Delete functions for a AcademicEducationOffer.
58       *
59       * @throws DataAccessException Thrown when a database exceptions occurs.
60       */
61      public void testCRUDOffer() throws DataAccessException {
62          //
63          // (1) Read al offers from the database to a list.
64          //
65          List offers = academicEducationOfferDao.getOffers();
66          assertTrue("read failed, amount of found objects not 5 but " + offers.size() , offers.size() == 5);
67          assertTrue("read failed ", offers.get(0).getClass() == AcademicEducationOffer.class);
68            
69          //
70          // (2) delete al offers 
71          //
72          boolean change = true;
73          for (Iterator i = offers.iterator(); i.hasNext();) {
74              AcademicEducationOffer academicEducationOffer =(AcademicEducationOffer)i.next();
75              if (change){
76              	
77                  getAcademicEducationOfferDao().deleteOffer(academicEducationOffer.getId());
78              } else {
79                  getAcademicEducationOfferDao().deleteOffer(academicEducationOffer);
80              }
81              change = !change;          
82          }
83          
84          List emptyAcademicEducationOffers = getAcademicEducationOfferDao().getOffers();
85          assertTrue("delete faled ", emptyAcademicEducationOffers.size() == 0);
86        
87          //
88          // (3) Save all offers (set version to null to save not update)
89          //
90          for (Iterator i = offers.iterator(); i.hasNext();) {
91              AcademicEducationOffer academicEducationOffer =(AcademicEducationOffer)i.next();
92              academicEducationOffer.setVersion(null);
93              academicEducationOffer.setId(null);
94              academicEducationOffer.getAddress().setVersion(null);
95              academicEducationOffer.getAddress().setId(null);
96              academicEducationOffer.setAddress(null);            
97              getAcademicEducationOfferDao().store(academicEducationOffer);
98          }
99          //List foundAcademicEducationOffers =getAcademicEducationOfferDao().getOffers();
100         assertTrue("save failed, amount of found objects not 5 but " + offers.size() + 1, offers.size() == 5);
101         assertDBAsExpected(TABLE_NAMES, IGNORE );
102     }
103     
104     /***
105      * Test the update/ get by id.
106      * @throws DataAccessException Thrown when a database exceptions occurs.
107      */
108     public void testUpdate() throws DataAccessException {
109 
110         // Get a academicEducationOffer by id
111         AcademicEducationOffer foundAcademicEducationOffer = (AcademicEducationOffer)getAcademicEducationOfferDao().getOffer(new Integer(500));
112         assertEquals("error in get by id", foundAcademicEducationOffer.getTitle(), "Java developer");
113         assertEquals("error in get by id", foundAcademicEducationOffer.getId(), new Integer(500));
114         
115         // Update the AcademicEducationOffer
116         foundAcademicEducationOffer.setTitle("update");
117         getAcademicEducationOfferDao().store(foundAcademicEducationOffer);
118         AcademicEducationOffer newAcademicEducationOffer = (AcademicEducationOffer)getAcademicEducationOfferDao().getOffer(new Integer(500));
119         assertEquals("error in update", newAcademicEducationOffer.getTitle(), "update");
120         assertEquals("error in update", newAcademicEducationOffer.getId(), new Integer(500));
121     }
122     
123     /*** 
124      * {@inheritDoc}
125      */
126     public void onSetUpInTransaction() throws Exception {
127         init(TABLE_NAMES);
128         
129     }
130     
131     
132 
133 }
134 /*** 
135  * $Log: AcademicEducationOfferDaoImplTests.java,v $
136  * Revision 1.11  2005/12/09 10:46:57  shally
137  * Opkuis voor checkstyle en PMD
138  *
139  * Revision 1.10  2005/08/24 08:12:19  schauwvliege
140  * addded resume
141  *
142  * Revision 1.9  2005/08/17 09:09:30  schauwvliege
143  * Fixed all unit tests
144  *
145  * Revision 1.8  2005/08/03 13:16:03  bme_jcs
146  * getDao's removed and storeObject renamed to store
147  *
148  * Revision 1.7  2005/08/03 09:10:58  ge0ffrey
149  * JAVACAREERS-247
150  *
151  * Revision 1.6  2005/07/06 12:10:10  schauwvliege
152  * added person/contact and location to model
153  *
154  * Revision 1.5  2005/07/05 15:09:31  schauwvliege
155  * added person/contact and location to model
156  *
157  * Revision 1.3  2005/06/14 13:34:56  schauwvliege
158  * fixed tests
159  *
160  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
161  * Fix initial import
162  *
163  * Revision 1.1  2005/05/25 11:06:53  ssc
164  * added DBUnit tests and fixed some errors
165  * 
166  **/