| 1 | package org.eclipse.linuxtools.lttng.tests.event; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.net.URL; |
| 5 | import java.util.HashMap; |
| 6 | |
| 7 | import junit.framework.TestCase; |
| 8 | |
| 9 | import org.eclipse.core.runtime.FileLocator; |
| 10 | import org.eclipse.core.runtime.Path; |
| 11 | import org.eclipse.linuxtools.lttng.event.LttngEvent; |
| 12 | import org.eclipse.linuxtools.lttng.event.LttngEventContent; |
| 13 | import org.eclipse.linuxtools.lttng.event.LttngEventField; |
| 14 | import org.eclipse.linuxtools.lttng.event.LttngEventType; |
| 15 | import org.eclipse.linuxtools.lttng.event.LttngTimestamp; |
| 16 | import org.eclipse.linuxtools.lttng.tests.LTTngCoreTestPlugin; |
| 17 | import org.eclipse.linuxtools.lttng.trace.LTTngTextTrace; |
| 18 | import org.eclipse.linuxtools.tmf.trace.TmfContext; |
| 19 | import org.eclipse.linuxtools.tmf.trace.TmfLocation; |
| 20 | |
| 21 | /* |
| 22 | Functions tested here : |
| 23 | |
| 24 | public LttngEventContent() |
| 25 | public LttngEventContent(LttngEvent thisParent) |
| 26 | public LttngEventContent(LttngEvent thisParent, HashMap<String, LttngEventField> thisContent) |
| 27 | public LttngEventContent(LttngEventContent oldContent) |
| 28 | |
| 29 | public void emptyContent() |
| 30 | |
| 31 | public LttngEventField[] getFields() |
| 32 | public LttngEventField getField(int position) |
| 33 | public LttngEventField getField(String name) |
| 34 | public LttngEvent getEvent() |
| 35 | public LttngEventType getType() |
| 36 | public Object[] getContent() |
| 37 | public HashMap<String, LttngEventField> getRawContent() |
| 38 | |
| 39 | public void setType(LttngEventType newType) |
| 40 | public void setEvent(LttngEvent newParent) |
| 41 | |
| 42 | public String toString() |
| 43 | */ |
| 44 | |
| 45 | @SuppressWarnings("nls") |
| 46 | public class LttngEventContentTest extends TestCase { |
| 47 | private final static String tracepath1="traceset/trace-15316events_nolost_newformat.txt"; |
| 48 | // private final static boolean skipIndexing=true; |
| 49 | |
| 50 | private final static String firstEventContentFirstField = "alignment:0"; |
| 51 | private final static String firstEventContentFirstFieldName = "alignment"; |
| 52 | private final static String firstEventContentType = "metadata/0/core_marker_id"; |
| 53 | |
| 54 | private final static String secondEventContentSecondField = "string:LTT state dump begin"; |
| 55 | private final static String secondEventContentSecondFieldName = "string"; |
| 56 | private final static String secondEventContentType = "kernel/0/vprintk"; |
| 57 | |
| 58 | private final static long timestampAfterMetadata = 13589760262237L; |
| 59 | |
| 60 | private static LTTngTextTrace testStream = null; |
| 61 | |
| 62 | private LTTngTextTrace initializeEventStream() { |
| 63 | if (testStream == null) { |
| 64 | try { |
| 65 | URL location = FileLocator.find(LTTngCoreTestPlugin.getPlugin().getBundle(), new Path(tracepath1), null); |
| 66 | File testfile = new File(FileLocator.toFileURL(location).toURI()); |
| 67 | LTTngTextTrace tmpStream = new LTTngTextTrace(testfile.getPath()); |
| 68 | testStream = tmpStream; |
| 69 | } |
| 70 | catch (Exception e) { |
| 71 | System.out.println("ERROR : Could not open " + tracepath1); |
| 72 | testStream = null; |
| 73 | } |
| 74 | } |
| 75 | else { |
| 76 | testStream.seekEvent(0); |
| 77 | } |
| 78 | |
| 79 | return testStream; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | private LttngEventContent prepareToTest() { |
| 84 | LttngEventContent tmpEventContent = null; |
| 85 | |
| 86 | // This trace should be valid |
| 87 | try { |
| 88 | testStream = null; |
| 89 | LTTngTextTrace tmpStream = initializeEventStream(); |
| 90 | tmpEventContent = (LttngEventContent)tmpStream.getNextEvent( new TmfContext(new TmfLocation<Long>(0L), 0) ).getContent(); |
| 91 | } |
| 92 | catch (Exception e) { |
| 93 | fail("ERROR : Failed to get content!"); |
| 94 | } |
| 95 | |
| 96 | return tmpEventContent; |
| 97 | } |
| 98 | |
| 99 | public void testConstructors() { |
| 100 | LttngEvent testEvent = null; |
| 101 | LttngEventContent testContent = null; |
| 102 | LttngEventContent testContent2 = null; |
| 103 | LttngEventField[] testFields = new LttngEventField[1]; |
| 104 | testFields[0] = new LttngEventField(testContent2, "test"); |
| 105 | |
| 106 | // Default construction with good argument |
| 107 | try { |
| 108 | testContent = new LttngEventContent(); |
| 109 | } |
| 110 | catch( Exception e) { |
| 111 | fail("Construction with format failed!"); |
| 112 | } |
| 113 | |
| 114 | // Construction with good parameters (parent event) |
| 115 | try { |
| 116 | testContent = new LttngEventContent(testEvent); |
| 117 | } |
| 118 | catch( Exception e) { |
| 119 | fail("Construction with format, content and fields failed!"); |
| 120 | } |
| 121 | |
| 122 | // Construction with good parameters (parent event and pre-parsed content) |
| 123 | try { |
| 124 | HashMap<String, LttngEventField> parsedContent = new HashMap<String, LttngEventField>(); |
| 125 | testContent = new LttngEventContent(testEvent, parsedContent); |
| 126 | } |
| 127 | catch( Exception e) { |
| 128 | fail("Construction with format, content and fields failed!"); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | // Copy constructor with correct parameters |
| 133 | try { |
| 134 | testContent = new LttngEventContent(testEvent); |
| 135 | testContent2 = new LttngEventContent(testContent); |
| 136 | } |
| 137 | catch( Exception e) { |
| 138 | fail("Copy constructor failed!"); |
| 139 | } |
| 140 | |
| 141 | } |
| 142 | |
| 143 | |
| 144 | public void testGetter() { |
| 145 | LttngEventContent testContent = null; |
| 146 | LTTngTextTrace tmpStream = null; |
| 147 | LttngEvent tmpEvent = null; |
| 148 | TmfContext tmpContext = null; |
| 149 | |
| 150 | // Require an event |
| 151 | tmpStream = initializeEventStream(); |
| 152 | tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0); |
| 153 | tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext); |
| 154 | testContent = prepareToTest(); |
| 155 | // getFieldS() |
| 156 | assertNotSame("getFields() returned null!",null,testContent.getFields() ); |
| 157 | |
| 158 | // *** FIXME *** |
| 159 | // Depending from the Java version because of the "hashcode()" on String. |
| 160 | // We can't really test that safetly |
| 161 | // |
| 162 | // getField(int) |
| 163 | //assertEquals("getField(int) returned unexpected result!",firstEventContentFirstField, testContent.getField(0).toString()); |
| 164 | assertNotSame("getField(int) returned unexpected result!",null, testContent.getField(0).toString()); |
| 165 | |
| 166 | |
| 167 | // getField(name) |
| 168 | assertEquals("getField(name) returned unexpected result!",firstEventContentFirstField, testContent.getField(firstEventContentFirstFieldName).toString()); |
| 169 | // getRawContent |
| 170 | assertNotSame("getRawContent() returned null!",null, testContent.getRawContent()); |
| 171 | // Test that get event return the correct event |
| 172 | assertTrue("getEvent() returned unexpected result!", tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue()); |
| 173 | // getType() |
| 174 | assertEquals("getType() returned unexpected result!",firstEventContentType, testContent.getType().toString()); |
| 175 | |
| 176 | //*** To test getFields with a fields number >0, we need to move to an event that have some more |
| 177 | tmpStream = initializeEventStream(); |
| 178 | tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0); |
| 179 | // Skip first events and seek to event pass metadata |
| 180 | tmpContext= tmpStream.seekEvent(new LttngTimestamp(timestampAfterMetadata) ); |
| 181 | // Skip first one |
| 182 | tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext); |
| 183 | |
| 184 | // Second event past metadata should have more fields |
| 185 | tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext); |
| 186 | // Get the content |
| 187 | testContent = tmpEvent.getContent(); |
| 188 | |
| 189 | // Test that get event return the correct event |
| 190 | assertTrue("getEvent() returned unexpected result!",tmpEvent.getTimestamp().getValue() == testContent.getEvent().getTimestamp().getValue()); |
| 191 | // getType() |
| 192 | assertEquals("getType() returned unexpected result!",secondEventContentType, testContent.getType().toString()); |
| 193 | |
| 194 | |
| 195 | // getFieldS() |
| 196 | assertNotSame("getFields() returned null!",null,testContent.getFields() ); |
| 197 | // getField(int) |
| 198 | assertEquals("getField(int) returned unexpected result!",secondEventContentSecondField, testContent.getField(1).toString()); |
| 199 | // getField(name) |
| 200 | assertEquals("getField(name) returned unexpected result!",secondEventContentSecondField, testContent.getField(secondEventContentSecondFieldName).toString()); |
| 201 | // getRawContent |
| 202 | assertNotSame("getRawContent() returned null!",null, testContent.getRawContent()); |
| 203 | |
| 204 | } |
| 205 | |
| 206 | public void testSetter() { |
| 207 | // Not much to test here, we will just make sure the set does not fail for any reason. |
| 208 | // It's pointless to test with a getter... |
| 209 | LTTngTextTrace tmpStream = null; |
| 210 | LttngEvent tmpEvent = null; |
| 211 | TmfContext tmpContext = null; |
| 212 | |
| 213 | // Require an event |
| 214 | tmpStream = initializeEventStream(); |
| 215 | tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0); |
| 216 | tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext); |
| 217 | |
| 218 | LttngEventContent tmpContent = prepareToTest(); |
| 219 | try { |
| 220 | tmpContent.setEvent(tmpEvent); |
| 221 | } |
| 222 | catch( Exception e) { |
| 223 | fail("setEvent(event) failed!"); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | LttngEventType testType = new LttngEventType(); |
| 228 | try { |
| 229 | tmpContent.setType(testType); |
| 230 | } |
| 231 | catch( Exception e) { |
| 232 | fail("setType(type) failed!"); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | public void testEmptyContent() { |
| 237 | LttngEventContent testContent = null; |
| 238 | LTTngTextTrace tmpStream = null; |
| 239 | LttngEvent tmpEvent = null; |
| 240 | TmfContext tmpContext = null; |
| 241 | |
| 242 | // Require an event |
| 243 | tmpStream = initializeEventStream(); |
| 244 | tmpContext = new TmfContext(new TmfLocation<Long>(0L), 0); |
| 245 | tmpEvent = (LttngEvent)tmpStream.getNextEvent(tmpContext); |
| 246 | // Get the content |
| 247 | testContent = tmpEvent.getContent(); |
| 248 | // Get all the fields to make sure there is something in the HashMap |
| 249 | testContent.getFields(); |
| 250 | // Just making sure there is something in the HashMap |
| 251 | assertNotSame("HashMap is empty but should not!", 0, testContent.getRawContent().size() ); |
| 252 | |
| 253 | // This is the actual test |
| 254 | testContent.emptyContent(); |
| 255 | assertSame("HashMap is not empty but should be!", 0, testContent.getRawContent().size() ); |
| 256 | } |
| 257 | |
| 258 | public void testToString() { |
| 259 | LttngEventContent tmpContent = prepareToTest(); |
| 260 | |
| 261 | // Just make sure toString() does not return null or the java reference |
| 262 | assertNotSame("toString returned null",null, tmpContent.toString() ); |
| 263 | assertNotSame("toString is not overridded!", tmpContent.getClass().getName() + '@' + Integer.toHexString(tmpContent.hashCode()), tmpContent.toString() ); |
| 264 | } |
| 265 | |
| 266 | } |