| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013, 2014 Ericsson |
| 3 | * All rights reserved. This program and the accompanying materials |
| 4 | * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | * which accompanies this distribution, and is available at |
| 6 | * http://www.eclipse.org/legal/epl-v10.html |
| 7 | * |
| 8 | * Contributors: |
| 9 | * Matthew Khouzam - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.linuxtools.ctf.core.tests.types; |
| 13 | |
| 14 | import static org.junit.Assert.assertEquals; |
| 15 | import static org.junit.Assert.assertNotNull; |
| 16 | import static org.junit.Assert.assertNull; |
| 17 | |
| 18 | import java.nio.ByteOrder; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
| 23 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; |
| 24 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 25 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; |
| 26 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration; |
| 27 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
| 28 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDeclaration; |
| 29 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; |
| 30 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
| 31 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 32 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; |
| 33 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
| 34 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
| 35 | import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; |
| 36 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 37 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration; |
| 38 | import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition; |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | |
| 42 | /** |
| 43 | * The class <code>VariantDefinitionTest</code> contains tests for the class |
| 44 | * <code>{@link VariantDefinition}</code>. |
| 45 | * |
| 46 | * @author ematkho |
| 47 | * @version $Revision: 1.0 $ |
| 48 | */ |
| 49 | public class VariantDefinitionTest { |
| 50 | |
| 51 | private VariantDefinition fixture; |
| 52 | |
| 53 | StructDefinition structDefinition; |
| 54 | private static final String TEST_STRUCT_ID = "testStruct"; |
| 55 | |
| 56 | private static final String ENUM_7 = "g"; |
| 57 | private static final String ENUM_6 = "f"; |
| 58 | private static final String ENUM_5 = "e"; |
| 59 | private static final String ENUM_4 = "d"; |
| 60 | private static final String ENUM_3 = "c"; |
| 61 | private static final String ENUM_2 = "b"; |
| 62 | private static final String ENUM_1 = "a"; |
| 63 | |
| 64 | private static final String TAG_ID = "a"; |
| 65 | |
| 66 | // private static final String INT_ID = "_id"; |
| 67 | // private static final String STRING_ID = "_args"; |
| 68 | // private static final String ENUM_ID = "_enumArgs"; |
| 69 | // private static final String SEQUENCE_ID = "_seq"; |
| 70 | |
| 71 | private static final String LENGTH_SEQ = "_len"; |
| 72 | private static final String VAR_FIELD_NAME = "var"; |
| 73 | private static final String ENUM_8 = null; |
| 74 | |
| 75 | /** |
| 76 | * Perform pre-test initialization. |
| 77 | * |
| 78 | * Not sure it needs to be that complicated, oh well... |
| 79 | */ |
| 80 | @Before |
| 81 | public void setUp() { |
| 82 | StructDeclaration sDec = new StructDeclaration(12); |
| 83 | StructDeclaration smallStruct = new StructDeclaration(8); |
| 84 | IntegerDeclaration iDec = new IntegerDeclaration(32, false, 32, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); |
| 85 | IntegerDeclaration lenDec = new IntegerDeclaration(8, false, 8, ByteOrder.BIG_ENDIAN, Encoding.NONE, null, 8); |
| 86 | StringDeclaration strDec = new StringDeclaration(); |
| 87 | EnumDeclaration enDec = new EnumDeclaration(iDec); |
| 88 | // SequenceDeclaration seqDec = new SequenceDeclaration(LENGTH_SEQ, iDec); |
| 89 | VariantDeclaration varDec = new VariantDeclaration(); |
| 90 | EnumDeclaration tagDec = new EnumDeclaration(iDec); |
| 91 | ArrayDeclaration arrDec = new ArrayDeclaration(2, iDec); |
| 92 | FloatDeclaration fDec = new FloatDeclaration(8, 24, ByteOrder.BIG_ENDIAN, 8); |
| 93 | tagDec.add(0, 1, ENUM_1); |
| 94 | tagDec.add(2, 3, ENUM_2); |
| 95 | tagDec.add(4, 5, ENUM_3); |
| 96 | //tagDec.add(6, 7, ENUM_4); // this should not work |
| 97 | tagDec.add(8, 9, ENUM_5); |
| 98 | tagDec.add(10, 11, ENUM_6); |
| 99 | tagDec.add(12, 13, ENUM_7); |
| 100 | varDec.addField(ENUM_4, lenDec); |
| 101 | varDec.addField(ENUM_7, fDec); |
| 102 | varDec.addField(ENUM_6, smallStruct); |
| 103 | varDec.addField(ENUM_5, enDec); |
| 104 | //varDec.addField(ENUM_4, seqDec);// this should not work |
| 105 | varDec.addField(ENUM_3, arrDec); |
| 106 | varDec.addField(ENUM_2, iDec); |
| 107 | varDec.addField(ENUM_1, strDec); |
| 108 | |
| 109 | sDec.addField(TAG_ID, tagDec); |
| 110 | sDec.addField(LENGTH_SEQ, lenDec); |
| 111 | // sDec.addField(SEQUENCE_ID, seqDec); |
| 112 | |
| 113 | sDec.addField(VAR_FIELD_NAME, varDec); |
| 114 | varDec.setTag(TAG_ID); |
| 115 | |
| 116 | structDefinition = sDec.createDefinition(null, TEST_STRUCT_ID); |
| 117 | fixture = (VariantDefinition) structDefinition.getDefinitions().get(VAR_FIELD_NAME); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Run the VariantDefinition(VariantDeclaration,DefinitionScope,String) |
| 122 | */ |
| 123 | @Test |
| 124 | public void testVariantDefinition() { |
| 125 | VariantDeclaration declaration = new VariantDeclaration(); |
| 126 | declaration.setTag(""); |
| 127 | VariantDeclaration variantDeclaration = new VariantDeclaration(); |
| 128 | variantDeclaration.setTag(""); |
| 129 | VariantDefinition variantDefinition = new VariantDefinition( |
| 130 | variantDeclaration, structDefinition, ""); |
| 131 | IDefinitionScope definitionScope = new StructDefinition( |
| 132 | new StructDeclaration(1L), variantDefinition, ""); |
| 133 | String fieldName = ""; |
| 134 | |
| 135 | VariantDefinition result = new VariantDefinition(declaration, |
| 136 | definitionScope, fieldName); |
| 137 | assertNotNull(result); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Run the Definition getCurrentField() method test. |
| 142 | */ |
| 143 | @Test |
| 144 | public void testGetCurrentField() { |
| 145 | Definition result = fixture.getCurrentField(); |
| 146 | assertNull(result); |
| 147 | fixture.setCurrentField(ENUM_1); |
| 148 | result = fixture.getCurrentField(); |
| 149 | assertNotNull(result); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Run the String getCurrentFieldName() method test. |
| 154 | */ |
| 155 | @Test |
| 156 | public void testGetCurrentFieldName() { |
| 157 | fixture.setCurrentField(ENUM_1); |
| 158 | String result = fixture.getCurrentFieldName(); |
| 159 | assertNotNull(result); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Run the VariantDeclaration getDeclaration() method test. |
| 164 | */ |
| 165 | @Test |
| 166 | public void testGetDeclaration() { |
| 167 | VariantDeclaration result = fixture.getDeclaration(); |
| 168 | assertNotNull(result); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Run the HashMap<String, Definition> getDefinitions() method test. |
| 173 | */ |
| 174 | @Test |
| 175 | public void testGetDefinitions() { |
| 176 | Map<String, Definition> result = fixture.getDefinitions(); |
| 177 | assertNotNull(result); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Run the String getPath() method test. |
| 182 | */ |
| 183 | @Test |
| 184 | public void testGetPath() { |
| 185 | String result = fixture.getPath(); |
| 186 | assertNotNull(result); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Run the EnumDefinition getTagDefinition() method test. |
| 191 | */ |
| 192 | @Test |
| 193 | public void testGetTagDefinition() { |
| 194 | EnumDefinition result = fixture.getTagDefinition(); |
| 195 | assertNotNull(result); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Run the ArrayDefinition lookupArray(String) method test. |
| 200 | */ |
| 201 | @Test |
| 202 | public void testLookupArray() { |
| 203 | ArrayDefinition result = fixture.lookupArray(ENUM_3); |
| 204 | assertNotNull(result); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Run the Definition lookupDefinition(String) method test. |
| 209 | */ |
| 210 | @Test |
| 211 | public void testLookupDefinition() { |
| 212 | Definition result = fixture.lookupDefinition(ENUM_1); |
| 213 | assertNotNull(result); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Run the EnumDefinition lookupEnum(String) method test. |
| 218 | */ |
| 219 | @Test |
| 220 | public void testLookupEnum() { |
| 221 | EnumDefinition result = fixture.lookupEnum(ENUM_5); |
| 222 | assertNotNull(result); |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Run the IntegerDefinition lookupInteger(String) method test. |
| 227 | */ |
| 228 | @Test |
| 229 | public void testLookupInteger() { |
| 230 | IntegerDefinition result = fixture.lookupInteger(ENUM_2); |
| 231 | assertNotNull(result); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Run the SequenceDefinition lookupSequence(String) method test. |
| 236 | */ |
| 237 | @Test |
| 238 | public void testLookupSequence_1() { |
| 239 | SequenceDefinition result = fixture.lookupSequence(ENUM_4); |
| 240 | assertNull(result); |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Run the StringDefinition lookupString(String) method test. |
| 245 | */ |
| 246 | @Test |
| 247 | public void testLookupString() { |
| 248 | StringDefinition result = fixture.lookupString(ENUM_1); |
| 249 | assertNotNull(result); |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Run the StructDefinition lookupStruct(String) method test. |
| 254 | */ |
| 255 | @Test |
| 256 | public void testLookupStruct() { |
| 257 | StructDefinition result = fixture.lookupStruct(ENUM_6); |
| 258 | assertNotNull(result); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Run the VariantDefinition lookupVariant(String) method test. |
| 263 | */ |
| 264 | @Test |
| 265 | public void testLookupVariant() { |
| 266 | VariantDefinition result = fixture.lookupVariant(ENUM_8); |
| 267 | assertNull(result); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Run the void setCurrentField(String) method test. |
| 272 | */ |
| 273 | @Test |
| 274 | public void testSetCurrentField() { |
| 275 | fixture.setCurrentField(ENUM_1); |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Run the void setDeclaration(VariantDeclaration) method test. |
| 280 | */ |
| 281 | @Test |
| 282 | public void testSetDeclaration() { |
| 283 | VariantDeclaration declaration = new VariantDeclaration(); |
| 284 | fixture.setDeclaration(declaration); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Run the void setDefinitions(HashMap<String,Definition>) method test. |
| 289 | */ |
| 290 | @Test |
| 291 | public void testSetDefinitions() { |
| 292 | HashMap<String, Definition> definitions = new HashMap<>(); |
| 293 | fixture.setDefinitions(definitions); |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Run the void setTagDefinition(EnumDefinition) method test. |
| 298 | */ |
| 299 | @Test |
| 300 | public void testSetTagDefinition(){ |
| 301 | VariantDeclaration vDecl; |
| 302 | VariantDefinition vDef; |
| 303 | StructDefinition structDef; |
| 304 | EnumDefinition tagDefinition; |
| 305 | String fName = ""; |
| 306 | |
| 307 | vDecl = new VariantDeclaration(); |
| 308 | vDecl.setTag(fName); |
| 309 | vDef = new VariantDefinition(vDecl, structDefinition, fName); |
| 310 | structDef = new StructDefinition(new StructDeclaration(1L), vDef, fName); |
| 311 | tagDefinition = new EnumDefinition(new EnumDeclaration( |
| 312 | new IntegerDeclaration(1, false, 1, ByteOrder.BIG_ENDIAN, |
| 313 | Encoding.ASCII, fName, 8)), structDef, fName); |
| 314 | |
| 315 | fixture.setTagDefinition(tagDefinition); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Run the String toString() method test. |
| 320 | */ |
| 321 | @Test |
| 322 | public void testToString() { |
| 323 | String result = fixture.toString(); |
| 324 | assertEquals("{ null = null }", result); |
| 325 | |
| 326 | fixture.setCurrentField(ENUM_2); |
| 327 | result = fixture.toString(); |
| 328 | assertEquals("{ b = 0 }", result); |
| 329 | } |
| 330 | } |