Refactor TmfTrace and dependencies - remove getTrace()
[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.internal.lttng.core.event.LttngEvent;
11 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventContent;
12 import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventType;
13 import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
14 import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTextTrace;
15 import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTrace;
16 import org.eclipse.linuxtools.lttng.jni.JniEvent;
17 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
18 import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
19 import org.osgi.framework.FrameworkUtil;
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 final URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path(tracepath1), null);
61 final File testfile = new File(FileLocator.toFileURL(location).toURI());
62 final LTTngTextTrace tmpStream = new LTTngTextTrace(null, testfile.getPath(), skipIndexing);
63 testStream = tmpStream;
64 }
65 catch (final Exception e) {
66 System.out.println("ERROR : Could not open " + tracepath1);
67 testStream = null;
68 }
69 else
70 testStream.seekEvent(0);
71
72 return testStream;
73 }
74
75 private LttngEvent prepareToTest() {
76 LttngEvent tmpEvent = null;
77
78 try {
79 final LTTngTextTrace tmpStream = initializeEventStream();
80 tmpEvent = (LttngEvent)tmpStream.getNextEvent(new TmfContext(new TmfLocation<Long>(0L), 0) );
81 }
82 catch (final Exception e) {
83 System.out.println("ERROR : Could not open " + tracepath1);
84 }
85
86 return tmpEvent;
87 }
88
89 public void testConstructors() {
90 LttngEvent testEvent = null;
91 final LTTngTrace testTrace = null;
92 LttngTimestamp testTime = null;
93 String testSource = null;
94 LttngEventType testType = null;
95 LttngEventContent testContent = null;
96 String testReference = null;
97 final JniEvent testJniEvent = null;
98 String[] testMarkerFields = null;
99
100 // This need to work if we want to perform tests
101 try {
102 // In order to test LttngEvent, we need all these constructors/functions to work.
103 // Make sure to run their unit tests first!
104 testMarkerFields = new String[] { "test" };
105 testEvent = null;
106 testTime = new LttngTimestamp(0L);
107 testSource = "test";
108 testType = new LttngEventType("test", 0L, "test", 0, testMarkerFields);
109 testContent = new LttngEventContent(testEvent);
110 testReference = "test";
111 }
112 catch( final Exception e) {
113 fail("Cannot allocate an EventStream, junit failed!");
114 }
115
116 // Test constructor with correct information
117 try {
118 testEvent = new LttngEvent(testTrace, testTime, testSource, testType, testContent, testReference, testJniEvent);
119 }
120 catch( final Exception e) {
121 fail("Construction with correct information failed!");
122 }
123
124 // Test about copy constructor
125 // Passing a null to copy constructor should fail
126 try {
127 new LttngEvent(null);
128 fail("Copy constructor with null old event should fail!");
129 }
130 catch( final Exception e) {
131 }
132
133 // Copy constructor used properly
134 testEvent = prepareToTest();
135 try {
136 new LttngEvent(testEvent);
137 }
138 catch( final Exception e) {
139 fail("Correct utilisation of copy constructor failed!");
140 }
141
142 }
143
144 public void testGetter() {
145 final LttngEvent testEvent = prepareToTest();
146
147 // These will test TMF functions but since we are expecting it to work...
148 assertEquals("Timestamp not what expected!",eventTimestamp,testEvent.getTimestamp().getValue());
149 assertEquals("Source not what expected!",eventSource,testEvent.getSource());
150 assertEquals("Reference not what expected!", eventReference, testEvent.getReference());
151
152 // These should be overridden functions
153 assertEquals("Type not what expected!",eventType,testEvent.getType().getName());
154 assertEquals("Channel not what expected!",eventChannel,testEvent.getChannelName());
155 assertEquals("CpuId not what expected!",eventCpu,testEvent.getCpuId());
156 assertEquals("Marker not what expected!",eventMarker,testEvent.getMarkerName());
157
158 // All events should have a parent
159 assertNotNull("Trace parent for this event is null!", testEvent.getTrace() );
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 final LttngEvent testEvent = prepareToTest();
171
172 LttngEventType testType = null;
173 LttngEventContent testContent = null;
174 final JniEvent testJniEvent = null;
175
176 final String[] testMarkerFields = new String[] { "test" };
177 testType = new LttngEventType("test", 0L, "test", 0, 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( final 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( final 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 final 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.035563 seconds and 5 git commands to generate.