Fix more warnings from FindBugs
[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.lttng.core.event.LttngEvent;
12 import org.eclipse.linuxtools.lttng.core.event.LttngEventContent;
13 import org.eclipse.linuxtools.lttng.core.event.LttngEventField;
14 import org.eclipse.linuxtools.lttng.core.event.LttngEventType;
15 import org.eclipse.linuxtools.lttng.core.event.LttngTimestamp;
16 import org.eclipse.linuxtools.lttng.core.tests.LTTngCoreTestPlugin;
17 import org.eclipse.linuxtools.lttng.core.trace.LTTngTextTrace;
18 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
19 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
20
21 /*
22 Functions tested here :
23
24 public LttngEventContent()
25 public LttngEventContent(LttngEvent thisParent)
26 public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent)
27 public LttngEventContent(LttngEventContent oldContent)
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
42 public String toString()
43 */
44
45 @SuppressWarnings("nls")
46 public class LttngEventContentTest extends TestCase {
47 private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt";
48 // private final static boolean skipIndexing=true;
49
50 private final static String firstEventContentFirstField = "alignment:0";
51 private final static String firstEventContentFirstFieldName = "alignment";
52 private final static String firstEventContentType = "metadata/0/core_marker_id";
53
54 private final static String secondEventContentSecondField = "string:LTT state dump begin";
55 private final static String secondEventContentSecondFieldName = "string";
56 private final static String secondEventContentType = "kernel/0/vprintk";
57
58 private final static long timestampAfterMetadata = 13589760262237L;
59
60 private static LTTngTextTrace testStream = null;
61
62 private LTTngTextTrace initializeEventStream() {
63 if (testStream == null) {
64 try {
65 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null);
66 File testfile = new File(FileLocator.toFileURL(location).toURI());
67 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getName(), testfile.getPath());
68 testStream = tmpStream;
69 }
70 catch (Exception e) {
71 System.out.println("ERROR : Could not open " + tracepath1);
72 testStream = null;
73 }
74 }
75 else {
76 testStream.seekEvent(0);
77 }
78
79 return testStream;
80 }
81
82
83 private LttngEventContent prepareToTest() {
84 LttngEventContent tmpEventContent = null;
85
86 // This trace should be valid
87 try {
88 testStream = null;
89 LTTngTextTrace tmpStream = initializeEventStream();
90 tmpEventContent = (LttngEventContent)tmpStream.getNextEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent();
91 }
92 catch (Exception e) {
93 fail("ERROR : Failed to get content!");
94 }
95
96 return tmpEventContent;
97 }
98
99 public void testConstructors() {
100 LttngEvent testEvent = null;
101 LttngEventContent testContent = null;
102 LttngEventField[] testFields = new LttngEventField[1];
103 testFields[0] = new LttngEventField("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
113 // Construction with good parameters (parent event)
114 try {
115 testContent = new LttngEventContent(testEvent);
116 }
117 catch( Exception e) {
118 fail("Construction with format, content and fields failed!");
119 }
120
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
131 // Copy constructor with correct parameters
132 try {
133 testContent = new LttngEventContent(testEvent);
134 new LttngEventContent(testContent);
135 }
136 catch( Exception e) {
137 fail("Copy constructor failed!");
138 }
139
140 }
141
142
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.getMapContent());
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.getEvent().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.getEvent().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.getMapContent());
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.getEvent().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.getMapContent().size() );
251
252 // This is the actual test
253 testContent.emptyContent();
254 assertSame("HashMap is not empty but should be!", 0, testContent.getMapContent().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 }
264
265 }
This page took 0.03899 seconds and 6 git commands to generate.