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.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import net.sf.ehcache.CacheException;
24  import net.sf.ehcache.CacheManager;
25  
26  import org.bejug.javacareers.jobs.common.AbstractSpringContextDBUnitTests;
27  import org.bejug.javacareers.jobs.model.JobOffer;
28  import org.springframework.dao.DataAccessException;
29  
30  /***
31   * @author Sven Schauwvliege (Last modified by $Author: shally $)
32   * @version $Revision: 1.14 $ - $Date: 2005/12/21 11:38:42 $
33   */
34  
35  
36  public class JobOfferDaoImplTests extends AbstractSpringContextDBUnitTests {
37  
38      /***
39       * the names of the tables need to be compaired
40       */
41      private static final String[] TABLE_NAMES = {"Resume", "Parameter", "Country", "Region", "Address", "Contact", "Organisation", "Person", "User", "Offer",
42              								"JobOffer", "Offer_OfferType", "Offer_Profile"};
43      /***
44       * the columns to ignore.
45       */
46      private static final String[] IGNORE = {"id", "modificationdate", "addressId", "contactId"};
47      /***
48       * the jobofferdao, injected through field-injection.
49       */
50      protected JobOfferDao jobOfferDao;
51  
52      /***
53       * @return Returns the jobOfferDao.
54       */
55      public JobOfferDao getJobOfferDao() {
56          return jobOfferDao;
57      }
58  
59      /***
60       * Test the Create, Read, Read all, Update and Delete functions for a JobOffer.
61       *
62       * @throws DataAccessException Thrown when a database exceptions occurs.
63       */
64      public void testCRUDOffer() throws DataAccessException {
65  
66          //
67          // (1) Read al offers from the database to a list.
68          //
69          List offers = jobOfferDao.getOffers();
70          assertTrue("read failed, amount of found objects not 5 but " + (offers.size() + 1), offers.size() == 5);
71          assertTrue("read failed ", offers.get(0).getClass() == JobOffer.class);
72   
73          //
74          // (2) delete al offers 
75          //
76          boolean change = true;
77          for (Iterator i = offers.iterator(); i.hasNext();) {
78              JobOffer jobOffer = (JobOffer) i.next();
79              if (change) {
80                  getJobOfferDao().deleteOffer(jobOffer.getId());
81              } else {
82                  getJobOfferDao().deleteOffer(jobOffer);
83              }
84              change = !change;
85          }
86          List emptyJobOffers = getJobOfferDao().getOffers();
87          assertTrue("delete faled ", emptyJobOffers.size() == 0);
88        
89          //
90          // (3) Save all offers (set version to null to save not update)
91          //
92          for (Iterator i = offers.iterator(); i.hasNext();) {
93              JobOffer jobOffer = (JobOffer) i.next();
94              jobOffer.setVersion(null);
95              jobOffer.setId(null);
96              getJobOfferDao().store(jobOffer);
97          }
98          List foundJobOffers = getJobOfferDao().getOffers();
99          assertTrue("save failed, amount of found objects not 5 but " + foundJobOffers.size() + 1, foundJobOffers.size() == 5);
100         assertDBAsExpected(TABLE_NAMES, IGNORE);
101         
102    
103         
104 
105     }
106 
107     /***
108      * Test the Read and Update functions for a JobOffer.
109      *
110      * @throws DataAccessException Thrown when a database exceptions occurs.
111      */
112     public void testUpdateReadJobOffer() throws DataAccessException {
113         // Get a jobOffer by id
114         JobOffer foundJobOffer = (JobOffer) getJobOfferDao().getOffer(new Integer(500));
115         assertEquals("error in get by id", foundJobOffer.getTitle(), "Java developer");
116         assertEquals("error in get by id", foundJobOffer.getId(), new Integer(500));
117         
118         // Update the JobOffer
119         foundJobOffer.setTitle("update");
120         getJobOfferDao().store(foundJobOffer);
121         JobOffer newJobOffer = (JobOffer) getJobOfferDao().getOffer(new Integer(500));
122         assertEquals("error in update", newJobOffer.getTitle(), "update");
123         assertEquals("error in update", newJobOffer.getId(), new Integer(500));
124     }
125     
126     
127     /***
128      * @throws CacheException in case of error
129      */
130     public void testCaches() throws CacheException {
131     	CacheManager cacheManager = CacheManager.getInstance();
132 
133     	
134 
135     	String[] cacheNames = cacheManager.getCacheNames();
136     	List cacheList = new ArrayList();
137     	for (int i = 0; i < cacheNames.length; i++)
138     	{
139     	cacheList.add(cacheManager.getCache(cacheNames[i]));
140     	}
141     }
142 
143     /***
144      * {@inheritDoc}
145      */
146     public void onSetUpInTransaction() throws Exception {
147         init(TABLE_NAMES);
148 
149     }
150 }
151 
152 /*** 
153  * $Log: JobOfferDaoImplTests.java,v $
154  * Revision 1.14  2005/12/21 11:38:42  shally
155  * *** empty log message ***
156  *
157  * Revision 1.13  2005/12/09 10:46:57  shally
158  * Opkuis voor checkstyle en PMD
159  *
160  * Revision 1.12  2005/12/08 14:53:46  shally
161  * Opkuis voor checkstyle.
162  *
163  * Revision 1.11  2005/08/24 08:12:19  schauwvliege
164  * addded resume
165  *
166  * Revision 1.10  2005/08/04 15:28:53  bme_jcs
167  * introduction of second level cache for hibernate
168  *
169  * Revision 1.9  2005/08/04 14:52:00  psong09
170  * Removed test methods
171  *
172  * Revision 1.8  2005/08/03 13:16:03  bme_jcs
173  * getDao's removed and storeObject renamed to store
174  *
175  * Revision 1.7  2005/08/03 09:10:59  ge0ffrey
176  * JAVACAREERS-247
177  *
178  * Revision 1.6  2005/07/06 12:10:10  schauwvliege
179  * added person/contact and location to model
180  *
181  * Revision 1.5  2005/07/05 15:09:30  schauwvliege
182  * added person/contact and location to model
183  *
184  * Revision 1.4  2005/06/29 09:00:26  psong09
185  * comment component updated
186  *
187  * Revision 1.3  2005/06/14 13:34:56  schauwvliege
188  * fixed tests
189  *
190  * Revision 1.2  2005/06/09 08:19:04  bejug_cc
191  * Fix initial import
192  *
193  * Revision 1.1  2005/05/25 11:06:53  ssc
194  * added DBUnit tests and fixed some errors
195  *
196  * Revision 1.1  2005/05/18 15:58:52  ssc
197  * added DBUnitTest for JobOfferImplTests
198  * 
199  **/