2010-01-22 Francois Chouinard <fchouinard@gmail.com>
[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 return testStream;
70 }
71
72 private LttngEvent prepareToTest() {
73 LttngEvent tmpEvent = null;
74
75 try {
76 LTTngTextTrace tmpStream = initializeEventStream();
77 tmpEvent = (LttngEvent)tmpStream.getNextEvent(new TmfTraceContext(0, new LttngTimestamp(0L), 0) );
78 }
79 catch (Exception e) {
80 System.out.println("ERROR : Could not open " + tracepath1);
81 }
82
83 return tmpEvent;
84 }
85
86 public void testConstructors() {
87 LttngEvent testEvent = null;
88 @SuppressWarnings("unused")
89 LttngEvent testAnotherEvent = null;
90 LttngTimestamp testTime = null;
91 TmfEventSource testSource = null;
92 LttngEventType testType = null;
93 LttngEventContent testContent = null;
94 LttngEventReference testReference = null;
95 JniEvent testJniEvent = null;
96 String[] testMarkerFields = null;
97
98 // This need to work if we want to perform tests
99 try {
100 // In order to test LttngEvent, we need all these constructors/functions to work.
101 // Make sure to run their unit tests first!
102 testMarkerFields = new String[1];
103 testEvent = null;
104 testTime = new LttngTimestamp(0L);
105 testSource = new TmfEventSource("test");
106 testType = new LttngEventType("test", 0L, "test", testMarkerFields);
107 testContent = new LttngEventContent(testEvent);
108 testReference = new LttngEventReference("test", "test");
109 }
110 catch( Exception e) {
111 fail("Cannot allocate an EventStream, junit failed!");
112 }
113
114 // Test constructor with correct information
115 try {
116 testEvent = new LttngEvent( testTime, testSource, testType, testContent, testReference, testJniEvent);
117 }
118 catch( Exception e) {
119 fail("Construction with correct information failed!");
120 }
121
122 // Test about copy constructor
123 // Passing a null to copy constructor should fail
124 try {
125 testAnotherEvent = new LttngEvent(null);
126 fail("Copy constructor with null old event should fail!");
127 }
128 catch( Exception e) {
129 }
130
131 // Copy constructor used properly
132 testEvent = prepareToTest();
133 try {
134 testAnotherEvent = new LttngEvent(testEvent);
135 }
136 catch( Exception e) {
137 fail("Correct utilisation of copy constructor failed!");
138 }
139
140 }
141
142 public void testGetter() {
143 LttngEvent testEvent = prepareToTest();
144
145 // These will test TMF functions but since we are expecting it to work...
146 assertEquals("Timestamp not what expected!",eventTimestamp,testEvent.getTimestamp().getValue());
147 assertEquals("Source not what expected!",eventSource,testEvent.getSource().getSourceId());
148 assertEquals("Reference not what expected!",eventReference,((String)testEvent.getReference().toString()) );
149
150 // These should be overridden functions
151 assertEquals("Type not what expected!",eventType,testEvent.getType().getTypeId());
152 assertEquals("Channel not what expected!",eventChannel,testEvent.getChannelName());
153 assertEquals("CpuId not what expected!",eventCpu,testEvent.getCpuId());
154 assertEquals("Marker not what expected!",eventMarker,testEvent.getMarkerName());
155 assertEquals("Content not what expected!",eventContent,testEvent.getContent().toString());
156 }
157
158 public void testSetter() {
159 LttngEvent testEvent = prepareToTest();
160
161 LttngEventType testType = null;
162 LttngEventContent testContent = null;
163 JniEvent testJniEvent = null;
164
165 String[] testMarkerFields = new String[1];
166 testType = new LttngEventType("test", 0L, "test", testMarkerFields);
167 testContent = new LttngEventContent(testEvent);
168
169 try {
170 // *** FIXME ***
171 // This won't do anything good on a text trace
172 testEvent.updateJniEventReference(testJniEvent);
173
174 testEvent.setContent(testContent);
175 testEvent.setType(testType);
176 }
177 catch( Exception e) {
178 fail("Setters raised an exception!");
179 }
180
181 assertSame("SetType failed : type not what expected!",testType,testEvent.getType());
182 assertSame("SetContent failed : content not what expected!",testContent,testEvent.getContent());
183
184 }
185
186
187 public void testConversion() {
188 @SuppressWarnings("unused")
189 JniEvent tmpJniEvent = null;
190 LttngEvent testEvent = null;
191
192 testEvent = prepareToTest();
193
194 try {
195 tmpJniEvent = testEvent.convertEventTmfToJni();
196 }
197 catch( Exception e) {
198 fail("Conversion raised an exception!");
199 }
200
201 // *** FIXME ***
202 // This test can't work with a text trace, commented for now
203 //assertNotSame("Conversion returned a null event!",null, tmpJniEvent );
204 }
205
206 public void testToString() {
207 LttngEvent tmpEvent = prepareToTest();
208
209 // Just make sure toString() does not return null or the java reference
210 assertNotSame("toString returned null",null, tmpEvent.toString() );
211 assertNotSame("toString is not overridded!", tmpEvent.getClass().getName() + '@' + Integer.toHexString(tmpEvent.hashCode()), tmpEvent.toString() );
212 }
213
214 }
This page took 0.036696 seconds and 6 git commands to generate.