KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > business > BookmarkTest


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18
19 package org.apache.roller.business;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24 import java.util.List JavaDoc;
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 /**
43  * Test Roller Bookmark Management.
44  */

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 JavaDoc name) {
54         super(name);
55     }
56     
57     
58     public static Test suite() {
59         return new TestSuite(BookmarkTest.class);
60     }
61     
62     
63     /**
64      * All tests in this suite require a user and a weblog.
65      */

66     public void setUp() throws Exception JavaDoc {
67         
68         try {
69             testUser = TestUtils.setupUser("bkmrkTestUser");
70             testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog", testUser);
71             TestUtils.endSession(true);
72         } catch (Exception JavaDoc ex) {
73             log.error(ex);
74             throw new Exception JavaDoc("Test setup failed", ex);
75         }
76     }
77     
78     public void tearDown() throws Exception JavaDoc {
79         
80         try {
81             TestUtils.teardownWeblog(testWeblog.getId());
82             TestUtils.teardownUser(testUser.getId());
83             TestUtils.endSession(true);
84         } catch (Exception JavaDoc ex) {
85             log.error(ex);
86             throw new Exception JavaDoc("Test teardown failed", ex);
87         }
88     }
89     
90     public Roller getRoller() {
91         return RollerFactory.getRoller();
92     }
93     
94     
95     public void testAddBookmarkToFolder() throws Exception JavaDoc {
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         // Add bookmark by adding to folder
111
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 JavaDoc(1),
118                 new Integer JavaDoc(12),
119                 "test.jpg");
120         folder.addBookmark(bookmark1);
121         
122         // Add another bookmark
123
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 JavaDoc(1),
130                 new Integer JavaDoc(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         // See that two bookmarks were stored
140
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         // Remove one bookmark
145
testFolder.removeBookmark(bookmarka);
146         bmgr.removeBookmark(bookmarka);
147         TestUtils.endSession(true);
148         
149         // Folder should now contain one bookmark
150
testFolder = bmgr.getFolder(folder.getId());
151         assertEquals(1, testFolder.getBookmarks().size());
152         TestUtils.endSession(true);
153         
154         // Remove folder
155
testFolder = bmgr.getFolder(folder.getId());
156         bmgr.removeFolder(testFolder);
157         TestUtils.endSession(true);
158         
159         // Folder and one remaining bookmark should be gone
160
assertNull( bmgr.getBookmark(bookmarkb.getId()) );
161         assertNull( bmgr.getFolder(folder.getId()) );
162     }
163     
164     
165     public void testBookmarkImport() throws Exception JavaDoc {
166         importBookmarks("/bookmarks.opml");
167     }
168     
169     
170     public void importBookmarks(String JavaDoc fileName) throws Exception JavaDoc {
171         
172         InputStream JavaDoc 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 JavaDoc fileToString( InputStream JavaDoc is ) throws java.io.IOException JavaDoc {
187         BufferedReader JavaDoc br = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(is));
188         String JavaDoc s = null;
189         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
190         while ( (s=br.readLine()) != null ) {
191             sb.append( s );
192         }
193         return sb.toString();
194     }
195     
196     
197     /**
198      * Creates folder tree like this:
199      * root/
200      * dest/
201      * f1/
202      * b1
203      * f2/
204      * f3/
205      */

206     public void testMoveFolderContents() throws Exception JavaDoc {
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         // create source folder f1
219
FolderData f1 = new FolderData();
220         f1.setName("f1");
221         f1.setParent(root);
222         f1.setWebsite(testWeblog);
223         bmgr.saveFolder(f1);
224         
225         // create bookmark b1 inside source folder f1
226
BookmarkData b1 = new BookmarkData(
227                 f1, "b1", "testbookmark",
228                 "http://example.com", "http://example.com/rss",
229                 new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
230         f1.addBookmark(b1);
231         
232         // create folder f2 inside f1
233
FolderData f2 = new FolderData();
234         f2.setName("f2");
235         f2.setParent(f1);
236         f2.setWebsite(testWeblog);
237         bmgr.saveFolder(f2);
238         
239         // create bookmark b2 inside folder f2
240
BookmarkData b2 = new BookmarkData(
241                 f2, "b2", "testbookmark",
242                 "http://example.com", "http://example.com/rss",
243                 new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
244         f2.addBookmark(b2);
245         
246         // create folder f3 inside folder f2
247
FolderData f3 = new FolderData();
248         f3.setName("f3");
249         f3.setParent(f2);
250         f3.setWebsite(testWeblog);
251         bmgr.saveFolder(f3);
252         
253         // crete bookmark b3 inside folder f3
254
BookmarkData b3 = new BookmarkData(
255                 f3, "b3", "testbookmark",
256                 "http://example.com", "http://example.com/rss",
257                 new Integer JavaDoc(1), new Integer JavaDoc(1), "image.gif");
258         f3.addBookmark(b3);
259         
260         TestUtils.endSession(true);
261         
262         // test that parent cannot be moved into child
263
boolean safe = false;
264         try {
265             
266             // Move folder into one of it's children
267
f1 = bmgr.getFolder(f1.getId());
268             f3 = bmgr.getFolder(f3.getId());
269             bmgr.moveFolderContents(f1, f3);
270             //f3.save();
271
//f1.save();
272
TestUtils.endSession(true);
273         } catch (RollerException e) {
274             safe = true;
275         }
276         assertTrue(safe);
277         
278         // verify number of entries in each folder
279
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 JavaDoc f1list = f1.retrieveBookmarks(true);
286         assertEquals(3, f1list.size());
287         
288         // move contents of source category c1 to destination catetory dest
289
f1.moveContents(dest);
290         bmgr.saveFolder(f1);
291         bmgr.saveFolder(dest);
292         TestUtils.endSession(true);
293         
294         // after move, verify number of entries in eacch folder
295
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     /** Test bookmark folder paths. */
305     public void testPaths() throws Exception JavaDoc {
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 JavaDoc pathString = bmgr.getPath(f3);
344             String JavaDoc[] 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     /** Ensure that duplicate folder name will throw RollerException */
357     public void testUniquenessOfFolderNames() throws Exception JavaDoc {
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             // first child folder
372
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             // child folder with same name as first
381
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