001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018: /*
019: * PingsTest.java
020: *
021: * Created on April 9, 2006, 1:27 PM
022: */
023:
024: package org.apache.roller.business;
025:
026: import java.util.ArrayList;
027: import java.util.List;
028: import junit.framework.Test;
029: import junit.framework.TestCase;
030: import junit.framework.TestSuite;
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.apache.roller.TestUtils;
034: import org.apache.roller.business.pings.AutoPingManager;
035: import org.apache.roller.business.pings.PingTargetManager;
036: import org.apache.roller.business.RollerFactory;
037: import org.apache.roller.pojos.AutoPingData;
038: import org.apache.roller.pojos.PingTargetData;
039: import org.apache.roller.pojos.UserData;
040: import org.apache.roller.pojos.WebsiteData;
041:
042: /**
043: * Test Pings related business operations.
044: */
045: public class PingsTest extends TestCase {
046:
047: public static Log log = LogFactory.getLog(PingsTest.class);
048:
049: UserData testUser = null;
050: WebsiteData testWeblog = null;
051: PingTargetData testCommonPing = null;
052: PingTargetData testCustomPing = null;
053:
054: public PingsTest(String name) {
055: super (name);
056: }
057:
058: public static Test suite() {
059: return new TestSuite(PingsTest.class);
060: }
061:
062: /**
063: * All tests in this suite require a user and a weblog.
064: */
065: public void setUp() throws Exception {
066:
067: try {
068: testUser = TestUtils.setupUser("wtTestUser");
069: testWeblog = TestUtils
070: .setupWeblog("wtTestWeblog", testUser);
071: TestUtils.endSession(true);
072: } catch (Exception ex) {
073: log.error(ex);
074: throw new Exception("Test setup failed", ex);
075: }
076:
077: testCommonPing = new PingTargetData();
078: testCommonPing.setName("testCommonPing");
079: testCommonPing.setPingUrl("http://localhost/testCommonPing");
080:
081: testCustomPing = new PingTargetData();
082: testCustomPing.setName("testCommonPing");
083: testCustomPing.setPingUrl("http://localhost/testCommonPing");
084: }
085:
086: public void tearDown() throws Exception {
087:
088: try {
089: TestUtils.teardownWeblog(testWeblog.getId());
090: TestUtils.teardownUser(testUser.getId());
091: TestUtils.endSession(true);
092: } catch (Exception ex) {
093: log.error(ex);
094: throw new Exception("Test teardown failed", ex);
095: }
096:
097: testCommonPing = null;
098: testCustomPing = null;
099: }
100:
101: /**
102: * Test basic persistence operations ... Create, Update, Delete
103: */
104: public void testPingTargetCRUD() throws Exception {
105:
106: PingTargetManager mgr = RollerFactory.getRoller()
107: .getPingTargetManager();
108: PingTargetData ping = null;
109:
110: // create common ping
111: mgr.savePingTarget(testCommonPing);
112: String commonId = testCommonPing.getId();
113: TestUtils.endSession(true);
114:
115: // make sure common ping was stored
116: ping = null;
117: ping = mgr.getPingTarget(commonId);
118: assertNotNull(ping);
119: assertEquals(testCommonPing.getPingUrl(), ping.getPingUrl());
120:
121: // create custom ping
122: testCustomPing.setWebsite(testWeblog);
123: mgr.savePingTarget(testCustomPing);
124: String customId = testCustomPing.getId();
125: TestUtils.endSession(true);
126:
127: // make sure custom ping was stored
128: ping = null;
129: ping = mgr.getPingTarget(customId);
130: assertNotNull(ping);
131: assertEquals(testCustomPing.getPingUrl(), ping.getPingUrl());
132:
133: // update common ping
134: ping = null;
135: ping = mgr.getPingTarget(commonId);
136: ping.setName("testtestCommon");
137: mgr.savePingTarget(ping);
138: TestUtils.endSession(true);
139:
140: // make sure common ping was updated
141: ping = null;
142: ping = mgr.getPingTarget(commonId);
143: assertNotNull(ping);
144: assertEquals("testtestCommon", ping.getName());
145:
146: // update custom ping
147: ping = null;
148: ping = mgr.getPingTarget(customId);
149: ping.setName("testtestCustom");
150: mgr.savePingTarget(ping);
151: TestUtils.endSession(true);
152:
153: // make sure custom ping was updated
154: ping = null;
155: ping = mgr.getPingTarget(customId);
156: assertNotNull(ping);
157: assertEquals("testtestCustom", ping.getName());
158:
159: // delete common ping
160: ping = null;
161: ping = mgr.getPingTarget(commonId);
162: mgr.removePingTarget(ping);
163: TestUtils.endSession(true);
164:
165: // make sure common ping was deleted
166: ping = null;
167: ping = mgr.getPingTarget(commonId);
168: assertNull(ping);
169:
170: // delete custom ping
171: ping = null;
172: ping = mgr.getPingTarget(customId);
173: mgr.removePingTarget(ping);
174: TestUtils.endSession(true);
175:
176: // make sure custom ping was deleted
177: ping = null;
178: ping = mgr.getPingTarget(customId);
179: assertNull(ping);
180: }
181:
182: /**
183: * Test lookup mechanisms ... id, all common, all custom for weblog
184: */
185: public void testPingTargetLookups() throws Exception {
186:
187: PingTargetManager mgr = RollerFactory.getRoller()
188: .getPingTargetManager();
189: PingTargetData ping = null;
190:
191: // create common ping
192: mgr.savePingTarget(testCommonPing);
193: String commonId = testCommonPing.getId();
194: TestUtils.endSession(true);
195:
196: // create custom ping
197: testCustomPing.setWebsite(testWeblog);
198: mgr.savePingTarget(testCustomPing);
199: String customId = testCustomPing.getId();
200: TestUtils.endSession(true);
201:
202: // lookup by id
203: ping = null;
204: ping = mgr.getPingTarget(commonId);
205: assertNotNull(ping);
206: assertEquals(testCommonPing.getName(), ping.getName());
207:
208: // lookup all common pings
209: List commonPings = mgr.getCommonPingTargets();
210: assertNotNull(commonPings);
211: assertEquals(1, commonPings.size());
212:
213: // lookup all custom pings for weblog
214: List customPings = mgr.getCustomPingTargets(testWeblog);
215: assertNotNull(customPings);
216: assertEquals(1, customPings.size());
217:
218: // delete common ping
219: ping = null;
220: ping = mgr.getPingTarget(commonId);
221: mgr.removePingTarget(ping);
222: TestUtils.endSession(true);
223:
224: // delete custom ping
225: ping = null;
226: ping = mgr.getPingTarget(customId);
227: mgr.removePingTarget(ping);
228: TestUtils.endSession(true);
229: }
230:
231: /**
232: * Test basic persistence operations ... Create, Update, Delete
233: */
234: public void testAutoPingCRUD() throws Exception {
235:
236: AutoPingManager mgr = RollerFactory.getRoller()
237: .getAutopingManager();
238: AutoPingData autoPing = null;
239:
240: // create ping target to use for tests
241: PingTargetData pingTarget = TestUtils.setupPingTarget(
242: "fooPing", "http://foo/null");
243: PingTargetData pingTarget2 = TestUtils.setupPingTarget(
244: "blahPing", "http://blah/null");
245: TestUtils.endSession(true);
246:
247: // create autoPing
248: autoPing = new AutoPingData(null, pingTarget, testWeblog);
249: mgr.saveAutoPing(autoPing);
250: String id = autoPing.getId();
251: TestUtils.endSession(true);
252:
253: // make sure autoPing was stored
254: autoPing = null;
255: autoPing = mgr.getAutoPing(id);
256: assertNotNull(autoPing);
257: assertEquals(pingTarget, autoPing.getPingTarget());
258:
259: // update autoPing
260: autoPing.setPingTarget(pingTarget2);
261: mgr.saveAutoPing(autoPing);
262: TestUtils.endSession(true);
263:
264: // make sure autoPing was updated
265: autoPing = null;
266: autoPing = mgr.getAutoPing(id);
267: assertNotNull(autoPing);
268: assertEquals(pingTarget2, autoPing.getPingTarget());
269:
270: // delete autoPing
271: mgr.removeAutoPing(autoPing);
272: TestUtils.endSession(true);
273:
274: // make sure common autoPing was deleted
275: autoPing = null;
276: autoPing = mgr.getAutoPing(id);
277: assertNull(autoPing);
278:
279: // teardown test ping target
280: TestUtils.teardownPingTarget(pingTarget.getId());
281: TestUtils.teardownPingTarget(pingTarget2.getId());
282: TestUtils.endSession(true);
283: }
284:
285: /**
286: * Test special ping target removal methods ... by weblog/target, collection, all
287: */
288: public void testPingTargetRemovals() throws Exception {
289:
290: AutoPingManager mgr = RollerFactory.getRoller()
291: .getAutopingManager();
292: AutoPingData testAutoPing = null;
293:
294: // create ping target to use for tests
295: PingTargetData pingTarget = TestUtils.setupPingTarget(
296: "fooPing", "http://foo/null");
297: PingTargetData pingTarget2 = TestUtils.setupPingTarget(
298: "blahPing", "http://blah/null");
299: PingTargetData pingTarget3 = TestUtils.setupPingTarget(
300: "gahPing", "http://gah/null");
301:
302: // create auto pings for test
303: AutoPingData autoPing = TestUtils.setupAutoPing(pingTarget,
304: testWeblog);
305: AutoPingData autoPing2 = TestUtils.setupAutoPing(pingTarget2,
306: testWeblog);
307: AutoPingData autoPing3 = TestUtils.setupAutoPing(pingTarget3,
308: testWeblog);
309: TestUtils.endSession(true);
310:
311: // remove by weblog/target
312: mgr.removeAutoPing(pingTarget, testWeblog);
313: TestUtils.endSession(true);
314:
315: // make sure remove succeeded
316: testAutoPing = null;
317: testAutoPing = mgr.getAutoPing(autoPing.getId());
318: assertNull(testAutoPing);
319:
320: // remove a collection
321: List autoPings = new ArrayList();
322: autoPings.add(autoPing2);
323: autoPings.add(autoPing3);
324: mgr.removeAutoPings(autoPings);
325: TestUtils.endSession(true);
326:
327: // make sure delete was successful
328: autoPings = mgr.getAutoPingsByWebsite(testWeblog);
329: assertNotNull(autoPings);
330: assertEquals(0, autoPings.size());
331:
332: // need to create more test pings
333: autoPing = TestUtils.setupAutoPing(pingTarget, testWeblog);
334: autoPing2 = TestUtils.setupAutoPing(pingTarget2, testWeblog);
335: autoPing3 = TestUtils.setupAutoPing(pingTarget3, testWeblog);
336: TestUtils.endSession(true);
337:
338: // remove all
339: mgr.removeAllAutoPings();
340: TestUtils.endSession(true);
341:
342: // make sure remove succeeded
343: autoPings = mgr.getAutoPingsByWebsite(testWeblog);
344: assertNotNull(autoPings);
345: assertEquals(0, autoPings.size());
346:
347: // teardown test ping target
348: TestUtils.teardownPingTarget(pingTarget.getId());
349: TestUtils.teardownPingTarget(pingTarget2.getId());
350: TestUtils.endSession(true);
351: }
352:
353: /**
354: * Test lookup mechanisms ... id, ping target, weblog
355: */
356: public void testAutoPingLookups() throws Exception {
357:
358: AutoPingManager mgr = RollerFactory.getRoller()
359: .getAutopingManager();
360: AutoPingData autoPing = null;
361:
362: // create autoPing target to use for tests
363: PingTargetData pingTarget = TestUtils.setupPingTarget(
364: "fooPing", "http://foo/null");
365: TestUtils.endSession(true);
366:
367: // create autoPing
368: autoPing = new AutoPingData(null, pingTarget, testWeblog);
369: mgr.saveAutoPing(autoPing);
370: String id = autoPing.getId();
371: TestUtils.endSession(true);
372:
373: // lookup by id
374: autoPing = null;
375: autoPing = mgr.getAutoPing(id);
376: assertNotNull(autoPing);
377: assertEquals(pingTarget, autoPing.getPingTarget());
378:
379: // lookup by ping target
380: List autoPings = mgr.getAutoPingsByTarget(pingTarget);
381: assertNotNull(autoPings);
382: assertEquals(1, autoPings.size());
383:
384: // lookup by weblog
385: autoPings = null;
386: autoPings = mgr.getAutoPingsByWebsite(testWeblog);
387: assertNotNull(autoPing);
388: assertEquals(1, autoPings.size());
389:
390: // delete autoPing
391: mgr.removeAutoPing(autoPing);
392: TestUtils.endSession(true);
393:
394: // teardown test ping target
395: TestUtils.teardownPingTarget(pingTarget.getId());
396: TestUtils.endSession(true);
397: }
398:
399: public void testApplicableAutoPings() throws Exception {
400:
401: }
402:
403: /**
404: * Test that we can properly remove a ping target when it has
405: * associated elements like auto pings and ping queue entries.
406: */
407: public void testRemoveLoadedPingTarget() throws Exception {
408: // TODO: implement this test
409: }
410:
411: }
|