Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 57e9c2561f49e415aa82f890835982b806368365 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package org.eclipse.linuxtools.lttng.core.tests.event;

import java.io.File;
import java.net.URL;

import junit.framework.TestCase;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.linuxtools.internal.lttng.core.event.LttngEvent;
import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventContent;
import org.eclipse.linuxtools.internal.lttng.core.event.LttngEventType;
import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTextTrace;
import org.eclipse.linuxtools.internal.lttng.core.trace.LTTngTrace;
import org.eclipse.linuxtools.lttng.jni.JniEvent;
import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
import org.eclipse.linuxtools.tmf.core.trace.TmfLocation;
import org.osgi.framework.FrameworkUtil;

/*
 Functions tested here :
    public LttngEvent(LttngTimestamp timestamp, TmfEventSource source, LttngEventType type, LttngEventContent content, LttngEventReference reference, JniEvent lttEvent)
    public LttngEvent(LttngEvent oldEvent)
    
    public String getChannelName()
    public long getCpuId()
    public String getMarkerName()
    public LttngEventType getType()
    public LttngEventContent getContent()
    
    public void updateJniEventReference(JniEvent newJniEventReference)
    public void setContent(LttngEventContent newContent)
    public void setType(LttngEventType newType)
    
    public JniEvent convertEventTmfToJni()
    
	public String toString()
 */

@SuppressWarnings("nls")
public class LttngEventTest extends TestCase {
    private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt";
    private final static boolean skipIndexing=true;
    
    private final static long   eventTimestamp 	= 13589759412128L;
    private final static String eventSource 	= "Kernel Core";
    private final static String eventType 		= "metadata/0/core_marker_id";
    private final static String eventChannel 	= "metadata";
    private final static long 	eventCpu 		= 0;
    private final static String eventMarker 	= "core_marker_id";
//    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 ";
    private final static String eventReference 	= eventChannel + "_" + eventCpu;
    
    
    private static LTTngTextTrace testStream = null;
    private LTTngTextTrace initializeEventStream() {
		if (testStream == null) {
			try {
				URL location = FileLocator.find(FrameworkUtil.getBundle(this.getClass()), new Path(tracepath1), null);
				File testfile = new File(FileLocator.toFileURL(location).toURI());
				LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getName(), testfile.getPath(), skipIndexing);
				testStream = tmpStream;
			} 
			catch (Exception e) {
				System.out.println("ERROR : Could not open " + tracepath1);
				testStream = null;
			}
		}
		else {
			testStream.seekEvent(0);
		}
		
		return testStream;
	}

    private LttngEvent prepareToTest() {
		LttngEvent tmpEvent = null;
		
		try {
			LTTngTextTrace tmpStream = initializeEventStream();
			tmpEvent = (LttngEvent)tmpStream.getNextEvent(new TmfContext(new TmfLocation<Long>(0L), 0) );
		}
		catch (Exception e) {
			System.out.println("ERROR : Could not open " + tracepath1);
		}

		return tmpEvent;
	}

	public void testConstructors() {
        LttngEvent 			testEvent 		= null;
        LTTngTrace			testTrace 		= null;
        LttngTimestamp		testTime		= null;
        String      		testSource 		= null;
        LttngEventType   	testType   		= null;
        LttngEventContent	testContent		= null;
        String              testReference 	= null;
        JniEvent			testJniEvent 	= null;
		String[]			testMarkerFields = null;
		
        // This need to work if we want to perform tests
        try {
    			// In order to test LttngEvent, we need all these constructors/functions to work.
            	// Make sure to run their unit tests first!
        		testMarkerFields = new String[] { "test" };
                testEvent 	= null;
                testTime	= new LttngTimestamp(0L);
                testSource 	= "test";
                testType   	= new LttngEventType("test", 0L, "test", 0, testMarkerFields);
                testContent	= new LttngEventContent(testEvent);
                testReference = "test";
        }
        catch( Exception e) {
                fail("Cannot allocate an EventStream, junit failed!");
        }
		
        // Test constructor with correct information
        try {
        		testEvent = new LttngEvent(testTrace, testTime, testSource, testType, testContent, testReference, testJniEvent);
        }
        catch( Exception e) { 
                fail("Construction with correct information failed!");
        }
        
        // Test about copy constructor
        // Passing a null to copy constructor should fail
        try {
        	 new LttngEvent(null);
            fail("Copy constructor with null old event should fail!");
		}
		catch( Exception e) { 
		}
        
		// Copy constructor used properly
        testEvent = prepareToTest();
        try {
        	new LttngEvent(testEvent);
		}
		catch( Exception e) { 
			fail("Correct utilisation of copy constructor failed!");
		}
		
	}
	
	public void testGetter() {
    	LttngEvent testEvent = prepareToTest();
    	
    	// These will test TMF functions but since we are expecting it to work...
    	assertEquals("Timestamp not what expected!",eventTimestamp,testEvent.getTimestamp().getValue());
    	assertEquals("Source not what expected!",eventSource,testEvent.getSource());
    	assertEquals("Reference not what expected!", eventReference, testEvent.getReference());
    	
    	// These should be overridden functions
    	assertEquals("Type not what expected!",eventType,testEvent.getType().getName());
    	assertEquals("Channel not what expected!",eventChannel,testEvent.getChannelName());
    	assertEquals("CpuId not what expected!",eventCpu,testEvent.getCpuId());
    	assertEquals("Marker not what expected!",eventMarker,testEvent.getMarkerName());
    	
    	// All events should have a parent
    	assertNotNull("Trace parent for this event is null!", testEvent.getTrace() );
    	
    	// *** FIXME ***
    	// Depending from the Java version because of the "hashcode()" on String. 
    	// We can't really test that safetly
    	//
    	//assertEquals("Content not what expected!",eventContent,testEvent.getContent().toString());
    	assertNotSame("Content is null!", null,testEvent.getContent());
    }
    
	public void testSetter() {
    	LttngEvent testEvent = prepareToTest();
    	
        LttngEventType   	testType   		= null;
        LttngEventContent	testContent		= null;
        JniEvent			testJniEvent 	= null;
		
        String[] testMarkerFields = new String[] { "test" };
        testType   	= new LttngEventType("test", 0L, "test", 0, testMarkerFields);
        testContent	= new LttngEventContent(testEvent);
        
    	try {
    		// *** FIXME ***
    		// This won't do anything good on a text trace
        	testEvent.updateJniEventReference(testJniEvent);
        	
        	testEvent.setContent(testContent);
        	testEvent.setType(testType);
		}
		catch( Exception e) { 
			fail("Setters raised an exception!");
		}
    	
		assertSame("SetType failed : type not what expected!",testType,testEvent.getType());
    	assertSame("SetContent failed : content not what expected!",testContent,testEvent.getContent());
		
	}
	
	
	public void testConversion() {
    	@SuppressWarnings("unused")
		JniEvent tmpJniEvent = null;
    	LttngEvent testEvent = null;
    	
        testEvent = prepareToTest();
        
        try {
        	tmpJniEvent = testEvent.convertEventTmfToJni();
		}
		catch( Exception e) { 
			fail("Conversion raised an exception!");
		}
		
		// *** FIXME ***
		// This test can't work with a text trace, commented for now 
		//assertNotSame("Conversion returned a null event!",null, tmpJniEvent );
    }
    
	public void testToString() {
    	LttngEvent tmpEvent = prepareToTest();
    	
		// Just make sure toString() does not return null or the java reference
		assertNotSame("toString returned null",null, tmpEvent.toString() );
		assertNotSame("toString is not overridded!", tmpEvent.getClass().getName() + '@' + Integer.toHexString(tmpEvent.hashCode()), tmpEvent.toString() );
    }
	
}

Back to the top