1 package org
.eclipse
.linuxtools
.lttng
.core
.tests
.event
;
5 import java
.util
.HashMap
;
7 import junit
.framework
.TestCase
;
9 import org
.eclipse
.core
.runtime
.FileLocator
;
10 import org
.eclipse
.core
.runtime
.Path
;
11 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngEvent
;
12 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngEventContent
;
13 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngEventField
;
14 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngEventType
;
15 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.event
.LttngTimestamp
;
16 import org
.eclipse
.linuxtools
.internal
.lttng
.core
.trace
.LTTngTextTrace
;
17 import org
.eclipse
.linuxtools
.lttng
.core
.tests
.LTTngCoreTestPlugin
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
23 Functions tested here :
25 public LttngEventContent()
26 public LttngEventContent(LttngEvent thisParent)
27 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
28 public LttngEventContent(LttngEventContent oldContent)
30 public void emptyContent()
32 public LttngEventField[] getFields()
33 public LttngEventField getField(int position)
34 public LttngEventField getField(String name)
35 public LttngEvent getEvent()
36 public LttngEventType getType()
37 public Object[] getContent()
38 public HashMap<String, LttngEventField> getRawContent()
40 public void setType(LttngEventType newType)
41 public void setEvent(LttngEvent newParent)
43 public String toString()
46 @SuppressWarnings("nls")
47 public class LttngEventContentTest
extends TestCase
{
48 private final static String tracepath1
="traceset/trace-15316events_nolost_newformat.txt";
49 // private final static boolean skipIndexing=true;
51 private final static String firstEventContentFirstField
= "alignment:0";
52 private final static String firstEventContentFirstFieldName
= "alignment";
53 private final static String firstEventContentType
= "metadata/0/core_marker_id";
55 private final static String secondEventContentSecondField
= "string:LTT state dump begin";
56 private final static String secondEventContentSecondFieldName
= "string";
57 private final static String secondEventContentType
= "kernel/0/vprintk";
59 private final static long timestampAfterMetadata
= 13589760262237L;
61 private static LTTngTextTrace testStream
= null;
63 private LTTngTextTrace
initializeEventStream() {
64 if (testStream
== null) {
66 URL location
= FileLocator
.find(LTTngCoreTestPlugin
.getPlugin().getBundle(), new Path(tracepath1
), null);
67 File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
68 LTTngTextTrace tmpStream
= new LTTngTextTrace(testfile
.getName(), testfile
.getPath());
69 testStream
= tmpStream
;
72 System
.out
.println("ERROR : Could not open " + tracepath1
);
77 testStream
.seekEvent(0);
84 private LttngEventContent
prepareToTest() {
85 LttngEventContent tmpEventContent
= null;
87 // This trace should be valid
90 LTTngTextTrace tmpStream
= initializeEventStream();
91 tmpEventContent
= (LttngEventContent
)tmpStream
.getNextEvent( new TmfContext(new TmfLocation
<Long
>(0L), 0) ).getContent();
94 fail("ERROR : Failed to get content!");
97 return tmpEventContent
;
100 public void testConstructors() {
101 LttngEvent testEvent
= null;
102 LttngEventContent testContent
= null;
103 LttngEventField
[] testFields
= new LttngEventField
[1];
104 testFields
[0] = new LttngEventField("test");
106 // Default construction with good argument
108 testContent
= new LttngEventContent();
110 catch( Exception e
) {
111 fail("Construction with format failed!");
114 // Construction with good parameters (parent event)
116 testContent
= new LttngEventContent(testEvent
);
118 catch( Exception e
) {
119 fail("Construction with format, content and fields failed!");
122 // Construction with good parameters (parent event and pre-parsed content)
124 HashMap
<String
, LttngEventField
> parsedContent
= new HashMap
<String
, LttngEventField
>();
125 testContent
= new LttngEventContent(testEvent
, parsedContent
);
127 catch( Exception e
) {
128 fail("Construction with format, content and fields failed!");
132 // Copy constructor with correct parameters
134 testContent
= new LttngEventContent(testEvent
);
135 new LttngEventContent(testContent
);
137 catch( Exception e
) {
138 fail("Copy constructor failed!");
144 public void testGetter() {
145 LttngEventContent testContent
= null;
146 LTTngTextTrace tmpStream
= null;
147 LttngEvent tmpEvent
= null;
148 ITmfContext tmpContext
= null;
151 tmpStream
= initializeEventStream();
152 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
153 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
154 testContent
= prepareToTest();
156 assertNotSame("getFields() returned null!", null, testContent
.getFields() );
159 // Depending from the Java version because of the "hashcode()" on String.
160 // We can't really test that safetly
163 //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString());
164 assertNotSame("getField(int) returned unexpected result!", null, testContent
.getField(0).toString());
168 assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField
, testContent
.getField(firstEventContentFirstFieldName
).toString());
170 assertNotSame("getRawContent() returned null!",null, testContent
.getMapContent());
171 // Test that get event return the correct event
172 assertTrue("getEvent() returned unexpected result!", tmpEvent
.getTimestamp().getValue() == testContent
.getEvent().getTimestamp().getValue());
174 assertEquals("getType() returned unexpected result!",firstEventContentType
, testContent
.getEvent().getType().toString());
176 //*** To test getFields with a fields number >0, we need to move to an event that have some more
177 tmpStream
= initializeEventStream();
178 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
179 // Skip first events and seek to event pass metadata
180 tmpContext
= tmpStream
.seekEvent(new LttngTimestamp(timestampAfterMetadata
) );
182 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
184 // Second event past metadata should have more fields
185 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
187 testContent
= tmpEvent
.getContent();
189 // Test that get event return the correct event
190 assertTrue("getEvent() returned unexpected result!",tmpEvent
.getTimestamp().getValue() == testContent
.getEvent().getTimestamp().getValue());
192 assertEquals("getType() returned unexpected result!",secondEventContentType
, testContent
.getEvent().getType().toString());
196 assertNotSame("getFields() returned null!", null, testContent
.getFields() );
198 assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField
, testContent
.getField(1).toString());
200 assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField
, testContent
.getField(secondEventContentSecondFieldName
).toString());
202 assertNotSame("getRawContent() returned null!", null, testContent
.getMapContent());
206 public void testSetter() {
207 // Not much to test here, we will just make sure the set does not fail for any reason.
208 // It's pointless to test with a getter...
209 LTTngTextTrace tmpStream
= null;
210 LttngEvent tmpEvent
= null;
211 TmfContext tmpContext
= null;
214 tmpStream
= initializeEventStream();
215 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
216 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
218 LttngEventContent tmpContent
= prepareToTest();
220 tmpContent
.setEvent(tmpEvent
);
222 catch( Exception e
) {
223 fail("setEvent(event) failed!");
227 LttngEventType testType
= new LttngEventType();
229 tmpContent
.getEvent().setType(testType
);
231 catch( Exception e
) {
232 fail("setType(type) failed!");
236 public void testEmptyContent() {
237 LttngEventContent testContent
= null;
238 LTTngTextTrace tmpStream
= null;
239 LttngEvent tmpEvent
= null;
240 TmfContext tmpContext
= null;
243 tmpStream
= initializeEventStream();
244 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
245 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
247 testContent
= tmpEvent
.getContent();
248 // Get all the fields to make sure there is something in the HashMap
249 testContent
.getFields();
250 // Just making sure there is something in the HashMap
251 assertNotSame("HashMap is empty but should not!", 0, testContent
.getMapContent().size() );
253 // This is the actual test
254 testContent
.emptyContent();
255 assertSame("HashMap is not empty but should be!", 0, testContent
.getMapContent().size() );
258 public void testToString() {
259 LttngEventContent tmpContent
= prepareToTest();
261 // Just make sure toString() does not return null or the java reference
262 assertNotSame("toString returned null",null, tmpContent
.toString() );
263 assertNotSame("toString is not overridded!", tmpContent
.getClass().getName() + '@' + Integer
.toHexString(tmpContent
.hashCode()), tmpContent
.toString() );