1 package org.e2etrace.trace;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
35
36
37
38
39
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
53
54 public void testGetCurrentSession() {
55 ITraceSession ts = new DefaultTraceSession("testGetCurrentSession",
56 new DefaultTraceStepFactory());
57
58 tsm.setCurrentSession(ts);
59
60
61 assertTrue("getCurrentSession liefert falsche Referenz zurück", tsm
62 .getCurrentSession() == ts);
63 }
64
65
66
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
91 assertEquals(0, tsm.getCurrentSession().getDuration());
92 assertNull(tsm.getCurrentSession().getCurrentStep());
93 assertNull(tsm.getCurrentSession().getRootStep());
94
95 }
96
97
98
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
118
119 public void testSetCurrentSessionWithNoopTraceSession() {
120 ITraceSession ts = new NoopTraceSession();
121
122 tsm.setCurrentSession(ts);
123
124
125 assertTrue("getCurrentSession liefert falsche Referenz zurück", tsm
126 .getCurrentSession() instanceof NoopTraceSession );
127
128
129 }
130
131
132
133
134
135
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
152 protected void tearDown() throws Exception {
153 super.tearDown();
154
155
156 tsm.setConfig(new DefaultTraceConfig());
157 tsm.releaseCurrentSession();
158
159 }
160
161 }