[Bug 303523] LTTng/TMF udpates:
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.tests / src / org / eclipse / linuxtools / lttng / tests / event / LttngEventContentTest.java
1 package org.eclipse.linuxtools.lttng.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.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;
19
20 /*
21 Functions tested here :
22
23 public LttngEventContent()
24 public LttngEventContent(LttngEvent thisParent)
25 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
26 public LttngEventContent(LttngEventContent oldContent)
27
28 public void emptyContent()
29
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()
37
38 public void setType(LttngEventType newType)
39 public void setEvent(LttngEvent newParent)
40
41 public String toString()
42 */
43
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;
47
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";
51
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";
55
56 private final static long timestampAfterMetadata = 13589760262237L;
57
58 private static LTTngTextTrace testStream = null;
59
60 private LTTngTextTrace initializeEventStream() {
61 if (testStream == null) {
62 try {
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;
67 }
68 catch (Exception e) {
69 System.out.println("ERROR : Could not open " + tracepath1);
70 testStream = null;
71 }
72 }
73 else {
74 testStream.seekEvent(0);
75 }
76
77 return testStream;
78 }
79
80
81 private LttngEventContent prepareToTest() {
82 LttngEventContent tmpEventContent = null;
83
84 // This trace should be valid
85 try {
86 testStream = null;
87 LTTngTextTrace tmpStream = initializeEventStream();
88 tmpEventContent = (LttngEventContent)tmpStream.getNextEvent( new TmfTraceContext(0L, 0) ).getContent();
89 }
90 catch (Exception e) {
91 fail("ERROR : Failed to get content!");
92 }
93
94 return tmpEventContent;
95 }
96
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");
103
104 // Default construction with good argument
105 try {
106 testContent = new LttngEventContent();
107 }
108 catch( 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( 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 HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>();
123 testContent = new LttngEventContent(testEvent, parsedContent);
124 }
125 catch( 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 testContent2 = new LttngEventContent(testContent);
134 }
135 catch( 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 TmfTraceContext tmpContext = null;
147
148 // Require an event
149 tmpStream = initializeEventStream();
150 tmpContext = new TmfTraceContext(0L, 0);
151 tmpEvent = (LttngEvent)tmpStream.getNextEvent(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.getRawContent());
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.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 TmfTraceContext(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.getNextEvent(tmpContext);
181
182 // Second event past metadata should have more fields
183 tmpEvent = (LttngEvent)tmpStream.getNextEvent(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.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.getRawContent());
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 TmfTraceContext tmpContext = null;
210
211 // Require an event
212 tmpStream = initializeEventStream();
213 tmpContext = new TmfTraceContext(0L, 0);
214 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
215
216 LttngEventContent tmpContent = prepareToTest();
217 try {
218 tmpContent.setEvent(tmpEvent);
219 }
220 catch( Exception e) {
221 fail("setEvent(event) failed!");
222 }
223
224
225 LttngEventType testType = new LttngEventType();
226 try {
227 tmpContent.setType(testType);
228 }
229 catch( 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 TmfTraceContext tmpContext = null;
239
240 // Require an event
241 tmpStream = initializeEventStream();
242 tmpContext = new TmfTraceContext(0L, 0);
243 tmpEvent = (LttngEvent)tmpStream.getNextEvent(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.getRawContent().size() );
250
251 // This is the actual test
252 testContent.emptyContent();
253 assertSame("HashMap is not empty but should be!", 0, testContent.getRawContent().size() );
254 }
255
256 public void testToString() {
257 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.042343 seconds and 5 git commands to generate.