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