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
.tmf
.core
.trace
.ITmfContext
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfLocation
;
20 import org
.osgi
.framework
.FrameworkUtil
;
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 final URL location
= FileLocator
.find(FrameworkUtil
.getBundle(this.getClass()), new Path(tracepath1
), null);
67 final File testfile
= new File(FileLocator
.toFileURL(location
).toURI());
68 final LTTngTextTrace tmpStream
= new LTTngTextTrace(null, testfile
.getPath());
69 testStream
= tmpStream
;
71 catch (final Exception e
) {
72 System
.out
.println("ERROR : Could not open " + tracepath1
);
76 testStream
.seekEvent(0);
82 private LttngEventContent
prepareToTest() {
83 LttngEventContent tmpEventContent
= null;
85 // This trace should be valid
88 final LTTngTextTrace tmpStream
= initializeEventStream();
89 tmpEventContent
= (LttngEventContent
)tmpStream
.readEvent( new TmfContext(new TmfLocation
<Long
>(0L), 0) ).getContent();
91 catch (final Exception e
) {
92 fail("ERROR : Failed to get content!");
95 return tmpEventContent
;
98 public void testConstructors() {
99 final LttngEvent testEvent
= null;
100 LttngEventContent testContent
= null;
101 final LttngEventField
[] testFields
= new LttngEventField
[1];
102 testFields
[0] = new LttngEventField("test");
104 // Default construction with good argument
106 testContent
= new LttngEventContent();
108 catch( final Exception e
) {
109 fail("Construction with format failed!");
112 // Construction with good parameters (parent event)
114 testContent
= new LttngEventContent(testEvent
);
116 catch( final Exception e
) {
117 fail("Construction with format, content and fields failed!");
120 // Construction with good parameters (parent event and pre-parsed content)
122 final HashMap
<String
, LttngEventField
> parsedContent
= new HashMap
<String
, LttngEventField
>();
123 testContent
= new LttngEventContent(testEvent
, parsedContent
);
125 catch( final Exception e
) {
126 fail("Construction with format, content and fields failed!");
130 // Copy constructor with correct parameters
132 testContent
= new LttngEventContent(testEvent
);
133 new LttngEventContent(testContent
);
135 catch( final Exception e
) {
136 fail("Copy constructor failed!");
142 public void testGetter() {
143 LttngEventContent testContent
= null;
144 LTTngTextTrace tmpStream
= null;
145 LttngEvent tmpEvent
= null;
146 ITmfContext tmpContext
= null;
149 tmpStream
= initializeEventStream();
150 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
151 tmpEvent
= (LttngEvent
)tmpStream
.readEvent(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
.getMapContent());
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
.getEvent().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 TmfContext(new TmfLocation
<Long
>(0L), 0);
177 // Skip first events and seek to event pass metadata
178 tmpContext
= tmpStream
.seekEvent(new LttngTimestamp(timestampAfterMetadata
) );
180 tmpEvent
= (LttngEvent
)tmpStream
.readEvent(tmpContext
);
182 // Second event past metadata should have more fields
183 tmpEvent
= (LttngEvent
)tmpStream
.readEvent(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
.getEvent().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
.getMapContent());
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 TmfContext tmpContext
= null;
212 tmpStream
= initializeEventStream();
213 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
214 tmpEvent
= (LttngEvent
)tmpStream
.readEvent(tmpContext
);
216 final LttngEventContent tmpContent
= prepareToTest();
218 tmpContent
.setEvent(tmpEvent
);
220 catch( final Exception e
) {
221 fail("setEvent(event) failed!");
225 final LttngEventType testType
= new LttngEventType();
227 tmpContent
.getEvent().setType(testType
);
229 catch( final Exception e
) {
230 fail("setType(type) failed!");
234 public void testEmptyContent() {
235 LttngEventContent testContent
= null;
236 LTTngTextTrace tmpStream
= null;
237 LttngEvent tmpEvent
= null;
238 TmfContext tmpContext
= null;
241 tmpStream
= initializeEventStream();
242 tmpContext
= new TmfContext(new TmfLocation
<Long
>(0L), 0);
243 tmpEvent
= (LttngEvent
)tmpStream
.readEvent(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
.getMapContent().size() );
251 // This is the actual test
252 testContent
.emptyContent();
253 assertSame("HashMap is not empty but should be!", 0, testContent
.getMapContent().size() );
256 public void testToString() {
257 final 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() );