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