1 16 17 package org.apache.roller.business; 18 19 import java.io.File ; 20 import java.sql.Timestamp ; 21 import java.util.ArrayList ; 22 import java.util.Date ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Set ; 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 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 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 ex) { 60 log.error(ex); 61 throw new RuntimeException (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 { 72 super.setUp(); 73 RollerConfig.setPlanetCachePath("." + File.separator + "planet-cache"); 74 } 75 76 public void testConfigurationStorage() throws Exception { 77 78 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 79 80 { 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 { PlanetConfigData config = planet.getConfiguration(); 89 config.setSiteURL("http://footest/lskdf/null"); 90 planet.saveConfiguration(config); 91 TestUtils.endSession(true); 92 } 93 { 94 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 { 103 104 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 105 106 { 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 { 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 { PlanetGroupData group = planet.getGroup("test_handle"); 122 planet.deleteGroup(group); 123 TestUtils.endSession(true); 124 } 125 { PlanetGroupData group = planet.getGroup("test_handle"); 127 assertNull(group); 128 } 129 } 130 131 132 public void testSubscriptionStorage() throws Exception { 133 134 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 135 136 { PlanetSubscriptionData sub = new PlanetSubscriptionData(); 138 sub.setFeedURL("test_url"); 139 planet.saveSubscription(sub); 140 TestUtils.endSession(true); 141 } 142 { 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 subs = new ArrayList (); 158 subs.add(sub1); 159 group.addSubscriptions(subs); 160 161 planet.saveGroup(group); 162 TestUtils.endSession(true); 163 } 164 { PlanetGroupData group = planet.getGroup("test_handle"); 166 Set subs = group.getSubscriptions(); 167 assertEquals(2, subs.size()); 168 planet.deleteGroup(group); 169 TestUtils.endSession(true); 170 } 171 { 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 { 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 { 192 193 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 194 195 { PlanetSubscriptionData sub = new PlanetSubscriptionData(); 197 sub.setFeedURL("test_url"); 198 planet.saveSubscription(sub); 199 TestUtils.endSession(true); 200 } 201 { 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 (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 (System.currentTimeMillis())); 217 sub.addEntry(entry2); 218 219 planet.saveSubscription(sub); 221 TestUtils.endSession(true); 222 223 sub = planet.getSubscription("test_url"); 225 assertEquals(2, sub.getEntries().size()); 226 } 227 { 228 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 (System.currentTimeMillis())); 237 planet.saveEntry(entry3); 238 TestUtils.endSession(true); 239 240 sub = planet.getSubscription("test_url"); 242 assertEquals(3, sub.getEntries().size()); 243 } 244 { 245 PlanetSubscriptionData sub = planet.getSubscription("test_url"); 247 sub.purgeEntries(); 248 planet.saveSubscription(sub); 249 TestUtils.endSession(true); 250 251 sub = planet.getSubscription("test_url"); 253 assertEquals(0, sub.getEntries().size()); 254 } 255 { 256 PlanetSubscriptionData sub = planet.getSubscription("test_url"); 258 planet.deleteSubscription(sub); 259 TestUtils.endSession(true); 260 261 sub = planet.getSubscription("test_url"); 263 assertNull(sub); 264 } 265 } 266 267 268 public void testRefreshEntries() throws Exception { 269 270 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 271 272 String 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 feed = planet.getSubscription(feed_url1); 306 assertNull(feed); 307 } 308 } 309 310 311 public void testAggregations() throws Exception { 312 313 try { 314 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 315 316 String feed_url1 = "http://rollerweblogger.org/rss/roller"; 317 String 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 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 bigag = planet.getAggregation(group, null, null, 0, 30); 359 assertEquals(30, bigag.size()); 360 361 List 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 e) { 370 e.printStackTrace(); 371 fail(); 372 } 373 } 374 375 376 public void testSubscriptionCount() throws Exception { 377 378 try { 379 PlanetManager planet = RollerFactory.getRoller().getPlanetManager(); 380 381 String feed_url1 = "http://rollerweblogger.org/rss/roller"; 382 String 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 e) { 400 e.printStackTrace(); 401 fail(); 402 } 403 } 404 405 } 406 407 | Popular Tags |