7ad8b7c1ea9b8e99ef8364784d120535cae13098
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.core.tests / src / org / eclipse / linuxtools / lttng / core / tests / event / LttngEventTest.java
1 package org.eclipse.linuxtools.lttng.core.tests.event;
2
3 import java.io.File;
4 import java.net.URL;
5
6 import junit.framework.TestCase;
7
8 import org.eclipse.core.runtime.FileLocator;
9 import org.eclipse.core.runtime.Path;
10 import org.eclipse.linuxtools.lttng.core.event.LttngEvent;
11 import org.eclipse.linuxtools.lttng.core.event.LttngEventContent;
12 import org.eclipse.linuxtools.lttng.core.event.LttngEventType;
13 import org.eclipse.linuxtools.lttng.core.event.LttngTimestamp;
14 import org.eclipse.linuxtools.lttng.core.tests.LTTngCoreTestPlugin;
15 import org.eclipse.linuxtools.lttng.core.trace.LTTngTextTrace;
16 import org.eclipse.linuxtools.lttng.core.trace.LTTngTrace;
17 import org.eclipse.linuxtools.lttng.jni.JniEvent;
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 public LttngEvent(LttngTimestamp timestamp, TmfEventSource source, LttngEventType type, LttngEventContent content, LttngEventReference reference, JniEvent lttEvent)
24 public LttngEvent(LttngEvent oldEvent)
25
26 public String getChannelName()
27 public long getCpuId()
28 public String getMarkerName()
29 public LttngEventType getType()
30 public LttngEventContent getContent()
31
32 public void updateJniEventReference(JniEvent newJniEventReference)
33 public void setContent(LttngEventContent newContent)
34 public void setType(LttngEventType newType)
35
36 public JniEvent convertEventTmfToJni()
37
38 public String toString()
39 */
40
41 @SuppressWarnings("nls")
42 public class LttngEventTest extends TestCase {
43 private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt";
44 private final static boolean skipIndexing=true;
45
46 private final static long eventTimestamp = 13589759412128L;
47 private final static String eventSource = "Kernel Core";
48 private final static String eventType = "metadata/0/core_marker_id";
49 private final static String eventChannel = "metadata";
50 private final static long eventCpu = 0;
51 private final static String eventMarker = "core_marker_id";
52 // private final static String eventContent = "alignment:0 size_t:4 int:4 name:vm_map pointer:4 event_id:0 long:4 channel:vm_state ";
53 private final static String eventReference = eventChannel + "_" + eventCpu;
54
55
56 private static LTTngTextTrace testStream = null;
57 private LTTngTextTrace initializeEventStream() {
58 if (testStream == null) {
59 try {
60 URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null);
61 File testfile = new File(FileLocator.toFileURL(location).toURI());
62 LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath(), skipIndexing);
63 testStream = tmpStream;
64 }
65 catch (Exception e) {
66 System.out.println("ERROR : Could not open " + tracepath1);
67 testStream = null;
68 }
69 }
70 else {
71 testStream.seekEvent(0);
72 }
73
74 return testStream;
75 }
76
77 private LttngEvent prepareToTest() {
78 LttngEvent tmpEvent = null;
79
80 try {
81 LTTngTextTrace tmpStream = initializeEventStream();
82 tmpEvent = (LttngEvent)tmpStream.getNextEvent(new TmfContext(new TmfLocation<Long>(0L), 0) );
83 }
84 catch (Exception e) {
85 System.out.println("ERROR : Could not open " + tracepath1);
86 }
87
88 return tmpEvent;
89 }
90
91 public void testConstructors() {
92 LttngEvent testEvent = null;
93 LTTngTrace testTrace = null;
94 @SuppressWarnings("unused")
95 LttngEvent testAnotherEvent = null;
96 LttngTimestamp testTime = null;
97 String testSource = null;
98 LttngEventType testType = null;
99 LttngEventContent testContent = null;
100 String testReference = null;
101 JniEvent testJniEvent = null;
102 String[] testMarkerFields = null;
103
104 // This need to work if we want to perform tests
105 try {
106 // In order to test LttngEvent, we need all these constructors/functions to work.
107 // Make sure to run their unit tests first!
108 testMarkerFields = new String[] { "test" };
109 testEvent = null;
110 testTime = new LttngTimestamp(0L);
111 testSource = "test";
112 testType = new LttngEventType("test", 0L, "test", 0, testMarkerFields);
113 testContent = new LttngEventContent(testEvent);
114 testReference = "test";
115 }
116 catch( Exception e) {
117 fail("Cannot allocate an EventStream, junit failed!");
118 }
119
120 // Test constructor with correct information
121 try {
122 testEvent = new LttngEvent(testTrace, testTime, testSource, testType, testContent, testReference, testJniEvent);
123 }
124 catch( Exception e) {
125 fail("Construction with correct information failed!");
126 }
127
128 // Test about copy constructor
129 // Passing a null to copy constructor should fail
130 try {
131 testAnotherEvent = new LttngEvent(null);
132 fail("Copy constructor with null old event should fail!");
133 }
134 catch( Exception e) {
135 }
136
137 // Copy constructor used properly
138 testEvent = prepareToTest();
139 try {
140 testAnotherEvent = new LttngEvent(testEvent);
141 }
142 catch( Exception e) {
143 fail("Correct utilisation of copy constructor failed!");
144 }
145
146 }
147
148 public void testGetter() {
149 LttngEvent testEvent = prepareToTest();
150
151 // These will test TMF functions but since we are expecting it to work...
152 assertEquals("Timestamp not what expected!",eventTimestamp,testEvent.getTimestamp().getValue());
153 assertEquals("Source not what expected!",eventSource,testEvent.getSource());
154 assertEquals("Reference not what expected!", eventReference, testEvent.getReference());
155
156 // These should be overridden functions
157 assertEquals("Type not what expected!",eventType,testEvent.getType().getName());
158 assertEquals("Channel not what expected!",eventChannel,testEvent.getChannelName());
159 assertEquals("CpuId not what expected!",eventCpu,testEvent.getCpuId());
160 assertEquals("Marker not what expected!",eventMarker,testEvent.getMarkerName());
161
162 // All events should have a parent
163 assertNotNull("Trace parent for this event is null!", testEvent.getTrace() );
164
165 // *** FIXME ***
166 // Depending from the Java version because of the "hashcode()" on String.
167 // We can't really test that safetly
168 //
169 //assertEquals("Content not what expected!",eventContent,testEvent.getContent().toString());
170 assertNotSame("Content is null!", null,testEvent.getContent());
171 }
172
173 public void testSetter() {
174 LttngEvent testEvent = prepareToTest();
175
176 LttngEventType testType = null;
177 LttngEventContent testContent = null;
178 JniEvent testJniEvent = null;
179
180 String[] testMarkerFields = new String[] { "test" };
181 testType = new LttngEventType("test", 0L, "test", 0, testMarkerFields);
182 testContent = new LttngEventContent(testEvent);
183
184 try {
185 // *** FIXME ***
186 // This won't do anything good on a text trace
187 testEvent.updateJniEventReference(testJniEvent);
188
189 testEvent.setContent(testContent);
190 testEvent.setType(testType);
191 }
192 catch( Exception e) {
193 fail("Setters raised an exception!");
194 }
195
196 assertSame("SetType failed : type not what expected!",testType,testEvent.getType());
197 assertSame("SetContent failed : content not what expected!",testContent,testEvent.getContent());
198
199 }
200
201
202 public void testConversion() {
203 @SuppressWarnings("unused")
204 JniEvent tmpJniEvent = null;
205 LttngEvent testEvent = null;
206
207 testEvent = prepareToTest();
208
209 try {
210 tmpJniEvent = testEvent.convertEventTmfToJni();
211 }
212 catch( Exception e) {
213 fail("Conversion raised an exception!");
214 }
215
216 // *** FIXME ***
217 // This test can't work with a text trace, commented for now
218 //assertNotSame("Conversion returned a null event!",null, tmpJniEvent );
219 }
220
221 public void testToString() {
222 LttngEvent tmpEvent = prepareToTest();
223
224 // Just make sure toString() does not return null or the java reference
225 assertNotSame("toString returned null",null, tmpEvent.toString() );
226 assertNotSame("toString is not overridded!", tmpEvent.getClass().getName() + '@' + Integer.toHexString(tmpEvent.hashCode()), tmpEvent.toString() );
227 }
228
229 }
This page took 0.036483 seconds and 5 git commands to generate.