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