Source code: com/RuntimeCollective/webapps/test/ModeratedExtensionTest.java
1 /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/test/ModeratedExtensionTest.java,v 1.5 2003/09/30 15:13:19 joe Exp $
2 * $Revision: 1.5 $
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.ModeratedExtension;
37 import com.RuntimeCollective.webapps.bean.SimpleUser;
38 import com.RuntimeCollective.webapps.bean.SimpleTrackedUser;
39 import com.RuntimeCollective.webapps.bean.TrackedUser;
40 import com.RuntimeCollective.webapps.bean.User;
41 import com.RuntimeCollective.webapps.test.WebappsTestCase;
42
43 import java.sql.SQLException;
44 import java.util.Date;
45 import java.util.Vector;
46 import java.util.Iterator;
47 import java.util.List;
48 import junit.framework.Test;
49 import junit.framework.TestCase;
50 import junit.framework.TestSuite;
51
52
53 /**
54 * Test class for ModeratedExtension.
55 *
56 * $Id: ModeratedExtensionTest.java,v 1.5 2003/09/30 15:13:19 joe Exp $
57 */
58 public class ModeratedExtensionTest extends WebappsTestCase {
59
60
61 public ModeratedExtensionTest(String name) {
62 super(name);
63 }
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(User.class.getName());
74 registerBeanClass(SimpleUser.class.getName());
75 registerBeanClass(TrackedUser.class.getName());
76 registerBeanClass(SimpleTrackedUser.class.getName());
77 registerBeanClass(Address.class.getName());
78 registerBeanClass(ModeratedExtension.class.getName());
79
80 super.setUp();
81 }
82
83
84 protected void tearDown() {
85 super.tearDown();
86 }
87
88
89
90
91 public void testModeratedExtension() {
92
93 LogTestSection("Testing ModeratedExtension");
94
95 // get: existing ModeratedExtensions
96 Iterator startAllModeratedExtension;
97 Iterator startAllModeratedExtensionIds;
98
99 ModeratedExtension existingModeratedExtension;
100 int next_id;
101
102 try {
103 startAllModeratedExtension = RuntimeParameters.getStore().getAll(ModeratedExtension.class.getName());
104 startAllModeratedExtensionIds = RuntimeParameters.getStore().getAllIds(ModeratedExtension.class.getName());
105
106 while (startAllModeratedExtensionIds.hasNext()) {
107 next_id = ((Integer)startAllModeratedExtensionIds.next()).intValue();
108 try {
109 existingModeratedExtension = (ModeratedExtension) RuntimeParameters.getStore().refresh(ModeratedExtension.class.getName(), next_id);
110 } catch (RuntimeException e) {
111 fail("We could not call RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\", next_id) with id "+next_id+", though the id was found in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\"): "+e);
112 }
113 }
114 } catch (SQLException e) {
115 fail("We could not call RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\") or RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\"): "+e);
116 }
117
118 // get: non-existing ModeratedExtension
119 int new_id = 0;
120 try {
121 new_id = RuntimeDataSource.nextId();
122 RuntimeParameters.getStore().get(ModeratedExtension.class.getName(), new_id);
123 fail("We could do RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\", new_id) on a new id: "+new_id);
124 } catch (RuntimeException e) {
125 assertTrue("RuntimeParameters.getStore().get(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\", new_id) on a new id throws an exception: "+e, true);
126 } catch (SQLException e) {
127 throw new RuntimeException("RuntimeDataSource.nextId(\"RC\") throws an exception: "+e);
128 }
129
130
131 // getAll and getAllIds: add/delete a new empty ModeratedExtension, and check that the sizes change
132 ModeratedExtension new_ModeratedExtension = null;
133 int startModeratedExtensionNo = 0;
134 int startModeratedExtensionIdsNo = 0;
135 boolean new_found;
136 int addedModeratedExtensionNo = 0;
137 int addedModeratedExtensionIdsNo = 0;
138 Iterator addedAllModeratedExtension;
139 Iterator addedAllModeratedExtensionIds;
140
141 try {
142 startAllModeratedExtensionIds = RuntimeParameters.getStore().getAllIds(ModeratedExtension.class.getName());
143 while (startAllModeratedExtensionIds.hasNext()) {
144 startModeratedExtensionIdsNo++;
145 startAllModeratedExtensionIds.next();
146 }
147 startAllModeratedExtension = RuntimeParameters.getStore().getAll(ModeratedExtension.class.getName());
148 while (startAllModeratedExtension.hasNext()) {
149 startModeratedExtensionNo++;
150 startAllModeratedExtension.next();
151 }
152 assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\") and RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\") produce the same number of items.", startModeratedExtensionNo, startModeratedExtensionIdsNo);
153
154 new_ModeratedExtension = (ModeratedExtension) RuntimeParameters.getStore().create(ModeratedExtension.class.getName());
155 new_id = new_ModeratedExtension.getId();
156 RuntimeParameters.getStore().save(ModeratedExtension.class.getName(), new_id);
157
158 new_found = false;
159 addedAllModeratedExtensionIds = RuntimeParameters.getStore().getAllIds(ModeratedExtension.class.getName());
160 while (addedAllModeratedExtensionIds.hasNext()) {
161 addedModeratedExtensionIdsNo++;
162 if (new_id == ((Integer)addedAllModeratedExtensionIds.next()).intValue())
163 new_found = true;
164 }
165 assertTrue("We could not find a new saved ModeratedExtension in RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\").",new_found);
166
167 new_found = false;
168 addedAllModeratedExtension = RuntimeParameters.getStore().getAll(ModeratedExtension.class.getName());
169 while (addedAllModeratedExtension.hasNext()) {
170 addedModeratedExtensionNo++;
171 if (new_id == ((ModeratedExtension)addedAllModeratedExtension.next()).getId())
172 new_found = true;
173 }
174 assertTrue("We could not find a new saved ModeratedExtension in RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\").",new_found);
175
176 assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.publication.bean.ModeratedExtension\") doesn't increase by one in size after saving a new ModeratedExtension: goes from "+startModeratedExtensionNo+" to "+addedModeratedExtensionNo+".", startModeratedExtensionNo + 1, addedModeratedExtensionNo);
177 assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.publication.bean.ModeratedExtension\") doesn't increase by one in size after saving a new ModeratedExtension: goes from "+startModeratedExtensionIdsNo+" to "+addedModeratedExtensionIdsNo+".", startModeratedExtensionIdsNo + 1, addedModeratedExtensionIdsNo);
178
179 RuntimeParameters.getStore().delete(ModeratedExtension.class.getName(), new_id);
180
181 addedAllModeratedExtensionIds = RuntimeParameters.getStore().getAllIds(ModeratedExtension.class.getName());
182 addedModeratedExtensionIdsNo = 0;
183 while (addedAllModeratedExtensionIds.hasNext()) {
184 addedModeratedExtensionIdsNo++;
185 if (new_id == ((Integer)addedAllModeratedExtensionIds.next()).intValue())
186 fail("We could find a deleted ModeratedExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\").");
187 }
188
189 addedAllModeratedExtension = RuntimeParameters.getStore().getAll(ModeratedExtension.class.getName());
190 addedModeratedExtensionNo = 0;
191 while (addedAllModeratedExtension.hasNext()) {
192 addedModeratedExtensionNo++;
193 if (new_id == ((ModeratedExtension)addedAllModeratedExtension.next()).getId())
194 fail("We could find a deleted ModeratedExtension in the RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\").");
195 }
196
197 assertEquals("RuntimeParameters.getStore().getAll(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\") doesn't go back to its original size after saving and deleting a new ModeratedExtension: goes from "+startModeratedExtensionNo+" to "+addedModeratedExtensionNo+".", startModeratedExtensionNo, addedModeratedExtensionNo);
198 assertEquals("RuntimeParameters.getStore().getAllIds(\"com.RuntimeCollective.webapps.bean.ModeratedExtension\") doesn't go back to its original size after saving and deleting a new ModeratedExtension: goes from "+startModeratedExtensionIdsNo+" to "+addedModeratedExtensionIdsNo+".", startModeratedExtensionIdsNo, addedModeratedExtensionIdsNo);
199
200 } catch (RuntimeException e) {
201 fail("Creating new ModeratedExtension and saving it throws an exception: "+e);
202 } catch (SQLException e) {
203 fail("Creating new ModeratedExtension and saving it throws an exception: "+e);
204 } finally {
205 // cleanup
206 if (new_ModeratedExtension != null) {
207 RuntimeParameters.getStore().delete(new_ModeratedExtension);
208 }
209 }
210
211
212
213 // add/edit/delete a new ModeratedExtension, check the save is not corrupting data
214 boolean isApproved = true;
215 boolean isModerated = true;
216 Address bean = null;
217 User user = null;
218
219 try {
220 // create and set data
221 new_ModeratedExtension = (ModeratedExtension) RuntimeParameters.getStore().create(ModeratedExtension.class.getName());
222 new_id = new_ModeratedExtension.getId();
223
224 new_ModeratedExtension.setIsApproved(isApproved);
225 new_ModeratedExtension.setIsModerated(isModerated);
226
227 user = (User) RuntimeParameters.getStore().create(SimpleTrackedUser.class.getName());
228 user.setFirstName("Test");
229 user.setLastName("Name");
230 user.setEmail("test@runtime-collective.com");
231 user.setPassword("changeme");
232 new_ModeratedExtension.setModeratorUser(user);
233
234 bean = (Address) RuntimeParameters.getStore().create(Address.class.getName());
235 new_ModeratedExtension.setEntityBean(bean);
236
237 // save and reload the ModeratedExtension
238 RuntimeParameters.getStore().save(SimpleTrackedUser.class.getName(), user.getId());
239 RuntimeParameters.getStore().save(Address.class.getName(), bean.getId());
240 RuntimeParameters.getStore().save(ModeratedExtension.class.getName(), new_id);
241 new_ModeratedExtension = (ModeratedExtension) RuntimeParameters.getStore().refresh(ModeratedExtension.class.getName(),
242 new_id);
243
244 // check basic fields
245 assertEquals("Saved and reloaded ModeratedExtension doesn't have the same isApproved field anymore.",
246 new_ModeratedExtension.getIsApproved(), isApproved);
247 assertEquals("Saved and reloaded ModeratedExtension doesn't have the same isModerated field anymore.",
248 new_ModeratedExtension.getIsModerated(), isModerated);
249 assertEquals("Saved and reloaded ModeratedExtension doesn't have the same User anymore.",
250 new_ModeratedExtension.getModeratorUser(), user);
251 assertEquals("Saved and reloaded ModeratedExtension doesn't have the same EntityBean bean anymore.",
252 new_ModeratedExtension.getEntityBean(), bean);
253
254 // check it has been "registered" properly
255 assertEquals("Saved and reloaded ModeratedExtension cannot be restored from the bean it links to.",
256 ModeratedExtension.getFor(bean), new_ModeratedExtension);
257
258 // check the static methods
259 assertEquals("Moderator user cannot be retrieved from a reference to a moderated bean.",
260 ModeratedExtension.getModeratorUser(bean), user);
261 assertEquals("Cannot correctly retrieve moderation status of a moderated bean.",
262 ModeratedExtension.isModerated(bean), isModerated);
263 assertEquals("Cannot correctly retrieve approval status of an approved bean.",
264 ModeratedExtension.needApproval(bean), !isApproved);
265
266 }
267 catch (Exception e) {
268 fail("Creating, editing, saving, reloading, checking a new ModeratedExtension throws an exception: "+e);
269 }
270 finally {
271 // cleanup
272 if (new_ModeratedExtension != null) {
273 RuntimeParameters.getStore().delete(new_ModeratedExtension);
274 }
275 if (user != null) {
276 RuntimeParameters.getStore().delete(user);
277 }
278 if (bean != null) {
279 RuntimeParameters.getStore().delete(bean);
280 }
281 }
282 }
283
284
285 } //end ModeratedExtensionTest.java
286