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