View Javadoc

1   package org.e2etrace.timer;
2   
3   /*
4    * Copyright 2006 Gunther Popp
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.e2etrace.timer.DefaultTimerFactory;
20  import org.e2etrace.timer.ExactTimer;
21  import org.e2etrace.timer.ITimer;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * JUnit testcase for {@link org.e2etrace.timer.ExactTimer}
27   *
28   * @author Gunther Popp
29   *
30   */
31  public class ExactTimerTest extends TestCase {
32  
33    public static void main(String[] args) {
34      junit.textui.TestRunner.run(ExactTimerTest.class);
35    }
36  
37    /**
38     * Tests the methods start/stop/getDuration.
39     *
40     * @throws InterruptedException
41     */
42    public void testGetDuration() throws InterruptedException {
43      DefaultTimerFactory tf = new DefaultTimerFactory(ExactTimer.class);
44      ITimer timer;
45      long duration;
46  
47      // OK: Get Duration
48      // --------------------------------------------------------------
49      timer = tf.newInstance();
50  
51      assertEquals(timer.getClass(), ExactTimer.class);
52  
53      timer.start();
54      Thread.sleep(100);
55      duration = timer.measure();
56  
57      assertTrue("Expected duration: between 90ms and 110ms, measured duration: "
58          + duration + "ms", duration >= 90 && duration <= 110);
59  
60      // NOK: Invalid calls to start/measure
61      // ----------------------------------
62      timer = tf.newInstance();
63      assertEquals(-1, timer.measure());
64  
65    }
66  
67  }