001: /*
002: * MCS Media Computer Software
003: * Copyright (c) 2005 by MCS
004: * --------------------------------------
005: * Created on 23.04.2005 by w.klaas
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: */
019: package de.mcs.jmeasurement.test;
020:
021: import de.mcs.jmeasurement.DefaultMonitor;
022: import de.mcs.jmeasurement.MeasurementException;
023: import de.mcs.jmeasurement.Monitor;
024:
025: /**
026: * This class will test the functionality of the <code>DefaultMonitor</code>
027: * Object.
028: *
029: * @author w.klaas
030: */
031: public class DefaultMonitorTest extends MCSTestCase {
032:
033: /** holding a private monitor */
034: private Monitor monitor;
035:
036: /** getting a monitor if don't exists */
037: private void init() {
038: if (null == monitor) {
039: monitor = new DefaultMonitor();
040: }
041: }
042:
043: public void testStart() throws InterruptedException,
044: MeasurementException {
045: init();
046: assertFalse(monitor.isRunning());
047: // starting the monitor
048: monitor.start();
049: Thread.sleep(100);
050: assertTrue(monitor.isRunning());
051: // stopping the monitor
052: monitor.stop();
053: assertFalse(monitor.isRunning());
054:
055: System.out.print("Monitor first 100 msec:");
056: System.out.println(monitor.getAccrued());
057:
058: // we allow precision of 10%
059: assertBetween(90, 110, monitor.getAccrued());
060:
061: // resetting the monitor
062: monitor.reset();
063:
064: // now the value must be zero
065: assertEquals(0, monitor.getAccrued());
066:
067: // starting monitor again
068: monitor.start();
069: Thread.sleep(100);
070: // pausing monitor
071: monitor.pause();
072: assertTrue(monitor.isRunning());
073: assertTrue(monitor.isPaused());
074: Thread.sleep(250);
075: // resuming monitor
076: monitor.resume();
077: assertFalse(monitor.isPaused());
078: Thread.sleep(100);
079: // stopping monitor
080: monitor.stop();
081: assertFalse(monitor.isRunning());
082: System.out.print("Monitor first 200 msec:");
083: System.out.println(monitor.getAccrued());
084:
085: // we allow precision of 10%
086: assertBetween(180, 220, monitor.getAccrued());
087: }
088:
089: public void testMonitorNotRunnig() throws MeasurementException {
090: init();
091: assertFalse(monitor.isRunning());
092:
093: boolean result = monitor.pause();
094: assertFalse("Monitor not Runnning ", result);
095:
096: result = monitor.resume();
097: assertFalse("Monitor not Runnning ", result);
098:
099: monitor.stop();
100: }
101:
102: public void testIncreaseDecrease() {
103: init();
104: assertFalse(monitor.isRunning());
105:
106: monitor.increase(736);
107: assertEquals(736, monitor.getAccrued());
108:
109: monitor.decrease(386);
110: assertEquals(736 - 386, monitor.getAccrued());
111:
112: }
113: }
|