001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business;
020:
021: import java.io.BufferedReader;
022: import java.io.InputStream;
023: import java.io.InputStreamReader;
024: import java.util.List;
025: import junit.framework.Test;
026: import junit.framework.TestCase;
027: import junit.framework.TestSuite;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.roller.RollerException;
031: import org.apache.roller.TestUtils;
032: import org.apache.roller.business.BookmarkManager;
033: import org.apache.roller.business.Roller;
034: import org.apache.roller.business.RollerFactory;
035: import org.apache.roller.pojos.BookmarkData;
036: import org.apache.roller.pojos.FolderData;
037: import org.apache.roller.pojos.UserData;
038: import org.apache.roller.pojos.WebsiteData;
039: import org.apache.roller.util.Utilities;
040:
041: /**
042: * Test Roller Bookmark Management.
043: */
044: public class BookmarkTest extends TestCase {
045:
046: public static Log log = LogFactory.getLog(BookmarkTest.class);
047:
048: UserData testUser = null;
049: WebsiteData testWeblog = null;
050:
051: public BookmarkTest(String name) {
052: super (name);
053: }
054:
055: public static Test suite() {
056: return new TestSuite(BookmarkTest.class);
057: }
058:
059: /**
060: * All tests in this suite require a user and a weblog.
061: */
062: public void setUp() throws Exception {
063:
064: try {
065: testUser = TestUtils.setupUser("bkmrkTestUser");
066: testWeblog = TestUtils.setupWeblog("bkmrkTestWeblog",
067: testUser);
068: TestUtils.endSession(true);
069: } catch (Exception ex) {
070: log.error(ex);
071: throw new Exception("Test setup failed", ex);
072: }
073: }
074:
075: public void tearDown() throws Exception {
076:
077: try {
078: TestUtils.teardownWeblog(testWeblog.getId());
079: TestUtils.teardownUser(testUser.getId());
080: TestUtils.endSession(true);
081: } catch (Exception ex) {
082: log.error(ex);
083: throw new Exception("Test teardown failed", ex);
084: }
085: }
086:
087: public Roller getRoller() {
088: return RollerFactory.getRoller();
089: }
090:
091: public void testAddBookmarkToFolder() throws Exception {
092:
093: BookmarkManager bmgr = getRoller().getBookmarkManager();
094: FolderData folder = null;
095: BookmarkData bookmark1 = null, bookmark2 = null;
096:
097: FolderData root = bmgr.getRootFolder(testWeblog);
098:
099: folder = new FolderData();
100: folder.setName("TestFolder1");
101: folder.setDescription("created by testAddBookmarkToFolder()");
102: folder.setWebsite(testWeblog);
103: folder.setParent(root);
104: bmgr.saveFolder(folder);
105:
106: // Add bookmark by adding to folder
107: bookmark1 = new BookmarkData(folder, "TestBookmark1",
108: "created by testAddBookmarkToFolder()",
109: "http://www.example.com",
110: "http://www.example.com/rss.xml", new Integer(1),
111: new Integer(12), "test.jpg");
112: folder.addBookmark(bookmark1);
113:
114: // Add another bookmark
115: bookmark2 = new BookmarkData(folder, "TestBookmark2",
116: "created by testAddBookmarkToFolder()",
117: "http://www.example.com",
118: "http://www.example.com/rss.xml", new Integer(1),
119: new Integer(12), "test.jpf");
120: folder.addBookmark(bookmark2);
121:
122: TestUtils.endSession(true);
123:
124: FolderData testFolder = null;
125: BookmarkData bookmarkb = null, bookmarka = null;
126:
127: // See that two bookmarks were stored
128: testFolder = bmgr.getFolder(folder.getId());
129: assertEquals(2, testFolder.getBookmarks().size());
130: bookmarka = (BookmarkData) testFolder.getBookmarks().iterator()
131: .next();
132: bookmarkb = (BookmarkData) testFolder.getBookmarks().iterator()
133: .next();
134: // Remove one bookmark
135: testFolder.removeBookmark(bookmarka);
136: bmgr.removeBookmark(bookmarka);
137: TestUtils.endSession(true);
138:
139: // Folder should now contain one bookmark
140: testFolder = bmgr.getFolder(folder.getId());
141: assertEquals(1, testFolder.getBookmarks().size());
142: TestUtils.endSession(true);
143:
144: // Remove folder
145: testFolder = bmgr.getFolder(folder.getId());
146: bmgr.removeFolder(testFolder);
147: TestUtils.endSession(true);
148:
149: // Folder and one remaining bookmark should be gone
150: assertNull(bmgr.getBookmark(bookmarkb.getId()));
151: assertNull(bmgr.getFolder(folder.getId()));
152: }
153:
154: public void testBookmarkImport() throws Exception {
155: importBookmarks("/bookmarks.opml");
156: }
157:
158: public void importBookmarks(String fileName) throws Exception {
159:
160: InputStream fis = this .getClass().getResourceAsStream(fileName);
161: getRoller().getBookmarkManager().importBookmarks(testWeblog,
162: "ZZZ_imports_ZZZ", fileToString(fis));
163: TestUtils.endSession(true);
164:
165: FolderData fd = null;
166:
167: fd = getRoller().getBookmarkManager().getFolder(testWeblog,
168: "ZZZ_imports_ZZZ");
169: assertTrue("no bookmarks found", fd.retrieveBookmarks(true)
170: .size() > 0);
171: getRoller().getBookmarkManager().removeFolder(fd);
172: TestUtils.endSession(true);
173: }
174:
175: public String fileToString(InputStream is)
176: throws java.io.IOException {
177: BufferedReader br = new BufferedReader(
178: new InputStreamReader(is));
179: String s = null;
180: StringBuffer sb = new StringBuffer();
181: while ((s = br.readLine()) != null) {
182: sb.append(s);
183: }
184: return sb.toString();
185: }
186:
187: /**
188: * Creates folder tree like this:
189: * root/
190: * dest/
191: * f1/
192: * b1
193: * f2/
194: * f3/
195: */
196: public void testMoveFolderContents() throws Exception {
197:
198: BookmarkManager bmgr = getRoller().getBookmarkManager();
199:
200: FolderData root = bmgr.getRootFolder(testWeblog);
201:
202: FolderData dest = new FolderData();
203: dest.setName("dest");
204: dest.setParent(root);
205: dest.setWebsite(testWeblog);
206: bmgr.saveFolder(dest);
207:
208: // create source folder f1
209: FolderData f1 = new FolderData();
210: f1.setName("f1");
211: f1.setParent(root);
212: f1.setWebsite(testWeblog);
213: bmgr.saveFolder(f1);
214:
215: // create bookmark b1 inside source folder f1
216: BookmarkData b1 = new BookmarkData(f1, "b1", "testbookmark",
217: "http://example.com", "http://example.com/rss",
218: new Integer(1), new Integer(1), "image.gif");
219: f1.addBookmark(b1);
220:
221: // create folder f2 inside f1
222: FolderData f2 = new FolderData();
223: f2.setName("f2");
224: f2.setParent(f1);
225: f2.setWebsite(testWeblog);
226: bmgr.saveFolder(f2);
227:
228: // create bookmark b2 inside folder f2
229: BookmarkData b2 = new BookmarkData(f2, "b2", "testbookmark",
230: "http://example.com", "http://example.com/rss",
231: new Integer(1), new Integer(1), "image.gif");
232: f2.addBookmark(b2);
233:
234: // create folder f3 inside folder f2
235: FolderData f3 = new FolderData();
236: f3.setName("f3");
237: f3.setParent(f2);
238: f3.setWebsite(testWeblog);
239: bmgr.saveFolder(f3);
240:
241: // crete bookmark b3 inside folder f3
242: BookmarkData b3 = new BookmarkData(f3, "b3", "testbookmark",
243: "http://example.com", "http://example.com/rss",
244: new Integer(1), new Integer(1), "image.gif");
245: f3.addBookmark(b3);
246:
247: TestUtils.endSession(true);
248:
249: // test that parent cannot be moved into child
250: boolean safe = false;
251: try {
252:
253: // Move folder into one of it's children
254: f1 = bmgr.getFolder(f1.getId());
255: f3 = bmgr.getFolder(f3.getId());
256: bmgr.moveFolderContents(f1, f3);
257: //f3.save();
258: //f1.save();
259: TestUtils.endSession(true);
260: } catch (RollerException e) {
261: safe = true;
262: }
263: assertTrue(safe);
264:
265: // verify number of entries in each folder
266: dest = bmgr.getFolder(dest.getId());
267: f1 = bmgr.getFolder(f1.getId());
268: assertEquals(0, dest.retrieveBookmarks(true).size());
269: assertEquals(0, dest.retrieveBookmarks(false).size());
270: assertEquals(1, f1.retrieveBookmarks(false).size());
271:
272: List f1list = f1.retrieveBookmarks(true);
273: assertEquals(3, f1list.size());
274:
275: // move contents of source category c1 to destination catetory dest
276: f1.moveContents(dest);
277: bmgr.saveFolder(f1);
278: bmgr.saveFolder(dest);
279: TestUtils.endSession(true);
280:
281: // after move, verify number of entries in eacch folder
282: dest = bmgr.getFolder(dest.getId());
283: f1 = bmgr.getFolder(f1.getId());
284: assertEquals(3, dest.retrieveBookmarks(true).size());
285: assertEquals(3, dest.retrieveBookmarks(false).size());
286: assertEquals(0, f1.retrieveBookmarks(true).size());
287: assertEquals(0, f1.retrieveBookmarks(false).size());
288: }
289:
290: /** Test bookmark folder paths. */
291: public void testPaths() throws Exception {
292:
293: BookmarkManager bmgr = getRoller().getBookmarkManager();
294:
295: try {
296: FolderData root = bmgr.getRootFolder(testWeblog);
297:
298: FolderData f1 = new FolderData();
299: f1.setName("f1");
300: f1.setParent(root);
301: f1.setWebsite(testWeblog);
302: bmgr.saveFolder(f1);
303:
304: FolderData f2 = new FolderData();
305: f2.setName("f2");
306: f2.setParent(f1);
307: f2.setWebsite(testWeblog);
308: bmgr.saveFolder(f2);
309:
310: FolderData f3 = new FolderData();
311: f3.setName("f3");
312: f3.setParent(f2);
313: f3.setWebsite(testWeblog);
314: bmgr.saveFolder(f3);
315:
316: TestUtils.endSession(true);
317: } catch (RollerException e) {
318: TestUtils.endSession(true);
319: log.error(e);
320: }
321:
322: try {
323:
324: assertEquals("f1", bmgr.getFolderByPath(testWeblog, null,
325: "/f1").getName());
326: assertEquals("f2", bmgr.getFolderByPath(testWeblog, null,
327: "/f1/f2").getName());
328: assertEquals("f3", bmgr.getFolderByPath(testWeblog, null,
329: "/f1/f2/f3").getName());
330:
331: FolderData f3 = bmgr.getFolderByPath(testWeblog, null,
332: "/f1/f2/f3");
333: String pathString = bmgr.getPath(f3);
334: String[] pathArray = Utilities.stringToStringArray(
335: pathString, "/");
336: assertEquals("f1", pathArray[0]);
337: assertEquals("f2", pathArray[1]);
338: assertEquals("f3", pathArray[2]);
339:
340: } catch (RollerException e) {
341: TestUtils.endSession(true);
342: log.error(e);
343: }
344: }
345:
346: /** Ensure that duplicate folder name will throw RollerException */
347: public void testUniquenessOfFolderNames() throws Exception {
348:
349: boolean exception = false;
350: FolderData f3 = null;
351: BookmarkManager bmgr = getRoller().getBookmarkManager();
352: try {
353: FolderData root = bmgr.getRootFolder(testWeblog);
354:
355: FolderData f1 = new FolderData();
356: f1.setName("f1");
357: f1.setParent(root);
358: f1.setWebsite(testWeblog);
359: bmgr.saveFolder(f1);
360:
361: // first child folder
362: FolderData f2 = new FolderData();
363: f2.setName("f2");
364: f2.setParent(f1);
365: f2.setWebsite(testWeblog);
366: bmgr.saveFolder(f2);
367:
368: TestUtils.endSession(true);
369:
370: // child folder with same name as first
371: f3 = new FolderData();
372: f3.setName("f2");
373: f3.setParent(f1);
374: f3.setWebsite(testWeblog);
375: bmgr.saveFolder(f3);
376:
377: TestUtils.endSession(true);
378: } catch (RollerException e) {
379: exception = true;
380: }
381:
382: assertTrue(exception);
383: }
384:
385: }
|