1 18 19 package org.apache.roller.business; 20 21 import java.io.BufferedReader ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.util.List ; 25 import junit.framework.Test; 26 import junit.framework.TestCase; 27 import junit.framework.TestSuite; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.apache.roller.RollerException; 31 import org.apache.roller.TestUtils; 32 import org.apache.roller.model.BookmarkManager; 33 import org.apache.roller.model.Roller; 34 import org.apache.roller.model.RollerFactory; 35 import org.apache.roller.pojos.BookmarkData; 36 import org.apache.roller.pojos.FolderData; 37 import org.apache.roller.pojos.UserData; 38 import org.apache.roller.pojos.WebsiteData; 39 import org.apache.roller.util.Utilities; 40 41 42 45 public class BookmarkTest extends TestCase { 46 47 public static Log log = LogFactory.getLog(BookmarkTest.class); 48 49 UserData testUser = null; 50 WebsiteData testWeblog = null; 51 52 53 public BookmarkTest(String name) { 54 super(name); 55 } 56 57 58 public static Test suite() { 59 return new TestSuite(BookmarkTest.class); 60 } 61 62 63 66 public void setUp() throws Exception { 67 68 try { 69 testUser = TestUtils.setupUser("bkmrkTestUser"); 70 testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog", testUser); 71 TestUtils.endSession(true); 72 } catch (Exception ex) { 73 log.error(ex); 74 throw new Exception ("Test setup failed", ex); 75 } 76 } 77 78 public void tearDown() throws Exception { 79 80 try { 81 TestUtils.teardownWeblog(testWeblog.getId()); 82 TestUtils.teardownUser(testUser.getId()); 83 TestUtils.endSession(true); 84 } catch (Exception ex) { 85 log.error(ex); 86 throw new Exception ("Test teardown failed", ex); 87 } 88 } 89 90 public Roller getRoller() { 91 return RollerFactory.getRoller(); 92 } 93 94 95 public void testAddBookmarkToFolder() throws Exception { 96 97 BookmarkManager bmgr = getRoller().getBookmarkManager(); 98 FolderData folder = null; 99 BookmarkData bookmark1 = null, bookmark2 = null; 100 101 FolderData root = bmgr.getRootFolder(testWeblog); 102 103 folder = new FolderData(); 104 folder.setName("TestFolder1"); 105 folder.setDescription("created by testAddBookmarkToFolder()"); 106 folder.setWebsite(testWeblog); 107 folder.setParent(root); 108 bmgr.saveFolder(folder); 109 110 bookmark1 = new BookmarkData( 112 folder, 113 "TestBookmark1", 114 "created by testAddBookmarkToFolder()", 115 "http://www.example.com", 116 "http://www.example.com/rss.xml", 117 new Integer (1), 118 new Integer (12), 119 "test.jpg"); 120 folder.addBookmark(bookmark1); 121 122 bookmark2 = new BookmarkData( 124 folder, 125 "TestBookmark2", 126 "created by testAddBookmarkToFolder()", 127 "http://www.example.com", 128 "http://www.example.com/rss.xml", 129 new Integer (1), 130 new Integer (12), 131 "test.jpf"); 132 folder.addBookmark(bookmark2); 133 134 TestUtils.endSession(true); 135 136 FolderData testFolder = null; 137 BookmarkData bookmarkb = null, bookmarka = null; 138 139 testFolder = bmgr.getFolder(folder.getId()); 141 assertEquals(2, testFolder.getBookmarks().size()); 142 bookmarka = (BookmarkData)testFolder.getBookmarks().iterator().next(); 143 bookmarkb = (BookmarkData)testFolder.getBookmarks().iterator().next(); 144 testFolder.removeBookmark(bookmarka); 146 bmgr.removeBookmark(bookmarka); 147 TestUtils.endSession(true); 148 149 testFolder = bmgr.getFolder(folder.getId()); 151 assertEquals(1, testFolder.getBookmarks().size()); 152 TestUtils.endSession(true); 153 154 testFolder = bmgr.getFolder(folder.getId()); 156 bmgr.removeFolder(testFolder); 157 TestUtils.endSession(true); 158 159 assertNull( bmgr.getBookmark(bookmarkb.getId()) ); 161 assertNull( bmgr.getFolder(folder.getId()) ); 162 } 163 164 165 public void testBookmarkImport() throws Exception { 166 importBookmarks("/bookmarks.opml"); 167 } 168 169 170 public void importBookmarks(String fileName) throws Exception { 171 172 InputStream fis = this.getClass().getResourceAsStream(fileName); 173 getRoller().getBookmarkManager().importBookmarks( 174 testWeblog, "ZZZ_imports_ZZZ", fileToString(fis)); 175 TestUtils.endSession(true); 176 177 FolderData fd = null; 178 179 fd = getRoller().getBookmarkManager().getFolder(testWeblog, "ZZZ_imports_ZZZ"); 180 assertTrue("no bookmarks found", fd.retrieveBookmarks(true).size() > 0 ); 181 getRoller().getBookmarkManager().removeFolder(fd); 182 TestUtils.endSession(true); 183 } 184 185 186 public String fileToString( InputStream is ) throws java.io.IOException { 187 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 188 String s = null; 189 StringBuffer sb = new StringBuffer (); 190 while ( (s=br.readLine()) != null ) { 191 sb.append( s ); 192 } 193 return sb.toString(); 194 } 195 196 197 206 public void testMoveFolderContents() throws Exception { 207 208 BookmarkManager bmgr = getRoller().getBookmarkManager(); 209 210 FolderData root = bmgr.getRootFolder(testWeblog); 211 212 FolderData dest = new FolderData(); 213 dest.setName("dest"); 214 dest.setParent(root); 215 dest.setWebsite(testWeblog); 216 bmgr.saveFolder(dest); 217 218 FolderData f1 = new FolderData(); 220 f1.setName("f1"); 221 f1.setParent(root); 222 f1.setWebsite(testWeblog); 223 bmgr.saveFolder(f1); 224 225 BookmarkData b1 = new BookmarkData( 227 f1, "b1", "testbookmark", 228 "http://example.com", "http://example.com/rss", 229 new Integer (1), new Integer (1), "image.gif"); 230 f1.addBookmark(b1); 231 232 FolderData f2 = new FolderData(); 234 f2.setName("f2"); 235 f2.setParent(f1); 236 f2.setWebsite(testWeblog); 237 bmgr.saveFolder(f2); 238 239 BookmarkData b2 = new BookmarkData( 241 f2, "b2", "testbookmark", 242 "http://example.com", "http://example.com/rss", 243 new Integer (1), new Integer (1), "image.gif"); 244 f2.addBookmark(b2); 245 246 FolderData f3 = new FolderData(); 248 f3.setName("f3"); 249 f3.setParent(f2); 250 f3.setWebsite(testWeblog); 251 bmgr.saveFolder(f3); 252 253 BookmarkData b3 = new BookmarkData( 255 f3, "b3", "testbookmark", 256 "http://example.com", "http://example.com/rss", 257 new Integer (1), new Integer (1), "image.gif"); 258 f3.addBookmark(b3); 259 260 TestUtils.endSession(true); 261 262 boolean safe = false; 264 try { 265 266 f1 = bmgr.getFolder(f1.getId()); 268 f3 = bmgr.getFolder(f3.getId()); 269 bmgr.moveFolderContents(f1, f3); 270 TestUtils.endSession(true); 273 } catch (RollerException e) { 274 safe = true; 275 } 276 assertTrue(safe); 277 278 dest = bmgr.getFolder(dest.getId()); 280 f1 = bmgr.getFolder(f1.getId()); 281 assertEquals(0, dest.retrieveBookmarks(true).size()); 282 assertEquals(0, dest.retrieveBookmarks(false).size()); 283 assertEquals(1, f1.retrieveBookmarks(false).size()); 284 285 List f1list = f1.retrieveBookmarks(true); 286 assertEquals(3, f1list.size()); 287 288 f1.moveContents(dest); 290 bmgr.saveFolder(f1); 291 bmgr.saveFolder(dest); 292 TestUtils.endSession(true); 293 294 dest = bmgr.getFolder(dest.getId()); 296 f1 = bmgr.getFolder(f1.getId()); 297 assertEquals(3, dest.retrieveBookmarks(true).size()); 298 assertEquals(3, dest.retrieveBookmarks(false).size()); 299 assertEquals(0, f1.retrieveBookmarks(true).size()); 300 assertEquals(0, f1.retrieveBookmarks(false).size()); 301 } 302 303 304 305 public void testPaths() throws Exception { 306 307 BookmarkManager bmgr = getRoller().getBookmarkManager(); 308 309 try { 310 FolderData root = bmgr.getRootFolder(testWeblog); 311 312 FolderData f1 = new FolderData(); 313 f1.setName("f1"); 314 f1.setParent(root); 315 f1.setWebsite(testWeblog); 316 bmgr.saveFolder(f1); 317 318 FolderData f2 = new FolderData(); 319 f2.setName("f2"); 320 f2.setParent(f1); 321 f2.setWebsite(testWeblog); 322 bmgr.saveFolder(f2); 323 324 FolderData f3 = new FolderData(); 325 f3.setName("f3"); 326 f3.setParent(f2); 327 f3.setWebsite(testWeblog); 328 bmgr.saveFolder(f3); 329 330 TestUtils.endSession(true); 331 } catch (RollerException e) { 332 TestUtils.endSession(true); 333 log.error(e); 334 } 335 336 try { 337 338 assertEquals("f1",bmgr.getFolderByPath(testWeblog, null, "/f1").getName()); 339 assertEquals("f2",bmgr.getFolderByPath(testWeblog, null, "/f1/f2").getName()); 340 assertEquals("f3",bmgr.getFolderByPath(testWeblog, null, "/f1/f2/f3").getName()); 341 342 FolderData f3 = bmgr.getFolderByPath(testWeblog, null, "/f1/f2/f3"); 343 String pathString = bmgr.getPath(f3); 344 String [] pathArray = Utilities.stringToStringArray(pathString,"/"); 345 assertEquals("f1", pathArray[0]); 346 assertEquals("f2", pathArray[1]); 347 assertEquals("f3", pathArray[2]); 348 349 } catch (RollerException e) { 350 TestUtils.endSession(true); 351 log.error(e); 352 } 353 } 354 355 356 357 public void testUniquenessOfFolderNames() throws Exception { 358 359 boolean exception = false; 360 FolderData f3 = null; 361 BookmarkManager bmgr = getRoller().getBookmarkManager(); 362 try { 363 FolderData root = bmgr.getRootFolder(testWeblog); 364 365 FolderData f1 = new FolderData(); 366 f1.setName("f1"); 367 f1.setParent(root); 368 f1.setWebsite(testWeblog); 369 bmgr.saveFolder(f1); 370 371 FolderData f2 = new FolderData(); 373 f2.setName("f2"); 374 f2.setParent(f1); 375 f2.setWebsite(testWeblog); 376 bmgr.saveFolder(f2); 377 378 TestUtils.endSession(true); 379 380 f3 = new FolderData(); 382 f3.setName("f2"); 383 f3.setParent(f1); 384 f3.setWebsite(testWeblog); 385 bmgr.saveFolder(f3); 386 387 TestUtils.endSession(true); 388 } catch (RollerException e) { 389 exception = true; 390 } 391 392 assertTrue(exception); 393 } 394 395 } 396 | Popular Tags |