portals-jetspeed-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tay...@apache.org
Subject svn commit: r330603 - /portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java
Date Thu, 03 Nov 2005 18:31:36 GMT
Author: taylor
Date: Thu Nov  3 10:31:22 2005
New Revision: 330603

URL: http://svn.apache.org/viewcvs?rev=330603&view=rev
Log:
http://issues.apache.org/jira/browse/JS2-362
Implemented database support for statistics component

Modified:
    portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java

Modified: portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java
URL: http://svn.apache.org/viewcvs/portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java?rev=330603&r1=330602&r2=330603&view=diff
==============================================================================
--- portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java
(original)
+++ portals/jetspeed-2/trunk/components/statistics/src/test/org/apache/jetspeed/statistics/TestStatistics.java
Thu Nov  3 10:31:22 2005
@@ -15,12 +15,17 @@
  */
 package org.apache.jetspeed.statistics;
 
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
 import org.apache.jetspeed.components.test.AbstractSpringTestCase;
 import org.apache.jetspeed.mockobjects.request.MockRequestContext;
+import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
 import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
 import org.apache.jetspeed.request.RequestContext;
 
@@ -29,9 +34,10 @@
 import com.mockrunner.mock.web.MockHttpSession;
 
 /**
- * TestProfiler
+ * TestStatistics
  * 
  * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
+ * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer</a>
  * @version $Id: $
  */
 public class TestStatistics extends AbstractSpringTestCase
@@ -45,6 +51,7 @@
      */
     protected void tearDown() throws Exception
     {
+        ctx.close();
         super.tearDown();
     }
 
@@ -58,31 +65,145 @@
     {
         junit.awtui.TestRunner.main(new String[]
         { TestStatistics.class.getName() });
+        
+        
     }
 
+    
     protected void setUp() throws Exception
     {
         super.setUp();
         this.statistics = (PortalStatistics) ctx.getBean("PortalStatistics");
         assertNotNull("statistics not found ", statistics);
     }
-
+    
+    public void clearDBs() {
+        
+        try
+        {
+            Connection con = statistics.getDataSource().getConnection();
+
+            PreparedStatement psmt = con
+                    .prepareStatement("DELETE FROM USER_STATISTICS");
+            psmt.execute();
+            psmt.close();
+            psmt = con.prepareStatement("DELETE FROM PAGE_STATISTICS");
+            psmt.execute();
+            psmt.close();
+            psmt = con.prepareStatement("DELETE FROM PORTLET_STATISTICS");
+            psmt.execute();
+            psmt.close();
+            if (con != null) con.close();
+        } catch (SQLException e)
+        {
+            fail("problem with database connection:" + e.toString());
+        }
+    }
+    
+    public int count(String query) {
+        int val = -1;
+        try
+        {
+            Connection con = statistics.getDataSource().getConnection();
+
+            PreparedStatement psmt = con
+                    .prepareStatement(query);
+            ResultSet rs = psmt.executeQuery();
+            
+            if(rs.next()) {
+                val = rs.getInt(1);
+            }
+            psmt.close();
+            if (con != null) con.close();
+        } catch (SQLException e)
+        {
+            fail("problem with database connection:" + e.toString());
+        }
+        return val;
+    }
+    public int countPages() {
+        return count("SELECT count(*) from PAGE_STATISTICS");
+    }
+        
+    public int countPortlets() {
+        return count("SELECT count(*) from PORTLET_STATISTICS");
+    }
+    public int countUsers() {
+        return count("SELECT count(*) from USER_STATISTICS");
+    }
+    
     public static Test suite()
     {
         // All methods starting with "test" will be executed in the test suite.
         return new TestSuite(TestStatistics.class);
     }
-
-
+    
     public void testPortletStatistics() 
     throws Exception
     {
+        statistics.forceFlush();
+        clearDBs();
+        
         assertNotNull("statistics service is null", statistics);
-        RequestContext request = initRequestContext();
         
+        RequestContext request = initRequestContext();
+        PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
+        app.setName("MyApp");
         PortletDefinitionImpl portlet = new PortletDefinitionImpl();
+        portlet.setPortletApplicationDefinition(app);
         portlet.setName("TestPortlet");
-        statistics.logAccess(request, portlet, "401");                
+        portlet.setPortletApplicationDefinition(app);
+        long elapsedTime = 123;
+        statistics.logPortletAccess(request, portlet.getUniqueName(), "401",elapsedTime);
+        statistics.logPageAccess(request, "401",elapsedTime);
+        statistics.logUserLogin(request,elapsedTime);
+        statistics.logUserLogout(request,elapsedTime);
+        
+        statistics.forceFlush();
+        int x = 1;
+        int pages = this.countPages();
+        int users = this.countUsers();
+        int portlets = this.countPortlets();
+        assertEquals("User Log count incorrect ",2*x,users);
+        assertEquals("Portlet Log count incorrect ",x,portlets);
+        assertEquals("Page Log count incorrect ",x,pages);
+        
+    }
+    
+    public void testLotsOfPortletStatistics() 
+    throws Exception
+    {
+        statistics.forceFlush();
+        clearDBs();
+        
+        int x = 37;
+        assertNotNull("statistics service is null", statistics);
+        for(int i = 0; i < x ; i++) {
+            RequestContext request = initRequestContext();
+            PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
+            app.setName("MyApp");
+            PortletDefinitionImpl portlet = new PortletDefinitionImpl();
+            portlet.setPortletApplicationDefinition(app);
+            portlet.setName("TestPortlet");
+            portlet.setPortletApplicationDefinition(app);
+            long elapsedTime = 123+i;
+            //System.out.println("logging something, number "+i);
+            statistics.logPortletAccess(request, portlet.getUniqueName(), "401",elapsedTime);
+            statistics.logPageAccess(request, "401",elapsedTime);
+            statistics.logUserLogin(request,elapsedTime);
+            statistics.logUserLogout(request,elapsedTime);
+            try { Thread.sleep(200);} catch(InterruptedException ie) {}
+        }
+        
+        statistics.forceFlush();
+        int pages = this.countPages();
+        int users = this.countUsers();
+        int portlets = this.countPortlets();
+        assertEquals("User Log count incorrect ",2*x,users);
+        assertEquals("Portlet Log count incorrect ",x,portlets);
+        assertEquals("Page Log count incorrect ",x,pages);
+        
+        
     }
 
     private RequestContext initRequestContext()
@@ -91,23 +212,28 @@
         MockHttpServletResponse response = new MockHttpServletResponse();
         MockHttpSession session = new MockHttpSession();
 
+        
         request.setRemoteAddr("192.168.2.3");
-
         request.setSession(session);
         request.setServerName("www.sporteportal.com");
         request.setScheme("http");
         request.setContextPath("/jetspeed");
         request.setServletPath("/portal");
-        request.setPathInfo("stuff");
-        request.setRequestURI("/jetspeed/portal/stuff");
-        request.setMethod("GET");
-        RequestContext rc = new MockRequestContext(request, response);       
+        request.setPathInfo("/news/default-page.psml");
+        request.setRequestURI("/jetspeed/portal/news/default-page.psml");
+        request.setMethod("GET");    
+        RequestContext rc = new MockRequestContext(request, response);
         return rc;
     }
     
     protected String[] getConfigurations()
     {
         return new String[]{"statistics.xml"};
+    }
+    
+    protected String[] getBootConfigurations()
+    {
+        return new String[]{"test-repository-datasource-spring.xml"};
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-dev-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-dev-help@portals.apache.org


Mime
View raw message