LTTng: CPU usage analysis from the LTTng kernel trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core.tests / src / org / eclipse / linuxtools / tmf / core / tests / ctfadaptor / CtfTmfEventTest.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial generation with CodePro tools
11 * Alexandre Montplaisir - Clean up, consolidate redundant tests
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor;
15
16 import static org.junit.Assert.assertArrayEquals;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertNotNull;
19 import static org.junit.Assert.assertNull;
20 import static org.junit.Assert.assertSame;
21 import static org.junit.Assume.assumeTrue;
22
23 import java.util.Set;
24
25 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
26 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator;
27 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
28 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEventFactory;
29 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
31 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
32 import org.eclipse.linuxtools.tmf.core.tests.shared.CtfTmfTestTrace;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
34 import org.junit.Before;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37
38 /**
39 * The class <code>CtfTmfEventTest</code> contains tests for the class
40 * <code>{@link CtfTmfEvent}</code>.
41 *
42 * @author ematkho
43 * @version $Revision: 1.0 $
44 */
45 public class CtfTmfEventTest {
46
47 private static final CtfTmfTestTrace testTrace = CtfTmfTestTrace.KERNEL;
48
49 private static CtfTmfEvent nullEvent;
50 private CtfTmfEvent fixture;
51
52 /**
53 * Test class initialization
54 */
55 @BeforeClass
56 public static void initialize() {
57 nullEvent = CtfTmfEventFactory.getNullEvent();
58 }
59
60 /**
61 * Perform pre-test initialization.
62 * @throws CTFReaderException error
63 */
64 @Before
65 public void setUp() throws CTFReaderException {
66 assumeTrue(testTrace.exists());
67 CtfTmfTrace trace = testTrace.getTrace();
68 CtfIterator tr = new CtfIterator(trace);
69 tr.advance();
70 fixture = tr.getCurrentEvent();
71 }
72
73 /**
74 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
75 */
76 @Test
77 public void testCTFEvent_read() {
78 assertNotNull(fixture);
79 }
80
81 /**
82 * Run the int getCPU() method test.
83 */
84 @Test
85 public void testGetCPU() {
86 int result = nullEvent.getCPU();
87 assertEquals(-1, result);
88 }
89
90 /**
91 * Run the String getEventName() method test.
92 */
93 @Test
94 public void testGetEventName() {
95 String result = nullEvent.getType().getName();
96 assertEquals("Empty CTF event", result);
97 }
98
99 /**
100 * Run the ArrayList<String> getFieldNames() method test.
101 */
102 @Test
103 public void testGetFieldNames() {
104 String[] result = fixture.getContent().getFieldNames();
105 assertNotNull(result);
106 }
107
108 /**
109 * Run the Object getFieldValue(String) method test.
110 */
111 @Test
112 public void testGetFieldValue() {
113 String fieldName = "pid";
114 ITmfEventField result = fixture.getContent().getField(fieldName);
115
116 assertNotNull(result);
117 assertNotNull(result.getValue());
118 }
119
120 /**
121 * Run the HashMap<String, CTFEventField> getFields() method test.
122 */
123 @Test
124 public void testGetFields() {
125 ITmfEventField[] fields = nullEvent.getContent().getFields();
126 ITmfEventField[] fields2 = new ITmfEventField[0];
127 assertArrayEquals(fields2, fields);
128 }
129
130 /**
131 * Run the ITmfEventField getSubFieldValue(String[]) method test.
132 */
133 @Test
134 public void testGetSubFieldValue() {
135 /* Field exists */
136 String[] names = { "pid" };
137 assertNotNull(fixture.getContent().getSubField(names));
138
139 /* First field exists, not the second */
140 String[] names2 = { "pid", "abcd" };
141 assertNull(fixture.getContent().getSubField(names2));
142
143 /* Both field do not exist */
144 String[] names3 = { "pfid", "abcd" };
145 assertNull(fixture.getContent().getSubField(names3));
146
147 /* TODO Missing case of embedded field, need event for it */
148 }
149
150 /**
151 * Run the long getID() method test.
152 */
153 @Test
154 public void testGetID() {
155 long result = nullEvent.getID();
156 assertEquals(-1L, result);
157 }
158
159 /**
160 * Run the long getTimestamp() method test.
161 */
162 @Test
163 public void testGetTimestamp() {
164 long result = nullEvent.getTimestamp().getValue();
165 assertEquals(-1L, result);
166 }
167
168 /**
169 * Test the getters for the reference, source and type.
170 */
171 @Test
172 public void testGetters() {
173 long rank = fixture.getRank();
174 CtfTmfTrace trace = fixture.getTrace();
175 String reference = fixture.getReference();
176 String source = fixture.getSource();
177 ITmfEventType type = fixture.getType();
178 assertEquals(ITmfContext.UNKNOWN_RANK, rank);
179 assertEquals("kernel", trace.getName());
180 assertEquals("channel0_1", reference);
181 assertEquals("1", source);
182 assertEquals("lttng_statedump_vm_map", type.toString());
183 }
184
185 /**
186 * Test the custom CTF attributes methods. The test trace doesn't have any,
187 * so the list of attributes should be empty.
188 */
189 @Test
190 public void testCustomAttributes() {
191 Set<String> attributes = fixture.listCustomAttributes();
192 assertEquals(0, attributes.size());
193
194 String attrib = fixture.getCustomAttribute("bozo");
195 assertNull(attrib);
196 }
197
198 /**
199 * Test the toString() method
200 */
201 @Test
202 public void testToString() {
203 String s = fixture.getContent().toString();
204 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s);
205 }
206
207 /**
208 * Test the {@link CtfTmfEventFactory#getNullEvent()} method, and the
209 * nullEvent's values.
210 */
211 @Test
212 public void testNullEvent() {
213 CtfTmfEvent nullEvent2 = CtfTmfEventFactory.getNullEvent();
214 assertSame(nullEvent2, nullEvent);
215 assertNotNull(nullEvent);
216 assertEquals(-1, nullEvent.getCPU());
217 assertEquals("Empty CTF event", nullEvent.getType().getName());
218 assertEquals("No stream", nullEvent.getReference());
219 assertArrayEquals(new ITmfEventField[0], nullEvent.getContent().getFields());
220 assertEquals(-1L, nullEvent.getID());
221 assertEquals(-1L, nullEvent.getTimestamp().getValue());
222 }
223 }
This page took 0.036245 seconds and 5 git commands to generate.