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
.TmfTraceContext
;
21 Functions tested here :
23 public LttngEventContent()
24 public LttngEventContent(LttngEvent thisParent)
25 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
26 public LttngEventContent(LttngEventContent oldContent)
28 public void emptyContent()
30 public LttngEventField[] getFields()
31 public LttngEventField getField(int position)
32 public LttngEventField getField(String name)
33 public LttngEvent getEvent()
34 public LttngEventType getType()
35 public Object[] getContent()
36 public HashMap<String, LttngEventField> getRawContent()
38 public void setType(LttngEventType newType)
39 public void setEvent(LttngEvent newParent)
41 public String toString()
44 public class LttngEventContentTest
extends TestCase
{
45 private final static String tracepath1
="traceset/trace-15316events_nolost_newformat.txt";
46 // private final static boolean skipIndexing=true;
48 private final static String firstEventContentFirstField
= "alignment:0";
49 private final static String firstEventContentFirstFieldName
= "alignment";
50 private final static String firstEventContentType
= "metadata/0/core_marker_id";
52 private final static String secondEventContentSecondField
= "string:LTT state dump begin";
53 private final static String secondEventContentSecondFieldName
= "string";
54 private final static String secondEventContentType
= "kernel/0/vprintk";
56 private final static long timestampAfterMetadata
= 13589760262237L;
58 private static LTTngTextTrace testStream
= null;
60 private LTTngTextTrace
initializeEventStream() {
61 if (testStream
== null) {
63 URL location
= FileLocator
.find(LTTngCoreTestPlugin
.getPlugin().getBundle(), new Path(tracepath1
), null);
64 File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
65 LTTngTextTrace tmpStream
= new LTTngTextTrace(testfile
.getPath());
66 testStream
= tmpStream
;
69 System
.out
.println("ERROR : Could not open " + tracepath1
);
74 testStream
.seekEvent(0);
81 private LttngEventContent
prepareToTest() {
82 LttngEventContent tmpEventContent
= null;
84 // This trace should be valid
87 LTTngTextTrace tmpStream
= initializeEventStream();
88 tmpEventContent
= (LttngEventContent
)tmpStream
.getNextEvent( new TmfTraceContext(0L, 0) ).getContent();
91 fail("ERROR : Failed to get content!");
94 return tmpEventContent
;
97 public void testConstructors() {
98 LttngEvent testEvent
= null;
99 LttngEventContent testContent
= null;
100 LttngEventContent testContent2
= null;
101 LttngEventField
[] testFields
= new LttngEventField
[1];
102 testFields
[0] = new LttngEventField(testContent2
, "test");
104 // Default construction with good argument
106 testContent
= new LttngEventContent();
108 catch( Exception e
) {
109 fail("Construction with format failed!");
112 // Construction with good parameters (parent event)
114 testContent
= new LttngEventContent(testEvent
);
116 catch( Exception e
) {
117 fail("Construction with format, content and fields failed!");
120 // Construction with good parameters (parent event and pre-parsed content)
122 HashMap
<String
, LttngEventField
> parsedContent
= new HashMap
<String
, LttngEventField
>();
123 testContent
= new LttngEventContent(testEvent
, parsedContent
);
125 catch( Exception e
) {
126 fail("Construction with format, content and fields failed!");
130 // Copy constructor with correct parameters
132 testContent
= new LttngEventContent(testEvent
);
133 testContent2
= new LttngEventContent(testContent
);
135 catch( Exception e
) {
136 fail("Copy constructor failed!");
142 public void testGetter() {
143 LttngEventContent testContent
= null;
144 LTTngTextTrace tmpStream
= null;
145 LttngEvent tmpEvent
= null;
146 TmfTraceContext tmpContext
= null;
149 tmpStream
= initializeEventStream();
150 tmpContext
= new TmfTraceContext(0L, 0);
151 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
152 testContent
= prepareToTest();
154 assertNotSame("getFields() returned null!",null,testContent
.getFields() );
157 // Depending from the Java version because of the "hashcode()" on String.
158 // We can't really test that safetly
161 //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString());
162 assertNotSame("getField(int) returned unexpected result!",null, testContent
.getField(0).toString());
166 assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField
, testContent
.getField(firstEventContentFirstFieldName
).toString());
168 assertNotSame("getRawContent() returned null!",null, testContent
.getRawContent());
169 // Test that get event return the correct event
170 assertTrue("getEvent() returned unexpected result!", tmpEvent
.getTimestamp().getValue() == testContent
.getEvent().getTimestamp().getValue());
172 assertEquals("getType() returned unexpected result!",firstEventContentType
, testContent
.getType().toString());
174 //*** To test getFields with a fields number >0, we need to move to an event that have some more
175 tmpStream
= initializeEventStream();
176 tmpContext
= new TmfTraceContext(0L, 0);
177 // Skip first events and seek to event pass metadata
178 tmpContext
= tmpStream
.seekEvent(new LttngTimestamp(timestampAfterMetadata
) );
180 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
182 // Second event past metadata should have more fields
183 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
185 testContent
= tmpEvent
.getContent();
187 // Test that get event return the correct event
188 assertTrue("getEvent() returned unexpected result!",tmpEvent
.getTimestamp().getValue() == testContent
.getEvent().getTimestamp().getValue());
190 assertEquals("getType() returned unexpected result!",secondEventContentType
, testContent
.getType().toString());
194 assertNotSame("getFields() returned null!",null,testContent
.getFields() );
196 assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField
, testContent
.getField(1).toString());
198 assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField
, testContent
.getField(secondEventContentSecondFieldName
).toString());
200 assertNotSame("getRawContent() returned null!",null, testContent
.getRawContent());
204 public void testSetter() {
205 // Not much to test here, we will just make sure the set does not fail for any reason.
206 // It's pointless to test with a getter...
207 LTTngTextTrace tmpStream
= null;
208 LttngEvent tmpEvent
= null;
209 TmfTraceContext tmpContext
= null;
212 tmpStream
= initializeEventStream();
213 tmpContext
= new TmfTraceContext(0L, 0);
214 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
216 LttngEventContent tmpContent
= prepareToTest();
218 tmpContent
.setEvent(tmpEvent
);
220 catch( Exception e
) {
221 fail("setEvent(event) failed!");
225 LttngEventType testType
= new LttngEventType();
227 tmpContent
.setType(testType
);
229 catch( Exception e
) {
230 fail("setType(type) failed!");
234 public void testEmptyContent() {
235 LttngEventContent testContent
= null;
236 LTTngTextTrace tmpStream
= null;
237 LttngEvent tmpEvent
= null;
238 TmfTraceContext tmpContext
= null;
241 tmpStream
= initializeEventStream();
242 tmpContext
= new TmfTraceContext(0L, 0);
243 tmpEvent
= (LttngEvent
)tmpStream
.getNextEvent(tmpContext
);
245 testContent
= tmpEvent
.getContent();
246 // Get all the fields to make sure there is something in the HashMap
247 testContent
.getFields();
248 // Just making sure there is something in the HashMap
249 assertNotSame("HashMap is empty but should not!", 0, testContent
.getRawContent().size() );
251 // This is the actual test
252 testContent
.emptyContent();
253 assertSame("HashMap is not empty but should be!", 0, testContent
.getRawContent().size() );
256 public void testToString() {
257 LttngEventContent tmpContent
= prepareToTest();
259 // Just make sure toString() does not return null or the java reference
260 assertNotSame("toString returned null",null, tmpContent
.toString() );
261 assertNotSame("toString is not overridded!", tmpContent
.getClass().getName() + '@' + Integer
.toHexString(tmpContent
.hashCode()), tmpContent
.toString() );