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: package org.apache.roller.business;
019:
020: import java.util.List;
021: import junit.framework.Test;
022: import junit.framework.TestCase;
023: import junit.framework.TestSuite;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.roller.TestUtils;
027: import org.apache.roller.business.RollerFactory;
028: import org.apache.roller.business.UserManager;
029: import org.apache.roller.pojos.CommentData;
030: import org.apache.roller.pojos.StatCount;
031: import org.apache.roller.pojos.UserData;
032: import org.apache.roller.pojos.WeblogEntryData;
033: import org.apache.roller.pojos.WebsiteData;
034:
035: /**
036: * Test Weblog related business operations.
037: */
038: public class WeblogTest extends TestCase {
039:
040: public static Log log = LogFactory.getLog(WeblogTest.class);
041:
042: UserData testUser = null;
043:
044: public WeblogTest(String name) {
045: super (name);
046: }
047:
048: public static Test suite() {
049: return new TestSuite(WeblogTest.class);
050: }
051:
052: /**
053: * All tests in this suite require a user.
054: */
055: public void setUp() throws Exception {
056:
057: try {
058: testUser = TestUtils.setupUser("weblogTestUser");
059: TestUtils.endSession(true);
060: } catch (Exception ex) {
061: log.error(ex);
062: throw new Exception("Test setup failed", ex);
063: }
064: }
065:
066: public void tearDown() throws Exception {
067:
068: try {
069: TestUtils.teardownUser(testUser.getId());
070: TestUtils.endSession(true);
071: } catch (Exception ex) {
072: log.error(ex);
073: throw new Exception("Test teardown failed", ex);
074: }
075: }
076:
077: /**
078: * Test basic persistence operations ... Create, Update, Delete.
079: */
080: public void testWeblogCRUD() throws Exception {
081:
082: UserManager mgr = RollerFactory.getRoller().getUserManager();
083: WebsiteData weblog = null;
084:
085: WebsiteData testWeblog = new WebsiteData();
086: testWeblog.setName("Test Weblog");
087: testWeblog.setDescription("Test Weblog");
088: testWeblog.setHandle("testweblog");
089: testWeblog.setEmailAddress("testweblog@dev.null");
090: testWeblog.setEditorPage("editor-text.jsp");
091: testWeblog.setBlacklist("");
092: testWeblog.setEmailFromAddress("");
093: testWeblog.setEditorTheme("basic");
094: testWeblog.setLocale("en_US");
095: testWeblog.setTimeZone("America/Los_Angeles");
096: testWeblog.setDateCreated(new java.util.Date());
097: testWeblog.setCreator(testUser);
098:
099: // make sure test weblog does not exist
100: weblog = mgr.getWebsiteByHandle(testWeblog.getHandle());
101: assertNull(weblog);
102:
103: // add test weblog
104: mgr.addWebsite(testWeblog);
105: String id = testWeblog.getId();
106: TestUtils.endSession(true);
107:
108: // make sure test weblog exists
109: weblog = null;
110: weblog = mgr.getWebsite(id);
111: assertNotNull(weblog);
112: assertEquals(testWeblog, weblog);
113:
114: // modify weblog and save
115: weblog.setName("testtesttest");
116: mgr.saveWebsite(weblog);
117: TestUtils.endSession(true);
118:
119: // make sure changes were saved
120: weblog = null;
121: weblog = mgr.getWebsite(id);
122: assertNotNull(weblog);
123: assertEquals("testtesttest", weblog.getName());
124:
125: // remove test weblog
126: mgr.removeWebsite(weblog);
127: TestUtils.endSession(true);
128:
129: // make sure weblog no longer exists
130: weblog = null;
131: weblog = mgr.getWebsite(id);
132: assertNull(weblog);
133: }
134:
135: /**
136: * Test lookup mechanisms.
137: */
138: public void testWeblogLookups() throws Exception {
139:
140: UserManager mgr = RollerFactory.getRoller().getUserManager();
141: WebsiteData weblog = null;
142:
143: // add test weblogs
144: WebsiteData testWeblog1 = TestUtils.setupWeblog("testWeblog1",
145: testUser);
146: WebsiteData testWeblog2 = TestUtils.setupWeblog("testWeblog2",
147: testUser);
148: TestUtils.endSession(true);
149:
150: // lookup by id
151: weblog = mgr.getWebsite(testWeblog1.getId());
152: assertNotNull(weblog);
153: assertEquals(testWeblog1.getHandle(), weblog.getHandle());
154:
155: // lookup by weblog handle
156: weblog = null;
157: weblog = mgr.getWebsiteByHandle(testWeblog1.getHandle());
158: assertNotNull(weblog);
159: assertEquals(testWeblog1.getHandle(), weblog.getHandle());
160:
161: // make sure disable weblogs are not returned
162: weblog.setEnabled(Boolean.FALSE);
163: mgr.saveWebsite(weblog);
164: weblog = null;
165: weblog = mgr.getWebsiteByHandle(testWeblog1.getHandle());
166: assertNull(weblog);
167:
168: // restore enabled state
169: weblog = mgr.getWebsiteByHandle(testWeblog1.getHandle(),
170: Boolean.FALSE);
171: weblog.setEnabled(Boolean.TRUE);
172: mgr.saveWebsite(weblog);
173: TestUtils.endSession(true);
174: weblog = null;
175: weblog = mgr.getWebsiteByHandle(testWeblog1.getHandle());
176: assertNotNull(weblog);
177:
178: // get all weblogs for user
179: weblog = null;
180: List weblogs1 = mgr.getWebsites(testUser, Boolean.TRUE,
181: Boolean.TRUE, null, null, 0, -1);
182: assertEquals(2, weblogs1.size());
183: weblog = (WebsiteData) weblogs1.get(0);
184: assertNotNull(weblog);
185:
186: // testing paging
187: List weblogs11 = mgr.getWebsites(testUser, Boolean.TRUE,
188: Boolean.TRUE, null, null, 0, 1);
189: assertEquals(1, weblogs11.size());
190: List weblogs12 = mgr.getWebsites(testUser, Boolean.TRUE,
191: Boolean.TRUE, null, null, 1, 1);
192: assertEquals(1, weblogs11.size());
193:
194: // make sure disabled weblogs are not returned
195: weblog.setEnabled(Boolean.FALSE);
196: mgr.saveWebsite(weblog);
197: TestUtils.endSession(true);
198: List weblogs2 = mgr.getWebsites(testUser, Boolean.TRUE,
199: Boolean.TRUE, null, null, 0, -1);
200: assertEquals(1, weblogs2.size());
201: weblog = (WebsiteData) weblogs2.get(0);
202: assertNotNull(weblog);
203:
204: // make sure inactive weblogs are not returned
205: weblog.setActive(Boolean.FALSE);
206: mgr.saveWebsite(weblog);
207: TestUtils.endSession(true);
208: List weblogs3 = mgr.getWebsites(testUser, Boolean.TRUE,
209: Boolean.TRUE, null, null, 0, -1);
210: assertEquals(0, weblogs3.size());
211:
212: // remove test weblogs
213: TestUtils.teardownWeblog(testWeblog1.getId());
214: TestUtils.teardownWeblog(testWeblog2.getId());
215: TestUtils.endSession(true);
216: }
217:
218: /**
219: * Test that we can safely remove a fully loaded weblog.
220: * That means a weblog with entries, categories, bookmarks, pings, etc.
221: */
222: public void testRemoveLoadedWeblog() throws Exception {
223: // TODO: implement testRemoveLoadedWeblog
224: }
225: }
|