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

Quick Search    Search Deep

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


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/test/TreeExtensionTest.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.IndexedEntityBeanStore;
33  import com.RuntimeCollective.webapps.RuntimeParameters;
34  import com.RuntimeCollective.webapps.RuntimeDataSource;
35  import com.RuntimeCollective.webapps.bean.Address;
36  import com.RuntimeCollective.webapps.bean.EntityBean;
37  import com.RuntimeCollective.webapps.bean.TreeExtension;
38  import com.RuntimeCollective.webapps.bean.SimpleUser;
39  import com.RuntimeCollective.webapps.bean.SimpleTrackedUser;
40  import com.RuntimeCollective.webapps.bean.TrackedUser;
41  import com.RuntimeCollective.webapps.bean.User;
42  import com.RuntimeCollective.webapps.test.WebappsTestCase;
43  
44  import java.sql.SQLException;
45  import java.util.Date;
46  import java.util.Vector;
47  import java.util.Iterator;
48  import java.util.List;
49  import junit.framework.Test;
50  import junit.framework.TestCase;
51  import junit.framework.TestSuite;
52  
53  
54  /** 
55   * Test class for TreeExtension.
56   *
57   * $Id: TreeExtensionTest.java,v 1.3 2003/09/30 15:13:19 joe Exp $
58   */
59  public class TreeExtensionTest extends WebappsTestCase {
60  
61  
62      public TreeExtensionTest(String name) {
63          super(name);
64      }
65  
66  
67      protected void setUp() {
68          resetBeanRegister();
69  
70          // default store
71          super.DEFAULT_STORE_CLASS = IndexedEntityBeanStore.class.getName();
72  
73          // register beans
74          registerBeanClass(User.class.getName());
75          registerBeanClass(SimpleUser.class.getName());
76          registerBeanClass(TrackedUser.class.getName());
77          registerBeanClass(SimpleTrackedUser.class.getName());
78          registerBeanClass(Address.class.getName());
79          registerBeanClass(TreeExtension.class.getName());
80  
81          super.setUp();
82      }
83  
84  
85      protected void tearDown() {    
86          super.tearDown();
87      }
88  
89  
90  
91  
92      public void testTreeExtension() {
93  
94    LogTestSection("Testing TreeExtension");
95  
96    // get: existing TreeExtensions
97    Iterator startAllTreeExtension;
98    Iterator startAllTreeExtensionIds;
99  
100   TreeExtension existingTreeExtension;
101   int next_id;
102 
103   try {
104       startAllTreeExtension = RuntimeParameters.getStore().getAll(TreeExtension.class.getName());
105       startAllTreeExtensionIds = RuntimeParameters.getStore().getAllIds(TreeExtension.class.getName());
106 
107       while (startAllTreeExtensionIds.hasNext()) {
108     next_id = ((Integer)startAllTreeExtensionIds.next()).intValue();
109     try {
110         existingTreeExtension = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), next_id);
111     } catch (RuntimeException e) {
112         fail("We could not call RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.TreeExtension\", next_id) with id "+next_id+", though the id was found in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\"): "+e);
113     }
114       }
115   } catch (SQLException e) {
116       fail("We could not call RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.TreeExtension\") or RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\"): "+e);
117   }
118 
119   // get: non-existing TreeExtension
120   int new_id = 0;
121   try {
122       new_id = RuntimeDataSource.nextId();
123       RuntimeParameters.getStore().get(TreeExtension.class.getName(), new_id);
124       fail("We could do RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.TreeExtension\", new_id) on a new id: "+new_id);
125   } catch (RuntimeException e) {
126       assertTrue("RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.TreeExtension\", new_id) on a new id throws an exception: "+e, true);
127   } catch (SQLException e) {
128       throw new RuntimeException("RuntimeDataSource.nextId(\"RC\") throws an exception: "+e);
129   }
130 
131 
132   // getAll and getAllIds: add/delete a new empty TreeExtension, and check that the sizes change
133   TreeExtension new_TreeExtension = null;
134   int startTreeExtensionNo = 0;
135   int startTreeExtensionIdsNo = 0;
136   boolean new_found;
137   int addedTreeExtensionNo = 0;
138   int addedTreeExtensionIdsNo = 0;
139   Iterator addedAllTreeExtension;
140   Iterator addedAllTreeExtensionIds;
141   Address tempBean = null;
142 
143   try {
144       startAllTreeExtensionIds = RuntimeParameters.getStore().getAllIds(TreeExtension.class.getName());
145       while (startAllTreeExtensionIds.hasNext()) {
146     startTreeExtensionIdsNo++;
147     startAllTreeExtensionIds.next();
148       }
149       startAllTreeExtension = RuntimeParameters.getStore().getAll(TreeExtension.class.getName());
150       while (startAllTreeExtension.hasNext()) {
151     startTreeExtensionNo++;
152     startAllTreeExtension.next();
153       }
154       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\") and RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.TreeExtension\") produce the same number of items.", startTreeExtensionNo, startTreeExtensionIdsNo);
155 
156       new_TreeExtension = (TreeExtension) RuntimeParameters.getStore().create(TreeExtension.class.getName());
157       new_id = new_TreeExtension.getId();
158 
159       tempBean = (Address) RuntimeParameters.getStore().create(Address.class.getName());
160       new_TreeExtension.setEntityBean(tempBean);
161         new_TreeExtension.setTreeName(""+Math.random());
162       RuntimeParameters.getStore().save(Address.class.getName(), tempBean.getId());
163 
164       RuntimeParameters.getStore().save(TreeExtension.class.getName(), new_id);
165       
166       new_found = false;
167       addedAllTreeExtensionIds = RuntimeParameters.getStore().getAllIds(TreeExtension.class.getName());
168       while (addedAllTreeExtensionIds.hasNext()) {
169     addedTreeExtensionIdsNo++;
170     if (new_id == ((Integer)addedAllTreeExtensionIds.next()).intValue())
171         new_found = true;
172       }
173       assertTrue("We could not find a new saved TreeExtension in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\").",new_found);
174 
175       new_found = false;
176       addedAllTreeExtension = RuntimeParameters.getStore().getAll(TreeExtension.class.getName());
177       while (addedAllTreeExtension.hasNext()) {
178     addedTreeExtensionNo++;
179     if (new_id == ((TreeExtension)addedAllTreeExtension.next()).getId())
180         new_found = true;
181       }
182       assertTrue("We could not find a new saved TreeExtension in RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.TreeExtension\").",new_found);
183 
184       assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.publication.bean.TreeExtension\") doesn't increase by one in size after saving a new TreeExtension: goes from "+startTreeExtensionNo+" to "+addedTreeExtensionNo+".", startTreeExtensionNo + 1, addedTreeExtensionNo);
185       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.publication.bean.TreeExtension\") doesn't increase by one in size after saving a new TreeExtension: goes from "+startTreeExtensionIdsNo+" to "+addedTreeExtensionIdsNo+".", startTreeExtensionIdsNo + 1, addedTreeExtensionIdsNo);
186 
187       RuntimeParameters.getStore().delete(TreeExtension.class.getName(), new_id);
188 
189       addedAllTreeExtensionIds = RuntimeParameters.getStore().getAllIds(TreeExtension.class.getName());
190       addedTreeExtensionIdsNo = 0;
191       while (addedAllTreeExtensionIds.hasNext()) {
192     addedTreeExtensionIdsNo++;
193     if (new_id == ((Integer)addedAllTreeExtensionIds.next()).intValue())
194         fail("We could find a deleted TreeExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\").");
195       }
196 
197       addedAllTreeExtension = RuntimeParameters.getStore().getAll(TreeExtension.class.getName());
198       addedTreeExtensionNo = 0;
199       while (addedAllTreeExtension.hasNext()) {
200     addedTreeExtensionNo++;
201     if (new_id == ((TreeExtension)addedAllTreeExtension.next()).getId())
202         fail("We could find a deleted TreeExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\").");
203       }
204 
205       assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.TreeExtension\") doesn't go back to its original size after saving and deleting a new TreeExtension: goes from "+startTreeExtensionNo+" to "+addedTreeExtensionNo+".", startTreeExtensionNo, addedTreeExtensionNo);
206       assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.TreeExtension\") doesn't go back to its original size after saving and deleting a new TreeExtension: goes from "+startTreeExtensionIdsNo+" to "+addedTreeExtensionIdsNo+".", startTreeExtensionIdsNo, addedTreeExtensionIdsNo);
207 
208   } catch (RuntimeException e) {
209       fail("Creating new TreeExtension and saving it throws an exception: "+e);
210   } catch (SQLException e) {
211       fail("Creating new TreeExtension and saving it throws an exception: "+e);
212   } finally {
213       // cleanup
214       if (new_TreeExtension != null) {
215             RuntimeParameters.getStore().delete(new_TreeExtension);
216       }      
217       if (tempBean != null) {
218             RuntimeParameters.getStore().delete(tempBean);
219       }      
220   }
221 
222 
223 
224   // add/edit/delete a new TreeExtension, check the save is not corrupting data
225   Address bean = null;
226   Address another = null;
227     TreeExtension anotherTE = null;
228     int anotherTEid = EntityBean.NULL_ID;
229     String treeName = ""+Math.random();
230     Address another2 = null;
231     TreeExtension another2TE = null;
232     int another2TEid = EntityBean.NULL_ID;
233     List exts = null;
234 
235   try {
236       // create and set data
237       new_TreeExtension = (TreeExtension) RuntimeParameters.getStore().create(TreeExtension.class.getName());
238       new_id = new_TreeExtension.getId();
239 
240       new_TreeExtension.setTreeName(treeName);
241 
242       bean = (Address) RuntimeParameters.getStore().create(Address.class.getName());
243       new_TreeExtension.setEntityBean(bean);
244 
245       // save and reload the TreeExtension
246       RuntimeParameters.getStore().save(Address.class.getName(), bean.getId());
247       RuntimeParameters.getStore().save(TreeExtension.class.getName(), new_id);
248       new_TreeExtension = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), new_id);
249 
250       // check basic fields
251       assertEquals("Saved and reloaded TreeExtension doesn't have the same treeName field anymore.",
252        new_TreeExtension.getTreeName(), treeName);
253       assertEquals("Saved and reloaded TreeExtension doesn't have the same bean field anymore.",
254        new_TreeExtension.getEntityBean(), bean);
255       assertEquals("Saved and reloaded TreeExtension doesn't have null parent.",
256        new_TreeExtension.getParent(), null);
257 
258       // check it has been "registered" properly
259       assertEquals("Saved and reloaded TreeExtension cannot be restored from the bean/treeName it links to.",
260        TreeExtension.getFor(bean, treeName), new_TreeExtension);
261         exts = TreeExtension.getAllFor(bean, treeName);
262       assertEquals("Bean doesn't have 1 single tree extension, as it should.",
263        exts.size(), 1);
264       assertEquals("Bean doesn't have the right tree extension.",
265        exts.get(0), new_TreeExtension);
266 
267         // check the parent is saved properly too
268       another = (Address) RuntimeParameters.getStore().create(Address.class.getName());
269 
270       anotherTE = (TreeExtension) RuntimeParameters.getStore().create(TreeExtension.class.getName());
271       anotherTEid = anotherTE.getId();
272       anotherTE.setTreeName(treeName);
273         anotherTE.setEntityBean(another);
274         anotherTE.addChild(new_TreeExtension);
275 
276       RuntimeParameters.getStore().save(Address.class.getName(), another.getId());
277       RuntimeParameters.getStore().save(TreeExtension.class.getName(), anotherTEid);
278 
279       assertEquals("TreeExtension doesn't give newly assigned parent.",
280                      anotherTE, new_TreeExtension.getParent());
281 
282       assertEquals("TreeExtension doesn't give new proper position.",
283                      0, new_TreeExtension.getPosition());
284 
285         new_TreeExtension = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), new_id);
286                 
287       assertEquals("Once refreshed, TreeExtension doesn't give newly assigned parent.",
288                      anotherTE, new_TreeExtension.getParent());
289 
290       assertEquals("Once refreshed, TreeExtension doesn't give new proper position.",
291                      0, new_TreeExtension.getPosition());
292 
293         // check the children list
294         exts = anotherTE.getChildren();
295       assertEquals("Incorrect child list length found.",
296        exts.size(), 1);
297       assertEquals("Incorrect only child found.",
298        exts.get(0), new_TreeExtension);
299 
300         // make a third one, to check positions
301       another2 = (Address) RuntimeParameters.getStore().create(Address.class.getName());
302       another2TE = (TreeExtension) RuntimeParameters.getStore().create(TreeExtension.class.getName());
303       another2TEid = another2TE.getId();
304         another2TE.setEntityBean(another2);
305       another2TE.setTreeName(treeName);
306       RuntimeParameters.getStore().save(Address.class.getName(), another2.getId());
307       RuntimeParameters.getStore().save(TreeExtension.class.getName(), another2TEid);
308 
309         anotherTE.addChild(another2TE);
310       RuntimeParameters.getStore().save(TreeExtension.class.getName(), anotherTEid);
311 
312       assertEquals("TreeExtension (another2TE) doesn't give newly assigned parent.",
313                      anotherTE, another2TE.getParent());
314 
315       assertEquals("TreeExtension (another2TE) doesn't give new proper position.",
316                      1, another2TE.getPosition());
317 
318         another2TE = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), another2TEid);
319 
320       assertEquals("Once refreshed, TreeExtension (another2TE) doesn't give newly assigned parent.",
321                      anotherTE, another2TE.getParent());
322 
323       assertEquals("Once refreshed, TreeExtension (another2TE) doesn't give new proper position.",
324                      1, another2TE.getPosition());
325 
326         // check the children list
327         exts = anotherTE.getChildren();
328       assertEquals("Incorrect child list length found.",
329        exts.size(), 2);
330       assertEquals("Incorrect 1st child found.",
331        exts.get(0), new_TreeExtension);
332       assertEquals("Incorrect 2nd child found.",
333        exts.get(1), another2TE);
334 
335         // remove the first one, and check positions again
336         anotherTE.removeChild(new_TreeExtension);
337       RuntimeParameters.getStore().save(TreeExtension.class.getName(), new_id);
338 
339       assertEquals("TreeExtension doesn't return null parent after being removed from parent.",
340                      null, new_TreeExtension.getParent());
341 
342       assertEquals("TreeExtension (another2TE) doesn't give correct parent.",
343                      anotherTE, another2TE.getParent());
344 
345       assertEquals("TreeExtension (another2TE) doesn't give new proper position.",
346                      0, another2TE.getPosition());
347 
348         new_TreeExtension = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), new_id);
349 
350         another2TE = (TreeExtension) RuntimeParameters.getStore().refresh(TreeExtension.class.getName(), another2TEid);
351 
352       assertEquals("Once refreshed, TreeExtension doesn't return null parent after being removed from parent.",
353                      null, new_TreeExtension.getParent());
354 
355       assertEquals("Once refreshed, TreeExtension (another2TE) doesn't give correct parent.",
356                      anotherTE, another2TE.getParent());
357 
358       assertEquals("Once refreshed, TreeExtension (another2TE) doesn't give new proper position.",
359                      0, another2TE.getPosition());
360 
361         // check the children list
362         exts = anotherTE.getChildren();
363       assertEquals("Incorrect child list length found.",
364        exts.size(), 1);
365       assertEquals("Incorrect only child found.",
366        exts.get(0), another2TE);
367 
368   }
369   catch (Exception e) {
370       fail("Creating, editing, saving, reloading, checking a new TreeExtension throws an exception: "+e);
371   }
372   finally {
373       // cleanup
374       if (another2TE != null) {
375             RuntimeParameters.getStore().delete(another2TE);
376       }      
377       if (another2 != null) {
378             RuntimeParameters.getStore().delete(another2);
379       }
380       if (anotherTE != null) {
381             RuntimeParameters.getStore().delete(anotherTE);
382       }      
383       if (another != null) {
384             RuntimeParameters.getStore().delete(another);
385       }
386       if (new_TreeExtension != null) {
387             RuntimeParameters.getStore().delete(new_TreeExtension);
388       }      
389       if (bean != null) {
390             RuntimeParameters.getStore().delete(bean);
391       }
392   }
393     }
394 }
395