001: /*
002: * Copyright 2005 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not 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.
015: */
016:
017: package org.apache.roller.business;
018:
019: import java.io.File;
020: import java.sql.Timestamp;
021: import java.util.HashSet;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Set;
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.TestUtils;
031: import org.apache.roller.config.RollerConfig;
032: import org.apache.roller.planet.business.PlanetManager;
033: import org.apache.roller.business.Roller;
034: import org.apache.roller.business.RollerFactory;
035: import org.apache.roller.planet.pojos.PlanetConfigData;
036: import org.apache.roller.planet.pojos.PlanetEntryData;
037: import org.apache.roller.planet.pojos.PlanetGroupData;
038: import org.apache.roller.planet.pojos.PlanetSubscriptionData;
039:
040: /**
041: * Test database implementation of PlanetManager.
042: */
043: public class PlanetManagerTest extends TestCase {
044:
045: public static Log log = LogFactory.getLog(PlanetManagerTest.class);
046:
047: private Roller roller = null;
048:
049: static {
050: try {
051: // planet config should always exist
052: PlanetConfigData config = new PlanetConfigData();
053: config.setTitle("test_title");
054: config.setAdminEmail("test_admin_email");
055: RollerFactory.getRoller().getPlanetManager()
056: .saveConfiguration(config);
057: TestUtils.endSession(true);
058: } catch (Exception ex) {
059: log.error(ex);
060: throw new RuntimeException(ex);
061: }
062: }
063:
064: public static Test suite() {
065: return new TestSuite(PlanetManagerTest.class);
066: }
067:
068: protected void setUp() throws Exception {
069: super .setUp();
070: //RollerConfig.setPlanetCachePath("." + File.separator + "planet-cache");
071: }
072:
073: public void testConfigurationStorage() throws Exception {
074:
075: PlanetManager planet = RollerFactory.getRoller()
076: .getPlanetManager();
077:
078: { // retrieve config
079: PlanetConfigData config = planet.getConfiguration();
080: assertNotNull(config);
081: assertEquals("test_title", config.getTitle());
082: assertEquals("test_admin_email", config.getAdminEmail());
083: assertNull(config.getSiteURL());
084: }
085: { // save config
086: PlanetConfigData config = planet.getConfiguration();
087: config.setSiteURL("http://footest/lskdf/null");
088: planet.saveConfiguration(config);
089: TestUtils.endSession(true);
090: }
091: {
092: // make sure config was saved
093: PlanetConfigData config = planet.getConfiguration();
094: assertNotNull(config);
095: assertEquals("http://footest/lskdf/null", config
096: .getSiteURL());
097: }
098: }
099:
100: public void testGroupStorage() throws Exception {
101:
102: PlanetManager planet = RollerFactory.getRoller()
103: .getPlanetManager();
104:
105: { // save group
106: PlanetGroupData group = new PlanetGroupData();
107: group.setDescription("test_group_desc");
108: group.setHandle("test_handle");
109: group.setTitle("test_title");
110: planet.saveGroup(group);
111: TestUtils.endSession(true);
112: }
113: { // retrieve group
114: PlanetGroupData group = planet.getGroup("test_handle");
115: assertEquals("test_group_desc", group.getDescription());
116: assertEquals("test_title", group.getTitle());
117: assertTrue(planet.getGroupHandles().size() > 0);
118: }
119: { // remove group
120: PlanetGroupData group = planet.getGroup("test_handle");
121: planet.deleteGroup(group);
122: TestUtils.endSession(true);
123: }
124: { // verify that it is gone
125: PlanetGroupData group = planet.getGroup("test_handle");
126: assertNull(group);
127: }
128: }
129:
130: public void testSubscriptionStorage() throws Exception {
131:
132: PlanetManager planet = RollerFactory.getRoller()
133: .getPlanetManager();
134:
135: { // save subscriptions and a group
136: PlanetSubscriptionData sub = new PlanetSubscriptionData();
137: sub.setFeedURL("test_url");
138: planet.saveSubscription(sub);
139:
140: PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
141: sub1.setFeedURL("test_url1");
142: planet.saveSubscription(sub1);
143:
144: PlanetGroupData group = new PlanetGroupData();
145: group.setDescription("test_group_desc");
146: group.setHandle("test_handle");
147: group.setTitle("test_title");
148: planet.saveGroup(group);
149:
150: TestUtils.endSession(true);
151: }
152: { // retrieve subscriptions and add to group
153:
154: PlanetSubscriptionData sub = planet
155: .getSubscription("test_url");
156: PlanetSubscriptionData sub1 = planet
157: .getSubscription("test_url1");
158: PlanetGroupData group = planet.getGroup("test_handle");
159:
160: group.getSubscriptions().add(sub);
161: sub.getGroups().add(group);
162:
163: group.getSubscriptions().add(sub1);
164: sub1.getGroups().add(group);
165:
166: planet.saveSubscription(sub);
167: planet.saveSubscription(sub1);
168: planet.saveGroup(group);
169:
170: TestUtils.endSession(true);
171: }
172: { // get group and remove one subscription
173: PlanetSubscriptionData sub = planet
174: .getSubscription("test_url");
175: PlanetGroupData group = planet.getGroup("test_handle");
176: group.getSubscriptions().remove(sub);
177: TestUtils.endSession(true);
178: }
179: { // get group and check it's subscriptions, remove it
180: PlanetGroupData group = planet.getGroup("test_handle");
181: Set subs = group.getSubscriptions();
182: assertEquals(1, subs.size());
183: planet.deleteGroup(group);
184: TestUtils.endSession(true);
185: }
186: { // make sure group gone, subs still there, then remove them too
187: PlanetGroupData group = planet.getGroup("test_handle");
188: assertNull(group);
189: PlanetSubscriptionData sub = planet
190: .getSubscription("test_url");
191: assertNotNull(sub);
192: PlanetSubscriptionData sub1 = planet
193: .getSubscription("test_url1");
194: assertNotNull(sub1);
195: planet.deleteSubscription(sub);
196: planet.deleteSubscription(sub1);
197: TestUtils.endSession(true);
198: }
199: { // make sure subscriptions are gone
200: PlanetSubscriptionData sub = planet
201: .getSubscription("test_url");
202: assertNull(sub);
203: PlanetSubscriptionData sub1 = planet
204: .getSubscription("test_url1");
205: assertNull(sub1);
206: }
207: }
208:
209: public void testSubscriptionEntryStorage() throws Exception {
210:
211: PlanetManager planet = RollerFactory.getRoller()
212: .getPlanetManager();
213:
214: { // save subscription
215: PlanetSubscriptionData sub = new PlanetSubscriptionData();
216: sub.setFeedURL("test_url");
217: planet.saveSubscription(sub);
218: TestUtils.endSession(true);
219: }
220: { // retrieve subscription and add entries
221: PlanetSubscriptionData sub = planet
222: .getSubscription("test_url");
223: assertNotNull(sub);
224:
225: PlanetEntryData entry1 = new PlanetEntryData();
226: entry1.setPermalink("test_entry1");
227: entry1.setCategoriesString("test,test2");
228: entry1
229: .setPubTime(new Timestamp(System
230: .currentTimeMillis()));
231: entry1.setSubscription(sub);
232: planet.saveEntry(entry1);
233: sub.addEntry(entry1);
234:
235: PlanetEntryData entry2 = new PlanetEntryData();
236: entry2.setPermalink("test_entry2");
237: entry2.setCategoriesString("test_cat1,test_cat2,test_cat3");
238: entry2
239: .setPubTime(new Timestamp(System
240: .currentTimeMillis()));
241: entry2.setSubscription(sub);
242: planet.saveEntry(entry2);
243: sub.addEntry(entry2);
244:
245: // save entries
246: planet.saveSubscription(sub);
247: TestUtils.endSession(true);
248:
249: // get sub and check it's entries
250: sub = planet.getSubscription("test_url");
251: assertEquals(2, sub.getEntries().size());
252: }
253: {
254: // add a single entry
255: PlanetSubscriptionData sub = planet
256: .getSubscription("test_url");
257: assertNotNull(sub);
258:
259: PlanetEntryData entry3 = new PlanetEntryData();
260: entry3.setPermalink("test_entry3");
261: entry3.setCategoriesString("test,test3");
262: entry3.setSubscription(sub);
263: entry3
264: .setPubTime(new Timestamp(System
265: .currentTimeMillis()));
266: planet.saveEntry(entry3);
267: TestUtils.endSession(true);
268:
269: // verify entry was added
270: sub = planet.getSubscription("test_url");
271: assertEquals(3, sub.getEntries().size());
272: }
273: {
274: // purge entries
275: PlanetSubscriptionData sub = planet
276: .getSubscription("test_url");
277: sub.purgeEntries();
278: planet.saveSubscription(sub);
279: TestUtils.endSession(true);
280:
281: // make sure they were removed
282: sub = planet.getSubscription("test_url");
283: assertEquals(0, sub.getEntries().size());
284: }
285: {
286: // remove test subscription
287: PlanetSubscriptionData sub = planet
288: .getSubscription("test_url");
289: planet.deleteSubscription(sub);
290: TestUtils.endSession(true);
291:
292: // make sure sub is gone
293: sub = planet.getSubscription("test_url");
294: assertNull(sub);
295: }
296: }
297:
298: public void testRefreshEntries() throws Exception {
299:
300: PlanetManager planet = RollerFactory.getRoller()
301: .getPlanetManager();
302:
303: String feed_url1 = "http://rollerweblogger.org/roller/feed/entries/rss";
304:
305: {
306: PlanetConfigData config = planet.getConfiguration();
307: config.setCacheDir("." + File.separator + "planet-cache");
308: planet.saveConfiguration(config);
309:
310: PlanetGroupData group = new PlanetGroupData();
311: group.setDescription("test_group_desc");
312: group.setHandle("test_handle");
313: group.setTitle("test_title");
314: planet.saveGroup(group);
315:
316: PlanetSubscriptionData sub = new PlanetSubscriptionData();
317: sub.setFeedURL(feed_url1);
318: planet.saveSubscription(sub);
319:
320: group.getSubscriptions().add(sub);
321: planet.saveGroup(group);
322: TestUtils.endSession(true);
323: }
324: {
325: planet
326: .refreshEntries("." + File.separator
327: + "planet-cache");
328: TestUtils.endSession(true);
329:
330: PlanetSubscriptionData sub = planet
331: .getSubscription(feed_url1);
332: int entriesSize = sub.getEntries().size();
333:
334: PlanetGroupData group = planet.getGroup("test_handle");
335: assertNotNull(group);
336:
337: planet.deleteGroup(group);
338: planet.deleteSubscription(sub);
339: TestUtils.endSession(true);
340:
341: assertTrue(entriesSize > 0);
342:
343: Object feed = planet.getSubscription(feed_url1);
344: assertNull(feed);
345: }
346: }
347:
348: public void testAggregations() throws Exception {
349:
350: try {
351: PlanetManager planet = RollerFactory.getRoller()
352: .getPlanetManager();
353:
354: String feed_url1 = "http://rollerweblogger.org/roller/feed/entries/rss";
355: String feed_url2 = "http://blogs.sun.com/main/feed/entries/atom";
356:
357: {
358: PlanetGroupData group = new PlanetGroupData();
359: group.setDescription("test_group_desc");
360: group.setHandle("test_handle");
361: group.setTitle("test_title");
362: planet.saveGroup(group);
363:
364: PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
365: sub1.setFeedURL(feed_url1);
366: planet.saveSubscription(sub1);
367:
368: PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
369: sub2.setFeedURL(feed_url2);
370: planet.saveSubscription(sub2);
371:
372: group.getSubscriptions().add(sub1);
373: group.getSubscriptions().add(sub2);
374: planet.saveGroup(group);
375: TestUtils.endSession(true);
376: }
377: {
378: planet.refreshEntries("." + File.separator
379: + "planet-cache");
380: TestUtils.endSession(true);
381:
382: int count = 0;
383: Iterator subs = planet.getAllSubscriptions();
384: while (subs.hasNext()) {
385: PlanetSubscriptionData sub = (PlanetSubscriptionData) subs
386: .next();
387: count += sub.getEntries().size();
388: }
389: PlanetSubscriptionData sub1 = planet
390: .getSubscription(feed_url1);
391: assertTrue(sub1.getEntries().size() > 0);
392: PlanetSubscriptionData sub2 = planet
393: .getSubscription(feed_url2);
394: assertTrue(sub2.getEntries().size() > 0);
395: assertEquals(count, sub1.getEntries().size()
396: + sub2.getEntries().size());
397:
398: PlanetGroupData group = planet.getGroup("test_handle");
399: assertNotNull(group);
400:
401: List bigag = planet.getAggregation(group, null, null,
402: 0, 30);
403: assertEquals(30, bigag.size());
404:
405: List littleag = planet.getAggregation(group, null,
406: null, 0, 10);
407: assertEquals(10, littleag.size());
408:
409: planet.deleteGroup(group);
410: planet.deleteSubscription(sub1);
411: planet.deleteSubscription(sub2);
412: TestUtils.endSession(true);
413: }
414: } catch (Exception e) {
415: e.printStackTrace();
416: fail();
417: }
418: }
419:
420: public void testSubscriptionCount() throws Exception {
421:
422: try {
423: PlanetManager planet = RollerFactory.getRoller()
424: .getPlanetManager();
425:
426: String feed_url1 = "http://rollerweblogger.org/roller/feed/entries/rss";
427: String feed_url2 = "http://blogs.sun.com/main/feed/entries/atom";
428:
429: {
430: PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
431: sub1.setFeedURL(feed_url1);
432: planet.saveSubscription(sub1);
433: PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
434: sub2.setFeedURL(feed_url2);
435: planet.saveSubscription(sub2);
436: TestUtils.endSession(true);
437:
438: assertEquals(2, planet.getSubscriptionCount());
439:
440: planet.deleteSubscription(planet
441: .getSubscription(feed_url1));
442: planet.deleteSubscription(planet
443: .getSubscription(feed_url2));
444: TestUtils.endSession(true);
445: }
446: } catch (Exception e) {
447: e.printStackTrace();
448: fail();
449: }
450: }
451:
452: }
|