01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */package org.apache.geronimo.tomcat;
17:
18: import java.io.BufferedReader;
19: import java.io.File;
20: import java.io.InputStreamReader;
21: import java.net.HttpURLConnection;
22: import java.net.URL;
23:
24: import javax.management.ObjectName;
25: import javax.management.j2ee.statistics.Statistic;
26: import javax.management.j2ee.statistics.Stats;
27:
28: /**
29: * @version $Revision: 598023 $ $Date: 2007-11-25 10:08:16 -0800 (Sun, 25 Nov 2007) $
30: */
31: public class StatTest extends AbstractWebModuleTest {
32:
33: private ObjectName webModuleName;
34:
35: public void testStats() throws Exception {
36: TomcatWebAppContext webModule;
37: webModule = setUpInsecureAppContext(
38: new File(BASEDIR,
39: "src/test/resources/deployables/war1/").toURI(),
40: new File(BASEDIR,
41: "src/test/resources/deployables/war1/WEB-INF/web.xml")
42: .toURL(), null, null, null, null);
43: HttpURLConnection connection = (HttpURLConnection) new URL(
44: connector.getConnectUrl() + "/test/hello.txt")
45: .openConnection();
46: BufferedReader reader = new BufferedReader(
47: new InputStreamReader(connection.getInputStream()));
48: assertEquals(HttpURLConnection.HTTP_OK, connection
49: .getResponseCode());
50: assertEquals("Hello World", reader.readLine());
51: //connection.disconnect();
52: // Stats stats = (Stats) kernel.getAttribute(webModuleName, "stats");
53:
54: int n = 3;
55: for (int k = 0; k < n; k++) {
56: if (k == n - 1) {
57: connector.resetStats();
58: webModule.resetStats();
59: }
60: //System.out.println("******* NOW IS " + System.currentTimeMillis());
61: Stats[] allStats = { webModule.getStats(),
62: connector.getStats() };
63: Stats stats;
64: for (int j = 0; j < allStats.length; j++) {
65: stats = allStats[j];
66: Statistic[] stts = stats.getStatistics();
67: Statistic aStts;
68: String[] sttsNames = stats.getStatisticNames();
69: for (int i = 0; i < sttsNames.length; i++) {
70: assertFalse(sttsNames[i].equals(stts[i].getName()));
71: aStts = stats.getStatistic(sttsNames[i]);
72: assertTrue("startTime was not set", aStts
73: .getStartTime() != 0);
74: assertTrue("lastSampleTime was not set", aStts
75: .getLastSampleTime() != 0);
76: //System.out.println("lastSampleTime = " + aStts.getLastSampleTime() + " startTime = " + aStts.getStartTime());
77: //System.out.println(aStts);
78: }
79: }
80: Thread.sleep(1000); // collection interval
81: }
82: connection.disconnect();
83: }
84:
85: protected void setUp() throws Exception {
86: super.setUp();
87: super.init(null);
88: }
89: }
|