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: * WeblogCategoryTest.java
020: *
021: * Created on April 13, 2006, 10:07 PM
022: */
023:
024: package org.apache.roller.business;
025:
026: import java.util.List;
027: import junit.framework.Test;
028: import junit.framework.TestCase;
029: import junit.framework.TestSuite;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.roller.TestUtils;
033: import org.apache.roller.business.RollerFactory;
034: import org.apache.roller.business.WeblogManager;
035: import org.apache.roller.pojos.UserData;
036: import org.apache.roller.pojos.WeblogCategoryData;
037: import org.apache.roller.pojos.WeblogEntryData;
038: import org.apache.roller.pojos.WebsiteData;
039: import org.apache.roller.util.Utilities;
040:
041: /**
042: * Test Weblog Category related business operations.
043: */
044: public class WeblogCategoryTest extends TestCase {
045:
046: public static Log log = LogFactory.getLog(WeblogCategoryTest.class);
047:
048: UserData testUser = null;
049: WebsiteData testWeblog = null;
050:
051: public WeblogCategoryTest() {
052: }
053:
054: public static Test suite() {
055: return new TestSuite(WeblogCategoryTest.class);
056: }
057:
058: /**
059: * All tests in this suite require a user and a weblog.
060: */
061: public void setUp() throws Exception {
062:
063: log.info("Setup " + this .getClass().getName());
064:
065: try {
066: testUser = TestUtils.setupUser("entryTestUser");
067: testWeblog = TestUtils.setupWeblog("entryTestWeblog",
068: testUser);
069: TestUtils.endSession(true);
070: } catch (Exception ex) {
071: log.error(ex);
072: throw new Exception("Test setup failed", ex);
073: }
074: }
075:
076: public void tearDown() throws Exception {
077:
078: log.info("Teardown " + this .getClass().getName());
079:
080: try {
081: TestUtils.teardownWeblog(testWeblog.getId());
082: TestUtils.teardownUser(testUser.getId());
083: TestUtils.endSession(true);
084: } catch (Exception ex) {
085: log.error(ex);
086: throw new Exception("Test teardown failed", ex);
087: }
088: }
089:
090: /**
091: * Test basic persistence operations ... Create, Update, Delete.
092: */
093: public void testWeblogCategoryCRUD() throws Exception {
094:
095: WeblogManager mgr = RollerFactory.getRoller()
096: .getWeblogManager();
097: WeblogCategoryData cat = null;
098: List cats = null;
099:
100: // we need to know how many categories we start the test with
101: int numCats = mgr.getRootWeblogCategory(testWeblog)
102: .getWeblogCategories().size();
103:
104: // add a new category
105: WeblogCategoryData newCat = new WeblogCategoryData();
106: newCat.setName("catTestCategory");
107: newCat.setParent(testWeblog.getDefaultCategory());
108: newCat.setWebsite(testWeblog);
109: mgr.saveWeblogCategory(newCat);
110: String id = newCat.getId();
111: TestUtils.endSession(true);
112:
113: // make sure category was added
114: cat = null;
115: cat = mgr.getWeblogCategory(id);
116: assertNotNull(cat);
117: assertEquals(newCat, cat);
118:
119: // make sure category count increased
120: testWeblog = RollerFactory.getRoller().getUserManager()
121: .getWebsite(testWeblog.getId());
122: assertEquals(numCats + 1, mgr.getRootWeblogCategory(testWeblog)
123: .getWeblogCategories().size());
124:
125: // update category
126: cat.setName("testtest");
127: mgr.saveWeblogCategory(cat);
128: TestUtils.endSession(true);
129:
130: // verify category was updated
131: cat = null;
132: cat = mgr.getWeblogCategory(id);
133: assertNotNull(cat);
134: assertEquals("testtest", cat.getName());
135:
136: // remove category
137: mgr.removeWeblogCategory(cat);
138: TestUtils.endSession(true);
139:
140: // make sure category was removed
141: cat = null;
142: mgr.getWeblogCategory(id);
143: assertNull(cat);
144:
145: // make sure category count decreased
146: testWeblog = RollerFactory.getRoller().getUserManager()
147: .getWebsite(testWeblog.getId());
148: assertEquals(numCats, mgr.getRootWeblogCategory(testWeblog)
149: .getWeblogCategories().size());
150: }
151:
152: /**
153: * Test lookup mechanisms ...
154: */
155: public void testWeblogCategoryLookups() throws Exception {
156:
157: }
158:
159: public void testWeblogCategoryPaths() throws Exception {
160:
161: WeblogCategoryData root = null;
162: WeblogManager mgr = RollerFactory.getRoller()
163: .getWeblogManager();
164:
165: root = mgr.getRootWeblogCategory(testWeblog);
166:
167: WeblogCategoryData f1 = new WeblogCategoryData();
168: f1.setName("f1");
169: f1.setParent(root);
170: f1.setWebsite(testWeblog);
171: mgr.saveWeblogCategory(f1);
172:
173: WeblogCategoryData f2 = new WeblogCategoryData();
174: f2.setName("f2");
175: f2.setParent(f1);
176: f2.setWebsite(testWeblog);
177: mgr.saveWeblogCategory(f2);
178:
179: WeblogCategoryData f3 = new WeblogCategoryData();
180: f3.setName("f3");
181: f3.setParent(f2);
182: f3.setWebsite(testWeblog);
183: mgr.saveWeblogCategory(f3);
184:
185: TestUtils.endSession(true);
186:
187: // check count of descendents and ancestors
188: f1 = mgr.getWeblogCategory(f1.getId());
189: assertEquals(2, f1.getAllDescendentAssocs().size());
190: assertEquals(1, f1.getAncestorAssocs().size());
191:
192: f2 = mgr.getWeblogCategory(f2.getId());
193: assertEquals(1, f2.getAllDescendentAssocs().size());
194: assertEquals(2, f2.getAncestorAssocs().size());
195:
196: f3 = mgr.getWeblogCategory(f3.getId());
197: assertEquals(0, f3.getAllDescendentAssocs().size());
198: assertEquals(3, f3.getAncestorAssocs().size());
199:
200: // test get by path
201: assertEquals("f1", mgr.getWeblogCategoryByPath(testWeblog,
202: null, "f1").getName());
203:
204: assertEquals("f1", mgr.getWeblogCategoryByPath(testWeblog,
205: null, "/f1").getName());
206:
207: assertEquals("f2", mgr.getWeblogCategoryByPath(testWeblog,
208: null, "/f1/f2").getName());
209:
210: assertEquals("f3", mgr.getWeblogCategoryByPath(testWeblog,
211: null, "/f1/f2/f3").getName());
212:
213: // test path creation
214: f3 = mgr.getWeblogCategoryByPath(testWeblog, null, "/f1/f2/f3");
215: String pathString = mgr.getPath(f3);
216: String[] pathArray = Utilities.stringToStringArray(pathString,
217: "/");
218: assertEquals("f1", pathArray[0]);
219: assertEquals("f2", pathArray[1]);
220: assertEquals("f3", pathArray[2]);
221: }
222:
223: public void testMoveWeblogCategory() throws Exception {
224:
225: WeblogManager mgr = RollerFactory.getRoller()
226: .getWeblogManager();
227:
228: // add some categories and entries to test with
229: WeblogCategoryData dest = new WeblogCategoryData();
230: dest.setName("c0");
231: dest.setParent(mgr.getRootWeblogCategory(testWeblog));
232: dest.setWebsite(testWeblog);
233: mgr.saveWeblogCategory(dest);
234:
235: WeblogCategoryData c1 = new WeblogCategoryData();
236: c1.setName("c1");
237: c1.setParent(mgr.getRootWeblogCategory(testWeblog));
238: c1.setWebsite(testWeblog);
239: mgr.saveWeblogCategory(c1);
240:
241: WeblogCategoryData c2 = new WeblogCategoryData();
242: c2.setName("c2");
243: c2.setParent(c1);
244: c2.setWebsite(testWeblog);
245: mgr.saveWeblogCategory(c2);
246:
247: WeblogCategoryData c3 = new WeblogCategoryData();
248: c3.setName("c3");
249: c3.setParent(c2);
250: c3.setWebsite(testWeblog);
251: mgr.saveWeblogCategory(c3);
252:
253: TestUtils.endSession(true);
254:
255: c1 = mgr.getWeblogCategory(c1.getId());
256: c2 = mgr.getWeblogCategory(c2.getId());
257: c3 = mgr.getWeblogCategory(c3.getId());
258: dest = mgr.getWeblogCategory(dest.getId());
259:
260: WeblogEntryData e1 = TestUtils.setupWeblogEntry("e1", c1,
261: testWeblog, testUser);
262: WeblogEntryData e2 = TestUtils.setupWeblogEntry("e2", c2,
263: testWeblog, testUser);
264: WeblogEntryData e3 = TestUtils.setupWeblogEntry("e3", c3,
265: testWeblog, testUser);
266:
267: TestUtils.endSession(true);
268:
269: // verify number of entries in each category
270: assertEquals(0, dest.retrieveWeblogEntries(true).size());
271: assertEquals(0, dest.retrieveWeblogEntries(false).size());
272: assertEquals(1, c1.retrieveWeblogEntries(false).size());
273: assertEquals(3, c1.retrieveWeblogEntries(true).size());
274:
275: // move contents of source category c1 to destination catetory dest
276: c1.setParent(dest);
277: mgr.saveWeblogCategory(c1);
278: TestUtils.endSession(true);
279:
280: // after move, verify number of entries in each category
281: dest = mgr.getWeblogCategory(dest.getId());
282: c1 = mgr.getWeblogCategory(c1.getId());
283: c2 = mgr.getWeblogCategory(c2.getId());
284: c3 = mgr.getWeblogCategory(c3.getId());
285:
286: assertEquals(3, dest.retrieveWeblogEntries(true).size());
287: assertEquals(0, dest.retrieveWeblogEntries(false).size());
288:
289: assertEquals(dest, c1.getParent());
290: assertEquals(c1, c2.getParent());
291: assertEquals(c2, c3.getParent());
292:
293: assertEquals(1, c1.retrieveWeblogEntries(false).size());
294: assertEquals(1, c2.retrieveWeblogEntries(false).size());
295: assertEquals(1, c3.retrieveWeblogEntries(false).size());
296:
297: List entries = c1.retrieveWeblogEntries(true);
298: assertEquals(3, entries.size());
299: }
300:
301: public void testMoveWeblogCategoryContents() throws Exception {
302:
303: WeblogManager mgr = RollerFactory.getRoller()
304: .getWeblogManager();
305:
306: // add some categories and entries to test with
307: WeblogCategoryData dest = new WeblogCategoryData();
308: dest.setName("c0");
309: dest.setParent(mgr.getRootWeblogCategory(testWeblog));
310: dest.setWebsite(testWeblog);
311: mgr.saveWeblogCategory(dest);
312:
313: WeblogCategoryData c1 = new WeblogCategoryData();
314: c1.setName("c1");
315: c1.setParent(mgr.getRootWeblogCategory(testWeblog));
316: c1.setWebsite(testWeblog);
317: mgr.saveWeblogCategory(c1);
318:
319: WeblogCategoryData c2 = new WeblogCategoryData();
320: c2.setName("c2");
321: c2.setParent(c1);
322: c2.setWebsite(testWeblog);
323: mgr.saveWeblogCategory(c2);
324:
325: WeblogCategoryData c3 = new WeblogCategoryData();
326: c3.setName("c3");
327: c3.setParent(c2);
328: c3.setWebsite(testWeblog);
329: mgr.saveWeblogCategory(c3);
330:
331: TestUtils.endSession(true);
332:
333: c1 = mgr.getWeblogCategory(c1.getId());
334: c2 = mgr.getWeblogCategory(c2.getId());
335: c3 = mgr.getWeblogCategory(c3.getId());
336: dest = mgr.getWeblogCategory(dest.getId());
337:
338: WeblogEntryData e1 = TestUtils.setupWeblogEntry("e1", c1,
339: testWeblog, testUser);
340: WeblogEntryData e2 = TestUtils.setupWeblogEntry("e2", c2,
341: testWeblog, testUser);
342: WeblogEntryData e3 = TestUtils.setupWeblogEntry("e3", c3,
343: testWeblog, testUser);
344:
345: TestUtils.endSession(true);
346:
347: // verify number of entries in each category
348: assertEquals(0, dest.retrieveWeblogEntries(true).size());
349: assertEquals(0, dest.retrieveWeblogEntries(false).size());
350: assertEquals(1, c1.retrieveWeblogEntries(false).size());
351: assertEquals(3, c1.retrieveWeblogEntries(true).size());
352:
353: // move contents of source category c1 to destination category dest
354: mgr.moveWeblogCategoryContents(c1, dest);
355: mgr.saveWeblogCategory(c1);
356: TestUtils.endSession(true);
357:
358: // after move, verify number of entries in each category
359: dest = mgr.getWeblogCategory(dest.getId());
360: c1 = mgr.getWeblogCategory(c1.getId());
361:
362: // Hierarchy is flattened under dest
363: assertEquals(3, dest.retrieveWeblogEntries(true).size());
364: assertEquals(3, dest.retrieveWeblogEntries(false).size());
365:
366: // c1 category should be empty now
367: assertEquals(0, c1.retrieveWeblogEntries(false).size());
368:
369: }
370: }
|