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