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 java.io.IOException;
20  import java.io.StringWriter;
21  
22  import org.e2etrace.config.DefaultTraceConfig;
23  import org.e2etrace.formatter.ITraceFormatter;
24  import org.e2etrace.formatter.PlainTextTraceFormatter;
25  import org.e2etrace.trace.DefaultTraceSession;
26  import org.e2etrace.trace.DefaultTraceSessionManager;
27  import org.e2etrace.trace.DefaultTraceStepFactory;
28  import org.e2etrace.trace.ITraceSession;
29  import org.e2etrace.trace.SimpleTraceStepId;
30  
31  import junit.framework.TestCase;
32  
33  /**
34   * JUnit testcase for {@link org.e2etrace.trace.DefaultTraceSessionManager}
35   *
36   * <em>Caution:</em> The test erases any trace sessions of the session
37   * manager!
38   *
39   * @author Gunther Popp
40   *
41   */
42  public class DefaultTraceSessionManagerTest extends TestCase {
43  
44    private static final DefaultTraceSessionManager tsm = DefaultTraceSessionManager
45        .getInstance();
46  
47    public static void main(String[] args) {
48      junit.textui.TestRunner.run(DefaultTraceSessionManagerTest.class);
49    }
50  
51    /**
52     * Test getCurrentSession
53     */
54    public void testGetCurrentSession() {
55      ITraceSession ts = new DefaultTraceSession("testGetCurrentSession",
56          new DefaultTraceStepFactory());
57  
58      tsm.setCurrentSession(ts);
59  
60      // The following test intentionally compares the trace session references
61      assertTrue("getCurrentSession liefert falsche Referenz zurück", tsm
62          .getCurrentSession() == ts);
63    }
64  
65    /**
66     * Test disabling of trace sessions
67     */
68    public void testDisableSession() {
69      MockTraceConfig tc;
70      DefaultTraceSession ts;
71  
72      tc = new MockTraceConfig();
73      tc.setTraceEnabledForId(new SimpleTraceStepId("$testDisableSession"), false);
74  
75      ts = new DefaultTraceSession("testDisableSession", new DefaultTraceStepFactory(
76          new MockTimerFactory(new long[] { 60, 20 })));
77  
78      tsm.setConfig(tc);
79      tsm.setCurrentSession(ts);
80  
81      tsm.getCurrentSession().enterStep("1");
82      tsm.getCurrentSession().enterStep("2");
83      tsm.getCurrentSession().enterStep("3");
84      tsm.getCurrentSession().leaveStep("3");
85      tsm.getCurrentSession().leaveStep("2");
86      tsm.getCurrentSession().leaveStep("1");
87  
88      printTraceTree(tsm.getCurrentSession());
89  
90      // Verify, that no steps have been added to the disabled session
91      assertEquals(0, tsm.getCurrentSession().getDuration());
92      assertNull(tsm.getCurrentSession().getCurrentStep());
93      assertNull(tsm.getCurrentSession().getRootStep());
94  
95    }
96  
97    /**
98     * Test calling enterStep/leaveStep without defining a current trace session.
99     */
100   public void testCallsWithoutSession() {
101 
102     tsm.getCurrentSession().enterStep("1");
103     tsm.getCurrentSession().enterStep("2");
104     tsm.getCurrentSession().enterStep("3");
105     tsm.getCurrentSession().leaveStep("3");
106     tsm.getCurrentSession().leaveStep("2");
107     tsm.getCurrentSession().leaveStep("1");
108 
109     printTraceTree(tsm.getCurrentSession());
110 
111     assertEquals(0, tsm.getCurrentSession().getDuration());
112     assertNull(tsm.getCurrentSession().getCurrentStep());
113     assertNull(tsm.getCurrentSession().getRootStep());
114   }
115 
116   /**
117    * Test method setCurrentSession() usind a NoopTraceSession
118    */
119   public void testSetCurrentSessionWithNoopTraceSession() {   
120     ITraceSession ts = new NoopTraceSession();
121 
122     tsm.setCurrentSession(ts);
123 
124     // The following test intentionally compares the trace session references
125     assertTrue("getCurrentSession liefert falsche Referenz zurück", tsm
126         .getCurrentSession() instanceof NoopTraceSession );
127 
128     
129   }
130 
131   /**
132    * Helper method: Print a tree of trace steps to stdout.
133    * <p>
134    *
135    * @param ts
136    */
137   private void printTraceTree(ITraceSession ts) {
138     ITraceFormatter tf = new PlainTextTraceFormatter();
139     StringWriter testOutput = new StringWriter();
140 
141     try {
142       tf.format(ts, testOutput);
143     } catch (IOException e) {
144       e.printStackTrace();
145     }
146 
147     System.out.println(testOutput.toString());
148 
149   }
150 
151   /** {@inheritDoc} */
152   protected void tearDown() throws Exception {
153     super.tearDown();
154 
155     // Clean up
156     tsm.setConfig(new DefaultTraceConfig());
157     tsm.releaseCurrentSession();
158 
159   }
160 
161 }