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: */package org.apache.geronimo.jetty6;
017:
018: import java.io.BufferedReader;
019: import java.io.File;
020: import java.io.InputStreamReader;
021: import java.net.HttpURLConnection;
022: import java.net.URL;
023:
024: import javax.management.ObjectName;
025: import javax.management.j2ee.statistics.Statistic;
026: import javax.management.j2ee.statistics.Stats;
027: import org.apache.geronimo.management.LazyStatisticsProvider;
028:
029: /**
030: * @version $Rev: 596514 $ $Date: 2007-11-19 17:56:22 -0800 (Mon, 19 Nov 2007) $
031: */
032: public class StatTest extends AbstractWebModuleTest {
033:
034: private ObjectName webModuleName;
035:
036: public void testContainerStats() throws Exception {
037: statsTest(container);
038: }
039:
040: public void testConnectorStats() throws Exception {
041: statsTest(connector);
042: }
043:
044: public void statsTest(LazyStatisticsProvider component)
045: throws Exception {
046: // start statistics collection
047: if (component instanceof LazyStatisticsProvider) {
048: assertTrue("Stats should be off initially", !component
049: .isStatsOn());
050: component.setStatsOn(true);
051: }
052: int n = 4; // no of connections
053: for (int k = 0; k < n; k++) {
054: HttpURLConnection connection = (HttpURLConnection) new URL(
055: connector.getConnectUrl() + "/test/hello.txt")
056: .openConnection();
057: BufferedReader reader = new BufferedReader(
058: new InputStreamReader(connection.getInputStream()));
059: assertEquals(HttpURLConnection.HTTP_OK, connection
060: .getResponseCode());
061: assertEquals("Hello World", reader.readLine());
062:
063: Stats stats = component.getStats();
064: Statistic[] stts = stats.getStatistics();
065: Statistic aStts;
066: String[] sttsNames = stats.getStatisticNames();
067: for (int i = 0; i < sttsNames.length; i++) {
068: // check that the names match the getter methods
069: String sttsName = sttsNames[i];
070: assertFalse(sttsName.equals(stts[i].getName()));
071: try {
072: stats.getClass().getMethod("get" + sttsName,
073: new Class[0]);
074: } catch (NoSuchMethodException e) {
075: continue; // ignore this statistics for now, JSR77.6.10.1.1
076: }
077: aStts = stats.getStatistic(sttsName);
078: assertTrue("startTime was not set for " + sttsName,
079: aStts.getStartTime() != 0);
080: assertTrue(
081: "lastSampleTime was not set for " + sttsName,
082: aStts.getLastSampleTime() != 0);
083: /*System.out.println(" lastSampleTime = " + aStts.getLastSampleTime() +
084: " startTime = " + aStts.getStartTime());
085: System.out.println(aStts);*/
086: }
087: if (k == n - 2)
088: component.resetStats(); // test reset
089: connection.disconnect();
090: Thread.sleep(1000); // connection interval
091: }
092: }
093:
094: protected void setUp() throws Exception {
095: super .setUp();
096: JettyWebAppContext app;
097: app = setUpAppContext(null, null, null, null, null, null, null,
098: "war1/");
099: setUpStaticContentServlet(app);
100: }
101: }
|