Commit | Line | Data |
---|---|---|
fc6ccf6f FC |
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.TmfDataProvider; | |
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> | |
34 | * TODO: Implement me. Please. | |
35 | */ | |
36 | public class TmfClientTest extends TestCase { | |
37 | ||
38 | TmfEventProviderStub fEventProvider; | |
39 | TmfSyntheticEventProviderStub fSyntheticEventProvider; | |
40 | ||
41 | public TmfClientTest(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(); | |
55 | fEventProvider.deregister(); | |
56 | fSyntheticEventProvider.deregister(); | |
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 | TmfDataProvider<TmfEvent>[] eventProviders = (TmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class); | |
68 | assertTrue(eventProviders.length == 2); | |
69 | ||
70 | eventProviders = (TmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfTraceStub.class); | |
71 | assertTrue(eventProviders.length == 1); | |
72 | ||
73 | eventProviders = (TmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); | |
74 | assertTrue(eventProviders.length == 1); | |
75 | ||
76 | // There should be 1 TmfSyntheticEventStub provider | |
77 | eventProviders = (TmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfSyntheticEventStub.class); | |
78 | assertTrue(eventProviders.length == 1); | |
79 | } | |
80 | ||
81 | // ------------------------------------------------------------------------ | |
82 | // getSyntheticEvent | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | @SuppressWarnings("unchecked") | |
86 | public void testGetPlainEvents() { | |
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 | TmfDataProvider<TmfEvent>[] eventProviders = (TmfDataProvider<TmfEvent>[]) TmfProviderManager.getProviders(TmfEvent.class, TmfEventProviderStub.class); | |
94 | TmfDataProvider<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) { | |
99 | @Override | |
100 | public void handleData() { | |
101 | TmfEvent[] events = getData(); | |
102 | for (TmfEvent e : events) { | |
103 | requestedEvents.add(e); | |
104 | } | |
105 | } | |
106 | }; | |
107 | provider.processRequest(request, true); | |
108 | ||
109 | assertEquals("nbEvents", NB_EVENTS, requestedEvents.size()); | |
110 | assertTrue("isCompleted", request.isCompleted()); | |
111 | assertFalse("isCancelled", request.isCancelled()); | |
112 | ||
113 | // Make that we have distinct events. | |
114 | // Don't go overboard: we are not validating the stub! | |
115 | for (int i = 0; i < NB_EVENTS; i++) { | |
116 | assertEquals("Distinct events", i+1, requestedEvents.get(i).getTimestamp().getValue()); | |
117 | } | |
118 | } | |
119 | ||
120 | @SuppressWarnings("unchecked") | |
121 | private void getSyntheticData(final TmfTimeRange range, final int nbEvents, final int blockSize) { | |
122 | ||
123 | final Vector<TmfSyntheticEventStub> requestedEvents = new Vector<TmfSyntheticEventStub>(); | |
124 | ||
125 | // Get the event provider | |
126 | TmfDataProvider<TmfSyntheticEventStub>[] eventProviders = (TmfDataProvider<TmfSyntheticEventStub>[]) TmfProviderManager.getProviders(TmfSyntheticEventStub.class); | |
127 | TmfDataProvider<TmfSyntheticEventStub> provider = eventProviders[0]; | |
128 | ||
129 | final TmfEventRequest<TmfSyntheticEventStub> request = | |
130 | new TmfEventRequest<TmfSyntheticEventStub>(TmfSyntheticEventStub.class, range, nbEvents, blockSize) { | |
131 | @Override | |
132 | public void handleData() { | |
133 | TmfSyntheticEventStub[] events = getData(); | |
134 | for (TmfSyntheticEventStub e : events) { | |
135 | requestedEvents.add(e); | |
136 | } | |
137 | } | |
138 | }; | |
139 | provider.processRequest(request, true); | |
140 | ||
141 | if (nbEvents != -1) | |
142 | assertEquals("nbEvents", nbEvents, requestedEvents.size()); | |
143 | assertTrue("isCompleted", request.isCompleted()); | |
144 | assertFalse("isCancelled", request.isCancelled()); | |
145 | ||
146 | // For each base event, the stub will queue 2 identical synthetic events | |
147 | // Ensure that the events are queued properly. | |
148 | // Don't go overboard: we are not validating the stub! | |
149 | for (int i = 0; i < (nbEvents / 2); i++) { | |
150 | assertEquals("Distinct events", i+1, requestedEvents.get(2 * i + 0).getTimestamp().getValue()); | |
151 | assertEquals("Distinct events", i+1, requestedEvents.get(2 * i + 1).getTimestamp().getValue()); | |
152 | } | |
153 | } | |
154 | ||
155 | // The following tests are the same but for the size of the requested blocks | |
156 | // with regards to the size of the TmfSyntheticEventProviderStub block | |
157 | public void testGetSyntheticEvents_EqualBlockSizes() { | |
158 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
159 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
160 | } | |
161 | ||
162 | public void testGetSyntheticEvents_SmallerBlock() { | |
163 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
164 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE / 2); | |
165 | } | |
166 | ||
167 | public void testGetSyntheticEvents_LargerBlock() { | |
168 | TmfTimeRange range = new TmfTimeRange(TmfTimestamp.BigBang, TmfTimestamp.BigCrunch); | |
169 | getSyntheticData(range, 1000, TmfSyntheticEventProviderStub.BLOCK_SIZE * 2); | |
170 | } | |
171 | ||
172 | public void testGetSyntheticEvents_TimeRange() { | |
173 | TmfTimestamp start = new TmfTimestamp( 1, (byte) -3, 0); | |
174 | TmfTimestamp end = new TmfTimestamp(1000, (byte) -3, 0); | |
175 | TmfTimeRange range = new TmfTimeRange(start, end); | |
176 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
177 | } | |
178 | ||
179 | public void testGetSyntheticEvents_WeirdTimeRange1() { | |
180 | TmfTimestamp start = TmfTimestamp.BigBang; | |
181 | TmfTimestamp end = new TmfTimestamp(0, (byte) -3, 0); | |
182 | TmfTimeRange range = new TmfTimeRange(start, end); | |
183 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
184 | } | |
185 | ||
186 | public void testGetSyntheticEvents_WeirdTimeRange2() { | |
187 | TmfTimestamp start = new TmfTimestamp(0, (byte) -3, 0); | |
188 | TmfTimestamp end = TmfTimestamp.BigCrunch; | |
189 | TmfTimeRange range = new TmfTimeRange(start, end); | |
190 | getSyntheticData(range, -1, TmfSyntheticEventProviderStub.BLOCK_SIZE); | |
191 | } | |
192 | ||
193 | } |