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.util.Iterator;
022: import java.util.List;
023: import junit.framework.Test;
024: import junit.framework.TestCase;
025: import junit.framework.TestSuite;
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.roller.TestUtils;
029: import org.apache.roller.business.RollerFactory;
030: import org.apache.roller.business.WeblogManager;
031: import org.apache.roller.pojos.HitCountData;
032: import org.apache.roller.pojos.UserData;
033: import org.apache.roller.pojos.WebsiteData;
034:
035: /**
036: * Test HitCount related business operations.
037: */
038: public class HitCountTest extends TestCase {
039:
040: public static Log log = LogFactory.getLog(HitCountTest.class);
041:
042: UserData testUser = null;
043: WebsiteData testWeblog = null;
044:
045: public HitCountTest(String name) {
046: super (name);
047: }
048:
049: public static Test suite() {
050: return new TestSuite(HitCountTest.class);
051: }
052:
053: /**
054: * All tests in this suite require a user and a weblog.
055: */
056: public void setUp() throws Exception {
057:
058: try {
059: testUser = TestUtils.setupUser("hitCountTestUser");
060: testWeblog = TestUtils.setupWeblog("hitCountTestWeblog",
061: testUser);
062: TestUtils.endSession(true);
063: } catch (Exception ex) {
064: log.error(ex);
065: throw new Exception("Test setup failed", ex);
066: }
067: }
068:
069: public void tearDown() throws Exception {
070:
071: try {
072: TestUtils.teardownWeblog(testWeblog.getId());
073: TestUtils.teardownUser(testUser.getId());
074: TestUtils.endSession(true);
075: } catch (Exception ex) {
076: log.error(ex);
077: throw new Exception("Test teardown failed", ex);
078: }
079: }
080:
081: /**
082: * Test basic persistence operations ... Create, Update, Delete.
083: */
084: public void testHitCountCRUD() throws Exception {
085:
086: WeblogManager mgr = RollerFactory.getRoller()
087: .getWeblogManager();
088:
089: HitCountData testCount = new HitCountData();
090: testCount.setWeblog(testWeblog);
091: testCount.setDailyHits(10);
092:
093: // create
094: mgr.saveHitCount(testCount);
095: String id = testCount.getId();
096: TestUtils.endSession(true);
097:
098: // make sure it was created
099: HitCountData hitCount = null;
100: hitCount = mgr.getHitCount(id);
101: assertNotNull(hitCount);
102: assertEquals(testCount, hitCount);
103: assertEquals(10, hitCount.getDailyHits());
104:
105: // update
106: hitCount.setDailyHits(25);
107: mgr.saveHitCount(hitCount);
108: TestUtils.endSession(true);
109:
110: // make sure it was updated
111: hitCount = null;
112: hitCount = mgr.getHitCount(id);
113: assertNotNull(hitCount);
114: assertEquals(testCount, hitCount);
115: assertEquals(25, hitCount.getDailyHits());
116:
117: // delete
118: mgr.removeHitCount(hitCount);
119: TestUtils.endSession(true);
120:
121: // make sure it was deleted
122: hitCount = null;
123: hitCount = mgr.getHitCount(id);
124: assertNull(hitCount);
125: }
126:
127: public void testHitCountLookups() throws Exception {
128:
129: WeblogManager mgr = RollerFactory.getRoller()
130: .getWeblogManager();
131:
132: HitCountData testCount = new HitCountData();
133: testCount.setWeblog(testWeblog);
134: testCount.setDailyHits(10);
135:
136: // create
137: mgr.saveHitCount(testCount);
138: String id = testCount.getId();
139: TestUtils.endSession(true);
140:
141: // test lookup by id
142: HitCountData hitCount = null;
143: hitCount = mgr.getHitCount(id);
144: assertNotNull(hitCount);
145: assertEquals(testCount, hitCount);
146: assertEquals(10, hitCount.getDailyHits());
147:
148: // test lookup by weblog
149: hitCount = null;
150: hitCount = mgr.getHitCountByWeblog(testWeblog);
151: assertNotNull(hitCount);
152: assertEquals(testCount, hitCount);
153: assertEquals(10, hitCount.getDailyHits());
154:
155: // delete
156: mgr.removeHitCount(hitCount);
157: TestUtils.endSession(true);
158:
159: // make sure it was deleted
160: hitCount = null;
161: hitCount = mgr.getHitCount(id);
162: assertNull(hitCount);
163: }
164:
165: public void testIncrementHitCount() throws Exception {
166:
167: WeblogManager mgr = RollerFactory.getRoller()
168: .getWeblogManager();
169:
170: HitCountData testCount = new HitCountData();
171: testCount.setWeblog(testWeblog);
172: testCount.setDailyHits(10);
173:
174: // create
175: mgr.saveHitCount(testCount);
176: String id = testCount.getId();
177: TestUtils.endSession(true);
178:
179: // make sure it was created
180: HitCountData hitCount = null;
181: hitCount = mgr.getHitCountByWeblog(testWeblog);
182: assertNotNull(hitCount);
183: assertEquals(10, hitCount.getDailyHits());
184:
185: // increment
186: mgr.incrementHitCount(testWeblog, 25);
187: TestUtils.endSession(true);
188:
189: // make sure it was incremented properly
190: hitCount = null;
191: hitCount = mgr.getHitCountByWeblog(testWeblog);
192: assertNotNull(hitCount);
193: assertEquals(35, hitCount.getDailyHits());
194:
195: // delete
196: mgr.removeHitCount(hitCount);
197: TestUtils.endSession(true);
198:
199: // make sure it was deleted
200: hitCount = null;
201: hitCount = mgr.getHitCount(id);
202: assertNull(hitCount);
203: }
204:
205: public void testResetHitCounts() throws Exception {
206:
207: WeblogManager mgr = RollerFactory.getRoller()
208: .getWeblogManager();
209:
210: WebsiteData blog1 = TestUtils.setupWeblog("hitCntTest1",
211: testUser);
212: WebsiteData blog2 = TestUtils.setupWeblog("hitCntTest2",
213: testUser);
214: WebsiteData blog3 = TestUtils.setupWeblog("hitCntTest3",
215: testUser);
216:
217: HitCountData cnt1 = TestUtils.setupHitCount(blog1, 10);
218: HitCountData cnt2 = TestUtils.setupHitCount(blog2, 20);
219: HitCountData cnt3 = TestUtils.setupHitCount(blog3, 30);
220:
221: TestUtils.endSession(true);
222:
223: // make sure data was properly initialized
224: HitCountData testCount = null;
225: testCount = mgr.getHitCount(cnt1.getId());
226: assertEquals(10, testCount.getDailyHits());
227: testCount = mgr.getHitCount(cnt2.getId());
228: assertEquals(20, testCount.getDailyHits());
229: testCount = mgr.getHitCount(cnt3.getId());
230: assertEquals(30, testCount.getDailyHits());
231:
232: // reset count for one weblog
233: mgr.resetHitCount(blog1);
234: TestUtils.endSession(true);
235:
236: // make sure it reset only one weblog
237: testCount = mgr.getHitCount(cnt1.getId());
238: assertEquals(0, testCount.getDailyHits());
239: testCount = mgr.getHitCount(cnt2.getId());
240: assertEquals(20, testCount.getDailyHits());
241: testCount = mgr.getHitCount(cnt3.getId());
242: assertEquals(30, testCount.getDailyHits());
243:
244: // reset all counts
245: mgr.resetAllHitCounts();
246: TestUtils.endSession(true);
247:
248: // make sure it reset all counts
249: testCount = mgr.getHitCount(cnt1.getId());
250: assertEquals(0, testCount.getDailyHits());
251: testCount = mgr.getHitCount(cnt2.getId());
252: assertEquals(0, testCount.getDailyHits());
253: testCount = mgr.getHitCount(cnt3.getId());
254: assertEquals(0, testCount.getDailyHits());
255:
256: // cleanup
257: TestUtils.teardownHitCount(cnt1.getId());
258: TestUtils.teardownHitCount(cnt2.getId());
259: TestUtils.teardownHitCount(cnt3.getId());
260: TestUtils.teardownWeblog(blog1.getId());
261: TestUtils.teardownWeblog(blog2.getId());
262: TestUtils.teardownWeblog(blog3.getId());
263: }
264:
265: public void testHotWeblogs() throws Exception {
266:
267: WeblogManager mgr = RollerFactory.getRoller()
268: .getWeblogManager();
269:
270: WebsiteData blog1 = TestUtils.setupWeblog("hitCntHotTest1",
271: testUser);
272: WebsiteData blog2 = TestUtils.setupWeblog("hitCntHotTest2",
273: testUser);
274: WebsiteData blog3 = TestUtils.setupWeblog("hitCntHotTest3",
275: testUser);
276:
277: HitCountData cnt1 = TestUtils.setupHitCount(blog1, 10);
278: HitCountData cnt2 = TestUtils.setupHitCount(blog2, 20);
279: HitCountData cnt3 = TestUtils.setupHitCount(blog3, 30);
280:
281: TestUtils.endSession(true);
282:
283: // make sure data was properly initialized
284: HitCountData testCount = null;
285: testCount = mgr.getHitCount(cnt1.getId());
286: assertEquals(10, testCount.getDailyHits());
287: testCount = mgr.getHitCount(cnt2.getId());
288: assertEquals(20, testCount.getDailyHits());
289: testCount = mgr.getHitCount(cnt3.getId());
290: assertEquals(30, testCount.getDailyHits());
291:
292: // get hot weblogs
293: List hotBlogs = mgr.getHotWeblogs(1, 0, 5);
294: assertNotNull(hotBlogs);
295: assertEquals(3, hotBlogs.size());
296:
297: // also check ordering and values
298: HitCountData hitCount = null;
299: Iterator it = hotBlogs.iterator();
300: for (int i = 3; it.hasNext(); i--) {
301: hitCount = (HitCountData) it.next();
302:
303: assertEquals(i * 10, hitCount.getDailyHits());
304: }
305:
306: // cleanup
307: TestUtils.teardownHitCount(cnt1.getId());
308: TestUtils.teardownHitCount(cnt2.getId());
309: TestUtils.teardownHitCount(cnt3.getId());
310: TestUtils.teardownWeblog(blog1.getId());
311: TestUtils.teardownWeblog(blog2.getId());
312: TestUtils.teardownWeblog(blog3.getId());
313: }
314:
315: }
|