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.ExactTimer;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * JUnit testcase for {@link org.e2etrace.timer.DefaultTimerFactory}
27   *
28   * @author Gunther Popp
29   *
30   */
31  public class DefaultTimerFactoryTest extends TestCase {
32  
33    public static void main(String[] args) {
34      junit.textui.TestRunner.run(DefaultTimerFactoryTest.class);
35    }
36  
37    /**
38     * Tests the methods newInstance/setTimerType/getTimerType and the different
39     * constructors
40     */
41    public void testNewInstance() {
42      DefaultTimerFactory tf = new DefaultTimerFactory();
43  
44      // OK: Default constructor
45      // Note: The default constructor returns different Timer classes depending
46      // on the JDK version.
47      float specVersion = new Float(System.getProperty("java.specification.version"))
48          .floatValue();
49  
50      if (specVersion < 1.5) {
51        assertEquals(DefaultTimer.class, tf.getTimerType());
52        assertEquals(DefaultTimer.class, tf.newInstance().getClass());
53      } else {
54        assertEquals(ExactTimer.class, tf.getTimerType());
55        assertEquals(ExactTimer.class, tf.newInstance().getClass());
56      }
57  
58      // OK: Override constructor
59      tf = new DefaultTimerFactory(ExactTimer.class);
60      assertEquals(ExactTimer.class, tf.getTimerType());
61      assertEquals(ExactTimer.class, tf.newInstance().getClass());
62  
63      // OK: Default constructor, set type explicitly
64      tf = new DefaultTimerFactory();
65      tf.setTimerType(ExactTimer.class);
66      assertEquals(ExactTimer.class, tf.getTimerType());
67      assertEquals(ExactTimer.class, tf.newInstance().getClass());
68  
69      // OK: Override constructor, set type explicitly
70      tf = new DefaultTimerFactory(ExactTimer.class);
71      tf.setTimerType(DefaultTimer.class);
72      assertEquals(DefaultTimer.class, tf.getTimerType());
73      assertEquals(DefaultTimer.class, tf.newInstance().getClass());
74    }
75  
76  }