| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2012, 2015 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 | * Patrick Tasse - Remove getSubField |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.tracecompass.tmf.ctf.core.tests.event; |
| 16 | |
| 17 | import static org.junit.Assert.assertEquals; |
| 18 | import static org.junit.Assert.assertNotNull; |
| 19 | import static org.junit.Assert.assertNull; |
| 20 | |
| 21 | import java.util.Collection; |
| 22 | import java.util.Set; |
| 23 | |
| 24 | import org.eclipse.jdt.annotation.NonNull; |
| 25 | import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator; |
| 26 | import org.eclipse.tracecompass.testtraces.ctf.CtfTestTrace; |
| 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
| 28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventType; |
| 29 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; |
| 30 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; |
| 31 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventFactory; |
| 32 | import org.eclipse.tracecompass.tmf.ctf.core.tests.shared.CtfTmfTestTraceUtils; |
| 33 | import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace; |
| 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 @NonNull CtfTestTrace testTrace = CtfTestTrace.KERNEL; |
| 47 | |
| 48 | private static CtfTmfEvent nullEvent; |
| 49 | private CtfTmfEvent fixture; |
| 50 | |
| 51 | /** |
| 52 | * Perform pre-test initialization. |
| 53 | */ |
| 54 | @Before |
| 55 | public void setUp() { |
| 56 | CtfTmfTrace trace = CtfTmfTestTraceUtils.getTrace(testTrace); |
| 57 | try (CtfIterator tr = (CtfIterator) trace.createIterator();) { |
| 58 | tr.advance(); |
| 59 | fixture = tr.getCurrentEvent(); |
| 60 | nullEvent = CtfTmfEventFactory.getNullEvent(trace); |
| 61 | } |
| 62 | trace.dispose(); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testCTFEvent_read() { |
| 70 | assertNotNull(fixture); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Run the int getCPU() method test. |
| 75 | */ |
| 76 | @Test |
| 77 | public void testGetCPU() { |
| 78 | int result = nullEvent.getCPU(); |
| 79 | assertEquals(-1, result); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Run the String getEventName() method test. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testGetEventName() { |
| 87 | String result = nullEvent.getType().getName(); |
| 88 | assertEquals("Empty CTF event", result); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Run the ArrayList<String> getFieldNames() method test. |
| 93 | */ |
| 94 | @Test |
| 95 | public void testGetFieldNames() { |
| 96 | Collection<String> result = fixture.getContent().getFieldNames(); |
| 97 | assertNotNull(result); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Run the Object getFieldValue(String) method test. |
| 102 | */ |
| 103 | @Test |
| 104 | public void testGetFieldValue() { |
| 105 | String fieldName = "pid"; |
| 106 | ITmfEventField result = fixture.getContent().getField(fieldName); |
| 107 | |
| 108 | assertNotNull(result); |
| 109 | assertNotNull(result.getValue()); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Run the HashMap<String, CTFEventField> getFields() method test. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testGetFields() { |
| 117 | Collection<? extends ITmfEventField> fields = nullEvent.getContent().getFields(); |
| 118 | assertEquals(0, fields.size()); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Run the ITmfEventField getSubFieldValue(String[]) method test. |
| 123 | */ |
| 124 | @Test |
| 125 | public void testGetSubFieldValue() { |
| 126 | /* Field exists */ |
| 127 | String[] names = { "pid" }; |
| 128 | assertNotNull(fixture.getContent().getField(names)); |
| 129 | |
| 130 | /* First field exists, not the second */ |
| 131 | String[] names2 = { "pid", "abcd" }; |
| 132 | assertNull(fixture.getContent().getField(names2)); |
| 133 | |
| 134 | /* Both field do not exist */ |
| 135 | String[] names3 = { "pfid", "abcd" }; |
| 136 | assertNull(fixture.getContent().getField(names3)); |
| 137 | |
| 138 | /* TODO Missing case of embedded field, need event for it */ |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Run the long getTimestamp() method test. |
| 143 | */ |
| 144 | @Test |
| 145 | public void testGetTimestamp() { |
| 146 | long result = nullEvent.getTimestamp().getValue(); |
| 147 | assertEquals(-1L, result); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Test the getters for the reference, source and type. |
| 152 | */ |
| 153 | @Test |
| 154 | public void testGetters() { |
| 155 | long rank = fixture.getRank(); |
| 156 | CtfTmfTrace trace = fixture.getTrace(); |
| 157 | assertEquals("kernel", trace.getName()); |
| 158 | |
| 159 | String reference = fixture.getChannel(); |
| 160 | int cpu = fixture.getCPU(); |
| 161 | ITmfEventType type = fixture.getType(); |
| 162 | assertEquals(ITmfContext.UNKNOWN_RANK, rank); |
| 163 | |
| 164 | assertEquals("channel0_1", reference); |
| 165 | assertEquals(1, cpu); |
| 166 | assertEquals("lttng_statedump_vm_map", type.toString()); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Test the custom CTF attributes methods. The test trace doesn't have any, |
| 171 | * so the list of attributes should be empty. |
| 172 | */ |
| 173 | @Test |
| 174 | public void testCustomAttributes() { |
| 175 | Set<String> attributes = fixture.listCustomAttributes(); |
| 176 | assertEquals(0, attributes.size()); |
| 177 | |
| 178 | String attrib = fixture.getCustomAttribute("bozo"); |
| 179 | assertNull(attrib); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Test the toString() method |
| 184 | */ |
| 185 | @Test |
| 186 | public void testToString() { |
| 187 | String s = fixture.getContent().toString(); |
| 188 | assertEquals("pid=1922, start=0xb73ea000, end=0xb73ec000, flags=0x8000075, inode=917738, pgoff=0", s); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Test the {@link CtfTmfEventFactory#getNullEvent()} method, and |
| 193 | * the nullEvent's values. |
| 194 | */ |
| 195 | @Test |
| 196 | public void testNullEvent() { |
| 197 | CtfTmfEvent nullEvent2 = CtfTmfEventFactory.getNullEvent(fixture.getTrace()); |
| 198 | assertEquals(nullEvent2, nullEvent); |
| 199 | assertNotNull(nullEvent); |
| 200 | assertEquals(-1, nullEvent.getCPU()); |
| 201 | assertEquals("Empty CTF event", nullEvent.getType().getName()); |
| 202 | assertEquals("", nullEvent.getChannel()); |
| 203 | assertEquals(0, nullEvent.getContent().getFields().size()); |
| 204 | assertEquals(-1L, nullEvent.getTimestamp().getValue()); |
| 205 | } |
| 206 | } |