[Bug304438] Introduced TmfLocation
[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.TmfContext;
20 import org.eclipse.linuxtools.tmf.trace.TmfLocation;
21
22 /*
23 Functions tested here :
24 public LttngEvent(LttngTimestamp timestamp, TmfEventSource source, LttngEventType type, LttngEventContent content, LttngEventReference reference, JniEvent lttEvent)
25 public LttngEvent(LttngEvent oldEvent)
26
27 public String getChannelName()
28 public long getCpuId()
29 public String getMarkerName()
30 public LttngEventType getType()
31 public LttngEventContent getContent()
32
33 public void updateJniEventReference(JniEvent newJniEventReference)
34 public void setContent(LttngEventContent newContent)
35 public void setType(LttngEventType newType)
36
37 public JniEvent convertEventTmfToJni()
38
39 public String toString()
40 */
41
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 @SuppressWarnings("unused")
94 LttngEvent testAnotherEvent = null;
95 LttngTimestamp testTime = null;
96 TmfEventSource testSource = null;
97 LttngEventType testType = null;
98 LttngEventContent testContent = null;
99 LttngEventReference testReference = null;
100 JniEvent testJniEvent = null;
101 String[] testMarkerFields = null;
102
103 // This need to work if we want to perform tests
104 try {
105 // In order to test LttngEvent, we need all these constructors/functions to work.
106 // Make sure to run their unit tests first!
107 testMarkerFields = new String[1];
108 testEvent = null;
109 testTime = new LttngTimestamp(0L);
110 testSource = new TmfEventSource("test");
111 testType = new LttngEventType("test", 0L, "test", testMarkerFields);
112 testContent = new LttngEventContent(testEvent);
113 testReference = new LttngEventReference("test", "test");
114 }
115 catch( Exception e) {
116 fail("Cannot allocate an EventStream, junit failed!");
117 }
118
119 // Test constructor with correct information
120 try {
121 testEvent = new LttngEvent( testTime, testSource, testType, testContent, testReference, testJniEvent);
122 }
123 catch( Exception e) {
124 fail("Construction with correct information failed!");
125 }
126
127 // Test about copy constructor
128 // Passing a null to copy constructor should fail
129 try {
130 testAnotherEvent = new LttngEvent(null);
131 fail("Copy constructor with null old event should fail!");
132 }
133 catch( Exception e) {
134 }
135
136 // Copy constructor used properly
137 testEvent = prepareToTest();
138 try {
139 testAnotherEvent = new LttngEvent(testEvent);
140 }
141 catch( Exception e) {
142 fail("Correct utilisation of copy constructor failed!");
143 }
144
145 }
146
147 public void testGetter() {
148 LttngEvent testEvent = prepareToTest();
149
150 // These will test TMF functions but since we are expecting it to work...
151 assertEquals("Timestamp not what expected!",eventTimestamp,testEvent.getTimestamp().getValue());
152 assertEquals("Source not what expected!",eventSource,testEvent.getSource().getSourceId());
153 assertEquals("Reference not what expected!",eventReference,((String)testEvent.getReference().toString()) );
154
155 // These should be overridden functions
156 assertEquals("Type not what expected!",eventType,testEvent.getType().getTypeId());
157 assertEquals("Channel not what expected!",eventChannel,testEvent.getChannelName());
158 assertEquals("CpuId not what expected!",eventCpu,testEvent.getCpuId());
159 assertEquals("Marker not what expected!",eventMarker,testEvent.getMarkerName());
160
161 // *** FIXME ***
162 // Depending from the Java version because of the "hashcode()" on String.
163 // We can't really test that safetly
164 //
165 //assertEquals("Content not what expected!",eventContent,testEvent.getContent().toString());
166 assertNotSame("Content is null!", null,testEvent.getContent());
167 }
168
169 public void testSetter() {
170 LttngEvent testEvent = prepareToTest();
171
172 LttngEventType testType = null;
173 LttngEventContent testContent = null;
174 JniEvent testJniEvent = null;
175
176 String[] testMarkerFields = new String[1];
177 testType = new LttngEventType("test", 0L, "test", testMarkerFields);
178 testContent = new LttngEventContent(testEvent);
179
180 try {
181 // *** FIXME ***
182 // This won't do anything good on a text trace
183 testEvent.updateJniEventReference(testJniEvent);
184
185 testEvent.setContent(testContent);
186 testEvent.setType(testType);
187 }
188 catch( Exception e) {
189 fail("Setters raised an exception!");
190 }
191
192 assertSame("SetType failed : type not what expected!",testType,testEvent.getType());
193 assertSame("SetContent failed : content not what expected!",testContent,testEvent.getContent());
194
195 }
196
197
198 public void testConversion() {
199 @SuppressWarnings("unused")
200 JniEvent tmpJniEvent = null;
201 LttngEvent testEvent = null;
202
203 testEvent = prepareToTest();
204
205 try {
206 tmpJniEvent = testEvent.convertEventTmfToJni();
207 }
208 catch( Exception e) {
209 fail("Conversion raised an exception!");
210 }
211
212 // *** FIXME ***
213 // This test can't work with a text trace, commented for now
214 //assertNotSame("Conversion returned a null event!",null, tmpJniEvent );
215 }
216
217 public void testToString() {
218 LttngEvent tmpEvent = prepareToTest();
219
220 // Just make sure toString() does not return null or the java reference
221 assertNotSame("toString returned null",null, tmpEvent.toString() );
222 assertNotSame("toString is not overridded!", tmpEvent.getClass().getName() + '@' + Integer.toHexString(tmpEvent.hashCode()), tmpEvent.toString() );
223 }
224
225 }
This page took 0.035673 seconds and 6 git commands to generate.