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.DefaultTimer;
20  import org.e2etrace.timer.DefaultTimerFactory;
21  import org.e2etrace.timer.ITimer;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * JUnit testcase for {@link org.e2etrace.timer.DefaultTimer}
27   *
28   * @author Gunther Popp
29   *
30   */
31  public class DefaultTimerTest extends TestCase {
32  
33    public static void main(String[] args) {
34      junit.textui.TestRunner.run(DefaultTimerTest.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(DefaultTimer.class);
44      ITimer timer;
45  
46      // OK: Get Duration
47      // --------------------------------------------------------------
48      timer = tf.newInstance();
49  
50      assertEquals(timer.getClass(), DefaultTimer.class);
51  
52      timer.start();
53      Thread.sleep(100);
54  
55      assertTrue("Timer executed at least 100ms", timer.measure() >= 100);
56  
57      // NOK: Invalid calls to start/measure
58      // ----------------------------------
59      timer = tf.newInstance();
60      assertEquals(-1, timer.measure());
61  
62    }
63  
64  }