View Javadoc

1   package org.e2etrace.trace;
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.trace.DefaultTraceStepFactory;
20  import org.e2etrace.trace.ITraceStep;
21  import org.e2etrace.trace.SimpleTraceStepId;
22  
23  import junit.framework.TestCase;
24  
25  /**
26   * JUnit testcase for {@link org.e2etrace.trace.DefaultTraceStep}
27   *
28   * All tests use {@link org.e2etrace.trace.MockTimer} to fake the measurement of
29   * execution times.
30   * <p>
31   *
32   * @author Gunther Popp
33   *
34   */
35  public class DefaultTraceStepTest extends TestCase {
36  
37    public static void main(String[] args) {
38      junit.textui.TestRunner.run(DefaultTraceStepTest.class);
39    }
40  
41    /**
42     * Tests constructor/getId.
43     */
44    public void testGetId() {
45      DefaultTraceStepFactory factory;
46      factory = new DefaultTraceStepFactory();
47      ITraceStep ts = factory.newInstance(new SimpleTraceStepId("1"));
48  
49      assertEquals(new SimpleTraceStepId("1"), ts.getId());
50    }
51  
52    /**
53     * Tests enter/leave
54     *
55     * @throws InterruptedException
56     */
57    public void testEnterLeave() throws InterruptedException {
58      DefaultTraceStepFactory factory;
59      ITraceStep ts;
60      long duration;
61      
62      factory = new DefaultTraceStepFactory(new MockTimerFactory(new long[] { 10 }));
63      ts = factory.newInstance(new SimpleTraceStepId("1"));
64  
65      // OK: Test time measuring
66      ts.enter();
67      ts.leave();
68  
69      duration = ts.getIsolatedDuration();
70  
71      assertEquals(10, duration);
72  
73      // OK: Repeated calls to enter() should be handled gracefully
74      ts = factory.newInstance(new SimpleTraceStepId("1"));
75      ts.enter();
76      ts.enter();
77  
78      // OK: Calls to leave() without enter() should be handled gracefully
79      ts = factory.newInstance(new SimpleTraceStepId("2"));
80      ts.leave();
81  
82    }
83  
84    /**
85     * Tests addChild/getChildren/getAccumulatedDuration/getParent
86     */
87    public void testAddChildEtc() {
88      DefaultTraceStepFactory factory;
89      ITraceStep ts;
90      ITraceStep tsChild1;
91      ITraceStep tsChild1_1;
92      ITraceStep tsChild2;
93      ITraceStep[] children;
94  
95      factory = new DefaultTraceStepFactory(new MockTimerFactory(
96          new long[] { 35, 20, 10, 5 }));
97  
98      ts = factory.newInstance(new SimpleTraceStepId("root"));
99      tsChild1 = factory.newInstance(new SimpleTraceStepId("1"));
100     tsChild1_1 = factory.newInstance(new SimpleTraceStepId("1_1"));
101     tsChild2 = factory.newInstance(new SimpleTraceStepId("2"));
102 
103     // OK: Check for empty list of children
104     assertEquals(0, ts.getChildren().length);
105 
106     // OK: Add valid children
107     tsChild1.addChild(tsChild1_1);
108 
109     ts.addChild(tsChild1);
110     ts.addChild(tsChild2);
111 
112     children = ts.getChildren();
113 
114     assertEquals(2, children.length);
115     assertEquals(new SimpleTraceStepId("1"), children[0].getId());
116     assertEquals(new SimpleTraceStepId("2"), children[1].getId());
117 
118     assertEquals(new SimpleTraceStepId("1_1"), (children[0].getChildren())[0].getId());
119 
120     // OK: Add invalid child
121     ts.addChild(null);
122 
123     // OK: Get accumulated duration
124     // Measure execution times
125     ts.enter();
126     tsChild1.enter();
127     tsChild1_1.enter();
128     tsChild2.enter();
129     tsChild2.leave();
130     tsChild1_1.leave();
131     tsChild1.leave();
132     ts.leave();
133 
134     assertEquals(35, ts.getDuration());
135 
136     // OK: Get isolated durations
137     assertEquals(35 - 20 - 5, ts.getIsolatedDuration());
138     assertEquals(20 - 10, tsChild1.getIsolatedDuration());
139     assertEquals(10, tsChild1_1.getIsolatedDuration());
140     assertEquals(5, tsChild2.getIsolatedDuration());
141 
142     // OK: Try to add an existing child again
143     ts.addChild(tsChild2);
144     assertEquals(35, ts.getDuration());
145 
146     // OK: Verify parents
147     assertEquals(new SimpleTraceStepId("root"), children[0].getParent().getId());
148     assertEquals(new SimpleTraceStepId("root"), children[1].getParent().getId());
149 
150     assertEquals(new SimpleTraceStepId("1"), (children[0].getChildren())[0].getParent()
151         .getId());
152   }
153 
154   /**
155    * Tests isActive
156    */
157   public void testIsActive() {
158     DefaultTraceStepFactory factory;
159     factory = new DefaultTraceStepFactory();
160     ITraceStep ts = factory.newInstance(new SimpleTraceStepId("1"));
161 
162     assertFalse(ts.isActive());
163     ts.enter();
164     assertTrue(ts.isActive());
165     ts.leave();
166     assertFalse(ts.isActive());
167 
168   }
169 
170   /**
171    * Tests equals
172    */
173   public void testEquals() {
174     DefaultTraceStepFactory factory;
175 
176     factory = new DefaultTraceStepFactory(new MockTimerFactory(new long[] { 1, 2, 3 }));
177     ITraceStep ts1 = factory.newInstance(new SimpleTraceStepId("1"));
178     ITraceStep ts2 = factory.newInstance(new SimpleTraceStepId("2"));
179     ITraceStep ts3 = factory.newInstance(new SimpleTraceStepId("1"));
180 
181     // Assign the execution times of the MockTimerFactory to the three steps
182     ts1.enter();
183     ts1.leave();
184     ts2.enter();
185     ts2.leave();
186     ts3.enter();
187     ts3.leave();
188 
189     // equals is supposed to compare _only_ the ids of trace steps. Hence,
190     // ts1 and ts3 are equals, regardless of the different execution times
191     assertTrue(ts1.equals(ts3));
192     assertFalse(ts1.equals(ts2));
193 
194   }
195 }