Update TmfTrace as per ITmfTrace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core.tests / src / org / eclipse / linuxtools / lttng / core / tests / event / LttngEventContentTest.java
1 package org.eclipse.linuxtools.lttng.core.tests.event;
2
3 import java.io.File;
4 import java.net.URL;
5 import java.util.HashMap;
6
7 import junit.framework.TestCase;
8
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;
21
22 /*
23 Functions tested here :
24
25 public LttngEventContent()
26 public LttngEventContent(LttngEvent thisParent)
27 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
28 public LttngEventContent(LttngEventContent oldContent)
29
30 public void emptyContent()
31
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()
39
40 public void setType(LttngEventType newType)
41 public void setEvent(LttngEvent newParent)
42
43 public String toString()
44 */
45
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;
50
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";
54
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";
58
59 private final static long timestampAfterMetadata = 13589760262237L;
60
61 private static LTTngTextTrace testStream = null;
62
63 private LTTngTextTrace initializeEventStream() {
64 if (testStream == null) {
65 try {
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;
70 }
71 catch (Exception e) {
72 System.out.println("ERROR : Could not open " + tracepath1);
73 testStream = null;
74 }
75 }
76 else {
77 testStream.seekEvent(0);
78 }
79
80 return testStream;
81 }
82
83
84 private LttngEventContent prepareToTest() {
85 LttngEventContent tmpEventContent = null;
86
87 // This trace should be valid
88 try {
89 testStream = null;
90 LTTngTextTrace tmpStream = initializeEventStream();
91 tmpEventContent = (LttngEventContent)tmpStream.getNextEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent();
92 }
93 catch (Exception e) {
94 fail("ERROR : Failed to get content!");
95 }
96
97 return tmpEventContent;
98 }
99
100 public void testConstructors() {
101 LttngEvent testEvent = null;
102 LttngEventContent testContent = null;
103 LttngEventField[] testFields = new LttngEventField[1];
104 testFields[0] = new LttngEventField("test");
105
106 // Default construction with good argument
107 try {
108 testContent = new LttngEventContent();
109 }
110 catch( Exception e) {
111 fail("Construction with format failed!");
112 }
113
114 // Construction with good parameters (parent event)
115 try {
116 testContent = new LttngEventContent(testEvent);
117 }
118 catch( Exception e) {
119 fail("Construction with format, content and fields failed!");
120 }
121
122 // Construction with good parameters (parent event and pre-parsed content)
123 try {
124 HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>();
125 testContent = new LttngEventContent(testEvent, parsedContent);
126 }
127 catch( Exception e) {
128 fail("Construction with format, content and fields failed!");
129 }
130
131
132 // Copy constructor with correct parameters
133 try {
134 testContent = new LttngEventContent(testEvent);
135 new LttngEventContent(testContent);
136 }
137 catch( Exception e) {
138 fail("Copy constructor failed!");
139 }
140
141 }
142
143
144 public void testGetter() {
145 LttngEventContent testContent = null;
146 LTTngTextTrace tmpStream = null;
147 LttngEvent tmpEvent = null;
148 ITmfContext tmpContext = null;
149
150 // Require an event
151 tmpStream = initializeEventStream();
152 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
153 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
154 testContent = prepareToTest();
155 // getFieldS()
156 assertNotSame("getFields() returned null!", null, testContent.getFields() );
157
158 // *** FIXME ***
159 // Depending from the Java version because of the "hashcode()" on String.
160 // We can't really test that safetly
161 //
162 // getField(int)
163 //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString());
164 assertNotSame("getField(int) returned unexpected result!", null, testContent.getField(0).toString());
165
166
167 // getField(name)
168 assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField, testContent.getField(firstEventContentFirstFieldName).toString());
169 // getRawContent
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());
173 // getType()
174 assertEquals("getType() returned unexpected result!",firstEventContentType, testContent.getEvent().getType().toString());
175
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) );
181 // Skip first one
182 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
183
184 // Second event past metadata should have more fields
185 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
186 // Get the content
187 testContent = tmpEvent.getContent();
188
189 // Test that get event return the correct event
190 assertTrue("getEvent() returned unexpected result!",tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue());
191 // getType()
192 assertEquals("getType() returned unexpected result!",secondEventContentType, testContent.getEvent().getType().toString());
193
194
195 // getFieldS()
196 assertNotSame("getFields() returned null!", null, testContent.getFields() );
197 // getField(int)
198 assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField, testContent.getField(1).toString());
199 // getField(name)
200 assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField, testContent.getField(secondEventContentSecondFieldName).toString());
201 // getRawContent
202 assertNotSame("getRawContent() returned null!", null, testContent.getMapContent());
203
204 }
205
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;
212
213 // Require an event
214 tmpStream = initializeEventStream();
215 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
216 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
217
218 LttngEventContent tmpContent = prepareToTest();
219 try {
220 tmpContent.setEvent(tmpEvent);
221 }
222 catch( Exception e) {
223 fail("setEvent(event) failed!");
224 }
225
226
227 LttngEventType testType = new LttngEventType();
228 try {
229 tmpContent.getEvent().setType(testType);
230 }
231 catch( Exception e) {
232 fail("setType(type) failed!");
233 }
234 }
235
236 public void testEmptyContent() {
237 LttngEventContent testContent = null;
238 LTTngTextTrace tmpStream = null;
239 LttngEvent tmpEvent = null;
240 TmfContext tmpContext = null;
241
242 // Require an event
243 tmpStream = initializeEventStream();
244 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
245 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
246 // Get the content
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() );
252
253 // This is the actual test
254 testContent.emptyContent();
255 assertSame("HashMap is not empty but should be!", 0, testContent.getMapContent().size() );
256 }
257
258 public void testToString() {
259 LttngEventContent tmpContent = prepareToTest();
260
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() );
264 }
265
266 }
This page took 0.037663 seconds and 6 git commands to generate.