2411dd2199f793cefaf44d2f3718496ace786e87
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core.tests / src / org / eclipse / linuxtools / tmf / ctf / core / tests / 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.ctf.core.tests;
15
16 import static org.junit.Assert.assertEquals;
17 import static org.junit.Assert.assertNotNull;
18 import static org.junit.Assert.assertNull;
19 import static org.junit.Assert.assertSame;
20 import static org.junit.Assume.assumeTrue;
21
22 import java.util.Collection;
23 import java.util.Set;
24
25 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
26 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
27 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
29 import org.eclipse.linuxtools.tmf.ctf.core.CtfIterator;
30 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEvent;
31 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfEventFactory;
32 import org.eclipse.linuxtools.tmf.ctf.core.CtfTmfTrace;
33 import org.eclipse.linuxtools.tmf.ctf.core.tests.shared.CtfTmfTestTrace;
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 *
63 * @throws CTFReaderException
64 * error
65 */
66 @Before
67 public void setUp() throws CTFReaderException {
68 assumeTrue(testTrace.exists());
69 try (CtfTmfTrace trace = testTrace.getTrace();
70 CtfIterator tr = new CtfIterator(trace);) {
71 tr.advance();
72 fixture = tr.getCurrentEvent();
73 }
74 }
75
76 /**
77 * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test.
78 */
79 @Test
80 public void testCTFEvent_read() {
81 assertNotNull(fixture);
82 }
83
84 /**
85 * Run the int getCPU() method test.
86 */
87 @Test
88 public void testGetCPU() {
89 int result = nullEvent.getCPU();
90 assertEquals(-1, result);
91 }
92
93 /**
94 * Run the String getEventName() method test.
95 */
96 @Test
97 public void testGetEventName() {
98 String result = nullEvent.getType().getName();
99 assertEquals("Empty CTF event", result);
100 }
101
102 /**
103 * Run the ArrayList<String> getFieldNames() method test.
104 */
105 @Test
106 public void testGetFieldNames() {
107 Collection<String> result = fixture.getContent().getFieldNames();
108 assertNotNull(result);
109 }
110
111 /**
112 * Run the Object getFieldValue(String) method test.
113 */
114 @Test
115 public void testGetFieldValue() {
116 String fieldName = "pid";
117 ITmfEventField result = fixture.getContent().getField(fieldName);
118
119 assertNotNull(result);
120 assertNotNull(result.getValue());
121 }
122
123 /**
124 * Run the HashMap<String, CTFEventField> getFields() method test.
125 */
126 @Test
127 public void testGetFields() {
128 Collection<? extends ITmfEventField> fields = nullEvent.getContent().getFields();
129 assertEquals(0, fields.size());
130 }
131
132 /**
133 * Run the ITmfEventField getSubFieldValue(String[]) method test.
134 */
135 @Test
136 public void testGetSubFieldValue() {
137 /* Field exists */
138 String[] names = { "pid" };
139 assertNotNull(fixture.getContent().getSubField(names));
140
141 /* First field exists, not the second */
142 String[] names2 = { "pid", "abcd" };
143 assertNull(fixture.getContent().getSubField(names2));
144
145 /* Both field do not exist */
146 String[] names3 = { "pfid", "abcd" };
147 assertNull(fixture.getContent().getSubField(names3));
148
149 /* TODO Missing case of embedded field, need event for it */
150 }
151
152 /**
153 * Run the long getID() method test.
154 */
155 @Test
156 public void testGetID() {
157 long result = nullEvent.getID();
158 assertEquals(-1L, result);
159 }
160
161 /**
162 * Run the long getTimestamp() method test.
163 */
164 @Test
165 public void testGetTimestamp() {
166 long result = nullEvent.getTimestamp().getValue();
167 assertEquals(-1L, result);
168 }
169
170 /**
171 * Test the getters for the reference, source and type.
172 */
173 @Test
174 public void testGetters() {
175 long rank = fixture.getRank();
176 try (CtfTmfTrace trace = fixture.getTrace();) {
177 assertEquals("kernel", trace.getName());
178 }
179 String reference = fixture.getReference();
180 String source = fixture.getSource();
181 ITmfEventType type = fixture.getType();
182 assertEquals(ITmfContext.UNKNOWN_RANK, rank);
183
184 assertEquals("channel0_1", reference);
185 assertEquals("1", source);
186 assertEquals("lttng_statedump_vm_map", type.toString());
187 }
188
189 /**
190 * Test the custom CTF attributes methods. The test trace doesn't have any,
191 * so the list of attributes should be empty.
192 */
193 @Test
194 public void testCustomAttributes() {
195 Set<String> attributes = fixture.listCustomAttributes();
196 assertEquals(0, attributes.size());
197
198 String attrib = fixture.getCustomAttribute("bozo");
199 assertNull(attrib);
200 }
201
202 /**
203 * Test the toString() method
204 */
205 @Test
206 public void testToString() {
207 String s = fixture.getContent().toString();
208 assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s);
209 }
210
211 /**
212 * Test the {@link CtfTmfEventFactory#getNullEvent()} method, and the
213 * nullEvent's values.
214 */
215 @Test
216 public void testNullEvent() {
217 CtfTmfEvent nullEvent2 = CtfTmfEventFactory.getNullEvent();
218 assertSame(nullEvent2, nullEvent);
219 assertNotNull(nullEvent);
220 assertEquals(-1, nullEvent.getCPU());
221 assertEquals("Empty CTF event", nullEvent.getType().getName());
222 assertNull(nullEvent.getReference());
223 assertEquals(0, nullEvent.getContent().getFields().size());
224 assertEquals(-1L, nullEvent.getID());
225 assertEquals(-1L, nullEvent.getTimestamp().getValue());
226 }
227 }
This page took 0.037798 seconds and 4 git commands to generate.