1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.tests
.component
;
15 import java
.io
.IOException
;
16 import java
.util
.Vector
;
18 import junit
.framework
.TestCase
;
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
;
32 * <b><u>TmfClientTest</u></b>
34 * TODO: Implement me. Please.
36 public class TmfClientTest
extends TestCase
{
38 TmfEventProviderStub fEventProvider
;
39 TmfSyntheticEventProviderStub fSyntheticEventProvider
;
41 public TmfClientTest(String name
) throws IOException
{
46 protected void setUp() throws Exception
{
48 fEventProvider
= new TmfEventProviderStub();
49 fSyntheticEventProvider
= new TmfSyntheticEventProviderStub();
53 protected void tearDown() throws Exception
{
55 fEventProvider
.deregister();
56 fSyntheticEventProvider
.deregister();
59 // ------------------------------------------------------------------------
60 // getProviders (more a sanity check than a test)
61 // ------------------------------------------------------------------------
63 @SuppressWarnings("unchecked")
64 public void testGetProviders() {
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);
70 eventProviders
= (TmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfTraceStub
.class);
71 assertTrue(eventProviders
.length
== 1);
73 eventProviders
= (TmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfEventProviderStub
.class);
74 assertTrue(eventProviders
.length
== 1);
76 // There should be 1 TmfSyntheticEventStub provider
77 eventProviders
= (TmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfSyntheticEventStub
.class);
78 assertTrue(eventProviders
.length
== 1);
81 // ------------------------------------------------------------------------
83 // ------------------------------------------------------------------------
85 @SuppressWarnings("unchecked")
86 public void testGetPlainEvents() {
88 final int BLOCK_SIZE
= 100;
89 final int NB_EVENTS
= 1000;
90 final Vector
<TmfEvent
> requestedEvents
= new Vector
<TmfEvent
>();
92 // Get the TmfSyntheticEventStub provider
93 TmfDataProvider
<TmfEvent
>[] eventProviders
= (TmfDataProvider
<TmfEvent
>[]) TmfProviderManager
.getProviders(TmfEvent
.class, TmfEventProviderStub
.class);
94 TmfDataProvider
<TmfEvent
> provider
= eventProviders
[0];
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
) {
100 public void handleData() {
101 TmfEvent
[] events
= getData();
102 for (TmfEvent e
: events
) {
103 requestedEvents
.add(e
);
107 provider
.processRequest(request
, true);
109 assertEquals("nbEvents", NB_EVENTS
, requestedEvents
.size());
110 assertTrue("isCompleted", request
.isCompleted());
111 assertFalse("isCancelled", request
.isCancelled());
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());
120 @SuppressWarnings("unchecked")
121 private void getSyntheticData(final TmfTimeRange range
, final int nbEvents
, final int blockSize
) {
123 final Vector
<TmfSyntheticEventStub
> requestedEvents
= new Vector
<TmfSyntheticEventStub
>();
125 // Get the event provider
126 TmfDataProvider
<TmfSyntheticEventStub
>[] eventProviders
= (TmfDataProvider
<TmfSyntheticEventStub
>[]) TmfProviderManager
.getProviders(TmfSyntheticEventStub
.class);
127 TmfDataProvider
<TmfSyntheticEventStub
> provider
= eventProviders
[0];
129 final TmfEventRequest
<TmfSyntheticEventStub
> request
=
130 new TmfEventRequest
<TmfSyntheticEventStub
>(TmfSyntheticEventStub
.class, range
, nbEvents
, blockSize
) {
132 public void handleData() {
133 TmfSyntheticEventStub
[] events
= getData();
134 for (TmfSyntheticEventStub e
: events
) {
135 requestedEvents
.add(e
);
139 provider
.processRequest(request
, true);
142 assertEquals("nbEvents", nbEvents
, requestedEvents
.size());
143 assertTrue("isCompleted", request
.isCompleted());
144 assertFalse("isCancelled", request
.isCancelled());
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());
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
);
162 public void testGetSyntheticEvents_SmallerBlock() {
163 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
164 getSyntheticData(range
, 1000, TmfSyntheticEventProviderStub
.BLOCK_SIZE
/ 2);
167 public void testGetSyntheticEvents_LargerBlock() {
168 TmfTimeRange range
= new TmfTimeRange(TmfTimestamp
.BigBang
, TmfTimestamp
.BigCrunch
);
169 getSyntheticData(range
, 1000, TmfSyntheticEventProviderStub
.BLOCK_SIZE
* 2);
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
);
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
);
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
);