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.sql.Timestamp;
021: import java.util.Calendar;
022: import java.util.Date;
023: import java.util.List;
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.roller.RollerException;
030: import org.apache.roller.TestUtils;
031: import org.apache.roller.business.PropertiesManager;
032: import org.apache.roller.business.referrers.RefererManager;
033: import org.apache.roller.business.RollerFactory;
034: import org.apache.roller.business.UserManager;
035: import org.apache.roller.pojos.RefererData;
036: import org.apache.roller.pojos.RollerPropertyData;
037: import org.apache.roller.pojos.UserData;
038: import org.apache.roller.pojos.WebsiteData;
039: import org.apache.roller.pojos.WebsiteDisplayData;
040: import org.apache.roller.util.DateUtil;
041:
042: /**
043: * Test Roller Referer Management.
044: */
045: public class RefererTest extends TestCase {
046:
047: public static Log log = LogFactory.getLog(RefererTest.class);
048:
049: UserData testUser = null;
050: WebsiteData testWeblog = null;
051: WebsiteData testWeblog2 = null;
052:
053: int count = 20;
054: String testDay;
055: String origSpamWords;
056:
057: public RefererTest(String name) {
058: super (name);
059: }
060:
061: public static Test suite() {
062: return new TestSuite(RefererTest.class);
063: }
064:
065: /**
066: * All tests in this suite require a user and a weblog.
067: */
068: public void setUp() throws Exception {
069:
070: try {
071: testUser = TestUtils.setupUser("referTestUser");
072: testWeblog = TestUtils.setupWeblog("referTestWeblog1",
073: testUser);
074: testWeblog2 = TestUtils.setupWeblog("referTestWeblog2",
075: testUser);
076:
077: // add "spamtest" to refererSpamWords
078: PropertiesManager pmgr = RollerFactory.getRoller()
079: .getPropertiesManager();
080: RollerPropertyData spamprop = pmgr
081: .getProperty("spam.blacklist");
082: this .origSpamWords = spamprop.getValue();
083: spamprop.setValue(spamprop.getValue() + ", spamtest");
084: pmgr.saveProperty(spamprop);
085:
086: // add a number of referers to play with
087: RefererManager rmgr = RollerFactory.getRoller()
088: .getRefererManager();
089: Calendar lCalendar = Calendar.getInstance();
090: lCalendar.setTime(new Date());
091: for (int i = 0; i < count; i++) {
092: lCalendar.add(Calendar.DATE, -1);
093: Timestamp day = new Timestamp(lCalendar.getTime()
094: .getTime());
095: testDay = DateUtil.format8chars(day);
096:
097: rmgr.processReferrer("http://test" + i, "http://test"
098: + i, testWeblog.getHandle(), null, testDay);
099: if (i % 2 == 0) { // half the referrers from weblog 2
100: rmgr.processReferrer("http://test" + i,
101: "http://test" + i, testWeblog2.getHandle(),
102: null, testDay);
103: }
104: }
105:
106: TestUtils.endSession(true);
107:
108: } catch (Exception ex) {
109: log.error(ex);
110: throw new Exception("Test setup failed", ex);
111: }
112: }
113:
114: public void tearDown() throws Exception {
115:
116: try {
117: // reset refererSpamWords to original value
118: PropertiesManager pmgr = RollerFactory.getRoller()
119: .getPropertiesManager();
120: RollerPropertyData spamprop = pmgr
121: .getProperty("spam.blacklist");
122: spamprop.setValue(this .origSpamWords);
123: pmgr.saveProperty(spamprop);
124:
125: TestUtils.teardownWeblog(testWeblog.getId());
126: TestUtils.teardownWeblog(testWeblog2.getId());
127: TestUtils.teardownUser(testUser.getId());
128:
129: TestUtils.endSession(true);
130:
131: } catch (Exception ex) {
132: log.error(ex);
133: throw new Exception("Test teardown failed", ex);
134: }
135: }
136:
137: /**
138: * Test basic persistence operations ... Create, Update, Delete
139: */
140: public void testRefererCRUD() throws Exception {
141:
142: RefererManager mgr = RollerFactory.getRoller()
143: .getRefererManager();
144: RefererData referer = null;
145:
146: RefererData testReferer = new RefererData();
147: testReferer.setWebsite(testWeblog);
148: testReferer.setDateString("20060420");
149: testReferer.setRefererUrl("blah");
150: testReferer.setRefererPermalink("blah");
151: testReferer.setRequestUrl("foo");
152: testReferer.setTitle("lksdjf");
153: testReferer.setTotalHits(new Integer(3));
154: testReferer.setDayHits(new Integer(2));
155: testReferer.setVisible(Boolean.TRUE);
156: testReferer.setDuplicate(Boolean.FALSE);
157: testReferer.setExcerpt("");
158:
159: // save referer
160: mgr.saveReferer(testReferer);
161: String id = testReferer.getId();
162: TestUtils.endSession(true);
163:
164: // check that create was successful
165: referer = null;
166: referer = mgr.getReferer(id);
167: assertNotNull(referer);
168: assertEquals(testReferer, referer);
169:
170: // update referer
171: referer.setTitle("testtesttest");
172: mgr.saveReferer(referer);
173: TestUtils.endSession(true);
174:
175: // check that update was successful
176: referer = null;
177: referer = mgr.getReferer(id);
178: assertNotNull(referer);
179: assertEquals("testtesttest", referer.getTitle());
180:
181: // delete referer
182: mgr.removeReferer(referer);
183: TestUtils.endSession(true);
184:
185: // check that delete was successful
186: referer = null;
187: referer = mgr.getReferer(id);
188: assertNull(referer);
189: }
190:
191: public void testGetReferersToDate() throws Exception {
192:
193: RefererManager rmgr = RollerFactory.getRoller()
194: .getRefererManager();
195:
196: List referers = rmgr.getReferersToDate(testWeblog, testDay);
197: assertEquals("Should be one Referer.", referers.size(), 1);
198: }
199:
200: public void testRefererProcessing() throws RollerException {
201:
202: RefererManager rmgr = RollerFactory.getRoller()
203: .getRefererManager();
204:
205: List refs = rmgr.getReferers(testWeblog);
206: assertEquals("number of referers should equal count", count,
207: refs.size());
208:
209: int hits = rmgr.getDayHits(testWeblog);
210: assertEquals("There should be one fewer hits than referers",
211: count, hits);
212: }
213:
214: public void testApplyRefererFilters() throws Exception {
215:
216: log.info("Test apply referers (global)");
217:
218: RefererManager rmgr = RollerFactory.getRoller()
219: .getRefererManager();
220:
221: List refs = rmgr.getReferers(testWeblog);
222: assertEquals(count, refs.size());
223:
224: PropertiesManager pmgr = RollerFactory.getRoller()
225: .getPropertiesManager();
226: RollerPropertyData spamprop = pmgr
227: .getProperty("spam.blacklist");
228: String origWords = spamprop.getValue();
229: spamprop.setValue(spamprop.getValue() + ", test");
230: pmgr.saveProperty(spamprop);
231: TestUtils.endSession(true);
232:
233: rmgr.applyRefererFilters();
234: TestUtils.endSession(true);
235:
236: refs = rmgr.getReferers(testWeblog);
237: assertEquals(0, refs.size());
238:
239: spamprop = pmgr.getProperty("spam.blacklist");
240: spamprop.setValue(origWords);
241: pmgr.saveProperty(spamprop);
242: TestUtils.endSession(true);
243: }
244:
245: public void testApplyRefererFiltersWebsite() throws Exception {
246:
247: log.info("Test apply referers (weblog)");
248:
249: RefererManager rmgr = RollerFactory.getRoller()
250: .getRefererManager();
251:
252: List refs = rmgr.getReferers(testWeblog);
253: assertEquals(count, refs.size());
254: String origWords = null;
255:
256: UserManager umgr = RollerFactory.getRoller().getUserManager();
257: testWeblog = umgr.getWebsite(testWeblog.getId());
258: origWords = testWeblog.getBlacklist();
259: testWeblog.setBlacklist("test");
260: umgr.saveWebsite(testWeblog);
261: TestUtils.endSession(true);
262:
263: rmgr.applyRefererFilters(testWeblog);
264: TestUtils.endSession(true);
265:
266: refs = rmgr.getReferers(testWeblog);
267: assertEquals(0, refs.size());
268: }
269:
270: public void testGetDaysPopularWebsites() throws Exception {
271: log.info("Test apply referers (weblog)");
272: RefererManager rmgr = RollerFactory.getRoller()
273: .getRefererManager();
274:
275: List sites = rmgr.getDaysPopularWebsites(0, 1);
276: assertEquals(1, sites.size());
277:
278: sites = rmgr.getDaysPopularWebsites(1, 1);
279: assertEquals(1, sites.size());
280:
281: sites = rmgr.getDaysPopularWebsites(0, 20);
282: assertEquals(2, sites.size());
283:
284: WebsiteDisplayData site1 = (WebsiteDisplayData) sites.get(0);
285: assertEquals(testWeblog.getHandle(), site1.getWebsiteHandle());
286: assertEquals(count, site1.getHits().intValue());
287:
288: WebsiteDisplayData site2 = (WebsiteDisplayData) sites.get(1);
289: assertEquals(testWeblog2.getHandle(), site2.getWebsiteHandle());
290: assertEquals(count / 2, site2.getHits().intValue());
291: }
292: }
|