Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/test/AuditedExtensionTest.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/test/AuditedExtensionTest.java,v 1.3 2003/09/30 15:13:19 joe Exp $
2    * $Revision: 1.3 $
3    * $Date: 2003/09/30 15:13:19 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.test;
31  
32  import com.RuntimeCollective.webapps.bean.Address;
33  import com.RuntimeCollective.webapps.bean.AuditedExtension;
34  import com.RuntimeCollective.webapps.bean.EntityBean;
35  import com.RuntimeCollective.webapps.bean.User;
36  import com.RuntimeCollective.webapps.bean.TrackedUser;
37  import com.RuntimeCollective.webapps.bean.SimpleTrackedUser;
38  import com.RuntimeCollective.webapps.test.WebappsTestCase;
39  import com.RuntimeCollective.webapps.IndexedEntityBeanStore;
40  import com.RuntimeCollective.webapps.RuntimeParameters;
41  import com.RuntimeCollective.webapps.RuntimeDataSource;
42  
43  import java.sql.SQLException;
44  import java.util.Date;
45  import java.util.Iterator;
46  import java.util.Vector;
47  
48  import junit.framework.Test;
49  import junit.framework.TestCase;
50  import junit.framework.TestSuite;
51  
52  
53  /** 
54   * Test class for AuditedExtension.
55   *
56   * $Id: AuditedExtensionTest.java,v 1.3 2003/09/30 15:13:19 joe Exp $
57   */
58  public class AuditedExtensionTest extends WebappsTestCase {
59  
60  
61  
62      public AuditedExtensionTest(String name) {
63          super(name);
64      }
65  
66      protected void setUp() {
67          resetBeanRegister();
68  
69          // default store
70          super.DEFAULT_STORE_CLASS = IndexedEntityBeanStore.class.getName();
71  
72          // register beans
73          registerBeanClass(Address.class.getName());
74          registerBeanClass(AuditedExtension.class.getName());
75          registerBeanClass(User.class.getName());
76          registerBeanClass(TrackedUser.class.getName());
77          registerBeanClass(SimpleTrackedUser.class.getName());
78  
79          super.setUp();
80      }
81  
82  
83      protected void tearDown() {    
84          super.tearDown();
85      }
86  
87  
88  
89  
90  
91  
92  
93      public void testAuditedExtension() {
94  
95    LogTestSection("Testing AuditedExtension");
96  
97    // get: existing AuditedExtensions
98    Iterator startAllAuditedExtension;
99    Iterator startAllAuditedExtensionIds;
100 
101   AuditedExtension existingAuditedExtension;
102   int next_id;
103 
104   try {
105       startAllAuditedExtension = RuntimeParameters.getStore().getAll(AuditedExtension.class.getName());
106       startAllAuditedExtensionIds = RuntimeParameters.getStore().getAllIds(AuditedExtension.class.getName());
107 
108       while (startAllAuditedExtensionIds.hasNext()) {
109     next_id = ((Integer)startAllAuditedExtensionIds.next()).intValue();
110     try {
111         existingAuditedExtension = (AuditedExtension) RuntimeParameters.getStore().refresh(AuditedExtension.class.getName(), next_id);
112     } catch (RuntimeException e) {
113         fail("We could not call RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.AuditedExtension\", next_id) with id "+next_id+", though the id was found in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\"): "+e);
114     }
115       }
116   } catch (SQLException e) {
117       fail("We could not call RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.AuditedExtension\") or RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\"): "+e);
118   }
119 
120   // get: non-existing AuditedExtension
121   int new_id = 0;
122   try {
123       new_id = RuntimeDataSource.nextId();
124       RuntimeParameters.getStore().get(AuditedExtension.class.getName(), new_id);
125       fail("We could do RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.AuditedExtension\", new_id) on a new id: "+new_id);
126   } catch (RuntimeException e) {
127       assertTrue("RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.AuditedExtension\", new_id) on a new id throws an exception: "+e, true);
128   } catch (SQLException e) {
129       throw new RuntimeException("RuntimeDataSource.nextId(\"RC\") throws an exception: "+e);
130   }
131 
132 
133   // getAll and getAllIds: add/delete a new empty AuditedExtension, and check that the sizes change
134   AuditedExtension new_AuditedExtension = null;
135   int startAuditedExtensionNo = 0;
136   int startAuditedExtensionIdsNo = 0;
137   boolean new_found;
138   int addedAuditedExtensionNo = 0;
139   int addedAuditedExtensionIdsNo = 0;
140   Iterator addedAllAuditedExtension;
141   Iterator addedAllAuditedExtensionIds;
142   Address tempBean = null;
143 
144   try {
145       startAllAuditedExtensionIds = RuntimeParameters.getStore().getAllIds(AuditedExtension.class.getName());
146       while (startAllAuditedExtensionIds.hasNext()) {
147     startAuditedExtensionIdsNo++;
148     startAllAuditedExtensionIds.next();
149       }
150       startAllAuditedExtension = RuntimeParameters.getStore().getAll(AuditedExtension.class.getName());
151       while (startAllAuditedExtension.hasNext()) {
152     startAuditedExtensionNo++;
153     startAllAuditedExtension.next();
154       }
155       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\") and RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.AuditedExtension\") produce the same number of items.", startAuditedExtensionNo, startAuditedExtensionIdsNo);
156 
157       new_AuditedExtension = (AuditedExtension) RuntimeParameters.getStore().create(AuditedExtension.class.getName());
158       new_id = new_AuditedExtension.getId();
159 
160       tempBean = (Address) RuntimeParameters.getStore().create(Address.class.getName());
161       new_AuditedExtension.setEntityBean(tempBean);
162       RuntimeParameters.getStore().save(Address.class.getName(), tempBean.getId());
163 
164       RuntimeParameters.getStore().save(AuditedExtension.class.getName(), new_id);
165       
166       new_found = false;
167       addedAllAuditedExtensionIds = RuntimeParameters.getStore().getAllIds(AuditedExtension.class.getName());
168       while (addedAllAuditedExtensionIds.hasNext()) {
169     addedAuditedExtensionIdsNo++;
170     if (new_id == ((Integer)addedAllAuditedExtensionIds.next()).intValue())
171         new_found = true;
172       }
173       assertTrue("We could not find a new saved AuditedExtension in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\").",new_found);
174 
175       new_found = false;
176       addedAllAuditedExtension = RuntimeParameters.getStore().getAll(AuditedExtension.class.getName());
177       while (addedAllAuditedExtension.hasNext()) {
178     addedAuditedExtensionNo++;
179     if (new_id == ((AuditedExtension)addedAllAuditedExtension.next()).getId())
180         new_found = true;
181       }
182       assertTrue("We could not find a new saved AuditedExtension in RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.AuditedExtension\").",new_found);
183 
184       assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.publication.bean.AuditedExtension\") doesn't increase by one in size after saving a new AuditedExtension: goes from "+startAuditedExtensionNo+" to "+addedAuditedExtensionNo+".", startAuditedExtensionNo + 1, addedAuditedExtensionNo);
185       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.publication.bean.AuditedExtension\") doesn't increase by one in size after saving a new AuditedExtension: goes from "+startAuditedExtensionIdsNo+" to "+addedAuditedExtensionIdsNo+".", startAuditedExtensionIdsNo + 1, addedAuditedExtensionIdsNo);
186 
187       RuntimeParameters.getStore().delete(AuditedExtension.class.getName(), new_id);
188 
189       addedAllAuditedExtensionIds = RuntimeParameters.getStore().getAllIds(AuditedExtension.class.getName());
190       addedAuditedExtensionIdsNo = 0;
191       while (addedAllAuditedExtensionIds.hasNext()) {
192     addedAuditedExtensionIdsNo++;
193     if (new_id == ((Integer)addedAllAuditedExtensionIds.next()).intValue())
194         fail("We could find a deleted AuditedExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\").");
195       }
196 
197       addedAllAuditedExtension = RuntimeParameters.getStore().getAll(AuditedExtension.class.getName());
198       addedAuditedExtensionNo = 0;
199       while (addedAllAuditedExtension.hasNext()) {
200     addedAuditedExtensionNo++;
201     if (new_id == ((AuditedExtension)addedAllAuditedExtension.next()).getId())
202         fail("We could find a deleted AuditedExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\").");
203       }
204 
205       assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.AuditedExtension\") doesn't go back to its original size after saving and deleting a new AuditedExtension: goes from "+startAuditedExtensionNo+" to "+addedAuditedExtensionNo+".", startAuditedExtensionNo, addedAuditedExtensionNo);
206       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.AuditedExtension\") doesn't go back to its original size after saving and deleting a new AuditedExtension: goes from "+startAuditedExtensionIdsNo+" to "+addedAuditedExtensionIdsNo+".", startAuditedExtensionIdsNo, addedAuditedExtensionIdsNo);
207 
208   } catch (RuntimeException e) {
209       fail("Creating new AuditedExtension and saving it throws an exception: "+e);
210   } catch (SQLException e) {
211       fail("Creating new AuditedExtension and saving it throws an exception: "+e);
212   } finally {
213       // cleanup
214       if (new_AuditedExtension != null) {
215             RuntimeParameters.getStore().delete(new_AuditedExtension);
216       }      
217       if (tempBean != null) {
218             RuntimeParameters.getStore().delete(tempBean);
219       }      
220   }
221 
222 
223 
224   // add/edit/delete a new AuditedExtension, check the save is not corrupting data
225   Address bean = null;
226     User creationUser = null;
227     Date creationDate = new Date();
228     User lastModifiedUser = null;
229     Date lastModifiedDate = new Date();
230 
231   try {
232       // create and set data
233       new_AuditedExtension = (AuditedExtension) RuntimeParameters.getStore().create(AuditedExtension.class.getName());
234       new_id = new_AuditedExtension.getId();
235 
236       bean = (Address) RuntimeParameters.getStore().create(Address.class.getName());
237 
238       creationUser = (User) RuntimeParameters.getStore().create(SimpleTrackedUser.class.getName());
239       creationUser.setFirstName("Test");
240       creationUser.setLastName("Name");
241       creationUser.setEmail(Math.random()+"test@runtime-collective.com");
242       creationUser.setPassword("changeme");
243 
244       lastModifiedUser = (User) RuntimeParameters.getStore().create(SimpleTrackedUser.class.getName());
245       lastModifiedUser.setFirstName("Test");
246       lastModifiedUser.setLastName("Name");
247       lastModifiedUser.setEmail(Math.random()+"test@runtime-collective.com");
248       lastModifiedUser.setPassword("changeme");
249 
250       new_AuditedExtension.setEntityBean(bean);
251         new_AuditedExtension.setCreationUser(creationUser);
252         new_AuditedExtension.setCreationDate(creationDate);
253         new_AuditedExtension.setLastModifiedUser(lastModifiedUser);
254         new_AuditedExtension.setLastModifiedDate(lastModifiedDate);
255 
256 
257       // save and reload the AuditedExtension
258       RuntimeParameters.getStore().save(Address.class.getName(), bean.getId());
259       RuntimeParameters.getStore().save(User.class.getName(), creationUser.getId());
260       RuntimeParameters.getStore().save(User.class.getName(), lastModifiedUser.getId());
261       RuntimeParameters.getStore().save(AuditedExtension.class.getName(), new_id);
262       new_AuditedExtension = (AuditedExtension) RuntimeParameters.getStore().refresh(AuditedExtension.class.getName(), new_id);
263 
264       // check basic fields
265       assertEquals("Saved and reloaded AuditedExtension doesn't have the same bean field anymore.",
266                      new_AuditedExtension.getEntityBean(), bean);
267 
268       assertEquals("Saved and reloaded AuditedExtension doesn't have the same creationUser anymore.",
269                      new_AuditedExtension.getCreationUser(), creationUser);
270 
271       assertEquals("Saved and reloaded AuditedExtension doesn't have the same creationDate anymore.",
272                      RuntimeDataSource.toSqlString(new_AuditedExtension.getCreationDate()),
273                      RuntimeDataSource.toSqlString(creationDate));
274 
275       assertEquals("Saved and reloaded AuditedExtension doesn't have the same lastModifiedUser anymore.",
276                      new_AuditedExtension.getLastModifiedUser(), lastModifiedUser);
277         
278       assertEquals("Saved and reloaded AuditedExtension doesn't have the same lastModifiedDate anymore.",
279                      RuntimeDataSource.toSqlString(new_AuditedExtension.getLastModifiedDate()),
280                      RuntimeDataSource.toSqlString(lastModifiedDate));
281 
282       // check it has been "registered" properly
283       assertEquals("Saved and reloaded AuditedExtension cannot be restored from the bean/auditedName it links to.",
284        AuditedExtension.getFor(bean), new_AuditedExtension);
285 
286   }
287   catch (Exception e) {
288       fail("Creating, editing, saving, reloading, checking a new AuditedExtension throws an exception: "+e);
289   }
290   finally {
291       // cleanup
292       if (new_AuditedExtension != null) {
293             RuntimeParameters.getStore().delete(new_AuditedExtension);
294       }      
295       if (bean != null) {
296             RuntimeParameters.getStore().delete(bean);
297       }    
298       if (creationUser != null) {
299             RuntimeParameters.getStore().delete(creationUser);
300       } 
301       if (lastModifiedUser != null) {
302             RuntimeParameters.getStore().delete(lastModifiedUser);
303       }
304   }
305     }
306 }