| 1 | package org.eclipse.linuxtools.ctf.core.tests.types; |
| 2 | |
| 3 | import static org.junit.Assert.assertFalse; |
| 4 | import static org.junit.Assert.assertNotNull; |
| 5 | import static org.junit.Assert.assertTrue; |
| 6 | |
| 7 | import java.nio.ByteBuffer; |
| 8 | import java.nio.ByteOrder; |
| 9 | |
| 10 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 11 | import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer; |
| 12 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; |
| 13 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; |
| 14 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
| 15 | import org.eclipse.linuxtools.ctf.core.event.types.Encoding; |
| 16 | import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope; |
| 17 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
| 18 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
| 19 | import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration; |
| 20 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; |
| 21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 22 | import org.eclipse.linuxtools.ctf.core.tests.TestParams; |
| 23 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; |
| 24 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | |
| 29 | /** |
| 30 | * The class <code>ArrayDefinitionTest</code> contains tests for the class |
| 31 | * <code>{@link ArrayDefinition}</code>. |
| 32 | * |
| 33 | * @author ematkho |
| 34 | * @version $Revision: 1.0 $ |
| 35 | */ |
| 36 | public class ArrayDefinitionTest { |
| 37 | |
| 38 | private CTFTrace trace; |
| 39 | private ArrayDefinition fixture; |
| 40 | |
| 41 | /** |
| 42 | * Launch the test. |
| 43 | * |
| 44 | * @param args |
| 45 | * the command line arguments |
| 46 | */ |
| 47 | public static void main(String[] args) { |
| 48 | new org.junit.runner.JUnitCore().run(ArrayDefinitionTest.class); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Perform pre-test initialization. |
| 53 | * |
| 54 | * structDef shouldn't be null after parsing the CTFTraceReader object, so |
| 55 | * we can ignore the warning. |
| 56 | */ |
| 57 | |
| 58 | @Before |
| 59 | public void setUp() { |
| 60 | this.trace = TestParams.createTrace(); |
| 61 | |
| 62 | CTFTraceReader tr = new CTFTraceReader(this.trace); |
| 63 | String name = ""; //$NON-NLS-1$ |
| 64 | StructDefinition structDef = null; |
| 65 | boolean foundArray = false; |
| 66 | |
| 67 | while (tr.hasMoreEvents() && !foundArray) { |
| 68 | tr.advance(); |
| 69 | EventDefinition ed = tr.getCurrentEventDef(); |
| 70 | for (String key : ed.fields.getDefinitions().keySet()) { |
| 71 | structDef = ed.fields; |
| 72 | Definition d = structDef.lookupDefinition(key); |
| 73 | if (d instanceof ArrayDefinition) { |
| 74 | foundArray = true; |
| 75 | name = key; |
| 76 | break; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | fixture = structDef.lookupArray(name); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Perform post-test clean-up. |
| 85 | */ |
| 86 | @After |
| 87 | public void tearDown() { |
| 88 | // Add additional tear down code here |
| 89 | } |
| 90 | |
| 91 | private static StringDefinition[] createDefs() { |
| 92 | int size = 4; |
| 93 | StringDefinition[] defs = new StringDefinition[size]; |
| 94 | for (int i = 0; i < size; i++) { |
| 95 | |
| 96 | String content = "test" + i; //$NON-NLS-1$ |
| 97 | defs[i] = new StringDefinition( |
| 98 | new StringDeclaration(Encoding.UTF8), null, content); |
| 99 | defs[i].setString(new StringBuilder(content)); |
| 100 | } |
| 101 | return defs; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) |
| 106 | * constructor test. |
| 107 | */ |
| 108 | @Test |
| 109 | public void testArrayDefinition_baseDeclaration() { |
| 110 | ArrayDeclaration declaration = fixture.getDeclaration(); |
| 111 | String fieldName = ""; //$NON-NLS-1$ |
| 112 | |
| 113 | ArrayDefinition result = new ArrayDefinition(declaration, this.trace, |
| 114 | fieldName); |
| 115 | |
| 116 | assertNotNull(result); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Run the ArrayDefinition(ArrayDeclaration,DefinitionScope,String) |
| 121 | * constructor test. |
| 122 | */ |
| 123 | @Test |
| 124 | public void testArrayDefinition_newDeclaration() { |
| 125 | ArrayDeclaration declaration = new ArrayDeclaration(0, |
| 126 | new StringDeclaration()); |
| 127 | IDefinitionScope definitionScope = null; |
| 128 | String fieldName = ""; //$NON-NLS-1$ |
| 129 | |
| 130 | ArrayDefinition result = new ArrayDefinition(declaration, |
| 131 | definitionScope, fieldName); |
| 132 | |
| 133 | assertNotNull(result); |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Run the ArrayDeclaration getDeclaration() method test. |
| 138 | */ |
| 139 | @Test |
| 140 | public void testGetDeclaration() { |
| 141 | fixture.setDefinitions(new Definition[] {}); |
| 142 | ArrayDeclaration result = fixture.getDeclaration(); |
| 143 | |
| 144 | assertNotNull(result); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Run the Definition getElem(int) method test. |
| 149 | */ |
| 150 | @Test |
| 151 | public void testGetElem_noDefs() { |
| 152 | int i = 0; |
| 153 | Definition result = fixture.getElem(i); |
| 154 | |
| 155 | assertNotNull(result); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Run the Definition getElem(int) method test. |
| 160 | */ |
| 161 | @Test |
| 162 | public void testGetElem_withDefs() { |
| 163 | Definition defs[] = createDefs(); |
| 164 | fixture.setDefinitions(defs); |
| 165 | int j = 1; |
| 166 | |
| 167 | Definition result = fixture.getElem(j); |
| 168 | |
| 169 | assertNotNull(result); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Run the boolean isString() method test. |
| 174 | */ |
| 175 | @Test |
| 176 | public void testIsString_ownDefs() { |
| 177 | StringDefinition[] defs = createDefs(); |
| 178 | fixture.setDefinitions(defs); |
| 179 | |
| 180 | boolean result = fixture.isString(); |
| 181 | |
| 182 | assertFalse(result); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Run the boolean isString() method test. |
| 187 | */ |
| 188 | @Test |
| 189 | public void testIsString_complex() { |
| 190 | final IntegerDeclaration id = new IntegerDeclaration(8, false, 16, |
| 191 | ByteOrder.LITTLE_ENDIAN, Encoding.UTF8); |
| 192 | ArrayDeclaration ad = new ArrayDeclaration(0, id); |
| 193 | ArrayDefinition ownFixture = new ArrayDefinition(ad, this.trace, |
| 194 | "Testx"); //$NON-NLS-1$ |
| 195 | |
| 196 | int size = 4; |
| 197 | IntegerDefinition[] defs = new IntegerDefinition[size]; |
| 198 | for (int i = 0; i < size; i++) { |
| 199 | |
| 200 | String content = "test" + i; //$NON-NLS-1$ |
| 201 | defs[i] = new IntegerDefinition(new IntegerDeclaration(8, false, |
| 202 | 16, ByteOrder.LITTLE_ENDIAN, Encoding.UTF8), null, content); |
| 203 | defs[i].setValue(i); |
| 204 | } |
| 205 | |
| 206 | ownFixture.setDefinitions(defs); |
| 207 | boolean result = ownFixture.isString(); |
| 208 | |
| 209 | assertTrue(result); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Run the boolean isString() method test. |
| 214 | */ |
| 215 | @Test |
| 216 | public void testIsString_emptyDef() { |
| 217 | fixture.setDefinitions(new Definition[] {}); |
| 218 | boolean result = fixture.isString(); |
| 219 | |
| 220 | assertFalse(result); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Run the void read(BitBuffer) method test. |
| 225 | */ |
| 226 | @Test |
| 227 | public void testRead_noDefs() { |
| 228 | BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128)); |
| 229 | |
| 230 | fixture.read(input); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Run the void read(BitBuffer) method test. |
| 235 | */ |
| 236 | @Test |
| 237 | public void testRead_withDefs() { |
| 238 | fixture.setDefinitions(new Definition[] {}); |
| 239 | BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocateDirect(128)); |
| 240 | |
| 241 | fixture.read(input); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Run the String toString() method test. |
| 246 | */ |
| 247 | @Test |
| 248 | public void testToString_base() { |
| 249 | String result = fixture.toString(); |
| 250 | |
| 251 | assertNotNull(result); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Run the String toString() method test. |
| 256 | */ |
| 257 | @Test |
| 258 | public void testToString_withDefs() { |
| 259 | int size = 2; |
| 260 | StringDefinition[] defs = new StringDefinition[size]; |
| 261 | for (int i = 0; i < size; i++) { |
| 262 | defs[i] = new StringDefinition(null, null, ("test" + i)); //$NON-NLS-1$ |
| 263 | } |
| 264 | fixture.setDefinitions(defs); |
| 265 | String result = fixture.toString(); |
| 266 | |
| 267 | assertNotNull(result); |
| 268 | } |
| 269 | } |