1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.statistics;
18
19 import java.security.Principal;
20 import java.sql.Connection;
21 import java.sql.DatabaseMetaData;
22 import java.sql.PreparedStatement;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.util.List;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.jetspeed.components.util.DatasourceEnabledSpringTestCase;
31 import org.apache.jetspeed.mockobjects.request.MockRequestContext;
32 import org.apache.jetspeed.om.portlet.impl.PortletApplicationDefinitionImpl;
33 import org.apache.jetspeed.om.portlet.impl.PortletDefinitionImpl;
34 import org.apache.jetspeed.request.RequestContext;
35 import org.apache.jetspeed.security.impl.UserPrincipalImpl;
36 import org.apache.jetspeed.statistics.impl.StatisticsQueryCriteriaImpl;
37
38 import com.mockrunner.mock.web.MockHttpServletRequest;
39 import com.mockrunner.mock.web.MockHttpServletResponse;
40 import com.mockrunner.mock.web.MockHttpSession;
41
42 /***
43 * TestStatistics
44 *
45 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
46 * @author <a href="mailto:chris@bluesunrise.com">Chris Schaefer </a>
47 * @version $Id: $
48 */
49 public class TestStatistics extends DatasourceEnabledSpringTestCase
50 {
51 String USERNAME = "anotherFaker";
52
53 private PortalStatistics statistics = null;
54
55
56
57
58
59
60 protected void tearDown() throws Exception
61 {
62 ctx.close();
63 super.tearDown();
64 }
65
66 /***
67 * Start the tests.
68 *
69 * @param args
70 * the arguments. Not used
71 */
72 public static void main(String args[])
73 {
74 junit.awtui.TestRunner.main(new String[]
75 { TestStatistics.class.getName()});
76
77 }
78
79 protected void setUp() throws Exception
80 {
81 super.setUp();
82
83 this.statistics = (PortalStatistics) ctx.getBean("PortalStatistics");
84 assertNotNull("statistics not found ", statistics);
85 }
86
87 public void clearDBs()
88 {
89
90
91 try
92 {
93 DatabaseMetaData dmd = statistics.getDataSource().getConnection().getMetaData();
94 System.out.println("Oh... just for reference we're running against "+dmd.getDatabaseProductName());
95 System.out.println("with the driver = "+dmd.getDriverName());
96 System.out.println(" with the url = "+dmd.getURL());
97
98 Connection con = statistics.getDataSource().getConnection();
99
100 PreparedStatement psmt = con
101 .prepareStatement("DELETE FROM USER_STATISTICS");
102 psmt.execute();
103 psmt.close();
104 psmt = con.prepareStatement("DELETE FROM PAGE_STATISTICS");
105 psmt.execute();
106 psmt.close();
107 psmt = con.prepareStatement("DELETE FROM PORTLET_STATISTICS");
108 psmt.execute();
109 psmt.close();
110 if (con != null) con.close();
111 } catch (SQLException e)
112 {
113 fail("problem with database connection:" + e.toString());
114 }
115 }
116
117 public int count(String query)
118 {
119 int val = -1;
120 try
121 {
122 Connection con = statistics.getDataSource().getConnection();
123
124 PreparedStatement psmt = con.prepareStatement(query);
125 ResultSet rs = psmt.executeQuery();
126
127 if (rs.next())
128 {
129 val = rs.getInt(1);
130 }
131 psmt.close();
132 if (con != null) con.close();
133 } catch (SQLException e)
134 {
135 fail("problem with database connection:" + e.toString());
136 }
137 return val;
138 }
139
140 public int countPages()
141 {
142 return count("SELECT count(*) from PAGE_STATISTICS");
143 }
144
145 public int countPortlets()
146 {
147 return count("SELECT count(*) from PORTLET_STATISTICS");
148 }
149
150 public int countUsers()
151 {
152 return count("SELECT count(*) from USER_STATISTICS");
153 }
154
155 public static Test suite()
156 {
157
158 return new TestSuite(TestStatistics.class);
159 }
160
161 public void testPortletStatistics() throws Exception
162 {
163 System.out.println("testing one of each ");
164 statistics.forceFlush();
165 clearDBs();
166
167 assertNotNull("statistics service is null", statistics);
168
169 RequestContext request = initRequestContext();
170 PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
171 app.setName("MyApp");
172 PortletDefinitionImpl portlet = new PortletDefinitionImpl();
173 portlet.setPortletApplicationDefinition(app);
174 portlet.setName("TestPortlet");
175 portlet.setPortletApplicationDefinition(app);
176 long elapsedTime = 123;
177 statistics.logPortletAccess(request, portlet.getUniqueName(), "401",
178 elapsedTime);
179 statistics.logPageAccess(request, "401", elapsedTime);
180 statistics.logUserLogin(request, elapsedTime);
181
182 assertEquals("number of users incorrect", 1, statistics
183 .getNumberOfCurrentUsers());
184
185 List l = statistics.getListOfLoggedInUsers();
186 assertNotNull("list returned is null", l);
187 assertEquals("wrong number of users in list", 1, l.size());
188
189
190
191 statistics.logUserLogout("123.234.145.156", USERNAME,
192 elapsedTime);
193
194 statistics.forceFlush();
195
196 assertEquals("number of users incorrect", statistics
197 .getNumberOfCurrentUsers(), 0);
198
199 int x = 1;
200 int pages = this.countPages();
201 int users = this.countUsers();
202 int portlets = this.countPortlets();
203 assertEquals("User Log count incorrect ", 2 * x, users);
204 assertEquals("Portlet Log count incorrect ", x, portlets);
205 assertEquals("Page Log count incorrect ", x, pages);
206
207 }
208
209 public void testLotsOfPortletStatistics() throws Exception
210 {
211 System.out.println("testing Multiple portlet stats");
212 statistics.forceFlush();
213 clearDBs();
214
215 int x = 37;
216 assertNotNull("statistics service is null", statistics);
217 for (int i = 0; i < x; i++)
218 {
219 RequestContext request = initRequestContext();
220 PortletApplicationDefinitionImpl app = new PortletApplicationDefinitionImpl();
221 app.setName("MyApp");
222 PortletDefinitionImpl portlet = new PortletDefinitionImpl();
223 portlet.setPortletApplicationDefinition(app);
224 portlet.setName("TestPortlet");
225 portlet.setPortletApplicationDefinition(app);
226 long elapsedTime = 123 + i;
227
228 statistics.logPortletAccess(request, portlet.getUniqueName(),
229 "401", elapsedTime);
230 statistics.logPageAccess(request, "401", elapsedTime);
231 statistics.logUserLogin(request, elapsedTime);
232 assertEquals("number of users incorrect", 1, statistics
233 .getNumberOfCurrentUsers());
234 List l = statistics.getListOfLoggedInUsers();
235 assertNotNull("list returned is null", l);
236 assertEquals("wrong number of users in list", 1, l.size());
237
238
239
240 statistics.logUserLogout("123.234.145.156", USERNAME,
241 elapsedTime);
242 try
243 {
244 Thread.sleep(200);
245 } catch (InterruptedException ie)
246 {
247 }
248 }
249
250 statistics.forceFlush();
251
252 assertEquals("number of users incorrect", statistics
253 .getNumberOfCurrentUsers(), 0);
254
255 int pages = this.countPages();
256 int users = this.countUsers();
257 int portlets = this.countPortlets();
258 assertEquals("User Log count incorrect ", 2 * x, users);
259 assertEquals("Portlet Log count incorrect ", x, portlets);
260 assertEquals("Page Log count incorrect ", x, pages);
261
262 }
263
264
265 public void testQuerySystem() throws Exception
266 {
267 System.out.println("testing Query System");
268 StatisticsQueryCriteria sqc = new StatisticsQueryCriteriaImpl();
269 sqc.setQueryType(PortalStatistics.QUERY_TYPE_USER);
270 int desired = 5;
271 sqc.setListsize(""+desired);
272 sqc.setSorttype("count");
273 sqc.setSortorder("desc");
274 AggregateStatistics as = statistics.queryStatistics(sqc);
275 assertNotNull(as);
276 System.out.println("user = " + as);
277 int size = as.getStatlist().size();
278 assertTrue( (size <=desired));
279
280 sqc.setQueryType(PortalStatistics.QUERY_TYPE_PORTLET);
281 sqc.setListsize(""+desired);
282 sqc.setSorttype("count");
283 sqc.setSortorder("desc");
284 as = statistics.queryStatistics(sqc);
285 assertNotNull(as);
286 System.out.println("portlet = " + as);
287 size = as.getStatlist().size();
288 assertTrue( (size <=desired));
289
290 sqc.setQueryType(PortalStatistics.QUERY_TYPE_PAGE);
291 sqc.setListsize(""+desired);
292 sqc.setSorttype("count");
293 sqc.setSortorder("desc");
294 as = statistics.queryStatistics(sqc);
295 assertNotNull(as);
296 System.out.println("page = " + as);
297 size = as.getStatlist().size();
298 assertTrue( (size <=desired));
299
300 }
301
302 private RequestContext initRequestContext()
303 {
304 MockHttpServletRequest request = new MockHttpServletRequest();
305 MockHttpServletResponse response = new MockHttpServletResponse();
306 MockHttpSession session = new MockHttpSession();
307
308
309 Principal p = new UserPrincipalImpl(USERNAME);
310
311 request.setUserPrincipal(p);
312
313 request.setRemoteAddr("123.234.145.156");
314 request.setSession(session);
315 request.setServerName("www.sporteportal.com");
316 request.setScheme("http");
317 request.setContextPath("/jetspeed");
318 request.setServletPath("/portal");
319 request.setPathInfo("/news/default-page.psml");
320 request.setRequestURI("/jetspeed/portal/news/default-page.psml");
321 request.setMethod("GET");
322 RequestContext rc = new MockRequestContext(request, response);
323 return rc;
324 }
325
326 protected String[] getConfigurations()
327 {
328 return new String[]
329 { "statistics.xml", "transaction.xml", "boot/datasource.xml"};
330 }
331
332 protected String[] getBootConfigurations()
333 {
334 return new String[]
335 { "boot/datasource.xml"};
336 }
337
338 }