KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > business > RefererManagerTest


1 package org.roller.business;
2
3 import java.sql.Timestamp JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.List JavaDoc;
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 /**
24  * Test Roller Referer Management.
25  */

26 public class RefererManagerTest extends RollerTestBase
27 {
28     RefererManager rmgr;
29     //List refs;
30
int count = 20;
31     String JavaDoc testDay;
32     String JavaDoc origSpamWords;
33
34     //------------------------------------------------------------------------
35
public RefererManagerTest(String JavaDoc name)
36     {
37         super(name);
38     }
39     
40     //------------------------------------------------------------------------
41
public static void main(String JavaDoc args[])
42     {
43         junit.textui.TestRunner.run(RefererManagerTest.class);
44     }
45     
46     //------------------------------------------------------------------------
47
public static Test suite()
48     {
49         return new TestSuite(RefererManagerTest.class);
50     }
51
52     protected void setUp() throws Exception JavaDoc
53     {
54         super.setUp();
55         
56         // add "spamtest" to refererSpamWords
57
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         // Process count unique referers
65
rmgr = getRoller().getRefererManager();
66         Calendar JavaDoc lCalendar = Calendar.getInstance();
67         lCalendar.setTime(new Date JavaDoc());
68         for (int i = 0; i < count; i++)
69         {
70             lCalendar.add(Calendar.DATE, -1);
71             Timestamp JavaDoc day = new Timestamp JavaDoc(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 JavaDoc
89     {
90         List JavaDoc refs = rmgr.getReferers(mWebsite);
91         // Remove all referers processes
92
for (int i = 0; i < refs.size(); i++)
93         {
94             rmgr.removeReferer(((RefererData)refs.get(i)).getId());
95         }
96         
97         // Make sure all were removed
98
refs = rmgr.getReferers(mWebsite);
99         assertEquals(0,refs.size());
100
101         // reset refererSpamWords to original value
102
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     //------------------------------------------------------------------------
112
public void testGetReferersToDate() throws Exception JavaDoc
113     {
114         List JavaDoc referers = rmgr.getReferersToDate(mWebsite, testDay);
115         assertEquals("Should be one Referer.", referers.size(), 1);
116     }
117     
118     //------------------------------------------------------------------------
119
public void testRefererProcessing() throws RollerException
120     {
121         List JavaDoc 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         // test against "self referrals"
131
getRoller().begin(UserData.SYSTEM_USER);
132         // create "direct" referer
133
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         // now create self-referer
146
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         // number of referrers should not have changed
159
List JavaDoc refs = rmgr.getReferers(mWebsite);
160         assertEquals("self referal not ignored", newRefCount, refs.size());
161         
162         // now create self-referer from editor page
163
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         // number of referrers should not have changed
175
refs = rmgr.getReferers(mWebsite);
176         assertEquals("editor referal not ignored", newRefCount, refs.size());
177     }
178     
179     /**
180      * Test to see if Referer Spam detection works.
181      */

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         //assertTrue("failed to detect referer spam", isSpam);
193
}
194     
195     public void testApplyRefererFilters() throws Exception JavaDoc
196     {
197         List JavaDoc refs = rmgr.getReferers(mWebsite);
198         assertEquals(count, refs.size());
199         String JavaDoc 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 JavaDoc
225     {
226         List JavaDoc refs = rmgr.getReferers(mWebsite);
227         assertEquals(count, refs.size());
228         String JavaDoc 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 JavaDoc mDateStr = null;
249     private String JavaDoc mRefUrl = null;
250     private String JavaDoc mReqUrl = null;
251     private WeblogEntryData mEntry = null;
252     private WebsiteData mWebsite = null;
253     private boolean mIsDateSpecified = false;
254     
255     public MockRequest(
256         String JavaDoc dateStr, String JavaDoc refUrl, String JavaDoc 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     /**
269      * @see org.roller.pojos.ParsedRequest#getDateString()
270      */

271     public String JavaDoc getDateString()
272     {
273         return mDateStr;
274     }
275
276     /**
277      * @see org.roller.pojos.ParsedRequest#getRefererURL()
278      */

279     public String JavaDoc getRefererURL()
280     {
281         return mRefUrl;
282     }
283
284     /**
285      * @see org.roller.pojos.ParsedRequest#getRequestURL()
286      */

287     public String JavaDoc getRequestURL()
288     {
289         return mReqUrl;
290     }
291
292     /**
293      * @see org.roller.pojos.ParsedRequest#getWeblogEntry()
294      */

295     public WeblogEntryData getWeblogEntry()
296     {
297         return mEntry;
298     }
299
300     /**
301      * @see org.roller.pojos.ParsedRequest#getWebsite()
302      */

303     public WebsiteData getWebsite()
304     {
305         return mWebsite;
306     }
307
308     /**
309      * Returns the isDateSpecified.
310      * @return boolean
311      */

312     public boolean isDateSpecified()
313     {
314         return mIsDateSpecified;
315     }
316
317     /**
318      * Sets the isDateSpecified.
319      * @param isDateSpecified The isDateSpecified to set
320      */

321     public void setDateSpecified(boolean isDateSpecified)
322     {
323         mIsDateSpecified = isDateSpecified;
324     }
325
326     /**
327      * @see org.roller.pojos.ParsedRequest#isEnableLinkback()
328      */

329     public boolean isEnableLinkback()
330     {
331         return false;
332     }
333 }
334
Popular Tags