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