KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > 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 package org.roller.business;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.roller.RollerException;
29 import org.roller.model.PlanetManager;
30 import org.roller.model.Roller;
31 import org.roller.model.RollerFactory;
32 import org.roller.pojos.PlanetConfigData;
33 import org.roller.pojos.PlanetEntryData;
34 import org.roller.pojos.PlanetGroupData;
35 import org.roller.pojos.PlanetSubscriptionData;
36 import org.roller.pojos.UserData;
37
38 /**
39  * Test database implementation of PlanetManager.
40  * @author Dave Johnson
41  */

42 public class PlanetManagerTest extends TestCase
43 {
44     private Roller roller = null;
45     
46     public static void main(String JavaDoc[] args)
47     {
48         junit.textui.TestRunner.run(PlanetManagerTest.class);
49     }
50
51     public void testConfigurationStorage() throws Exception JavaDoc
52     {
53         Roller roller = getRoller();
54         assertNotNull(roller);
55         PlanetManager planet = roller.getPlanetManager();
56         assertNotNull(planet);
57         
58         { // save config with default group
59
roller.begin();
60             
61             PlanetConfigData config = new PlanetConfigData();
62             config.setCacheDir("test_cache_dir");
63             config.setTitle("test_title");
64             config.setAdminEmail("test_admin_email");
65
66             PlanetGroupData group = new PlanetGroupData();
67             group.setDescription("test_group_desc");
68             group.setHandle("test_handle");
69             group.setTitle("test_title");
70             planet.saveGroup(group);
71             
72             config.setDefaultGroup(group);
73             planet.saveConfiguration(config);
74             
75             roller.commit();
76         }
77         { // retrieve config and default group
78
roller.begin();
79             PlanetConfigData config = planet.getConfiguration();
80             assertEquals("test_cache_dir",config.getCacheDir());
81             assertEquals("test_title", config.getTitle());
82             assertEquals("test_admin_email", config.getAdminEmail());
83             
84             PlanetGroupData group = config.getDefaultGroup();
85             assertEquals("test_group_desc",group.getDescription());
86             assertEquals("test_title",group.getTitle());
87             
88             roller.rollback();
89         }
90         { // remove config
91
roller.begin();
92             PlanetConfigData config = planet.getConfiguration();
93             config.remove();
94             roller.commit();
95         }
96         {
97             // make sure config and group are gone
98
roller.begin();
99             PlanetConfigData config = planet.getConfiguration();
100             assertNull(config);
101             PlanetGroupData group = planet.getGroup("test_handle");
102             assertNull(group);
103             roller.rollback();
104         }
105     }
106     
107     public void testGroupStorage() throws Exception JavaDoc
108     {
109         Roller roller = getRoller();
110         assertNotNull(roller);
111         PlanetManager planet = roller.getPlanetManager();
112         assertNotNull(planet);
113         
114         { // save group
115
roller.begin(UserData.SYSTEM_USER);
116             PlanetGroupData group = new PlanetGroupData();
117             group.setDescription("test_group_desc");
118             group.setHandle("test_handle");
119             group.setTitle("test_title");
120             planet.saveGroup(group);
121             roller.commit();
122         }
123         { // retrieve group
124
roller.begin();
125             PlanetGroupData group = planet.getGroup("test_handle");
126             assertEquals("test_group_desc",group.getDescription());
127             assertEquals("test_title",group.getTitle());
128             assertEquals(1, planet.getGroupHandles().size());
129             roller.rollback();
130         }
131         { // remove group
132
roller.begin();
133             PlanetGroupData group = planet.getGroup("test_handle");
134             planet.deleteGroup(group);
135             roller.commit();
136         }
137         { // verify that it is gone
138
roller.begin();
139             PlanetGroupData group = planet.getGroup("test_handle");
140             assertNull(group);
141             roller.rollback();
142         }
143     }
144     
145     public void testSubscriptionStorage() throws Exception JavaDoc
146     {
147         Roller roller = getRoller();
148         assertNotNull(roller);
149         PlanetManager planet = roller.getPlanetManager();
150         assertNotNull(planet);
151         
152         { // save subscription
153
roller.begin();
154             PlanetSubscriptionData sub = new PlanetSubscriptionData();
155             sub.setFeedUrl("test_url");
156             planet.saveSubscription(sub);
157             roller.commit();
158         }
159         { // retrieve subscription and add to group
160
roller.begin();
161             
162             PlanetGroupData group = new PlanetGroupData();
163             group.setDescription("test_group_desc");
164             group.setHandle("test_handle");
165             group.setTitle("test_title");
166             planet.saveGroup(group);
167             
168             PlanetSubscriptionData sub = planet.getSubscription("test_url");
169             assertNotNull(sub);
170             group.addSubscription(sub);
171             
172             PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
173             sub1.setFeedUrl("test_url1");
174             planet.saveSubscription(sub1);
175             
176             List JavaDoc subs = new ArrayList JavaDoc();
177             subs.add(sub1);
178             group.addSubscriptions(subs);
179            
180             planet.saveGroup(group);
181
182             roller.commit();
183         }
184         { // get group and check it's subscriptions, remove it
185
roller.begin();
186             PlanetGroupData group = planet.getGroup("test_handle");
187             Set JavaDoc subs = group.getSubscriptions();
188             assertEquals(2, subs.size());
189             planet.deleteGroup(group);
190             roller.commit();
191         }
192         { // make sure group gone, subs still there, then remove them too
193
roller.begin();
194             PlanetGroupData group = planet.getGroup("test_handle");
195             assertNull(group);
196             PlanetSubscriptionData sub = planet.getSubscription("test_url");
197             assertNotNull(sub);
198             PlanetSubscriptionData sub1 = planet.getSubscription("test_url1");
199             assertNotNull(sub1);
200             planet.deleteSubscription(sub);
201             planet.deleteSubscription(sub1);
202             roller.commit();
203         }
204         { // make sure subscriptions are gone
205
roller.begin();
206             PlanetSubscriptionData sub = planet.getSubscription("test_url");
207             assertNull(sub);
208             PlanetSubscriptionData sub1 = planet.getSubscription("test_url1");
209             assertNull(sub1);
210             roller.rollback();
211         }
212     }
213     public void testSubscriptionEntryStorage() throws Exception JavaDoc
214     {
215         Roller roller = getRoller();
216         assertNotNull(roller);
217         PlanetManager planet = roller.getPlanetManager();
218         assertNotNull(planet);
219         
220         { // save subscription
221
roller.begin();
222             PlanetSubscriptionData sub = new PlanetSubscriptionData();
223             sub.setFeedUrl("test_url");
224             planet.saveSubscription(sub);
225             roller.commit();
226         }
227         { // retrieve subscription and add entries
228
roller.begin();
229             PlanetSubscriptionData sub = planet.getSubscription("test_url");
230             assertNotNull(sub);
231             
232             PlanetEntryData entry1 = new PlanetEntryData();
233             entry1.setPermalink("test_entry1");
234             entry1.setCategoriesString("test,test2");
235             entry1.setSubscription(sub);
236             entry1.setPublished(new Date JavaDoc());
237             entry1.save();
238             sub.addEntry(entry1);
239             
240             PlanetEntryData entry2 = new PlanetEntryData();
241             entry2.setPermalink("test_entry2");
242             entry2.setCategoriesString("test_cat1,test_cat2,test_cat3");
243             entry2.setSubscription(sub);
244             entry2.setPublished(new Date JavaDoc());
245             entry2.save();
246             sub.addEntry(entry2);
247             
248             roller.commit();
249         }
250         { // get sub and check it's entries, remove it
251
roller.begin();
252             PlanetSubscriptionData sub = planet.getSubscription("test_url");
253             assertEquals(2, sub.getEntries().size());
254             planet.deleteSubscription(sub);
255             roller.commit();
256         }
257         { // make sure sub is gone
258
roller.begin();
259             PlanetSubscriptionData sub = planet.getSubscription("test_url");
260             assertNull(sub);
261             roller.rollback();
262         }
263     }
264     public void testRefreshEntries()
265     {
266         try
267         {
268             Roller roller = getRoller();
269             assertNotNull(roller);
270             PlanetManager planet = roller.getPlanetManager();
271             assertNotNull(planet);
272             String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
273             {
274                 roller.begin();
275                 PlanetConfigData config = new PlanetConfigData();
276                 config.setCacheDir("/tmp"); // TODO use real temp dir
277
config.setTitle("test_title");
278                 config.setAdminEmail("test_admin_email");
279                 planet.saveConfiguration(config);
280                 
281                 PlanetGroupData group = new PlanetGroupData();
282                 group.setDescription("test_group_desc");
283                 group.setHandle("test_handle");
284                 group.setTitle("test_title");
285                 planet.saveGroup(group);
286                 
287                 PlanetSubscriptionData sub = new PlanetSubscriptionData();
288                 sub.setFeedUrl(feed_url1);
289                 planet.saveSubscription(sub);
290                 
291                 group.addSubscription(sub);
292                 planet.saveGroup(group);
293                 
294                 roller.commit();
295             }
296             {
297                 roller.begin();
298                 planet.refreshEntries();
299                 roller.commit();
300                 
301                 roller.begin();
302                 PlanetSubscriptionData sub = planet.getSubscription(feed_url1);
303                 assertTrue(sub.getEntries().size() > 0);
304           
305                 PlanetGroupData group = planet.getGroup("test_handle");
306                 assertNotNull(group);
307                 
308                 planet.deleteSubscription(sub);
309                 planet.deleteGroup(group);
310                 
311                 PlanetConfigData config = planet.getConfiguration();
312                 config.remove();
313                 roller.commit();
314             }
315         }
316         catch (Exception JavaDoc e)
317         {
318             e.printStackTrace();
319         }
320     }
321     public void testAggregations() throws Exception JavaDoc
322     {
323         try
324         {
325             Roller roller = getRoller();
326             assertNotNull(roller);
327             PlanetManager planet = roller.getPlanetManager();
328             assertNotNull(planet);
329             String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
330             String JavaDoc feed_url2 = "http://linuxintegrators.com/acoliver/?flavor=rss2";
331             {
332                 roller.begin();
333                 PlanetConfigData config = new PlanetConfigData();
334                 config.setCacheDir("/tmp"); // TODO use real temp dir
335
config.setTitle("test_title");
336                 config.setAdminEmail("test_admin_email");
337                 planet.saveConfiguration(config);
338                 
339                 PlanetGroupData group = new PlanetGroupData();
340                 group.setDescription("test_group_desc");
341                 group.setHandle("test_handle");
342                 group.setTitle("test_title");
343                 planet.saveGroup(group);
344                 
345                 PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
346                 sub1.setFeedUrl(feed_url1);
347                 planet.saveSubscription(sub1);
348                 
349                 PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
350                 sub2.setFeedUrl(feed_url2);
351                 planet.saveSubscription(sub2);
352                 
353                 group.addSubscription(sub1);
354                 group.addSubscription(sub2);
355                 planet.saveGroup(group);
356                 
357                 roller.commit();
358             }
359             {
360                 roller.begin();
361                 planet.refreshEntries();
362                 roller.commit();
363                 
364                 roller.begin();
365                 int count = 0;
366                 Iterator JavaDoc subs = planet.getAllSubscriptions();
367                 while (subs.hasNext())
368                 {
369                     PlanetSubscriptionData sub= (PlanetSubscriptionData)subs.next();
370                     count += sub.getEntries().size();
371                 }
372                 PlanetSubscriptionData sub1 = planet.getSubscription(feed_url1);
373                 assertTrue(sub1.getEntries().size() > 0);
374                 PlanetSubscriptionData sub2 = planet.getSubscription(feed_url2);
375                 assertTrue(sub2.getEntries().size() > 0);
376                 assertEquals(count, sub1.getEntries().size() + sub2.getEntries().size());
377                 
378                 PlanetGroupData group = planet.getGroup("test_handle");
379                 assertNotNull(group);
380     
381                 List JavaDoc bigag = planet.getAggregation(group, 1000);
382                 assertEquals(bigag.size(), count);
383                       
384                 List JavaDoc littleag = planet.getAggregation(group, 10);
385                 assertEquals(littleag.size(), count);
386                                   
387                 planet.deleteSubscription(sub1);
388                 planet.deleteSubscription(sub2);
389                 planet.deleteGroup(group);
390                 
391                 PlanetConfigData config = planet.getConfiguration();
392                 config.remove();
393                 roller.commit();
394             }
395         }
396         catch (Exception JavaDoc e)
397         {
398             e.printStackTrace();
399         }
400     }
401     public void testSubscriptionCount() throws Exception JavaDoc
402     {
403         try
404         {
405             Roller roller = getRoller();
406             assertNotNull(roller);
407             PlanetManager planet = roller.getPlanetManager();
408             assertNotNull(planet);
409             String JavaDoc feed_url1 = "http://rollerweblogger.org/rss/roller";
410             String JavaDoc feed_url2 = "http://linuxintegrators.com/acoliver/?flavor=rss2";
411             {
412                 roller.begin();
413                 PlanetSubscriptionData sub1 = new PlanetSubscriptionData();
414                 sub1.setFeedUrl(feed_url1);
415                 planet.saveSubscription(sub1);
416                 PlanetSubscriptionData sub2 = new PlanetSubscriptionData();
417                 sub2.setFeedUrl(feed_url2);
418                 planet.saveSubscription(sub2);
419                 roller.commit();
420     
421                 roller.begin();
422                 assertEquals(2, planet.getSubscriptionCount());
423                 roller.rollback();
424             }
425         }
426         catch (Exception JavaDoc e)
427         {
428             e.printStackTrace();
429         }
430     }
431     /*
432     public void testRankingRange()
433     {
434         int limit = 1000;
435         int count = 5500;
436         int mod = (count / limit) + 1;
437         for (int i=0; i<20; i++)
438         {
439             int start = (i % mod) * limit;
440             int end = start + 999;
441             end = end > count ? count : end;
442             System.out.println("start="+start+" end="+end);
443         }
444     }
445     */

446     public Roller getRoller() throws RollerException
447     {
448         return RollerFactory.getRoller();
449     }
450
451     public static Test suite()
452     {
453         return new TestSuite(PlanetManagerTest.class);
454     }
455 }
456     
457
Popular Tags