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.ConsultingServiceOffer;
24  import org.springframework.dao.DataAccessException;
25  
26  /***
27   * @author Sven Schauwvliege (Last modified by $Author: shally $)
28   * @version $Revision: 1.10 $ - $Date: 2005/12/09 10:46:57 $
29   */
30  
31  
32  public class ConsultingServiceOfferDaoImplTests 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", "ServiceOffer",
38              "ConsultingServiceOffer", "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 consultingServiceOfferDao;
47  
48      /***
49       * @return Returns the consultingServiceOfferDao.
50       */
51      public OfferDao getConsultingServiceOfferDao() {
52          return consultingServiceOfferDao;
53      }
54  
55      /***
56       * Test the Create, Read, Read all, Update and Delete functions for a ConsultingServiceOffer.
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 = consultingServiceOfferDao.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() == ConsultingServiceOffer.class);
67          
68          //
69          // (2) delete al offers 
70          //
71          boolean change = true;
72          for (Iterator i = offers.iterator(); i.hasNext();) {
73              ConsultingServiceOffer consultingServiceOffer = (ConsultingServiceOffer) i.next();
74              if (change) {
75                  getConsultingServiceOfferDao().deleteOffer(consultingServiceOffer.getId());
76              } else {
77                  getConsultingServiceOfferDao().deleteOffer(consultingServiceOffer);
78              }
79              change = !change;
80          }
81          List emptyConsultingServiceOffers = getConsultingServiceOfferDao().getOffers();
82          assertTrue("delete faled ", emptyConsultingServiceOffers.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              ConsultingServiceOffer consultingServiceOffer = (ConsultingServiceOffer) i.next();
89              consultingServiceOffer.setVersion(null);
90              consultingServiceOffer.setId(null);
91              getConsultingServiceOfferDao().store(consultingServiceOffer);
92          }
93          List foundConsultingServiceOffers = getConsultingServiceOfferDao().getOffers();
94          assertTrue("save failed, amount of found objects not 5 but " + foundConsultingServiceOffers.size() + 1, foundConsultingServiceOffers.size() == 5);
95  
96          assertDBAsExpected(TABLE_NAMES, IGNORE);
97  
98      }
99  
100     /***
101      * Test the update/ get by id.
102      *
103      * @throws DataAccessException Thrown when a database exceptions occurs.
104      */
105     public void testUpdate() throws DataAccessException {
106 
107         // (4) Get a consultingServiceOffer by id
108         ConsultingServiceOffer foundConsultingServiceOffer = (ConsultingServiceOffer) getConsultingServiceOfferDao().getOffer(new Integer(500));
109         assertEquals("error in get by id", foundConsultingServiceOffer.getTitle(), "Java developer");
110         assertEquals("error in get by id", foundConsultingServiceOffer.getId(), new Integer(500));
111         
112         // (5) Update the ConsultingServiceOffer
113         foundConsultingServiceOffer.setTitle("update");
114         getConsultingServiceOfferDao().store(foundConsultingServiceOffer);
115         ConsultingServiceOffer newConsultingServiceOffer = (ConsultingServiceOffer) getConsultingServiceOfferDao().getOffer(new Integer(500));
116         assertEquals("error in update", newConsultingServiceOffer.getTitle(), "update");
117         assertEquals("error in update", newConsultingServiceOffer.getId(), new Integer(500));
118     }
119 
120     /***
121      * {@inheritDoc}
122      */
123     public void onSetUpInTransaction() throws Exception {
124         init(TABLE_NAMES);
125 
126     }
127 
128 
129 }
130 
131 /***
132  * $Log: ConsultingServiceOfferDaoImplTests.java,v $
133  * Revision 1.10  2005/12/09 10:46:57  shally
134  * Opkuis voor checkstyle en PMD
135  *
136  * Revision 1.9  2005/08/24 08:12:19  schauwvliege
137  * addded resume
138  *
139  * Revision 1.8  2005/08/03 13:16:02  bme_jcs
140  * getDao's removed and storeObject renamed to store
141  *
142  * Revision 1.7  2005/08/03 09:10:59  ge0ffrey
143  * JAVACAREERS-247
144  *
145  * Revision 1.6  2005/07/06 12:10:10  schauwvliege
146  * added person/contact and location to model
147  *
148  * Revision 1.5  2005/07/05 15:09:30  schauwvliege
149  * added person/contact and location to model
150  *
151  * Revision 1.4  2005/06/29 09:00:26  psong09
152  * comment component updated
153  *
154  * Revision 1.3  2005/06/14 13:34:56  schauwvliege
155  * fixed tests
156  *
157  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
158  * Fix initial import
159  *
160  * Revision 1.1  2005/05/25 11:06:53  ssc
161  * added DBUnit tests and fixed some errors
162  * 
163  **/