KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2005 Sun Microsystems, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17 package org.apache.roller.business;
18
19 import java.io.File JavaDoc;
20 import java.sql.Timestamp JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Set JavaDoc;
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.roller.TestUtils;
32 import org.apache.roller.config.RollerConfig;
33 import org.apache.roller.model.PlanetManager;
34 import org.apache.roller.model.Roller;
35 import org.apache.roller.model.RollerFactory;
36 import org.apache.roller.pojos.PlanetConfigData;
37 import org.apache.roller.pojos.PlanetEntryData;
38 import org.apache.roller.pojos.PlanetGroupData;
39 import org.apache.roller.pojos.PlanetSubscriptionData;
40
41
42 /**
43  * Test database implementation of PlanetManager.
44  */

45 public class PlanetManagerTest extends TestCase {
46     
47     public static Log log = LogFactory.getLog(PlanetManagerTest.class);
48     
49     private Roller roller = null;
50     
51     static {
52         try {
53             // planet config should always exist
54
PlanetConfigData config = new PlanetConfigData();
55             config.setTitle("test_title");
56             config.setAdminEmail("test_admin_email");
57             RollerFactory.getRoller().getPlanetManager().saveConfiguration(config);
58             TestUtils.endSession(true);
59         } catch (Exception JavaDoc ex) {
60             log.error(ex);
61             throw new RuntimeException JavaDoc(ex);
62         }
63     }
64     
65     
66     public static Test suite() {
67         return new TestSuite(PlanetManagerTest.class);
68     }
69     
70     
71     protected void setUp() throws Exception JavaDoc {
72         super.setUp();
73         RollerConfig.setPlanetCachePath("." + File.separator + "planet-cache");
74     }
75     
76     public void testConfigurationStorage() throws Exception JavaDoc {
77         
78         PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
79         
80         { // retrieve config
81
PlanetConfigData config = planet.getConfiguration();
82             assertNotNull(config);
83             assertEquals("test_title", config.getTitle());
84             assertEquals("test_admin_email", config.getAdminEmail());
85             assertNull(config.getSiteURL());
86         }
87         { // save config
88
PlanetConfigData config = planet.getConfiguration();
89             config.setSiteURL("http://footest/lskdf/null");
90             planet.saveConfiguration(config);
91             TestUtils.endSession(true);
92         }
93         {
94             // make sure config was saved
95
PlanetConfigData config = planet.getConfiguration();
96             assertNotNull(config);
97             assertEquals("http://footest/lskdf/null", config.getSiteURL());
98         }
99     }
100     
101     
102     public void testGroupStorage() throws Exception JavaDoc {
103         
104         PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
105         
106         { // save group
107
PlanetGroupData group = new PlanetGroupData();
108             group.setDescription("test_group_desc");
109             group.setHandle("test_handle");
110             group.setTitle("test_title");
111             planet.saveGroup(group);
112             TestUtils.endSession(true);
113         }
114         { // retrieve group
115
PlanetGroupData group = planet.getGroup("test_handle");
116             assertEquals("test_group_desc",group.getDescription());
117             assertEquals("test_title",group.getTitle());
118             assertTrue(planet.getGroupHandles().size() > 0);
119         }
120         { // remove group
121
PlanetGroupData group = planet.getGroup("test_handle");
122             planet.deleteGroup(group);
123             TestUtils.endSession(true);
124         }
125         { // verify that it is gone
126
PlanetGroupData group = planet.getGroup("test_handle");
127             assertNull(group);
128         }
129     }
130     
131     
132     public void testSubscriptionStorage() throws Exception JavaDoc {
133         
134         PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
135         
136         { // save subscription
137
PlanetSubscriptionData sub = new PlanetSubscriptionData();
138             sub.setFeedURL("test_url");
139             planet.saveSubscription(sub);
140             TestUtils.endSession(true);
141         }
142         { // retrieve subscription and add to group
143
PlanetGroupData group = new PlanetGroupData();
144             group.setDescription("test_group_desc");
145             group.setHandle("test_handle");
146             group.setTitle("test_title");
147             planet.saveGroup(group);
148             
149             PlanetSubscriptionData sub = planet.getSubscription("test_url");
150             assertNotNull(sub);
151             group.addSubscription(sub);
152             
153             PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
154             sub1.setFeedURL("test_url1");
155             planet.saveSubscription(sub1);
156             
157             List JavaDoc subs = new ArrayList JavaDoc();
158             subs.add(sub1);
159             group.addSubscriptions(subs);
160             
161             planet.saveGroup(group);
162             TestUtils.endSession(true);
163         }
164         { // get group and check it's subscriptions, remove it
165
PlanetGroupData group = planet.getGroup("test_handle");
166             Set JavaDoc subs = group.getSubscriptions();
167             assertEquals(2, subs.size());
168             planet.deleteGroup(group);
169             TestUtils.endSession(true);
170         }
171         { // make sure group gone, subs still there, then remove them too
172
PlanetGroupData group = planet.getGroup("test_handle");
173             assertNull(group);
174             PlanetSubscriptionData sub = planet.getSubscription("test_url");
175             assertNotNull(sub);
176             PlanetSubscriptionData sub1 = planet.getSubscription("test_url1");
177             assertNotNull(sub1);
178             planet.deleteSubscription(sub);
179             planet.deleteSubscription(sub1);
180             TestUtils.endSession(true);
181         }
182         { // make sure subscriptions are gone
183
PlanetSubscriptionData sub = planet.getSubscription("test_url");
184             assertNull(sub);
185             PlanetSubscriptionData sub1 = planet.getSubscription("test_url1");
186             assertNull(sub1);
187         }
188     }
189     
190     
191     public void testSubscriptionEntryStorage() throws Exception JavaDoc {
192         
193         PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
194         
195         { // save subscription
196
PlanetSubscriptionData sub = new PlanetSubscriptionData();
197             sub.setFeedURL("test_url");
198             planet.saveSubscription(sub);
199             TestUtils.endSession(true);
200         }
201         { // retrieve subscription and add entries
202
PlanetSubscriptionData sub = planet.getSubscription("test_url");
203             assertNotNull(sub);
204             
205             PlanetEntryData entry1 = new PlanetEntryData();
206             entry1.setPermalink("test_entry1");
207             entry1.setCategoriesString("test,test2");
208             entry1.setSubscription(sub);
209             entry1.setPubTime(new Timestamp JavaDoc(System.currentTimeMillis()));
210             sub.addEntry(entry1);
211             
212             PlanetEntryData entry2 = new PlanetEntryData();
213             entry2.setPermalink("test_entry2");
214             entry2.setCategoriesString("test_cat1,test_cat2,test_cat3");
215             entry2.setSubscription(sub);
216             entry2.setPubTime(new Timestamp JavaDoc(System.currentTimeMillis()));
217             sub.addEntry(entry2);
218             
219             // save entries
220
planet.saveSubscription(sub);
221             TestUtils.endSession(true);
222             
223             // get sub and check it's entries
224
sub = planet.getSubscription("test_url");
225             assertEquals(2, sub.getEntries().size());
226         }
227         {
228             // add a single entry
229
PlanetSubscriptionData sub = planet.getSubscription("test_url");
230             assertNotNull(sub);
231             
232             PlanetEntryData entry3 = new PlanetEntryData();
233             entry3.setPermalink("test_entry3");
234             entry3.setCategoriesString("test,test3");
235             entry3.setSubscription(sub);
236             entry3.setPubTime(new Timestamp JavaDoc(System.currentTimeMillis()));
237             planet.saveEntry(entry3);
238             TestUtils.endSession(true);
239             
240             // verify entry was added
241
sub = planet.getSubscription("test_url");
242             assertEquals(3, sub.getEntries().size());
243         }
244         {
245             // purge entries
246
PlanetSubscriptionData sub = planet.getSubscription("test_url");
247             sub.purgeEntries();
248             planet.saveSubscription(sub);
249             TestUtils.endSession(true);
250             
251             // make sure they were removed
252
sub = planet.getSubscription("test_url");
253             assertEquals(0, sub.getEntries().size());
254         }
255         {
256             // remove test subscription
257
PlanetSubscriptionData sub = planet.getSubscription("test_url");
258             planet.deleteSubscription(sub);
259             TestUtils.endSession(true);
260             
261             // make sure sub is gone
262
sub = planet.getSubscription("test_url");
263             assertNull(sub);
264         }
265     }
266     
267     
268     public void testRefreshEntries() throws Exception JavaDoc {
269         
270         PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
271         
272         String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
273         
274         {
275             PlanetGroupData group = new PlanetGroupData();
276             group.setDescription("test_group_desc");
277             group.setHandle("test_handle");
278             group.setTitle("test_title");
279             planet.saveGroup(group);
280             
281             PlanetSubscriptionData sub = new PlanetSubscriptionData();
282             sub.setFeedURL(feed_url1);
283             planet.saveSubscription(sub);
284             
285             group.addSubscription(sub);
286             planet.saveGroup(group);
287             TestUtils.endSession(true);
288         }
289         {
290             planet.refreshEntries();
291             TestUtils.endSession(true);
292             
293             PlanetSubscriptionData sub = planet.getSubscription(feed_url1);
294             int entriesSize = sub.getEntries().size();
295             
296             PlanetGroupData group = planet.getGroup("test_handle");
297             assertNotNull(group);
298             
299             planet.deleteGroup(group);
300             planet.deleteSubscription(sub);
301             TestUtils.endSession(true);
302             
303             assertTrue(entriesSize > 0);
304             
305             Object JavaDoc feed = planet.getSubscription(feed_url1);
306             assertNull(feed);
307         }
308     }
309     
310     
311     public void testAggregations() throws Exception JavaDoc {
312         
313         try {
314             PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
315             
316             String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
317             String JavaDoc feed_url2 = "http://linuxintegrators.com/acoliver/?flavor=rss2";
318             
319             {
320                 PlanetGroupData group = new PlanetGroupData();
321                 group.setDescription("test_group_desc");
322                 group.setHandle("test_handle");
323                 group.setTitle("test_title");
324                 planet.saveGroup(group);
325                 
326                 PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
327                 sub1.setFeedURL(feed_url1);
328                 planet.saveSubscription(sub1);
329                 
330                 PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
331                 sub2.setFeedURL(feed_url2);
332                 planet.saveSubscription(sub2);
333                 
334                 group.addSubscription(sub1);
335                 group.addSubscription(sub2);
336                 planet.saveGroup(group);
337                 TestUtils.endSession(true);
338             }
339             {
340                 planet.refreshEntries();
341                 TestUtils.endSession(true);
342                 
343                 int count = 0;
344                 Iterator JavaDoc subs = planet.getAllSubscriptions();
345                 while (subs.hasNext()) {
346                     PlanetSubscriptionData sub= (PlanetSubscriptionData)subs.next();
347                     count += sub.getEntries().size();
348                 }
349                 PlanetSubscriptionData sub1 = planet.getSubscription(feed_url1);
350                 assertTrue(sub1.getEntries().size() > 0);
351                 PlanetSubscriptionData sub2 = planet.getSubscription(feed_url2);
352                 assertTrue(sub2.getEntries().size() > 0);
353                 assertEquals(count, sub1.getEntries().size() + sub2.getEntries().size());
354                 
355                 PlanetGroupData group = planet.getGroup("test_handle");
356                 assertNotNull(group);
357                 
358                 List JavaDoc bigag = planet.getAggregation(group, null, null, 0, 30);
359                 assertEquals(30, bigag.size());
360                 
361                 List JavaDoc littleag = planet.getAggregation(group, null, null, 0, 10);
362                 assertEquals(10, littleag.size());
363                 
364                 planet.deleteGroup(group);
365                 planet.deleteSubscription(sub1);
366                 planet.deleteSubscription(sub2);
367                 TestUtils.endSession(true);
368             }
369         } catch (Exception JavaDoc e) {
370             e.printStackTrace();
371             fail();
372         }
373     }
374     
375     
376     public void testSubscriptionCount() throws Exception JavaDoc {
377         
378         try {
379             PlanetManager planet = RollerFactory.getRoller().getPlanetManager();
380             
381             String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
382             String JavaDoc feed_url2 = "http://linuxintegrators.com/acoliver/?flavor=rss2";
383             
384             {
385                 PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
386                 sub1.setFeedURL(feed_url1);
387                 planet.saveSubscription(sub1);
388                 PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
389                 sub2.setFeedURL(feed_url2);
390                 planet.saveSubscription(sub2);
391                 TestUtils.endSession(true);
392                 
393                 assertEquals(2, planet.getSubscriptionCount());
394                 
395                 planet.deleteSubscription(planet.getSubscription(feed_url1));
396                 planet.deleteSubscription(planet.getSubscription(feed_url2));
397                 TestUtils.endSession(true);
398             }
399         } catch (Exception JavaDoc e) {
400             e.printStackTrace();
401             fail();
402         }
403     }
404     
405 }
406
407
Popular Tags