roller-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From agillil...@apache.org
Subject svn commit: r394755 - in /incubator/roller/branches/roller-newbackend: build.xml src/org/roller/business/hibernate/HibernateRefererManagerImpl.java tests/org/roller/business/RefererManagerTest.java
Date Mon, 17 Apr 2006 19:16:43 GMT
Author: agilliland
Date: Mon Apr 17 12:16:41 2006
New Revision: 394755

URL: http://svn.apache.org/viewcvs?rev=394755&view=rev
Log:
cleanup referer manager and enable referer unit test.


Modified:
    incubator/roller/branches/roller-newbackend/build.xml
    incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java
    incubator/roller/branches/roller-newbackend/tests/org/roller/business/RefererManagerTest.java

Modified: incubator/roller/branches/roller-newbackend/build.xml
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/build.xml?rev=394755&r1=394754&r2=394755&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/build.xml (original)
+++ incubator/roller/branches/roller-newbackend/build.xml Mon Apr 17 12:16:41 2006
@@ -829,6 +829,7 @@
 		<include name="org/roller/business/FileManagerTest.class"/>
 	        <include name="org/roller/business/PlanetManagerTest.class"/>
 		<include name="org/roller/business/BookmarkManagerTest.class"/>
+		<include name="org/roller/business/RefererManagerTest.class"/>
             </fileset>
         </batchtest>
     </junit>

Modified: incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java?rev=394755&r1=394754&r2=394755&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java
(original)
+++ incubator/roller/branches/roller-newbackend/src/org/roller/business/hibernate/HibernateRefererManagerImpl.java
Mon Apr 17 12:16:41 2006
@@ -75,7 +75,7 @@
      * @see org.roller.pojos.RefererManager#storeReferer(org.roller.pojos.RefererData)
      */
     public void storeReferer(RefererData referer) throws RollerException {
-        strategy.storeAndCommit(referer);
+        strategy.store(referer);
     }
     
     
@@ -83,36 +83,17 @@
      * @see org.roller.pojos.RefererManager#removeReferer(java.lang.String)
      */
     public void removeReferer(String id) throws RollerException {
-        strategy.removeAndCommit(id, RefererData.class);
+        strategy.remove(id, RefererData.class);
     }
     
     
     // convenience method for removing a list of referers
     private void removeReferers(List referers) throws RollerException {
         
-        try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
-            // just go through the list and remove each auto ping
-            Iterator referer = referers.iterator();
-            while (referer.hasNext()) {
-                    this.strategy.remove((RefererData) referer.next());
-            }
-            
-            // commit changes
-            this.strategy.getSession().getTransaction().commit();
-        } catch (HibernateException ex) {
-            
-            try {
-                this.strategy.getSession().getTransaction().rollback();
-            } catch(HibernateException he) {
-                log.error("Error doing rollback", he);
-            }
-            
-            strategy.release();
-            
-            throw new RollerException(ex);
+        // just go through the list and remove each auto ping
+        Iterator referer = referers.iterator();
+        while (referer.hasNext()) {
+            this.strategy.remove((RefererData) referer.next());
         }
     }
     
@@ -126,9 +107,6 @@
             throw new RollerException("entryid is null");
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.createAlias("weblogEntry","e");
@@ -151,9 +129,6 @@
             log.debug("clearReferrers");
         }
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Dialect currentDialect = ((SessionFactoryImplementor)session.getSessionFactory()).getDialect();
             String reset = "update RefererData set dayHits=0";
@@ -182,9 +157,6 @@
             log.debug("clearReferrers");
         }
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Dialect currentDialect = ((SessionFactoryImplementor)session.getSessionFactory()).getDialect();
             String reset = "update RefererData set dayHits=0 where website=:site";
@@ -208,10 +180,8 @@
      * Apply ignoreWord/spam filters to all referers in system.
      */
     public void applyRefererFilters() throws RollerException {
+        
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             
@@ -241,13 +211,11 @@
      * Apply ignoreWord/spam filters to all referers in website.
      */
     public void applyRefererFilters(WebsiteData website) throws RollerException {
+        
         if (null == website) throw new RollerException("website is null");
         if (null == website.getBlacklist()) return;
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             
@@ -280,9 +248,6 @@
             String permalink) throws RollerException {
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.conjunction()
@@ -304,9 +269,6 @@
             String refererUrl) throws RollerException {
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.conjunction()
@@ -481,9 +443,6 @@
             throw new RollerException("website is null");
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.eq("website",website));
@@ -504,9 +463,6 @@
             throw new RollerException("website is null");
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.eq("website", website));
@@ -535,9 +491,6 @@
             throw new RollerException("Date is null");
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.eq("website", website));
@@ -561,9 +514,6 @@
             throw new RollerException("entryid is null");
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.createAlias("weblogEntry","e");
@@ -588,9 +538,6 @@
             throws RollerException {
         
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             criteria.add(Expression.eq("website", website));
@@ -611,10 +558,8 @@
                                             String title,
                                             String excerpt)
             throws RollerException {
+        
         try {
-            // begin transaction
-            this.strategy.getSession().beginTransaction();
-            
             Session session = ((HibernatePersistenceStrategy)strategy).getSession();
             Criteria criteria = session.createCriteria(RefererData.class);
             

Modified: incubator/roller/branches/roller-newbackend/tests/org/roller/business/RefererManagerTest.java
URL: http://svn.apache.org/viewcvs/incubator/roller/branches/roller-newbackend/tests/org/roller/business/RefererManagerTest.java?rev=394755&r1=394754&r2=394755&view=diff
==============================================================================
--- incubator/roller/branches/roller-newbackend/tests/org/roller/business/RefererManagerTest.java
(original)
+++ incubator/roller/branches/roller-newbackend/tests/org/roller/business/RefererManagerTest.java
Mon Apr 17 12:16:41 2006
@@ -1 +1 @@
-package org.roller.business; 

import java.sql.Timestamp;
import java.util.Calendar;
import
java.util.Date;
import java.util.List;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.roller.RollerException;
import org.roller.model.PropertiesManager;
import org.roller.model.RefererManager;
import
org.roller.model.Roller;
import org.roller.pojos.RefererData;
import org.roller.pojos.RollerPropertyData;
import
org.roller.pojos.UserData;
import org.roller.pojos.WeblogEntryData;
import org.roller.pojos.WebsiteData;
import
org.roller.util.DateUtil;
import org.roller.RollerTestBase;

/**
 * Test Roller Referer Management.
*/
public class RefererManagerTest extends RollerTestBase 
{
    RefererManager rmgr;
   
//List refs;
    int count = 20;        
    String testDay;
    String origSpamWords;

 
  //------------------------------------------------------------------------
	public RefererManagerTest(String
name) 
    {
		super(name);
	}
    
    //--------
 ----------------------------------------------------------------
	public static void main(String
args[]) 
    {
		junit.textui.TestRunner.run(RefererManagerTest.class);
	}
    
    //------------------------------------------------------------------------
   public static Test suite() 
    {
        return new TestSuite(RefererManagerTest.class);
   }

    public void setUp() throws Exception
    {
        super.setUp();
        
    
   // add "spamtest" to refererSpamWords
        Roller mRoller = getRoller();
        PropertiesManager
pmgr = mRoller.getPropertiesManager();
        RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist");
       this.origSpamWords = spamprop.getValue();
        spamprop.setValue(spamprop.getValue()
+ ", spamtest");
        pmgr.store(spamprop);
        
        // Process count unique referers
       rmgr = getRoller().getRefererManager();
        Calendar lCalendar = Calendar.getInstance();
       lCalendar.setTime(new Date()
 );
        for (int i = 0; i < count; i++)
        {
            lCalendar.add(Calendar.DATE,
-1);
            Timestamp day = new Timestamp(lCalendar.getTime().getTime());
          
 testDay = DateUtil.format8chars(day);
            
            /*
            MockRequest
mock = new MockRequest(
                                DateUtil.format8chars(day),
     
                          "http://test"+i,
                                "http://test"+i,
                               null,
                                mWebsite
           
);
            rmgr.processRequest(mock);
            */
            rmgr.processReferrer("http://test"+i,
"http://test"+i, 
                    mWebsite.getHandle(), null, testDay);
            getRoller().commit();
           
            //testDay = mock.getDateString();
        }
    }

    public void
tearDown() throws Exception
    {      
        List refs = rmgr.getReferers(mWebsite);
 
      // Remove all referers processes
   
      for (int i = 0; i < refs.size(); i++)
        {
            rmgr.removeReferer(((RefererData)refs.get(i)).getId());
   
        }
        
        // Make sure all were removed
        refs = rmgr.getReferers(mWebsite);
           
        assertEquals(0,refs.size());

        // reset refererSpamWords to original
value
        Roller mRoller = getRoller();
        PropertiesManager pmgr = mRoller.getPropertiesManager();
       RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist");
        spamprop.setValue(this.origSpamWords);
       pmgr.store(spamprop);
        
        super.tearDown();
    }
    
    //------------------------------------------------------------------------
   public void testGetReferersToDate() throws Exception
    {
        List referers = rmgr.getReferersToDate(mWebsite,
testDay);
        assertEquals("Should be one Referer.", referers.size(), 1);
    }
    
   //--------------------------------------------------------------------
 ----
    public void testRefererProcessing() throws RollerException
    {
        List refs
= rmgr.getReferers(mWebsite);            
        assertEquals("number of referers should
equal count", count, refs.size());
        
        int hits = rmgr.getDayHits(mWebsite);
       assertEquals("There should be one fewer hits than referers", count, hits);        

    }
    
    /* Self-Referrer Denial is now caught by the RefererFilter -- Allen G
   
public void testSelfRefererDenial() throws RollerException
    {
        // test against "self
referrals"
        getRoller().begin(UserData.SYSTEM_USER);
        // create "direct" referer
       boolean isSpam = rmgr.processRequest(
            new MockRequest(
               
"20020101",
                "direct",
                "http://test.com",
                null,
mWebsite
            )
        );
        getRoller().commit();
        assertFalse("is not
spam", isSpam);
        int newRefCount = rmgr.getReferers(mWebsite)
 .size();
        
        // now create self-referer
        getRoller().begin(UserData.SYSTEM_USER);
       
        isSpam = rmgr.processRequest(
            new MockRequest(
              
 "20020202",
                "http://test.com/page/" + mWebsite.getHandle(),
            
   "http://test.com",
                null, mWebsite
            )
        );
        getRoller().commit();
       assertFalse("is not spam", isSpam);
        
        // number of referrers should
not have changed
        List refs = rmgr.getReferers(mWebsite);            
        assertEquals("self
referal not ignored", newRefCount, refs.size()); 
        
        // now create self-referer
from editor page
        isSpam = rmgr.processRequest(
            new MockRequest(
     
          "20020202",
                "http://test.com/weblog.do",
                "http://test.com",
               null, mWebsite
            )
        );
        getRoller().commit();
    
   assertFalse("is not spa
 m", isSpam);
        
        // number of referrers should not have changed
        refs
= rmgr.getReferers(mWebsite);            
        assertEquals("editor referal not ignored",
newRefCount, refs.size()); 
    }
    */
    
    /**
     * Test to see if Referer Spam detection
works.
     */
    /* Referrer spam detection now happens in the RefererFilter -- Allen G
   public void testSpamBlocking()
    {
        boolean isSpam = rmgr.processRequest(
   
        new MockRequest(
                "20040101",
                "http://www.spamtest.com",
               "http://test.com",
                null, mWebsite
            )
        );
       //assertTrue("failed to detect referer spam", isSpam);
    }
    */
    
    public
void testApplyRefererFilters() throws Exception
    {
        List refs = rmgr.getReferers(mWebsite);
       assertEquals(count, refs.size());
        String origWords = null;
        
      
 Roller mRoller = getRoller();
        PropertiesManag
 er pmgr = mRoller.getPropertiesManager();
        RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist");
       origWords = spamprop.getValue();
        spamprop.setValue(spamprop.getValue() + ",
test");
        pmgr.store(spamprop);
        getRoller().commit();
        
        getRoller().getRefererManager().applyRefererFilters();
       getRoller().commit();
        
        refs = rmgr.getReferers(mWebsite);
        assertEquals(0,
refs.size());

        spamprop = pmgr.getProperty("spam.blacklist");
        spamprop.setValue(origWords);
       pmgr.store(spamprop);
        getRoller().commit();
    }
    
    public void testApplyRefererFiltersWebsite()
throws Exception
    {
        List refs = rmgr.getReferers(mWebsite);
        assertEquals(count,
refs.size());
        String origWords = null;
        
        mWebsite = getRoller().getUserManager().retrieveWebsite(mWebsite.getId());
       origWords = mWebsite.getBlacklist();
        mWebsite.setBlac
 klist("test");
        mWebsite.save();
        getRoller().commit();
        
        getRoller().getRefererManager().applyRefererFilters();
       getRoller().commit();
        
        refs = rmgr.getReferers(mWebsite);
        assertEquals(0,
refs.size());
    }
}
\ No newline at end of file
+package org.roller.business;

import java.sql.Timestamp;
import java.util.Calendar;
import
java.util.Date;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import
junit.framework.TestSuite;import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import
org.roller.RollerException;
import org.roller.model.PropertiesManager;
import org.roller.model.RefererManager;
import
org.roller.pojos.RollerPropertyData;
import org.roller.pojos.UserData;
import org.roller.pojos.WebsiteData;
import
org.roller.util.DateUtil;
import org.roller.TestUtils;
import org.roller.model.RollerFactory;
import
org.roller.model.UserManager;


/**
 * Test Roller Referer Management.
 */
public class RefererManagerTest
extends TestCase {
    
    public static Log log = LogFactory.getLog(RefererManagerTest.class);
   
    UserData testUser = null;
    WebsiteData testWeblog = null;
    
    int count =
20;
    String testDay;
    String origSpa
 mWords;
    
    
    public RefererManagerTest(String name) {
        super(name);
    }
   
    
    public static Test suite() {
        return new TestSuite(RefererManagerTest.class);
   }
    
    
    /**
     * All tests in this suite require a user and a weblog.
     */
   public void setUp() throws Exception {
        
        try {
            testUser = TestUtils.setupUser("referTestUser");
           testWeblog = TestUtils.setupWeblog("referTestWeblog", testUser);
            
           // add "spamtest" to refererSpamWords
            PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
           RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist");
            this.origSpamWords
= spamprop.getValue();
            spamprop.setValue(spamprop.getValue() + ", spamtest");
           pmgr.store(spamprop);
            
            // add a number of referers to play
with
            RefererManager rmgr = RollerFactory.getRoll
 er().getRefererManager();
            Calendar lCalendar = Calendar.getInstance();
     
      lCalendar.setTime(new Date());
            for (int i = 0; i < count; i++) {
   
            lCalendar.add(Calendar.DATE, -1);
                Timestamp day = new Timestamp(lCalendar.getTime().getTime());
               testDay = DateUtil.format8chars(day);
                
                rmgr.processReferrer("http://test"+i,
"http://test"+i,
                        testWeblog.getHandle(), null, testDay);
        
   }
            
            TestUtils.endSession(true);
            
        } catch (Exception
ex){
            log.error(ex);
            throw new Exception("Test setup failed", ex);
       }
    }
    
    public void tearDown() throws Exception {
        
        try {
           // reset refererSpamWords to original value
            PropertiesManager pmgr
= RollerFactory.getRoller().getPropertiesManager();
            RollerPropertyData spamprop
= pmgr.getPro
 perty("spam.blacklist");
            spamprop.setValue(this.origSpamWords);
            pmgr.store(spamprop);
           
            TestUtils.teardownWeblog(testWeblog.getId());
            TestUtils.teardownUser(testUser.getId());
           
            TestUtils.endSession(true);
        } catch (Exception ex) {
    
       log.error(ex);
            throw new Exception("Test teardown failed", ex);
      
 }
    }
    
    
    public void testGetReferersToDate() throws Exception {
        
  
     RefererManager rmgr = RollerFactory.getRoller().getRefererManager();
        
      
 List referers = rmgr.getReferersToDate(testWeblog, testDay);
        assertEquals("Should
be one Referer.", referers.size(), 1);
    }
    
    
    public void testRefererProcessing()
throws RollerException {
        
        RefererManager rmgr = RollerFactory.getRoller().getRefererManager();
       
        List refs = rmgr.getReferers(testWeblog);
        assertEquals("number of
refer
 ers should equal count", count, refs.size());
        
        int hits = rmgr.getDayHits(testWeblog);
       assertEquals("There should be one fewer hits than referers", count, hits);
    }
 
  
    
    public void testApplyRefererFilters() throws Exception {
        
        RefererManager
rmgr = RollerFactory.getRoller().getRefererManager();
        
        List refs = rmgr.getReferers(testWeblog);
       assertEquals(count, refs.size());
        
        PropertiesManager pmgr = RollerFactory.getRoller().getPropertiesManager();
       RollerPropertyData spamprop = pmgr.getProperty("spam.blacklist");
        String origWords
= spamprop.getValue();
        spamprop.setValue(spamprop.getValue() + ", test");
       
pmgr.store(spamprop);
        TestUtils.endSession(true);
        
        rmgr.applyRefererFilters();
       TestUtils.endSession(true);
        
        refs = rmgr.getReferers(testWeblog);
       assertEquals(0, refs.size());
        
        spamprop =
  pmgr.getProperty("spam.blacklist");
        spamprop.setValue(origWords);
        pmgr.store(spamprop);
       TestUtils.endSession(true);
    }
    
    
    public void testApplyRefererFiltersWebsite()
throws Exception {
        
        RefererManager rmgr = RollerFactory.getRoller().getRefererManager();
       
        List refs = rmgr.getReferers(testWeblog);
        assertEquals(count, refs.size());
       String origWords = null;
        
        UserManager umgr = RollerFactory.getRoller().getUserManager();
       testWeblog = umgr.retrieveWebsite(testWeblog.getId());
        origWords = testWeblog.getBlacklist();
       testWeblog.setBlacklist("test");
        umgr.storeWebsite(testWeblog);
        TestUtils.endSession(true);
       
        rmgr.applyRefererFilters();
        TestUtils.endSession(true);
        
       refs = rmgr.getReferers(testWeblog);
        assertEquals(0, refs.size());
    }
 
  
}
\ No newline at end of file



Mime
View raw message