KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > BookmarkManagerTest


1
2 package org.roller.business;
3
4 import java.io.BufferedReader JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.util.List JavaDoc;
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 /**
25  * Test Roller Bookmark Management.
26  */

27 public class BookmarkManagerTest extends RollerTestBase
28 {
29     //------------------------------------------------------------------------
30
public BookmarkManagerTest()
31     {
32         super();
33     }
34
35     //------------------------------------------------------------------------
36
public BookmarkManagerTest(String JavaDoc name)
37     {
38         super(name);
39     }
40
41     //------------------------------------------------------------------------
42
public static Test suite()
43     {
44         return new TestSuite(BookmarkManagerTest.class);
45     }
46
47     //------------------------------------------------------------------------
48
public static void main(String JavaDoc args[])
49     {
50         junit.textui.TestRunner.run( BookmarkManagerTest.class );
51     }
52
53     //------------------------------------------------------------------------
54

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         // Add bookmark by adding to folder
75
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 JavaDoc(1),
82             new Integer JavaDoc(12),
83             "test.jpg");
84         folder.addBookmark(bookmark1);
85
86         // Add another bookmark
87
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 JavaDoc(1),
94             new Integer JavaDoc(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         // See that two bookmarks were stored
105
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         // Remove one bookmark
110
testFolder.removeBookmark(bookmarka);
111         bmgr.removeBookmark(bookmarka.getId());
112         getRoller().commit();
113
114         getRoller().begin(UserData.SYSTEM_USER);
115         // Folder should now contain one bookmark
116
testFolder = bmgr.retrieveFolder(folder.getId());
117         assertEquals(1, testFolder.getBookmarks().size());
118         getRoller().release();
119
120         // Remove folder
121
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         // Folder and one remaining bookmark should be gone
128
assertNull( bmgr.retrieveBookmark(bookmarkb.getId()) );
129         assertNull( bmgr.retrieveFolder(folder.getId()) );
130         getRoller().release();
131     }
132
133     //------------------------------------------------------------------------
134
public void testBookmarkImport() throws Exception JavaDoc
135     {
136         importBookmarks("/bookmarks.opml");
137     }
138
139     //------------------------------------------------------------------------
140
public void importBookmarks(String JavaDoc fileName) throws Exception JavaDoc
141     {
142         getRoller().begin(UserData.SYSTEM_USER);
143         InputStream JavaDoc 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     //------------------------------------------------------------------------
159
public String JavaDoc fileToString( InputStream JavaDoc is ) throws java.io.IOException JavaDoc
160     {
161         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
162         String JavaDoc s = null;
163         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
164         while ( (s=br.readLine()) != null )
165         {
166             sb.append( s );
167         }
168         return sb.toString();
169     }
170
171     //------------------------------------------------------------------------
172

173     /**
174      * Creates folder tree like this:
175      * root/
176      * dest/
177      * f1/
178      * b1
179      * f2/
180      * f3/
181      */

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         // create source folder f1
199
FolderData f1 = bmgr.createFolder();
200         f1.setName("f1");
201         f1.setParent(root);
202         f1.setWebsite(wd);
203         f1.save();
204
205         // create bookmark b1 inside source folder f1
206
BookmarkData b1 = bmgr.createBookmark(
207             f1, "b1", "testbookmark",
208             "http://example.com", "http://example.com/rss",
209             new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
210         b1.save();
211
212         // create folder f2 inside f1
213
FolderData f2 = bmgr.createFolder();
214         f2.setName("f2");
215         f2.setParent(f1);
216         f2.setWebsite(wd);
217         f2.save();
218
219         // create bookmark b2 inside folder f2
220
BookmarkData b2 = bmgr.createBookmark(
221             f2, "b2", "testbookmark",
222             "http://example.com", "http://example.com/rss",
223             new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
224         b2.save();
225
226         // create folder f3 inside folder f2
227
FolderData f3 = bmgr.createFolder();
228         f3.setName("f3");
229         f3.setParent(f2);
230         f3.setWebsite(wd);
231         f3.save();
232
233         // crete bookmark b3 inside folder f3
234
BookmarkData b3 = bmgr.createBookmark(
235             f3, "b3", "testbookmark",
236             "http://example.com", "http://example.com/rss",
237             new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
238         b3.save();
239
240         getRoller().commit();
241         
242         // test that parent cannot be moved into child
243
boolean safe = false;
244         try
245         {
246             getRoller().begin(UserData.SYSTEM_USER);
247                    
248             // Move folder into one of it's children
249
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         // verify number of entries in each folder
265
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 JavaDoc f1list = f1.retrieveBookmarks(true);
272         assertEquals(3, f1list.size());
273
274         // move contents of source category c1 to destination catetory dest
275
f1.moveContents(dest);
276
277         getRoller().commit();
278
279
280
281         getRoller().begin(UserData.SYSTEM_USER);
282
283         // after move, verify number of entries in eacch folder
284
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     /** Test bookmark folder paths. */
295     public void testPaths() throws Exception JavaDoc
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 JavaDoc pathString = bmgr.getPath(f3);
342             String JavaDoc[] 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     /** Ensure that duplicate folder name will throw RollerException */
357     public void testUniquenessOfFolderNames() throws Exception JavaDoc
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             // first child folder
377
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             // child folder with same name as first
388
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