001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.statistics;
018:
019: import java.security.Principal;
020: import java.sql.Connection;
021: import java.sql.DatabaseMetaData;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.util.List;
026:
027: import junit.framework.Test;
028: import junit.framework.TestSuite;
029:
030: import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
031: import org.apache.jetspeed.mockobjects.request.MockRequestContext;
032: import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
033: import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
034: import org.apache.jetspeed.request.RequestContext;
035: import org.apache.jetspeed.security.impl.UserPrincipalImpl;
036: import org.apache.jetspeed.statistics.impl.StatisticsQueryCriteriaImpl;
037:
038: import com.mockrunner.mock.web.MockHttpServletRequest;
039: import com.mockrunner.mock.web.MockHttpServletResponse;
040: import com.mockrunner.mock.web.MockHttpSession;
041:
042: /**
043: * TestStatistics
044: *
045: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
046: * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer </a>
047: * @version $Id: $
048: */
049: public class TestStatistics extends DatasourceEnabledSpringTestCase {
050: String USERNAME = "anotherFaker";
051:
052: private PortalStatistics statistics = null;
053:
054: /*
055: * (non-Javadoc)
056: *
057: * @see junit.framework.TestCase#tearDown()
058: */
059: protected void tearDown() throws Exception {
060: ctx.close();
061: super .tearDown();
062: }
063:
064: /**
065: * Start the tests.
066: *
067: * @param args
068: * the arguments. Not used
069: */
070: public static void main(String args[]) {
071: junit.awtui.TestRunner.main(new String[] { TestStatistics.class
072: .getName() });
073:
074: }
075:
076: protected void setUp() throws Exception {
077: super .setUp();
078:
079: this .statistics = (PortalStatistics) ctx
080: .getBean("PortalStatistics");
081: assertNotNull("statistics not found ", statistics);
082: }
083:
084: public void clearDBs() {
085:
086: try {
087: DatabaseMetaData dmd = statistics.getDataSource()
088: .getConnection().getMetaData();
089: System.out
090: .println("Oh... just for reference we're running against "
091: + dmd.getDatabaseProductName());
092: System.out.println("with the driver = "
093: + dmd.getDriverName());
094: System.out.println(" with the url = " + dmd.getURL());
095:
096: Connection con = statistics.getDataSource().getConnection();
097:
098: PreparedStatement psmt = con
099: .prepareStatement("DELETE FROM USER_STATISTICS");
100: psmt.execute();
101: psmt.close();
102: psmt = con.prepareStatement("DELETE FROM PAGE_STATISTICS");
103: psmt.execute();
104: psmt.close();
105: psmt = con
106: .prepareStatement("DELETE FROM PORTLET_STATISTICS");
107: psmt.execute();
108: psmt.close();
109: if (con != null)
110: con.close();
111: } catch (SQLException e) {
112: fail("problem with database connection:" + e.toString());
113: }
114: }
115:
116: public int count(String query) {
117: int val = -1;
118: try {
119: Connection con = statistics.getDataSource().getConnection();
120:
121: PreparedStatement psmt = con.prepareStatement(query);
122: ResultSet rs = psmt.executeQuery();
123:
124: if (rs.next()) {
125: val = rs.getInt(1);
126: }
127: psmt.close();
128: if (con != null)
129: con.close();
130: } catch (SQLException e) {
131: fail("problem with database connection:" + e.toString());
132: }
133: return val;
134: }
135:
136: public int countPages() {
137: return count("SELECT count(*) from PAGE_STATISTICS");
138: }
139:
140: public int countPortlets() {
141: return count("SELECT count(*) from PORTLET_STATISTICS");
142: }
143:
144: public int countUsers() {
145: return count("SELECT count(*) from USER_STATISTICS");
146: }
147:
148: public static Test suite() {
149: // All methods starting with "test" will be executed in the test suite.
150: return new TestSuite(TestStatistics.class);
151: }
152:
153: public void testPortletStatistics() throws Exception {
154: System.out.println("testing one of each ");
155: statistics.forceFlush();
156: clearDBs();
157:
158: assertNotNull("statistics service is null", statistics);
159:
160: RequestContext request = initRequestContext();
161: PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
162: app.setName("MyApp");
163: PortletDefinitionImpl portlet = new PortletDefinitionImpl();
164: portlet.setPortletApplicationDefinition(app);
165: portlet.setName("TestPortlet");
166: portlet.setPortletApplicationDefinition(app);
167: long elapsedTime = 123;
168: statistics.logPortletAccess(request, portlet.getUniqueName(),
169: "401", elapsedTime);
170: statistics.logPageAccess(request, "401", elapsedTime);
171: statistics.logUserLogin(request, elapsedTime);
172:
173: assertEquals("number of users incorrect", 1, statistics
174: .getNumberOfCurrentUsers());
175:
176: List l = statistics.getListOfLoggedInUsers();
177: assertNotNull("list returned is null", l);
178: assertEquals("wrong number of users in list", 1, l.size());
179:
180: // statistics.logUserLogout("123.234.145.156", "SuperFakeyUser",
181: // elapsedTime);
182: statistics.logUserLogout("123.234.145.156", USERNAME,
183: elapsedTime);
184:
185: statistics.forceFlush();
186:
187: assertEquals("number of users incorrect", statistics
188: .getNumberOfCurrentUsers(), 0);
189:
190: int x = 1;
191: int pages = this .countPages();
192: int users = this .countUsers();
193: int portlets = this .countPortlets();
194: assertEquals("User Log count incorrect ", 2 * x, users);
195: assertEquals("Portlet Log count incorrect ", x, portlets);
196: assertEquals("Page Log count incorrect ", x, pages);
197:
198: }
199:
200: public void testLotsOfPortletStatistics() throws Exception {
201: System.out.println("testing Multiple portlet stats");
202: statistics.forceFlush();
203: clearDBs();
204:
205: int x = 37;
206: assertNotNull("statistics service is null", statistics);
207: for (int i = 0; i < x; i++) {
208: RequestContext request = initRequestContext();
209: PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
210: app.setName("MyApp");
211: PortletDefinitionImpl portlet = new PortletDefinitionImpl();
212: portlet.setPortletApplicationDefinition(app);
213: portlet.setName("TestPortlet");
214: portlet.setPortletApplicationDefinition(app);
215: long elapsedTime = 123 + i;
216: //System.out.println("logging something, number "+i);
217: statistics.logPortletAccess(request, portlet
218: .getUniqueName(), "401", elapsedTime);
219: statistics.logPageAccess(request, "401", elapsedTime);
220: statistics.logUserLogin(request, elapsedTime);
221: assertEquals("number of users incorrect", 1, statistics
222: .getNumberOfCurrentUsers());
223: List l = statistics.getListOfLoggedInUsers();
224: assertNotNull("list returned is null", l);
225: assertEquals("wrong number of users in list", 1, l.size());
226:
227: // statistics.logUserLogout("123.234.145.156", "SuperFakeyUser",
228: // elapsedTime);
229: statistics.logUserLogout("123.234.145.156", USERNAME,
230: elapsedTime);
231: try {
232: Thread.sleep(200);
233: } catch (InterruptedException ie) {
234: }
235: }
236:
237: statistics.forceFlush();
238:
239: assertEquals("number of users incorrect", statistics
240: .getNumberOfCurrentUsers(), 0);
241:
242: int pages = this .countPages();
243: int users = this .countUsers();
244: int portlets = this .countPortlets();
245: assertEquals("User Log count incorrect ", 2 * x, users);
246: assertEquals("Portlet Log count incorrect ", x, portlets);
247: assertEquals("Page Log count incorrect ", x, pages);
248:
249: }
250:
251: public void testQuerySystem() throws Exception {
252: System.out.println("testing Query System");
253: StatisticsQueryCriteria sqc = new StatisticsQueryCriteriaImpl();
254: sqc.setQueryType(PortalStatistics.QUERY_TYPE_USER);
255: int desired = 5;
256: sqc.setListsize("" + desired);
257: sqc.setSorttype("count");
258: sqc.setSortorder("desc");
259: AggregateStatistics as = statistics.queryStatistics(sqc);
260: assertNotNull(as);
261: System.out.println("user = " + as);
262: int size = as.getStatlist().size();
263: assertTrue((size <= desired));
264:
265: sqc.setQueryType(PortalStatistics.QUERY_TYPE_PORTLET);
266: sqc.setListsize("" + desired);
267: sqc.setSorttype("count");
268: sqc.setSortorder("desc");
269: as = statistics.queryStatistics(sqc);
270: assertNotNull(as);
271: System.out.println("portlet = " + as);
272: size = as.getStatlist().size();
273: assertTrue((size <= desired));
274:
275: sqc.setQueryType(PortalStatistics.QUERY_TYPE_PAGE);
276: sqc.setListsize("" + desired);
277: sqc.setSorttype("count");
278: sqc.setSortorder("desc");
279: as = statistics.queryStatistics(sqc);
280: assertNotNull(as);
281: System.out.println("page = " + as);
282: size = as.getStatlist().size();
283: assertTrue((size <= desired));
284:
285: }
286:
287: private RequestContext initRequestContext() {
288: MockHttpServletRequest request = new MockHttpServletRequest();
289: MockHttpServletResponse response = new MockHttpServletResponse();
290: MockHttpSession session = new MockHttpSession();
291:
292: // Principal p = new UserPrincipalImpl("anotherFaker");
293: Principal p = new UserPrincipalImpl(USERNAME);
294:
295: request.setUserPrincipal(p);
296:
297: request.setRemoteAddr("123.234.145.156");
298: request.setSession(session);
299: request.setServerName("www.sporteportal.com");
300: request.setScheme("http");
301: request.setContextPath("/jetspeed");
302: request.setServletPath("/portal");
303: request.setPathInfo("/news/default-page.psml");
304: request
305: .setRequestURI("/jetspeed/portal/news/default-page.psml");
306: request.setMethod("GET");
307: RequestContext rc = new MockRequestContext(request, response);
308: return rc;
309: }
310:
311: protected String[] getConfigurations() {
312: return new String[] { "statistics.xml", "transaction.xml",
313: "boot/datasource.xml" };
314: }
315:
316: protected String[] getBootConfigurations() {
317: return new String[] { "boot/datasource.xml" };
318: }
319:
320: }
|