1 18 package org.apache.roller.business; 19 20 import java.sql.Timestamp ; 21 import java.util.Calendar ; 22 import java.util.Date ; 23 import java.util.List ; 24 import junit.framework.Test; 25 import junit.framework.TestCase; 26 import junit.framework.TestSuite; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 import org.apache.roller.RollerException; 30 import org.apache.roller.TestUtils; 31 import org.apache.roller.model.PropertiesManager; 32 import org.apache.roller.model.RefererManager; 33 import org.apache.roller.model.RollerFactory; 34 import org.apache.roller.model.UserManager; 35 import org.apache.roller.pojos.RefererData; 36 import org.apache.roller.pojos.RollerPropertyData; 37 import org.apache.roller.pojos.UserData; 38 import org.apache.roller.pojos.WebsiteData; 39 import org.apache.roller.pojos.WebsiteDisplayData; 40 import org.apache.roller.util.DateUtil; 41 42 43 46 public class RefererTest extends TestCase { 47 48 public static Log log = LogFactory.getLog(RefererTest.class); 49 50 UserData testUser = null; 51 WebsiteData testWeblog = null; 52 WebsiteData testWeblog2 = null; 53 54 int count = 20; 55 String testDay; 56 String origSpamWords; 57 58 59 public RefererTest(String name) { 60 super(name); 61 } 62 63 64 public static Test suite() { 65 return new TestSuite(RefererTest.class); 66 } 67 68 69 72 public void setUp() throws Exception { 73 74 try { 75 testUser = TestUtils.setupUser("referTestUser"); 76 testWeblog = TestUtils.setupWeblog("referTestWeblog1", testUser); 77 testWeblog2 = TestUtils.setupWeblog("referTestWeblog2", testUser); 78 79 PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager(); 81 RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist"); 82 this.origSpamWords = spamprop.getValue(); 83 spamprop.setValue(spamprop.getValue() + ", spamtest"); 84 pmgr.saveProperty(spamprop); 85 86 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 88 Calendar lCalendar = Calendar.getInstance(); 89 lCalendar.setTime(new Date ()); 90 for (int i = 0; i < count; i++) { 91 lCalendar.add(Calendar.DATE, -1); 92 Timestamp day = new Timestamp (lCalendar.getTime().getTime()); 93 testDay = DateUtil.format8chars(day); 94 95 rmgr.processReferrer("http://test"+i, "http://test"+i, 96 testWeblog.getHandle(), null, testDay); 97 if (i % 2 == 0) { rmgr.processReferrer("http://test"+i, "http://test"+i, 99 testWeblog2.getHandle(), null, testDay); 100 } 101 } 102 103 TestUtils.endSession(true); 104 105 } catch (Exception ex){ 106 log.error(ex); 107 throw new Exception ("Test setup failed", ex); 108 } 109 } 110 111 public void tearDown() throws Exception { 112 113 try { 114 PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager(); 116 RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist"); 117 spamprop.setValue(this.origSpamWords); 118 pmgr.saveProperty(spamprop); 119 120 TestUtils.teardownWeblog(testWeblog.getId()); 121 TestUtils.teardownWeblog(testWeblog2.getId()); 122 TestUtils.teardownUser(testUser.getId()); 123 124 TestUtils.endSession(true); 125 126 } catch (Exception ex) { 127 log.error(ex); 128 throw new Exception ("Test teardown failed", ex); 129 } 130 } 131 132 133 136 public void testRefererCRUD() throws Exception { 137 138 RefererManager mgr = RollerFactory.getRoller().getRefererManager(); 139 RefererData referer = null; 140 141 RefererData testReferer = new RefererData(); 142 testReferer.setWebsite(testWeblog); 143 testReferer.setDateString("20060420"); 144 testReferer.setRefererUrl("blah"); 145 testReferer.setRefererPermalink("blah"); 146 testReferer.setRequestUrl("foo"); 147 testReferer.setTitle("lksdjf"); 148 testReferer.setTotalHits(new Integer (3)); 149 testReferer.setDayHits(new Integer (2)); 150 testReferer.setVisible(Boolean.TRUE); 151 testReferer.setDuplicate(Boolean.FALSE); 152 testReferer.setExcerpt(""); 153 154 mgr.saveReferer(testReferer); 156 String id = testReferer.getId(); 157 TestUtils.endSession(true); 158 159 referer = null; 161 referer = mgr.getReferer(id); 162 assertNotNull(referer); 163 assertEquals(testReferer, referer); 164 165 referer.setTitle("testtesttest"); 167 mgr.saveReferer(referer); 168 TestUtils.endSession(true); 169 170 referer = null; 172 referer = mgr.getReferer(id); 173 assertNotNull(referer); 174 assertEquals("testtesttest", referer.getTitle()); 175 176 mgr.removeReferer(referer); 178 TestUtils.endSession(true); 179 180 referer = null; 182 referer = mgr.getReferer(id); 183 assertNull(referer); 184 } 185 186 187 public void testGetReferersToDate() throws Exception { 188 189 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 190 191 List referers = rmgr.getReferersToDate(testWeblog, testDay); 192 assertEquals("Should be one Referer.", referers.size(), 1); 193 } 194 195 196 public void testRefererProcessing() throws RollerException { 197 198 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 199 200 List refs = rmgr.getReferers(testWeblog); 201 assertEquals("number of referers should equal count", count, refs.size()); 202 203 int hits = rmgr.getDayHits(testWeblog); 204 assertEquals("There should be one fewer hits than referers", count, hits); 205 } 206 207 208 public void testApplyRefererFilters() throws Exception { 209 210 log.info("Test apply referers (global)"); 211 212 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 213 214 List refs = rmgr.getReferers(testWeblog); 215 assertEquals(count, refs.size()); 216 217 PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager(); 218 RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist"); 219 String origWords = spamprop.getValue(); 220 spamprop.setValue(spamprop.getValue() + ", test"); 221 pmgr.saveProperty(spamprop); 222 TestUtils.endSession(true); 223 224 rmgr.applyRefererFilters(); 225 TestUtils.endSession(true); 226 227 refs = rmgr.getReferers(testWeblog); 228 assertEquals(0, refs.size()); 229 230 spamprop = pmgr.getProperty("spam.blacklist"); 231 spamprop.setValue(origWords); 232 pmgr.saveProperty(spamprop); 233 TestUtils.endSession(true); 234 } 235 236 237 public void testApplyRefererFiltersWebsite() throws Exception { 238 239 log.info("Test apply referers (weblog)"); 240 241 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 242 243 List refs = rmgr.getReferers(testWeblog); 244 assertEquals(count, refs.size()); 245 String origWords = null; 246 247 UserManager umgr = RollerFactory.getRoller().getUserManager(); 248 testWeblog = umgr.getWebsite(testWeblog.getId()); 249 origWords = testWeblog.getBlacklist(); 250 testWeblog.setBlacklist("test"); 251 umgr.saveWebsite(testWeblog); 252 TestUtils.endSession(true); 253 254 rmgr.applyRefererFilters(testWeblog); 255 TestUtils.endSession(true); 256 257 refs = rmgr.getReferers(testWeblog); 258 assertEquals(0, refs.size()); 259 } 260 261 public void testGetDaysPopularWebsites() throws Exception { 262 log.info("Test apply referers (weblog)"); 263 RefererManager rmgr = RollerFactory.getRoller().getRefererManager(); 264 265 List sites = rmgr.getDaysPopularWebsites(0, 1); 266 assertEquals(1, sites.size()); 267 268 sites = rmgr.getDaysPopularWebsites(1, 1); 269 assertEquals(1, sites.size()); 270 271 sites = rmgr.getDaysPopularWebsites(0, 20); 272 assertEquals(2, sites.size()); 273 274 WebsiteDisplayData site1 = (WebsiteDisplayData)sites.get(0); 275 assertEquals(testWeblog.getHandle(), site1.getWebsiteHandle()); 276 assertEquals(count, site1.getHits().intValue()); 277 278 WebsiteDisplayData site2 = (WebsiteDisplayData)sites.get(1); 279 assertEquals(testWeblog2.getHandle(), site2.getWebsiteHandle()); 280 assertEquals(count/2, site2.getHits().intValue()); 281 } 282 } 283 284 | Popular Tags |