Refactor TmfTrace and dependencies - move parseEvent to ITmfEventParser
[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.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;
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 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;
70 }
71 catch (final Exception e) {
72 System.out.println("ERROR : Could not open " + tracepath1);
73 testStream = null;
74 }
75 else
76 testStream.seekEvent(0);
77
78 return testStream;
79 }
80
81
82 private LttngEventContent prepareToTest() {
83 LttngEventContent tmpEventContent = null;
84
85 // This trace should be valid
86 try {
87 testStream = null;
88 final LTTngTextTrace tmpStream = initializeEventStream();
89 tmpEventContent = (LttngEventContent)tmpStream.readEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent();
90 }
91 catch (final Exception e) {
92 fail("ERROR : Failed to get content!");
93 }
94
95 return tmpEventContent;
96 }
97
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");
103
104 // Default construction with good argument
105 try {
106 testContent = new LttngEventContent();
107 }
108 catch( final Exception e) {
109 fail("Construction with format failed!");
110 }
111
112 // Construction with good parameters (parent event)
113 try {
114 testContent = new LttngEventContent(testEvent);
115 }
116 catch( final Exception e) {
117 fail("Construction with format, content and fields failed!");
118 }
119
120 // Construction with good parameters (parent event and pre-parsed content)
121 try {
122 final HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>();
123 testContent = new LttngEventContent(testEvent, parsedContent);
124 }
125 catch( final Exception e) {
126 fail("Construction with format, content and fields failed!");
127 }
128
129
130 // Copy constructor with correct parameters
131 try {
132 testContent = new LttngEventContent(testEvent);
133 new LttngEventContent(testContent);
134 }
135 catch( final Exception e) {
136 fail("Copy constructor failed!");
137 }
138
139 }
140
141
142 public void testGetter() {
143 LttngEventContent testContent = null;
144 LTTngTextTrace tmpStream = null;
145 LttngEvent tmpEvent = null;
146 ITmfContext tmpContext = null;
147
148 // Require an event
149 tmpStream = initializeEventStream();
150 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
151 tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext);
152 testContent = prepareToTest();
153 // getFieldS()
154 assertNotSame("getFields() returned null!", null, testContent.getFields() );
155
156 // *** FIXME ***
157 // Depending from the Java version because of the "hashcode()" on String.
158 // We can't really test that safetly
159 //
160 // getField(int)
161 //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString());
162 assertNotSame("getField(int) returned unexpected result!", null, testContent.getField(0).toString());
163
164
165 // getField(name)
166 assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField, testContent.getField(firstEventContentFirstFieldName).toString());
167 // getRawContent
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());
171 // getType()
172 assertEquals("getType() returned unexpected result!",firstEventContentType, testContent.getEvent().getType().toString());
173
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) );
179 // Skip first one
180 tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext);
181
182 // Second event past metadata should have more fields
183 tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext);
184 // Get the content
185 testContent = tmpEvent.getContent();
186
187 // Test that get event return the correct event
188 assertTrue("getEvent() returned unexpected result!",tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue());
189 // getType()
190 assertEquals("getType() returned unexpected result!",secondEventContentType, testContent.getEvent().getType().toString());
191
192
193 // getFieldS()
194 assertNotSame("getFields() returned null!", null, testContent.getFields() );
195 // getField(int)
196 assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField, testContent.getField(1).toString());
197 // getField(name)
198 assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField, testContent.getField(secondEventContentSecondFieldName).toString());
199 // getRawContent
200 assertNotSame("getRawContent() returned null!", null, testContent.getMapContent());
201
202 }
203
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;
210
211 // Require an event
212 tmpStream = initializeEventStream();
213 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
214 tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext);
215
216 final LttngEventContent tmpContent = prepareToTest();
217 try {
218 tmpContent.setEvent(tmpEvent);
219 }
220 catch( final Exception e) {
221 fail("setEvent(event) failed!");
222 }
223
224
225 final LttngEventType testType = new LttngEventType();
226 try {
227 tmpContent.getEvent().setType(testType);
228 }
229 catch( final Exception e) {
230 fail("setType(type) failed!");
231 }
232 }
233
234 public void testEmptyContent() {
235 LttngEventContent testContent = null;
236 LTTngTextTrace tmpStream = null;
237 LttngEvent tmpEvent = null;
238 TmfContext tmpContext = null;
239
240 // Require an event
241 tmpStream = initializeEventStream();
242 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
243 tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext);
244 // Get the content
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() );
250
251 // This is the actual test
252 testContent.emptyContent();
253 assertSame("HashMap is not empty but should be!", 0, testContent.getMapContent().size() );
254 }
255
256 public void testToString() {
257 final LttngEventContent tmpContent = prepareToTest();
258
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() );
262 }
263
264 }
This page took 0.037335 seconds and 5 git commands to generate.