2010-11-05 Francois Chouinard <fchouinard@gmail.com> Fix for Bug329473
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.tests / src / org / eclipse / linuxtools / lttng / tests / event / LttngEventContentTest.java
CommitLineData
03c71d1e
ASL
1package org.eclipse.linuxtools.lttng.tests.event;
2
e1ab8984
FC
3import java.io.File;
4import java.net.URL;
a5ec08e5 5import java.util.HashMap;
e1ab8984 6
03c71d1e 7import junit.framework.TestCase;
e31e01e8 8
e1ab8984
FC
9import org.eclipse.core.runtime.FileLocator;
10import org.eclipse.core.runtime.Path;
e31e01e8
FC
11import org.eclipse.linuxtools.lttng.event.LttngEvent;
12import org.eclipse.linuxtools.lttng.event.LttngEventContent;
13import org.eclipse.linuxtools.lttng.event.LttngEventField;
14import org.eclipse.linuxtools.lttng.event.LttngEventType;
15import org.eclipse.linuxtools.lttng.event.LttngTimestamp;
e1ab8984 16import org.eclipse.linuxtools.lttng.tests.LTTngCoreTestPlugin;
03c71d1e 17import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace;
9f584e4c
FC
18import org.eclipse.linuxtools.tmf.trace.TmfContext;
19import org.eclipse.linuxtools.tmf.trace.TmfLocation;
03c71d1e
ASL
20
21/*
22 Functions tested here :
a5ec08e5
WB
23
24 public LttngEventContent()
25 public LttngEventContent(LttngEvent thisParent)
26 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
03c71d1e 27 public LttngEventContent(LttngEventContent oldContent)
a5ec08e5
WB
28
29 public void emptyContent()
30
31 public LttngEventField[] getFields()
32 public LttngEventField getField(int position)
33 public LttngEventField getField(String name)
34 public LttngEvent getEvent()
35 public LttngEventType getType()
36 public Object[] getContent()
37 public HashMap<String, LttngEventField> getRawContent()
38
39 public void setType(LttngEventType newType)
40 public void setEvent(LttngEvent newParent)
41
03c71d1e
ASL
42 public String toString()
43 */
44
45public class LttngEventContentTest extends TestCase {
46 private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt";
e31e01e8 47// private final static boolean skipIndexing=true;
03c71d1e
ASL
48
49 private final static String firstEventContentFirstField = "alignment:0";
a5ec08e5
WB
50 private final static String firstEventContentFirstFieldName = "alignment";
51 private final static String firstEventContentType = "metadata/0/core_marker_id";
52
53 private final static String secondEventContentSecondField = "string:LTT state dump begin";
54 private final static String secondEventContentSecondFieldName = "string";
55 private final static String secondEventContentType = "kernel/0/vprintk";
56
03c71d1e
ASL
57 private final static long timestampAfterMetadata = 13589760262237L;
58
e1ab8984 59 private static LTTngTextTrace testStream = null;
a5ec08e5 60
03c71d1e 61 private LTTngTextTrace initializeEventStream() {
a5ec08e5 62 if (testStream == null) {
e1ab8984
FC
63 try {
64 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null);
65 File testfile = new File(FileLocator.toFileURL(location).toURI());
a5ec08e5 66 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath());
e1ab8984
FC
67 testStream = tmpStream;
68 }
69 catch (Exception e) {
70 System.out.println("ERROR : Could not open " + tracepath1);
71 testStream = null;
72 }
03c71d1e 73 }
ccc4dc5b
WB
74 else {
75 testStream.seekEvent(0);
76 }
77
e1ab8984 78 return testStream;
a5ec08e5
WB
79 }
80
03c71d1e
ASL
81
82 private LttngEventContent prepareToTest() {
83 LttngEventContent tmpEventContent = null;
84
85 // This trace should be valid
86 try {
a5ec08e5 87 testStream = null;
03c71d1e 88 LTTngTextTrace tmpStream = initializeEventStream();
9f584e4c 89 tmpEventContent = (LttngEventContent)tmpStream.getNextEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent();
03c71d1e
ASL
90 }
91 catch (Exception e) {
92 fail("ERROR : Failed to get content!");
93 }
94
95 return tmpEventContent;
96 }
97
98 public void testConstructors() {
99 LttngEvent testEvent = null;
100 LttngEventContent testContent = null;
101 LttngEventContent testContent2 = null;
102 LttngEventField[] testFields = new LttngEventField[1];
103 testFields[0] = new LttngEventField(testContent2, "test");
104
105 // Default construction with good argument
106 try {
107 testContent = new LttngEventContent();
108 }
109 catch( Exception e) {
110 fail("Construction with format failed!");
111 }
112
a5ec08e5 113 // Construction with good parameters (parent event)
03c71d1e
ASL
114 try {
115 testContent = new LttngEventContent(testEvent);
116 }
117 catch( Exception e) {
118 fail("Construction with format, content and fields failed!");
119 }
120
a5ec08e5
WB
121 // Construction with good parameters (parent event and pre-parsed content)
122 try {
123 HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>();
124 testContent = new LttngEventContent(testEvent, parsedContent);
125 }
126 catch( Exception e) {
127 fail("Construction with format, content and fields failed!");
128 }
129
130
03c71d1e
ASL
131 // Copy constructor with correct parameters
132 try {
133 testContent = new LttngEventContent(testEvent);
134 testContent2 = new LttngEventContent(testContent);
135 }
136 catch( Exception e) {
137 fail("Copy constructor failed!");
138 }
139
140 }
141
142
cb866e08
FC
143 public void testGetter() {
144 LttngEventContent testContent = null;
145 LTTngTextTrace tmpStream = null;
146 LttngEvent tmpEvent = null;
147 TmfContext tmpContext = null;
148
149 // Require an event
150 tmpStream = initializeEventStream();
151 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
152 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
153 testContent = prepareToTest();
154 // getFieldS()
155 assertNotSame("getFields() returned null!",null,testContent.getFields() );
156
157 // *** FIXME ***
158 // Depending from the Java version because of the "hashcode()" on String.
159 // We can't really test that safetly
160 //
161 // getField(int)
162 //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString());
163 assertNotSame("getField(int) returned unexpected result!",null, testContent.getField(0).toString());
164
165
166 // getField(name)
167 assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField, testContent.getField(firstEventContentFirstFieldName).toString());
168 // getRawContent
169 assertNotSame("getRawContent() returned null!",null, testContent.getRawContent());
170 // Test that get event return the correct event
171 assertTrue("getEvent() returned unexpected result!", tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue());
172 // getType()
173 assertEquals("getType() returned unexpected result!",firstEventContentType, testContent.getType().toString());
174
175 //*** To test getFields with a fields number >0, we need to move to an event that have some more
176 tmpStream = initializeEventStream();
177 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
178 // Skip first events and seek to event pass metadata
179 tmpContext= tmpStream.seekEvent(new LttngTimestamp(timestampAfterMetadata) );
180 // Skip first one
181 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
182
183 // Second event past metadata should have more fields
184 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
185 // Get the content
186 testContent = tmpEvent.getContent();
187
188 // Test that get event return the correct event
189 assertTrue("getEvent() returned unexpected result!",tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue());
190 // getType()
191 assertEquals("getType() returned unexpected result!",secondEventContentType, testContent.getType().toString());
192
193
194 // getFieldS()
195 assertNotSame("getFields() returned null!",null,testContent.getFields() );
196 // getField(int)
197 assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField, testContent.getField(1).toString());
198 // getField(name)
199 assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField, testContent.getField(secondEventContentSecondFieldName).toString());
200 // getRawContent
201 assertNotSame("getRawContent() returned null!",null, testContent.getRawContent());
202
203 }
204
205 public void testSetter() {
206 // Not much to test here, we will just make sure the set does not fail for any reason.
207 // It's pointless to test with a getter...
208 LTTngTextTrace tmpStream = null;
209 LttngEvent tmpEvent = null;
210 TmfContext tmpContext = null;
211
212 // Require an event
213 tmpStream = initializeEventStream();
214 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
215 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
216
217 LttngEventContent tmpContent = prepareToTest();
218 try {
219 tmpContent.setEvent(tmpEvent);
220 }
221 catch( Exception e) {
222 fail("setEvent(event) failed!");
223 }
224
225
226 LttngEventType testType = new LttngEventType();
227 try {
228 tmpContent.setType(testType);
229 }
230 catch( Exception e) {
231 fail("setType(type) failed!");
232 }
233 }
234
235 public void testEmptyContent() {
236 LttngEventContent testContent = null;
237 LTTngTextTrace tmpStream = null;
238 LttngEvent tmpEvent = null;
239 TmfContext tmpContext = null;
240
241 // Require an event
242 tmpStream = initializeEventStream();
243 tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0);
244 tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext);
245 // Get the content
246 testContent = tmpEvent.getContent();
247 // Get all the fields to make sure there is something in the HashMap
248 testContent.getFields();
249 // Just making sure there is something in the HashMap
250 assertNotSame("HashMap is empty but should not!", 0, testContent.getRawContent().size() );
251
252 // This is the actual test
253 testContent.emptyContent();
254 assertSame("HashMap is not empty but should be!", 0, testContent.getRawContent().size() );
255 }
256
257 public void testToString() {
258 LttngEventContent tmpContent = prepareToTest();
259
260 // Just make sure toString() does not return null or the java reference
261 assertNotSame("toString returned null",null, tmpContent.toString() );
262 assertNotSame("toString is not overridded!", tmpContent.getClass().getName() + '@' + Integer.toHexString(tmpContent.hashCode()), tmpContent.toString() );
263 }
03c71d1e
ASL
264
265}
This page took 0.038165 seconds and 5 git commands to generate.