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.Country;
24  import org.bejug.javacareers.jobs.model.Region;
25  import org.springframework.dao.DataAccessException;
26  
27  /***
28   *
29   * @author Sven Schauwvliege (Last modified by $Author: shally $)
30   * @version $Revision: 1.5 $ - $Date: 2005/12/20 15:36:47 $
31   */
32  
33  
34  public class CountryDaoImplTests extends AbstractSpringContextDBUnitTests {
35      
36  
37      /***
38       * the names of the tables need to be compaired.
39       */
40      private static final String[] TABLE_NAMES = { "Country", "Region"};
41      
42      /***
43       * the columns to ignore.
44       */
45      private static final String[] IGNORE = {"id","modificationdate" };
46      /***
47       * the countrydao, injected through field-injection.
48       */
49      protected CountryDao countryDao;
50      
51      /***
52       * @return Returns the countryDao.
53       */
54      public CountryDao getCountryDao() {
55          return countryDao;
56      }
57      
58     
59      
60      /***
61       * Test the Create, Read, Read all, Update and Delete functions for a Country.
62       *
63       * @throws DataAccessException Thrown when a database exceptions occurs.
64       */
65      public void testCRUDCountry() throws DataAccessException {
66          //
67          // (1) Read al countrys from the database to a list.
68          //
69          List countrys = getCountryDao().getCountries();
70          assertTrue("read failed, amount of found objects not 1 but " + (countrys.size()), countrys.size() == 1);
71          assertTrue("read failed ", countrys.get(0) instanceof Country);
72          
73          
74          //
75          // (2) delete al countrys by id and by object (change boolean to test both delete methods)
76          //
77          boolean change = true;
78          for (Iterator i = countrys.iterator(); i.hasNext();) {
79              Country country =(Country)i.next();
80              if (change){
81                  getCountryDao().deleteCountry(country.getId());     
82              } else {              
83                  getCountryDao().deleteCountry(country);        
84              }
85              change = !change;          
86          }
87          List emptyCountrys = getCountryDao().getCountries();
88          assertTrue("delete failed ", emptyCountrys.size() == 0);
89        
90          //
91          // (3) Save all countrys (set version to null to save not update)
92          //
93          for (Iterator i = countrys.iterator(); i.hasNext();) {        
94              Country country =(Country)i.next();
95  	        for (Iterator r = country.getRegion().iterator(); r.hasNext();) {
96  	            ((Region) r.next()).setVersion(null);
97  	        }
98              getCountryDao().store(country); 
99          }
100         List foundCountrys = getCountryDao().getCountries();
101         assertTrue("save failed, amount of found objects not 1 but " + foundCountrys.size(), foundCountrys.size() == 1);
102         assertDBAsExpected(TABLE_NAMES, IGNORE);
103        
104     }  
105     
106     
107     
108     /***
109      * Test update read.
110      *
111      * @throws DataAccessException Thrown when a database exceptions occurs.
112      */
113     public void testUpdateReadCountry() throws DataAccessException {
114 	    // Get a country by id
115 	    Country foundCountry = getCountryDao().getCountry(new Integer(1));
116 	    assertEquals("error in get by id", foundCountry.getName(), "Belgium");
117 	    assertEquals("error in get by id", foundCountry.getId(), new Integer(1));
118 	    // Update the Country
119 	    foundCountry.setName("update");
120 	    
121 	    getCountryDao().store(foundCountry);
122 	    
123 	    Country newCountry = getCountryDao().getCountry(new Integer(1));
124 	    assertEquals("error in update", newCountry.getName(), "update");
125 	    assertEquals("error in update", newCountry.getId(), new Integer(1));
126     }
127     
128     /*** 
129      * {@inheritDoc}
130      */
131     public void onSetUpInTransaction() throws Exception {
132         init(TABLE_NAMES);
133         
134     }
135 }
136 
137 /*** 
138  * $Log: CountryDaoImplTests.java,v $
139  * Revision 1.5  2005/12/20 15:36:47  shally
140  * CheckStyle and PMD changes.
141  *
142  * Revision 1.4  2005/09/13 08:11:06  schauwvliege
143  * organize imports
144  *
145  * Revision 1.3  2005/08/03 13:16:02  bme_jcs
146  * getDao's removed and storeObject renamed to store
147  *
148  * Revision 1.2  2005/08/03 09:10:59  ge0ffrey
149  * JAVACAREERS-247
150  *
151  * Revision 1.1  2005/07/05 15:18:11  schauwvliege
152  * added person/contact and location to model
153  *
154  * 
155  **/