KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > PingsTest


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * PingsTest.java
20  *
21  * Created on April 9, 2006, 1:27 PM
22  */

23
24 package org.apache.roller.business;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28 import junit.framework.Test;
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.roller.TestUtils;
34 import org.apache.roller.model.AutoPingManager;
35 import org.apache.roller.model.PingTargetManager;
36 import org.apache.roller.model.RollerFactory;
37 import org.apache.roller.pojos.AutoPingData;
38 import org.apache.roller.pojos.PingTargetData;
39 import org.apache.roller.pojos.UserData;
40 import org.apache.roller.pojos.WebsiteData;
41
42
43 /**
44  * Test Pings related business operations.
45  */

46 public class PingsTest extends TestCase {
47     
48     public static Log log = LogFactory.getLog(PingsTest.class);
49     
50     UserData testUser = null;
51     WebsiteData testWeblog = null;
52     PingTargetData testCommonPing = null;
53     PingTargetData testCustomPing = null;
54     
55     
56     public PingsTest(String JavaDoc name) {
57         super(name);
58     }
59     
60     
61     public static Test suite() {
62         return new TestSuite(PingsTest.class);
63     }
64     
65     
66     /**
67      * All tests in this suite require a user and a weblog.
68      */

69     public void setUp() throws Exception JavaDoc {
70         
71         try {
72             testUser = TestUtils.setupUser("wtTestUser");
73             testWeblog = TestUtils.setupWeblog("wtTestWeblog", testUser);
74             TestUtils.endSession(true);
75         } catch (Exception JavaDoc ex) {
76             log.error(ex);
77             throw new Exception JavaDoc("Test setup failed", ex);
78         }
79         
80         testCommonPing = new PingTargetData();
81         testCommonPing.setName("testCommonPing");
82         testCommonPing.setPingUrl("http://localhost/testCommonPing");
83         
84         testCustomPing = new PingTargetData();
85         testCustomPing.setName("testCommonPing");
86         testCustomPing.setPingUrl("http://localhost/testCommonPing");
87     }
88     
89     public void tearDown() throws Exception JavaDoc {
90         
91         try {
92             TestUtils.teardownWeblog(testWeblog.getId());
93             TestUtils.teardownUser(testUser.getId());
94             TestUtils.endSession(true);
95         } catch (Exception JavaDoc ex) {
96             log.error(ex);
97             throw new Exception JavaDoc("Test teardown failed", ex);
98         }
99         
100         testCommonPing = null;
101         testCustomPing = null;
102     }
103     
104     
105     /**
106      * Test basic persistence operations ... Create, Update, Delete
107      */

108     public void testPingTargetCRUD() throws Exception JavaDoc {
109         
110         PingTargetManager mgr = RollerFactory.getRoller().getPingTargetManager();
111         PingTargetData ping = null;
112         
113         // create common ping
114
mgr.savePingTarget(testCommonPing);
115         String JavaDoc commonId = testCommonPing.getId();
116         TestUtils.endSession(true);
117         
118         // make sure common ping was stored
119
ping = null;
120         ping = mgr.getPingTarget(commonId);
121         assertNotNull(ping);
122         assertEquals(testCommonPing.getPingUrl(), ping.getPingUrl());
123         
124         // create custom ping
125
testCustomPing.setWebsite(testWeblog);
126         mgr.savePingTarget(testCustomPing);
127         String JavaDoc customId = testCustomPing.getId();
128         TestUtils.endSession(true);
129         
130         // make sure custom ping was stored
131
ping = null;
132         ping = mgr.getPingTarget(customId);
133         assertNotNull(ping);
134         assertEquals(testCustomPing.getPingUrl(), ping.getPingUrl());
135         
136         // update common ping
137
ping = null;
138         ping = mgr.getPingTarget(commonId);
139         ping.setName("testtestCommon");
140         mgr.savePingTarget(ping);
141         TestUtils.endSession(true);
142         
143         // make sure common ping was updated
144
ping = null;
145         ping = mgr.getPingTarget(commonId);
146         assertNotNull(ping);
147         assertEquals("testtestCommon", ping.getName());
148         
149         // update custom ping
150
ping = null;
151         ping = mgr.getPingTarget(customId);
152         ping.setName("testtestCustom");
153         mgr.savePingTarget(ping);
154         TestUtils.endSession(true);
155         
156         // make sure custom ping was updated
157
ping = null;
158         ping = mgr.getPingTarget(customId);
159         assertNotNull(ping);
160         assertEquals("testtestCustom", ping.getName());
161         
162         // delete common ping
163
ping = null;
164         ping = mgr.getPingTarget(commonId);
165         mgr.removePingTarget(ping);
166         TestUtils.endSession(true);
167         
168         // make sure common ping was deleted
169
ping = null;
170         ping = mgr.getPingTarget(commonId);
171         assertNull(ping);
172         
173         // delete custom ping
174
ping = null;
175         ping = mgr.getPingTarget(customId);
176         mgr.removePingTarget(ping);
177         TestUtils.endSession(true);
178         
179         // make sure custom ping was deleted
180
ping = null;
181         ping = mgr.getPingTarget(customId);
182         assertNull(ping);
183     }
184     
185     
186     /**
187      * Test lookup mechanisms ... id, all common, all custom for weblog
188      */

189     public void testPingTargetLookups() throws Exception JavaDoc {
190         
191         PingTargetManager mgr = RollerFactory.getRoller().getPingTargetManager();
192         PingTargetData ping = null;
193         
194         // create common ping
195
mgr.savePingTarget(testCommonPing);
196         String JavaDoc commonId = testCommonPing.getId();
197         TestUtils.endSession(true);
198         
199         // create custom ping
200
testCustomPing.setWebsite(testWeblog);
201         mgr.savePingTarget(testCustomPing);
202         String JavaDoc customId = testCustomPing.getId();
203         TestUtils.endSession(true);
204         
205         // lookup by id
206
ping = null;
207         ping = mgr.getPingTarget(commonId);
208         assertNotNull(ping);
209         assertEquals(testCommonPing.getName(), ping.getName());
210         
211         // lookup all common pings
212
List JavaDoc commonPings = mgr.getCommonPingTargets();
213         assertNotNull(commonPings);
214         assertEquals(1, commonPings.size());
215         
216         // lookup all custom pings for weblog
217
List JavaDoc customPings = mgr.getCustomPingTargets(testWeblog);
218         assertNotNull(customPings);
219         assertEquals(1, customPings.size());
220         
221         // delete common ping
222
ping = null;
223         ping = mgr.getPingTarget(commonId);
224         mgr.removePingTarget(ping);
225         TestUtils.endSession(true);
226         
227         // delete custom ping
228
ping = null;
229         ping = mgr.getPingTarget(customId);
230         mgr.removePingTarget(ping);
231         TestUtils.endSession(true);
232     }
233     
234     
235     /**
236      * Test basic persistence operations ... Create, Update, Delete
237      */

238     public void testAutoPingCRUD() throws Exception JavaDoc {
239         
240         AutoPingManager mgr = RollerFactory.getRoller().getAutopingManager();
241         AutoPingData autoPing = null;
242         
243         // create ping target to use for tests
244
PingTargetData pingTarget = TestUtils.setupPingTarget("fooPing", "http://foo/null");
245         PingTargetData pingTarget2 = TestUtils.setupPingTarget("blahPing", "http://blah/null");
246         TestUtils.endSession(true);
247         
248         // create autoPing
249
autoPing = new AutoPingData(null, pingTarget, testWeblog);
250         mgr.saveAutoPing(autoPing);
251         String JavaDoc id = autoPing.getId();
252         TestUtils.endSession(true);
253         
254         // make sure autoPing was stored
255
autoPing = null;
256         autoPing = mgr.getAutoPing(id);
257         assertNotNull(autoPing);
258         assertEquals(pingTarget, autoPing.getPingTarget());
259         
260         // update autoPing
261
autoPing.setPingTarget(pingTarget2);
262         mgr.saveAutoPing(autoPing);
263         TestUtils.endSession(true);
264         
265         // make sure autoPing was updated
266
autoPing = null;
267         autoPing = mgr.getAutoPing(id);
268         assertNotNull(autoPing);
269         assertEquals(pingTarget2, autoPing.getPingTarget());
270         
271         // delete autoPing
272
mgr.removeAutoPing(autoPing);
273         TestUtils.endSession(true);
274         
275         // make sure common autoPing was deleted
276
autoPing = null;
277         autoPing = mgr.getAutoPing(id);
278         assertNull(autoPing);
279         
280         // teardown test ping target
281
TestUtils.teardownPingTarget(pingTarget.getId());
282         TestUtils.teardownPingTarget(pingTarget2.getId());
283         TestUtils.endSession(true);
284     }
285     
286     
287     /**
288      * Test special ping target removal methods ... by weblog/target, collection, all
289      */

290     public void testPingTargetRemovals() throws Exception JavaDoc {
291         
292         AutoPingManager mgr = RollerFactory.getRoller().getAutopingManager();
293         AutoPingData testAutoPing = null;
294         
295         // create ping target to use for tests
296
PingTargetData pingTarget = TestUtils.setupPingTarget("fooPing", "http://foo/null");
297         PingTargetData pingTarget2 = TestUtils.setupPingTarget("blahPing", "http://blah/null");
298         PingTargetData pingTarget3 = TestUtils.setupPingTarget("gahPing", "http://gah/null");
299         
300         // create auto pings for test
301
AutoPingData autoPing = TestUtils.setupAutoPing(pingTarget, testWeblog);
302         AutoPingData autoPing2 = TestUtils.setupAutoPing(pingTarget2, testWeblog);
303         AutoPingData autoPing3 = TestUtils.setupAutoPing(pingTarget3, testWeblog);
304         TestUtils.endSession(true);
305         
306         // remove by weblog/target
307
mgr.removeAutoPing(pingTarget, testWeblog);
308         TestUtils.endSession(true);
309         
310         // make sure remove succeeded
311
testAutoPing = null;
312         testAutoPing = mgr.getAutoPing(autoPing.getId());
313         assertNull(testAutoPing);
314         
315         // remove a collection
316
List JavaDoc autoPings = new ArrayList JavaDoc();
317         autoPings.add(autoPing2);
318         autoPings.add(autoPing3);
319         mgr.removeAutoPings(autoPings);
320         TestUtils.endSession(true);
321         
322         // make sure delete was successful
323
autoPings = mgr.getAutoPingsByWebsite(testWeblog);
324         assertNotNull(autoPings);
325         assertEquals(0, autoPings.size());
326         
327         // need to create more test pings
328
autoPing = TestUtils.setupAutoPing(pingTarget, testWeblog);
329         autoPing2 = TestUtils.setupAutoPing(pingTarget2, testWeblog);
330         autoPing3 = TestUtils.setupAutoPing(pingTarget3, testWeblog);
331         TestUtils.endSession(true);
332         
333         // remove all
334
mgr.removeAllAutoPings();
335         TestUtils.endSession(true);
336         
337         // make sure remove succeeded
338
autoPings = mgr.getAutoPingsByWebsite(testWeblog);
339         assertNotNull(autoPings);
340         assertEquals(0, autoPings.size());
341         
342         // teardown test ping target
343
TestUtils.teardownPingTarget(pingTarget.getId());
344         TestUtils.teardownPingTarget(pingTarget2.getId());
345         TestUtils.endSession(true);
346     }
347     
348     
349     /**
350      * Test lookup mechanisms ... id, ping target, weblog
351      */

352     public void testAutoPingLookups() throws Exception JavaDoc {
353         
354         AutoPingManager mgr = RollerFactory.getRoller().getAutopingManager();
355         AutoPingData autoPing = null;
356         
357         // create autoPing target to use for tests
358
PingTargetData pingTarget = TestUtils.setupPingTarget("fooPing", "http://foo/null");
359         TestUtils.endSession(true);
360         
361         // create autoPing
362
autoPing = new AutoPingData(null, pingTarget, testWeblog);
363         mgr.saveAutoPing(autoPing);
364         String JavaDoc id = autoPing.getId();
365         TestUtils.endSession(true);
366         
367         // lookup by id
368
autoPing = null;
369         autoPing = mgr.getAutoPing(id);
370         assertNotNull(autoPing);
371         assertEquals(pingTarget, autoPing.getPingTarget());
372         
373         // lookup by ping target
374
List JavaDoc autoPings = mgr.getAutoPingsByTarget(pingTarget);
375         assertNotNull(autoPings);
376         assertEquals(1, autoPings.size());
377         
378         // lookup by weblog
379
autoPings = null;
380         autoPings = mgr.getAutoPingsByWebsite(testWeblog);
381         assertNotNull(autoPing);
382         assertEquals(1, autoPings.size());
383         
384         // delete autoPing
385
mgr.removeAutoPing(autoPing);
386         TestUtils.endSession(true);
387         
388         // teardown test ping target
389
TestUtils.teardownPingTarget(pingTarget.getId());
390         TestUtils.endSession(true);
391     }
392     
393     
394     public void testApplicableAutoPings() throws Exception JavaDoc {
395         
396     }
397     
398     
399     /**
400      * Test that we can properly remove a ping target when it has
401      * associated elements like auto pings and ping queue entries.
402      */

403     public void testRemoveLoadedPingTarget() throws Exception JavaDoc {
404         // TODO: implement this test
405
}
406     
407 }
408
Popular Tags