1 package org.roller.business; 2 3 import java.sql.Timestamp ; 4 import java.util.Calendar ; 5 import java.util.Date ; 6 import java.util.List ; 7 8 import junit.framework.Test; 9 import junit.framework.TestSuite; 10 11 import org.roller.RollerException; 12 import org.roller.model.PropertiesManager; 13 import org.roller.model.RefererManager; 14 import org.roller.model.Roller; 15 import org.roller.pojos.RefererData; 16 import org.roller.pojos.RollerPropertyData; 17 import org.roller.pojos.UserData; 18 import org.roller.pojos.WeblogEntryData; 19 import org.roller.pojos.WebsiteData; 20 import org.roller.util.DateUtil; 21 import org.roller.RollerTestBase; 22 23 26 public class RefererManagerTest extends RollerTestBase 27 { 28 RefererManager rmgr; 29 int count = 20; 31 String testDay; 32 String origSpamWords; 33 34 public RefererManagerTest(String name) 36 { 37 super(name); 38 } 39 40 public static void main(String args[]) 42 { 43 junit.textui.TestRunner.run(RefererManagerTest.class); 44 } 45 46 public static Test suite() 48 { 49 return new TestSuite(RefererManagerTest.class); 50 } 51 52 protected void setUp() throws Exception 53 { 54 super.setUp(); 55 56 Roller mRoller = getRoller(); 58 PropertiesManager pmgr = mRoller.getPropertiesManager(); 59 RollerPropertyData spamprop = pmgr.getProperty("spam.referers.ignorewords"); 60 this.origSpamWords = spamprop.getValue(); 61 spamprop.setValue(spamprop.getValue() + ", spamtest"); 62 pmgr.store(spamprop); 63 64 rmgr = getRoller().getRefererManager(); 66 Calendar lCalendar = Calendar.getInstance(); 67 lCalendar.setTime(new Date ()); 68 for (int i = 0; i < count; i++) 69 { 70 lCalendar.add(Calendar.DATE, -1); 71 Timestamp day = new Timestamp (lCalendar.getTime().getTime()); 72 73 getRoller().begin(UserData.SYSTEM_USER); 74 MockRequest mock = new MockRequest( 75 DateUtil.format8chars(day), 76 "http://test"+i, 77 "http://test"+i, 78 null, 79 mWebsite 80 ); 81 rmgr.processRequest(mock); 82 getRoller().commit(); 83 84 testDay = mock.getDateString(); 85 } 86 } 87 88 public void tearDown() throws Exception 89 { 90 List refs = rmgr.getReferers(mWebsite); 91 for (int i = 0; i < refs.size(); i++) 93 { 94 rmgr.removeReferer(((RefererData)refs.get(i)).getId()); 95 } 96 97 refs = rmgr.getReferers(mWebsite); 99 assertEquals(0,refs.size()); 100 101 Roller mRoller = getRoller(); 103 PropertiesManager pmgr = mRoller.getPropertiesManager(); 104 RollerPropertyData spamprop = pmgr.getProperty("spam.referers.ignorewords"); 105 spamprop.setValue(this.origSpamWords); 106 pmgr.store(spamprop); 107 108 super.tearDown(); 109 } 110 111 public void testGetReferersToDate() throws Exception 113 { 114 List referers = rmgr.getReferersToDate(mWebsite, testDay); 115 assertEquals("Should be one Referer.", referers.size(), 1); 116 } 117 118 public void testRefererProcessing() throws RollerException 120 { 121 List refs = rmgr.getReferers(mWebsite); 122 assertEquals("number of referers should equal count", count, refs.size()); 123 124 int hits = rmgr.getDayHits(mWebsite); 125 assertEquals("There should be one fewer hits than referers", count, hits); 126 } 127 128 public void testSelfRefererDenial() throws RollerException 129 { 130 getRoller().begin(UserData.SYSTEM_USER); 132 boolean isSpam = rmgr.processRequest( 134 new MockRequest( 135 "20020101", 136 "direct", 137 "http://test.com", 138 null, mWebsite 139 ) 140 ); 141 getRoller().commit(); 142 assertFalse("is not spam", isSpam); 143 int newRefCount = rmgr.getReferers(mWebsite).size(); 144 145 getRoller().begin(UserData.SYSTEM_USER); 147 isSpam = rmgr.processRequest( 148 new MockRequest( 149 "20020202", 150 "http://test.com/page/" + mWebsite.getUser().getUserName(), 151 "http://test.com", 152 null, mWebsite 153 ) 154 ); 155 getRoller().commit(); 156 assertFalse("is not spam", isSpam); 157 158 List refs = rmgr.getReferers(mWebsite); 160 assertEquals("self referal not ignored", newRefCount, refs.size()); 161 162 isSpam = rmgr.processRequest( 164 new MockRequest( 165 "20020202", 166 "http://test.com/weblog.do", 167 "http://test.com", 168 null, mWebsite 169 ) 170 ); 171 getRoller().commit(); 172 assertFalse("is not spam", isSpam); 173 174 refs = rmgr.getReferers(mWebsite); 176 assertEquals("editor referal not ignored", newRefCount, refs.size()); 177 } 178 179 182 public void testSpamBlocking() 183 { 184 boolean isSpam = rmgr.processRequest( 185 new MockRequest( 186 "20040101", 187 "http://www.spamtest.com", 188 "http://test.com", 189 null, mWebsite 190 ) 191 ); 192 } 194 195 public void testApplyRefererFilters() throws Exception 196 { 197 List refs = rmgr.getReferers(mWebsite); 198 assertEquals(count, refs.size()); 199 String origWords = null; 200 201 getRoller().begin(UserData.SYSTEM_USER); 202 Roller mRoller = getRoller(); 203 PropertiesManager pmgr = mRoller.getPropertiesManager(); 204 RollerPropertyData spamprop = pmgr.getProperty("spam.referers.ignorewords"); 205 origWords = spamprop.getValue(); 206 spamprop.setValue(spamprop.getValue() + ", test"); 207 pmgr.store(spamprop); 208 getRoller().commit(); 209 210 getRoller().begin(UserData.SYSTEM_USER); 211 getRoller().getRefererManager().applyRefererFilters(); 212 getRoller().commit(); 213 214 refs = rmgr.getReferers(mWebsite); 215 assertEquals(0, refs.size()); 216 217 getRoller().begin(UserData.SYSTEM_USER); 218 spamprop = pmgr.getProperty("spam.referers.ignorewords"); 219 spamprop.setValue(origWords); 220 pmgr.store(spamprop); 221 getRoller().commit(); 222 } 223 224 public void testApplyRefererFiltersWebsite() throws Exception 225 { 226 List refs = rmgr.getReferers(mWebsite); 227 assertEquals(count, refs.size()); 228 String origWords = null; 229 230 getRoller().begin(UserData.SYSTEM_USER); 231 mWebsite = getRoller().getUserManager().retrieveWebsite(mWebsite.getId()); 232 origWords = mWebsite.getIgnoreWords(); 233 mWebsite.setIgnoreWords("test"); 234 mWebsite.save(); 235 getRoller().commit(); 236 237 getRoller().begin(UserData.SYSTEM_USER); 238 getRoller().getRefererManager().applyRefererFilters(); 239 getRoller().commit(); 240 241 refs = rmgr.getReferers(mWebsite); 242 assertEquals(0, refs.size()); 243 } 244 } 245 246 class MockRequest implements org.roller.model.ParsedRequest 247 { 248 private String mDateStr = null; 249 private String mRefUrl = null; 250 private String mReqUrl = null; 251 private WeblogEntryData mEntry = null; 252 private WebsiteData mWebsite = null; 253 private boolean mIsDateSpecified = false; 254 255 public MockRequest( 256 String dateStr, String refUrl, String reqUrl, 257 WeblogEntryData entry, WebsiteData website) 258 { 259 mDateStr = dateStr; 260 mRefUrl = refUrl; 261 mReqUrl = reqUrl; 262 mEntry = entry; 263 mWebsite = website; 264 265 if (mDateStr != null) mIsDateSpecified = true; 266 } 267 268 271 public String getDateString() 272 { 273 return mDateStr; 274 } 275 276 279 public String getRefererURL() 280 { 281 return mRefUrl; 282 } 283 284 287 public String getRequestURL() 288 { 289 return mReqUrl; 290 } 291 292 295 public WeblogEntryData getWeblogEntry() 296 { 297 return mEntry; 298 } 299 300 303 public WebsiteData getWebsite() 304 { 305 return mWebsite; 306 } 307 308 312 public boolean isDateSpecified() 313 { 314 return mIsDateSpecified; 315 } 316 317 321 public void setDateSpecified(boolean isDateSpecified) 322 { 323 mIsDateSpecified = isDateSpecified; 324 } 325 326 329 public boolean isEnableLinkback() 330 { 331 return false; 332 } 333 } 334 | Popular Tags |