1 package org
.eclipse
.linuxtools
.lttng
.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
.lttng
.event
.LttngEvent
;
12 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventContent
;
13 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventField
;
14 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEventType
;
15 import org
.eclipse
.linuxtools
.lttng
.event
.LttngTimestamp
;
16 import org
.eclipse
.linuxtools
.lttng
.tests
.LTTngCoreTestPlugin
;
17 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngTextTrace
;
18 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfContext
;
19 import org
.eclipse
.linuxtools
.tmf
.trace
.TmfLocation
;
22 Functions tested here :
24 public LttngEventContent()
25 public LttngEventContent(LttngEvent thisParent)
26 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
27 public LttngEventContent(LttngEventContent oldContent)
29 public void emptyContent()
31 public LttngEventField[] getFields()
32 public LttngEventField getField(int position)
33 public LttngEventField getField(String name)
34 public LttngEvent getEvent()
35 public LttngEventType getType()
36 public Object[] getContent()
37 public HashMap<String, LttngEventField> getRawContent()
39 public void setType(LttngEventType newType)
40 public void setEvent(LttngEvent newParent)
42 public String toString()
45 @SuppressWarnings("nls")
46 public class LttngEventContentTest
extends TestCase
{
47 private final static String tracepath1
="traceset/trace-15316events_nolost_newformat.txt";
48 // private final static boolean skipIndexing=true;
50 private final static String firstEventContentFirstField
= "alignment:0";
51 private final static String firstEventContentFirstFieldName
= "alignment";
52 private final static String firstEventContentType
= "metadata/0/core_marker_id";
54 private final static String secondEventContentSecondField
= "string:LTT state dump begin";
55 private final static String secondEventContentSecondFieldName
= "string";
56 private final static String secondEventContentType
= "kernel/0/vprintk";
58 private final static long timestampAfterMetadata
= 13589760262237L;
60 private static LTTngTextTrace testStream
= null;
62 private LTTngTextTrace
initializeEventStream() {
63 if (testStream
== null) {
65 URL location
= FileLocator
.find(LTTngCoreTestPlugin
.getPlugin().getBundle(), new Path(tracepath1
), null);
66 File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
67 LTTngTextTrace tmpStream
= new LTTngTextTrace(testfile
.getPath());
68 testStream
= tmpStream
;
71 System
.out
.println("ERROR : Could not open " + tracepath1
);
76 testStream
.seekEvent(0);
83 private LttngEventContent
prepareToTest() {
84 LttngEventContent tmpEventContent
= null;
86 // This trace should be valid
89 LTTngTextTrace tmpStream
= initializeEventStream();
90 tmpEventContent
= (LttngEventContent
)tmpStream
.getNextEvent( new TmfContext(new TmfLocation
<Long
>(0L), 0) ).getContent();
93 fail("ERROR : Failed to get content!");
96 return tmpEventContent
;
99 public void testConstructors() {
100 LttngEvent testEvent
= null;
101 LttngEventContent testContent
= null;
102 LttngEventContent testContent2
= null;
103 LttngEventField
[] testFields
= new LttngEventField
[1];
104 testFields
[0] = new LttngEventField(testContent2
, "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 testContent2
= 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 TmfContext 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
.getRawContent());
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
.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
.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
.getRawContent());
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
.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
.getRawContent().size() );
253 // This is the actual test
254 testContent
.emptyContent();
255 assertSame("HashMap is not empty but should be!", 0, testContent
.getRawContent().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() );