Commit | Line | Data |
---|---|---|
6c13869b | 1 | package org.eclipse.linuxtools.lttng.core.tests.event; |
03c71d1e | 2 | |
e1ab8984 FC |
3 | import java.io.File; |
4 | import java.net.URL; | |
a5ec08e5 | 5 | import java.util.HashMap; |
e1ab8984 | 6 | |
03c71d1e | 7 | import junit.framework.TestCase; |
e31e01e8 | 8 | |
e1ab8984 FC |
9 | import org.eclipse.core.runtime.FileLocator; |
10 | import org.eclipse.core.runtime.Path; | |
5945cec9 FC |
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; | |
3791b5df | 17 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
6c13869b FC |
18 | import org.eclipse.linuxtools.tmf.core.trace.TmfContext; |
19 | import org.eclipse.linuxtools.tmf.core.trace.TmfLocation; | |
9269df72 | 20 | import org.osgi.framework.FrameworkUtil; |
03c71d1e ASL |
21 | |
22 | /* | |
23 | Functions tested here : | |
25e48683 | 24 | |
a5ec08e5 WB |
25 | public LttngEventContent() |
26 | public LttngEventContent(LttngEvent thisParent) | |
27 | public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) | |
03c71d1e | 28 | public LttngEventContent(LttngEventContent oldContent) |
25e48683 | 29 | |
a5ec08e5 | 30 | public void emptyContent() |
25e48683 | 31 | |
a5ec08e5 WB |
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() | |
25e48683 | 39 | |
a5ec08e5 WB |
40 | public void setType(LttngEventType newType) |
41 | public void setEvent(LttngEvent newParent) | |
25e48683 | 42 | |
03c71d1e ASL |
43 | public String toString() |
44 | */ | |
45 | ||
3b38ea61 | 46 | @SuppressWarnings("nls") |
03c71d1e ASL |
47 | public class LttngEventContentTest extends TestCase { |
48 | private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt"; | |
25e48683 FC |
49 | // private final static boolean skipIndexing=true; |
50 | ||
03c71d1e | 51 | private final static String firstEventContentFirstField = "alignment:0"; |
a5ec08e5 WB |
52 | private final static String firstEventContentFirstFieldName = "alignment"; |
53 | private final static String firstEventContentType = "metadata/0/core_marker_id"; | |
25e48683 | 54 | |
a5ec08e5 WB |
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"; | |
25e48683 | 58 | |
03c71d1e | 59 | private final static long timestampAfterMetadata = 13589760262237L; |
25e48683 | 60 | |
e1ab8984 | 61 | private static LTTngTextTrace testStream = null; |
25e48683 | 62 | |
03c71d1e | 63 | private LTTngTextTrace initializeEventStream() { |
25e48683 FC |
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; | |
a5ec08e5 | 79 | } |
25e48683 FC |
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(); | |
7e6347b0 | 89 | tmpEventContent = (LttngEventContent)tmpStream.readEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent(); |
25e48683 FC |
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]; | |
4c564a2d | 102 | testFields[0] = new LttngEventField("test"); |
25e48683 FC |
103 | |
104 | // Default construction with good argument | |
03c71d1e | 105 | try { |
25e48683 | 106 | testContent = new LttngEventContent(); |
03c71d1e | 107 | } |
25e48683 FC |
108 | catch( final Exception e) { |
109 | fail("Construction with format failed!"); | |
03c71d1e | 110 | } |
25e48683 | 111 | |
a5ec08e5 | 112 | // Construction with good parameters (parent event) |
03c71d1e | 113 | try { |
25e48683 | 114 | testContent = new LttngEventContent(testEvent); |
03c71d1e | 115 | } |
25e48683 FC |
116 | catch( final Exception e) { |
117 | fail("Construction with format, content and fields failed!"); | |
03c71d1e | 118 | } |
25e48683 | 119 | |
a5ec08e5 WB |
120 | // Construction with good parameters (parent event and pre-parsed content) |
121 | try { | |
25e48683 FC |
122 | final HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>(); |
123 | testContent = new LttngEventContent(testEvent, parsedContent); | |
a5ec08e5 | 124 | } |
25e48683 FC |
125 | catch( final Exception e) { |
126 | fail("Construction with format, content and fields failed!"); | |
a5ec08e5 | 127 | } |
25e48683 FC |
128 | |
129 | ||
03c71d1e ASL |
130 | // Copy constructor with correct parameters |
131 | try { | |
25e48683 FC |
132 | testContent = new LttngEventContent(testEvent); |
133 | new LttngEventContent(testContent); | |
03c71d1e | 134 | } |
25e48683 FC |
135 | catch( final Exception e) { |
136 | fail("Copy constructor failed!"); | |
03c71d1e | 137 | } |
25e48683 | 138 | |
cb866e08 | 139 | } |
25e48683 FC |
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); | |
7e6347b0 | 151 | tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext); |
25e48683 FC |
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 | |
7e6347b0 | 180 | tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext); |
25e48683 FC |
181 | |
182 | // Second event past metadata should have more fields | |
7e6347b0 | 183 | tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext); |
25e48683 FC |
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); | |
7e6347b0 | 214 | tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext); |
25e48683 FC |
215 | |
216 | final LttngEventContent tmpContent = prepareToTest(); | |
217 | try { | |
218 | tmpContent.setEvent(tmpEvent); | |
cb866e08 | 219 | } |
25e48683 FC |
220 | catch( final Exception e) { |
221 | fail("setEvent(event) failed!"); | |
cb866e08 | 222 | } |
25e48683 FC |
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); | |
7e6347b0 | 243 | tmpEvent = (LttngEvent)tmpStream.readEvent(tmpContext); |
25e48683 FC |
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() ); | |
cb866e08 | 254 | } |
25e48683 FC |
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 | ||
03c71d1e | 264 | } |