1 2 package org.roller.business; 3 4 import java.io.BufferedReader ; 5 import java.io.InputStream ; 6 import java.io.InputStreamReader ; 7 import java.util.List ; 8 9 import junit.framework.Test; 10 import junit.framework.TestSuite; 11 12 import org.roller.RollerException; 13 import org.roller.RollerPermissionsException; 14 import org.roller.model.BookmarkManager; 15 import org.roller.model.WeblogManager; 16 import org.roller.pojos.BookmarkData; 17 import org.roller.pojos.FolderData; 18 import org.roller.pojos.UserData; 19 import org.roller.pojos.WeblogCategoryData; 20 import org.roller.pojos.WebsiteData; 21 import org.roller.util.Utilities; 22 import org.roller.RollerTestBase; 23 24 27 public class BookmarkManagerTest extends RollerTestBase 28 { 29 public BookmarkManagerTest() 31 { 32 super(); 33 } 34 35 public BookmarkManagerTest(String name) 37 { 38 super(name); 39 } 40 41 public static Test suite() 43 { 44 return new TestSuite(BookmarkManagerTest.class); 45 } 46 47 public static void main(String args[]) 49 { 50 junit.textui.TestRunner.run( BookmarkManagerTest.class ); 51 } 52 53 55 public void testAddBookmarkToFolder() throws RollerException 56 { 57 BookmarkManager bmgr = getRoller().getBookmarkManager(); 58 FolderData folder = null; 59 BookmarkData bookmark1 = null, bookmark2 = null; 60 61 getRoller().begin(UserData.SYSTEM_USER); 62 63 WebsiteData website = 64 getRoller().getUserManager().retrieveWebsite(mWebsite.getId()); 65 FolderData root = bmgr.getRootFolder(website); 66 67 folder = bmgr.createFolder(); 68 folder.setName("TestFolder1"); 69 folder.setDescription("created by testAddBookmarkToFolder()"); 70 folder.setWebsite(website); 71 folder.setParent(root); 72 folder.save(); 73 74 bookmark1 = bmgr.createBookmark( 76 folder, 77 "TestBookmark1", 78 "created by testAddBookmarkToFolder()", 79 "http://www.example.com", 80 "http://www.example.com/rss.xml", 81 new Integer (1), 82 new Integer (12), 83 "test.jpg"); 84 folder.addBookmark(bookmark1); 85 86 bookmark2 = bmgr.createBookmark( 88 folder, 89 "TestBookmark2", 90 "created by testAddBookmarkToFolder()", 91 "http://www.example.com", 92 "http://www.example.com/rss.xml", 93 new Integer (1), 94 new Integer (12), 95 "test.jpf"); 96 folder.addBookmark(bookmark2); 97 98 getRoller().commit(); 99 100 FolderData testFolder = null; 101 BookmarkData bookmarkb = null, bookmarka = null; 102 103 getRoller().begin(UserData.SYSTEM_USER); 104 testFolder = bmgr.retrieveFolder(folder.getId()); 106 assertEquals(2, testFolder.getBookmarks().size()); 107 bookmarka = (BookmarkData)testFolder.getBookmarks().iterator().next(); 108 bookmarkb = (BookmarkData)testFolder.getBookmarks().iterator().next(); 109 testFolder.removeBookmark(bookmarka); 111 bmgr.removeBookmark(bookmarka.getId()); 112 getRoller().commit(); 113 114 getRoller().begin(UserData.SYSTEM_USER); 115 testFolder = bmgr.retrieveFolder(folder.getId()); 117 assertEquals(1, testFolder.getBookmarks().size()); 118 getRoller().release(); 119 120 getRoller().begin(UserData.SYSTEM_USER); 122 testFolder = bmgr.retrieveFolder(folder.getId()); 123 testFolder.remove(); 124 getRoller().commit(); 125 126 getRoller().begin(UserData.SYSTEM_USER); 127 assertNull( bmgr.retrieveBookmark(bookmarkb.getId()) ); 129 assertNull( bmgr.retrieveFolder(folder.getId()) ); 130 getRoller().release(); 131 } 132 133 public void testBookmarkImport() throws Exception 135 { 136 importBookmarks("/bookmarks.opml"); 137 } 138 139 public void importBookmarks(String fileName) throws Exception 141 { 142 getRoller().begin(UserData.SYSTEM_USER); 143 InputStream fis = this.getClass().getResourceAsStream(fileName); 144 getRoller().getBookmarkManager().importBookmarks( 145 mWebsite, "ZZZ_imports_ZZZ", fileToString(fis)); 146 getRoller().commit(); 147 getRoller().release(); 148 149 FolderData fd = null; 150 151 getRoller().begin(UserData.SYSTEM_USER); 152 fd = getRoller().getBookmarkManager().getFolder(mWebsite, "ZZZ_imports_ZZZ"); 153 assertTrue("no bookmarks found", fd.retrieveBookmarks(true).size() > 0 ); 154 fd.remove(); 155 getRoller().commit(); 156 } 157 158 public String fileToString( InputStream is ) throws java.io.IOException 160 { 161 BufferedReader br = new BufferedReader (new InputStreamReader (is)); 162 String s = null; 163 StringBuffer sb = new StringBuffer (); 164 while ( (s=br.readLine()) != null ) 165 { 166 sb.append( s ); 167 } 168 return sb.toString(); 169 } 170 171 173 182 public void testMoveFolderContents() throws RollerException 183 { 184 WebsiteData wd = null; 185 BookmarkManager bmgr = getRoller().getBookmarkManager(); 186 187 getRoller().begin(UserData.SYSTEM_USER); 188 189 wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId()); 190 FolderData root = bmgr.getRootFolder(wd); 191 192 FolderData dest = bmgr.createFolder(); 193 dest.setName("dest"); 194 dest.setParent(root); 195 dest.setWebsite(wd); 196 dest.save(); 197 198 FolderData f1 = bmgr.createFolder(); 200 f1.setName("f1"); 201 f1.setParent(root); 202 f1.setWebsite(wd); 203 f1.save(); 204 205 BookmarkData b1 = bmgr.createBookmark( 207 f1, "b1", "testbookmark", 208 "http://example.com", "http://example.com/rss", 209 new Integer (1), new Integer (1), "image.gif"); 210 b1.save(); 211 212 FolderData f2 = bmgr.createFolder(); 214 f2.setName("f2"); 215 f2.setParent(f1); 216 f2.setWebsite(wd); 217 f2.save(); 218 219 BookmarkData b2 = bmgr.createBookmark( 221 f2, "b2", "testbookmark", 222 "http://example.com", "http://example.com/rss", 223 new Integer (1), new Integer (1), "image.gif"); 224 b2.save(); 225 226 FolderData f3 = bmgr.createFolder(); 228 f3.setName("f3"); 229 f3.setParent(f2); 230 f3.setWebsite(wd); 231 f3.save(); 232 233 BookmarkData b3 = bmgr.createBookmark( 235 f3, "b3", "testbookmark", 236 "http://example.com", "http://example.com/rss", 237 new Integer (1), new Integer (1), "image.gif"); 238 b3.save(); 239 240 getRoller().commit(); 241 242 boolean safe = false; 244 try 245 { 246 getRoller().begin(UserData.SYSTEM_USER); 247 248 f1 = bmgr.retrieveFolder(f1.getId()); 250 f3 = bmgr.retrieveFolder(f3.getId()); 251 bmgr.moveFolderContents(f1, f3); 252 f3.save(); 253 f1.save(); 254 getRoller().commit(); 255 } 256 catch (RollerException e) 257 { 258 safe = true; 259 } 260 assertTrue(safe); 261 262 getRoller().begin(UserData.SYSTEM_USER); 263 264 dest = bmgr.retrieveFolder(dest.getId()); 266 f1 = bmgr.retrieveFolder(f1.getId()); 267 assertEquals(0, dest.retrieveBookmarks(true).size()); 268 assertEquals(0, dest.retrieveBookmarks(false).size()); 269 assertEquals(1, f1.retrieveBookmarks(false).size()); 270 271 List f1list = f1.retrieveBookmarks(true); 272 assertEquals(3, f1list.size()); 273 274 f1.moveContents(dest); 276 277 getRoller().commit(); 278 279 280 281 getRoller().begin(UserData.SYSTEM_USER); 282 283 dest = bmgr.retrieveFolder(dest.getId()); 285 f1 = bmgr.retrieveFolder(f1.getId()); 286 assertEquals(3, dest.retrieveBookmarks(true).size()); 287 assertEquals(3, dest.retrieveBookmarks(false).size()); 288 assertEquals(0, f1.retrieveBookmarks(true).size()); 289 assertEquals(0, f1.retrieveBookmarks(false).size()); 290 291 getRoller().commit(); 292 } 293 294 295 public void testPaths() throws Exception 296 { 297 WebsiteData wd = null; 298 BookmarkManager bmgr = getRoller().getBookmarkManager(); 299 try 300 { 301 getRoller().begin(UserData.SYSTEM_USER); 302 303 wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId()); 304 FolderData root = bmgr.getRootFolder(wd); 305 306 FolderData f1 = bmgr.createFolder(); 307 f1.setName("f1"); 308 f1.setParent(root); 309 f1.setWebsite(wd); 310 f1.save(); 311 312 FolderData f2 = bmgr.createFolder(); 313 f2.setName("f2"); 314 f2.setParent(f1); 315 f2.setWebsite(wd); 316 f2.save(); 317 318 FolderData f3 = bmgr.createFolder(); 319 f3.setName("f3"); 320 f3.setParent(f2); 321 f3.setWebsite(wd); 322 f3.save(); 323 324 getRoller().commit(); 325 } 326 catch (RollerException e) 327 { 328 getRoller().release(); 329 mLogger.error(e); 330 } 331 332 try 333 { 334 getRoller().begin(UserData.SYSTEM_USER); 335 336 assertEquals("f1",bmgr.getFolderByPath(wd, null, "/f1").getName()); 337 assertEquals("f2",bmgr.getFolderByPath(wd, null, "/f1/f2").getName()); 338 assertEquals("f3",bmgr.getFolderByPath(wd, null, "/f1/f2/f3").getName()); 339 340 FolderData f3 = bmgr.getFolderByPath(wd, null, "/f1/f2/f3"); 341 String pathString = bmgr.getPath(f3); 342 String [] pathArray = Utilities.stringToStringArray(pathString,"/"); 343 assertEquals("f1", pathArray[0]); 344 assertEquals("f2", pathArray[1]); 345 assertEquals("f3", pathArray[2]); 346 347 getRoller().release(); 348 } 349 catch (RollerException e) 350 { 351 getRoller().release(); 352 mLogger.error(e); 353 } 354 } 355 356 357 public void testUniquenessOfFolderNames() throws Exception 358 { 359 boolean exception = false; 360 WebsiteData wd = null; 361 FolderData f3 = null; 362 BookmarkManager bmgr = getRoller().getBookmarkManager(); 363 try 364 { 365 getRoller().begin(UserData.SYSTEM_USER); 366 367 wd = getRoller().getUserManager().retrieveWebsite(mWebsite.getId()); 368 FolderData root = bmgr.getRootFolder(wd); 369 370 FolderData f1 = bmgr.createFolder(); 371 f1.setName("f1"); 372 f1.setParent(root); 373 f1.setWebsite(wd); 374 f1.save(); 375 376 FolderData f2 = bmgr.createFolder(); 378 f2.setName("f2"); 379 f2.setParent(f1); 380 f2.setWebsite(wd); 381 f2.save(); 382 383 getRoller().commit(); 384 385 getRoller().begin(UserData.SYSTEM_USER); 386 387 f3 = bmgr.createFolder(); 389 f3.setName("f2"); 390 f3.setParent(f1); 391 f3.setWebsite(wd); 392 f3.save(); 393 394 getRoller().commit(); 395 } 396 catch (RollerException e) 397 { 398 exception = true; 399 } 400 401 assertTrue(exception); 402 } 403 404 405 } 406 407 408 409 410 411 412 413 | Popular Tags |