Commit | Line | Data |
---|---|---|
d18dd09b ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.tmf.tests.component; | |
14 | ||
15 | import java.io.IOException; | |
16 | import java.util.Vector; | |
17 | ||
18 | import junit.framework.TestCase; | |
19 | ||
20 | import org.eclipse.linuxtools.tmf.component.ITmfDataProvider; | |
21 | import org.eclipse.linuxtools.tmf.component.TmfEventProviderStub; | |
22 | import org.eclipse.linuxtools.tmf.component.TmfProviderManager; | |
23 | import org.eclipse.linuxtools.tmf.component.TmfSyntheticEventProviderStub; | |
24 | import org.eclipse.linuxtools.tmf.event.TmfEvent; | |
25 | import org.eclipse.linuxtools.tmf.event.TmfSyntheticEventStub; | |
26 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
27 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
28 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; | |
29 | import org.eclipse.linuxtools.tmf.trace.TmfTraceStub; | |
30 | ||
31 | /** | |
32 | * <b><u>TmfClientTest</u></b> | |
33 | * <p> | |
ff4ed569 | 34 | * Test suite for the TmfEventProvider class. |
d18dd09b ASL |
35 | */ |
36 | public class TmfEventProviderTest extends TestCase { | |
37 | ||
38 | TmfEventProviderStub fEventProvider; | |
39 | TmfSyntheticEventProviderStub fSyntheticEventProvider; | |
40 | ||
41 | public TmfEventProviderTest(String name) throws IOException { | |
42 | super(name); | |
43 | } | |
44 | ||
45 | @Override | |
46 | protected void setUp() throws Exception { | |
47 | super.setUp(); | |
48 | fEventProvider = new TmfEventProviderStub(); | |
49 | fSyntheticEventProvider = new TmfSyntheticEventProviderStub(); | |
50 | } | |
51 | ||
52 | @Override | |
53 | protected void tearDown() throws Exception { | |
54 | super.tearDown(); | |
2fb2eb37 FC |
55 | fEventProvider.dispose(); |
56 | fSyntheticEventProvider.dispose(); | |
d18dd09b ASL |
57 | } |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // getProviders (more a sanity check than a test) | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | @SuppressWarnings("unchecked") | |
64 | public void testGetProviders() { | |
65 | ||
66 | // There should be 2 TmfEvent providers: a TmfTraceStub and a TmfEventProviderStub | |
67 | ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class); | |
ff4ed569 | 68 | assertEquals("getProviders", 2, eventProviders.length); |
d18dd09b ASL |
69 | |
70 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); | |
ff4ed569 | 71 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
72 | |
73 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); | |
ff4ed569 | 74 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
75 | |
76 | // There should be 1 TmfSyntheticEventStub provider | |
77 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfSyntheticEventStub.class); | |
ff4ed569 | 78 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
79 | } |
80 | ||
81 | // ------------------------------------------------------------------------ | |
82 | // getSyntheticEvent | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | @SuppressWarnings("unchecked") | |
cb866e08 | 86 | public void testGetPlainEvents() { |
d18dd09b ASL |
87 | |
88 | final int BLOCK_SIZE = 100; | |
89 | final int NB_EVENTS = 1000; | |
90 | final Vector<TmfEvent> requestedEvents = new Vector<TmfEvent>(); | |
91 | ||
92 | // Get the TmfSyntheticEventStub provider | |
93 | ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); | |
94 | ITmfDataProvider<TmfEvent> provider = eventProviders[0]; | |
95 | ||
96 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
97 | final TmfEventRequest<TmfEvent> request = | |
98 | new TmfEventRequest<TmfEvent>(TmfEvent.class, range, NB_EVENTS, BLOCK_SIZE) { | |
f9673903 FC |
99 | @Override |
100 | public void handleData(TmfEvent event) { | |
101 | super.handleData(event); | |
102 | requestedEvents.add(event); | |
103 | } | |
104 | }; | |
d18dd09b | 105 | provider.sendRequest(request); |
cb866e08 FC |
106 | try { |
107 | request.waitForCompletion(); | |
108 | assertEquals("nbEvents", NB_EVENTS, requestedEvents.size()); | |
109 | assertTrue("isCompleted", request.isCompleted()); | |
110 | assertFalse("isCancelled", request.isCancelled()); | |
111 | ||
112 | // Make that we have distinct events. | |
113 | // Don't go overboard: we are not validating the stub! | |
114 | for (int i = 0; i < NB_EVENTS; i++) { | |
115 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); | |
116 | } | |
117 | } catch (InterruptedException e) { | |
118 | fail(); | |
119 | } | |
d18dd09b ASL |
120 | } |
121 | ||
122 | @SuppressWarnings("unchecked") | |
2fb2eb37 | 123 | private void getSyntheticData(final TmfTimeRange range, final int nbEvents, final int blockSize) throws InterruptedException { |
d18dd09b ASL |
124 | |
125 | final Vector<TmfSyntheticEventStub> requestedEvents = new Vector<TmfSyntheticEventStub>(); | |
126 | ||
127 | // Get the event provider | |
128 | ITmfDataProvider<TmfSyntheticEventStub>[] eventProviders = (ITmfDataProvider<TmfSyntheticEventStub>[]) TmfProviderManager.getProviders(TmfSyntheticEventStub.class); | |
129 | ITmfDataProvider<TmfSyntheticEventStub> provider = eventProviders[0]; | |
130 | ||
131 | final TmfEventRequest<TmfSyntheticEventStub> request = | |
132 | new TmfEventRequest<TmfSyntheticEventStub>(TmfSyntheticEventStub.class, range, nbEvents, blockSize) { | |
f9673903 FC |
133 | @Override |
134 | public void handleData(TmfSyntheticEventStub event) { | |
135 | super.handleData(event); | |
136 | requestedEvents.add(event); | |
137 | } | |
d18dd09b ASL |
138 | }; |
139 | provider.sendRequest(request); | |
d18dd09b | 140 | |
cb866e08 FC |
141 | request.waitForCompletion(); |
142 | if (nbEvents != -1) | |
143 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); | |
144 | assertTrue("isCompleted", request.isCompleted()); | |
145 | assertFalse("isCancelled", request.isCancelled()); | |
146 | ||
147 | // For each base event, the stub will queue 2 identical synthetic events | |
148 | // Ensure that the events are queued properly. | |
149 | // Don't go overboard: we are not validating the stub! | |
150 | for (int i = 0; i < (nbEvents / 2); i++) { | |
151 | assertEquals("Distinct events", i+1, requestedEvents.get(2 * i + 0).getTimestamp().getValue()); | |
152 | assertEquals("Distinct events", i+1, requestedEvents.get(2 * i + 1).getTimestamp().getValue()); | |
153 | } | |
d18dd09b ASL |
154 | } |
155 | ||
156 | // The following tests are the same but for the size of the requested blocks | |
157 | // with regards to the size of the TmfSyntheticEventProviderStub block | |
cb866e08 | 158 | public void testGetSyntheticEvents_EqualBlockSizes() { |
d18dd09b | 159 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
cb866e08 FC |
160 | try { |
161 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
162 | } catch (InterruptedException e) { | |
163 | fail(); | |
164 | } | |
d18dd09b ASL |
165 | } |
166 | ||
cb866e08 | 167 | public void testGetSyntheticEvents_SmallerBlock() { |
d18dd09b | 168 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
cb866e08 FC |
169 | try { |
170 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE / 2); | |
171 | } catch (InterruptedException e) { | |
172 | fail(); | |
173 | } | |
d18dd09b ASL |
174 | } |
175 | ||
cb866e08 | 176 | public void testGetSyntheticEvents_LargerBlock() { |
d18dd09b | 177 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); |
cb866e08 FC |
178 | try { |
179 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE * 2); | |
180 | } catch (InterruptedException e) { | |
181 | fail(); | |
182 | } | |
d18dd09b ASL |
183 | } |
184 | ||
cb866e08 | 185 | public void testGetSyntheticEvents_TimeRange() { |
d18dd09b ASL |
186 | TmfTimestamp start = new TmfTimestamp( 1, (byte) -3, 0); |
187 | TmfTimestamp end = new TmfTimestamp(1000, (byte) -3, 0); | |
188 | TmfTimeRange range = new TmfTimeRange(start, end); | |
cb866e08 FC |
189 | try { |
190 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
191 | } catch (InterruptedException e) { | |
192 | fail(); | |
193 | } | |
d18dd09b ASL |
194 | } |
195 | ||
cb866e08 | 196 | public void testGetSyntheticEvents_WeirdTimeRange1() { |
d18dd09b | 197 | TmfTimestamp start = TmfTimestamp.BigBang; |
9b635e61 | 198 | TmfTimestamp end = TmfTimestamp.Zero; // new TmfTimestamp(0, (byte) -3, 0); |
d18dd09b | 199 | TmfTimeRange range = new TmfTimeRange(start, end); |
cb866e08 FC |
200 | try { |
201 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
202 | } catch (InterruptedException e) { | |
203 | fail(); | |
204 | } | |
d18dd09b ASL |
205 | } |
206 | ||
cb866e08 | 207 | public void testGetSyntheticEvents_WeirdTimeRange2() { |
9b635e61 | 208 | TmfTimestamp start = TmfTimestamp.Zero; // new TmfTimestamp(0, (byte) -3, 0); |
d18dd09b ASL |
209 | TmfTimestamp end = TmfTimestamp.BigCrunch; |
210 | TmfTimeRange range = new TmfTimeRange(start, end); | |
cb866e08 FC |
211 | try { |
212 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
213 | } catch (InterruptedException e) { | |
214 | fail(); | |
215 | } | |
d18dd09b ASL |
216 | } |
217 | ||
218 | // ------------------------------------------------------------------------ | |
219 | // getProviders (more a sanity check than a test) | |
220 | // ------------------------------------------------------------------------ | |
221 | ||
222 | @SuppressWarnings("unchecked") | |
223 | public void testGetProviders2() { | |
224 | ||
225 | // There should be 2 TmfEvent providers: a TmfTraceStub and a TmfEventProviderStub | |
226 | ITmfDataProvider<TmfEvent>[] eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class); | |
ff4ed569 | 227 | assertEquals("getProviders", 2, eventProviders.length); |
d18dd09b ASL |
228 | |
229 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); | |
ff4ed569 | 230 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
231 | |
232 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); | |
ff4ed569 | 233 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
234 | |
235 | // There should be 1 TmfSyntheticEventStub provider | |
236 | eventProviders = (ITmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfSyntheticEventStub.class); | |
ff4ed569 | 237 | assertEquals("getProviders", 1, eventProviders.length); |
d18dd09b ASL |
238 | } |
239 | ||
240 | } |