@SuppressWarnings("javadoc")
public class CTFEventFieldTest {
- @NonNull private static final String fieldName = "id";
+ @NonNull
+ private static final String fieldName = "id";
/**
* Run the CTFEventField parseField(Definition,String) method test.
});
SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
- ByteBuffer byb = ByteBuffer.allocate(1024);
+ ByteBuffer byb = testMemory(ByteBuffer.allocate(1024));
for (int i = 0; i < 1024; i++) {
byb.put((byte) i);
}
assertNotNull(fieldDef);
}
+ @NonNull
+ private static ByteBuffer testMemory(ByteBuffer buffer) {
+ if (buffer == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ return buffer;
+ }
+
/**
* Run the CTFEventField parseField(Definition,String) method test.
*
public void testParseField_simple() throws CTFReaderException {
final StringDeclaration elemType = new StringDeclaration();
byte[] bytes = { 'T', 'e', 's', 't', '\0' };
- ByteBuffer bb = ByteBuffer.wrap(bytes);
-
+ ByteBuffer bb = testMemory(ByteBuffer.wrap(bytes));
Definition fieldDef = elemType.createDefinition(null, fieldName, new BitBuffer(bb));
assertNotNull(fieldDef);
*/
@Before
public void setUp() throws CTFReaderException {
- fixture = new BitBuffer(ByteBuffer.allocateDirect(128));
+ ByteBuffer allocateDirect = ByteBuffer.allocateDirect(128);
+ if (allocateDirect == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ fixture = new BitBuffer(allocateDirect);
fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
fixture = createBuffer();
}
for (int i = 0; i < j; i++) {
bytes[i] = (byte) (i % 0xff);
}
- BitBuffer fixture = new BitBuffer(ByteBuffer.wrap(bytes));
+ ByteBuffer wrap = ByteBuffer.wrap(bytes);
+ if (wrap == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ BitBuffer fixture = new BitBuffer(wrap);
fixture.position(1);
return fixture;
}
*/
@Before
public void setUp() throws CTFReaderException {
- fixture = new BitBuffer(java.nio.ByteBuffer.allocateDirect(1));
+ fixture = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(1)));
fixture.setByteOrder(ByteOrder.BIG_ENDIAN);
fixture.position(1);
}
assertNotNull(result);
assertEquals(0, result.position());
- assertNotNull( result.getByteBuffer());
+ assertNotNull(result.getByteBuffer());
}
/**
*/
@Test
public void testBitBuffer_fromByteBuffer() {
- ByteBuffer buf = ByteBuffer.allocate(0);
- BitBuffer result = new BitBuffer(buf);
-
+ BitBuffer result = new BitBuffer(Util.testMemory(ByteBuffer.allocate(0)));
assertNotNull(result);
assertEquals(0, result.position());
}
byte[] data = new byte[5];
// this string has been carefully selected and tested... don't change
// the string and expect the result to be the same.
- ByteBuffer bb = ByteBuffer.wrap(new String("hello world").getBytes());
- fixture = new BitBuffer(bb);
+ fixture = new BitBuffer(Util.testMemory(ByteBuffer.wrap(new String("hello world").getBytes())));
fixture.position(6 * 8);
fixture.get(data);
String actual = new String(data);
--- /dev/null
+/*******************************************************************************
+ * Copyright (c) 2014 Ericsson
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Matthew Khouzam - Initial API and implementation
+ ******************************************************************************/
+
+package org.eclipse.linuxtools.ctf.core.tests.io;
+
+import java.nio.ByteBuffer;
+
+import org.eclipse.jdt.annotation.NonNull;
+
+/**
+ * Helpers for the tests
+ *
+ * @author Matthew Khouzam
+ */
+public final class Util {
+
+ private Util() {
+ }
+
+ /**
+ * Wrapper to make sure the bytebuffer is not null
+ *
+ * @param buffer
+ * a potentially null byte buffer
+ * @return a non-null byte buffer or an illegal state exception
+ */
+ @NonNull
+ public static ByteBuffer testMemory(ByteBuffer buffer) {
+ if (buffer == null) {
+ throw new IllegalStateException("Failed to alloc");
+ }
+ return buffer;
+ }
+}
import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
import org.junit.Before;
IDefinitionScope definitionScope = null;
AbstractArrayDefinition result;
byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
- ByteBuffer byb = ByteBuffer.wrap(array);
- BitBuffer bb = new BitBuffer(byb);
+ BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(array)));
result = fixture.createDefinition(definitionScope, fieldName, bb);
assertNotNull(result);
}
+
+
/**
* Run the Declaration getElementType() method test.
*/
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StringDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
*/
public class ArrayDefinition2Test {
- @NonNull private CTFTrace trace = new CTFTrace();
+ private @NonNull CTFTrace trace = new CTFTrace();
private ArrayDefinition charArrayFixture;
private ArrayDefinition stringArrayFixture;
private ArrayDefinition longArrayFixture;
return temp;
}
- private @NonNull
- static List<Definition> createIntDefs(int size, int bits) {
+ @NonNull
+ private static List<Definition> createIntDefs(int size, int bits) {
List<Definition> defs = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
String content = "test" + i;
return defs;
}
- private @NonNull
- static List<Definition> createDefs() {
+ @NonNull
+ private static List<Definition> createDefs() {
int size = 4;
List<Definition> defs = new ArrayList<>();
for (int i = 0; i < size; i++) {
String fieldName = "";
@SuppressWarnings("null")
- ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName , Arrays.asList(new Definition[0]));
+ ArrayDefinition result = new ArrayDefinition(declaration, definitionScope, fieldName, Arrays.asList(new Definition[0]));
assertNotNull(result);
}
assertNotNull(result);
}
- @NonNull private static IDefinitionScope getDefinitionScope() {
+ @NonNull
+ private static IDefinitionScope getDefinitionScope() {
return new IDefinitionScope() {
@Override
*/
@Test
public void testRead_noDefs() throws CTFReaderException {
- BitBuffer input = new BitBuffer(ByteBuffer.allocateDirect(128));
+ BitBuffer input = new BitBuffer(Util.testMemory(ByteBuffer.allocateDirect(128)));
charArrayFixture.getDeclaration().createDefinition(null, "test", input);
}
import org.eclipse.linuxtools.ctf.core.event.types.EnumDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.junit.Before;
import org.junit.Test;
IDefinitionScope definitionScope = null;
String fieldName = "";
byte[] array = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
- ByteBuffer byb = ByteBuffer.wrap(array);
- BitBuffer bb = new BitBuffer(byb);
+ BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(array)));
EnumDefinition result = fixture.createDefinition(definitionScope,
fieldName, bb);
*/
public class IntegerEndiannessTest {
- @NonNull private static final String name = "testInt";
- @NonNull private static final String clockName = "clock";
+ private static final @NonNull String name = "testInt";
+ private static final @NonNull String clockName = "clock";
private ByteBuffer bb;
- @NonNull private BitBuffer input = new BitBuffer(java.nio.ByteBuffer.allocate(0));
+
+ private @NonNull BitBuffer input = new BitBuffer();
/**
* Set up the bit-buffer to be used
@Before
public void setUp() {
bb = java.nio.ByteBuffer.allocateDirect(8);
+ final ByteBuffer byb = bb;
+ if (byb == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
bb.put((byte) 0xab);
bb.put((byte) 0xcd);
bb.put((byte) 0xef);
bb.put((byte) 0x56);
bb.put((byte) 0x78);
bb.put((byte) 0x9a);
- input = new BitBuffer(bb);
+
+ input = new BitBuffer(byb);
}
/**
fixture = new SequenceDeclaration(FIELD_NAME, new StringDeclaration());
byte array[] = { 't', 'e', 's', 't', '\0', 't', 'h', 'i', 's', '\0' };
ByteBuffer byb = ByteBuffer.wrap(array);
+ if( byb == null){
+ throw new IllegalStateException("Failed to allocate memory");
+ }
input = new BitBuffer(byb);
}
import static org.junit.Assert.assertNotNull;
+import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
structDef = new StructDefinition(structDec, null, "x", wrap(lengthName), new Definition[] { new IntegerDefinition(id, null, lengthName, seqLen) });
SequenceDeclaration sd = new SequenceDeclaration(lengthName, id);
- BitBuffer input = new BitBuffer(
- java.nio.ByteBuffer.allocateDirect(seqLen * len));
+ ByteBuffer allocateDirect = java.nio.ByteBuffer.allocateDirect(seqLen * len);
+ if( allocateDirect == null){
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ BitBuffer input = new BitBuffer(allocateDirect);
for (int i = 0; i < seqLen; i++) {
input.putInt(i);
}
/**
* Run the StringDefinition createDefinition(DefinitionScope,String) method
* test.
- * @throws CTFReaderException out of buffer exception
+ *
+ * @throws CTFReaderException
+ * out of buffer exception
*/
@Test
public void testCreateDefinition() throws CTFReaderException {
IDefinitionScope definitionScope = null;
String fieldName = "id";
- BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+ ByteBuffer allocate = ByteBuffer.allocate(100);
+ if (allocate == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ BitBuffer bb = new BitBuffer(allocate);
StringDefinition result = fixture.createDefinition(definitionScope,
fieldName, bb);
assertEquals(1, result.ordinal());
}
- /**
+ /**
* Run the String toString() method test.
*/
@Test
/**
* Perform pre-test initialization.
*
- * @throws CTFReaderException won't happen
+ * @throws CTFReaderException
+ * won't happen
*/
@Before
public void setUp() throws CTFReaderException {
String name = "testString";
StringDeclaration stringDec = new StringDeclaration();
ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+ if (byteBuffer == null) {
+ throw new IllegalStateException("Failed to allocate memory");
+ }
BitBuffer bb = new BitBuffer(byteBuffer);
byteBuffer.mark();
testString = new String("testString");
@Test
public void testCreateDefinition() throws CTFReaderException {
String fieldName = "";
- BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+ ByteBuffer allocate = ByteBuffer.allocate(100);
+ if( allocate == null){
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ BitBuffer bb = new BitBuffer(allocate);
StructDefinition result = fixture.createDefinition(null, fieldName, bb);
assertNotNull(result);
}
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.internal.ctf.core.event.types.SequenceDeclaration;
import org.junit.Before;
*/
public class StructDefinitionTest {
- @NonNull private static final String TEST_STRUCT_ID = "testStruct";
- @NonNull private static final String ENUM_2 = "y";
- @NonNull private static final String ENUM_1 = "x";
- @NonNull private static final String TAG_ID = "Tag";
- @NonNull private static final String INT_ID = "_id";
- @NonNull private static final String STRING_ID = "_args";
- @NonNull private static final String ENUM_ID = "_enumArgs";
- @NonNull private static final String SEQUENCE_ID = "_seq";
- @NonNull private static final String LENGTH_SEQ = "_len";
- @NonNull private static final String VAR_FIELD_NAME = "SomeVariant";
+ private static final @NonNull String TEST_STRUCT_ID = "testStruct";
+ private static final @NonNull String ENUM_2 = "y";
+ private static final @NonNull String ENUM_1 = "x";
+ private static final @NonNull String TAG_ID = "Tag";
+ private static final @NonNull String INT_ID = "_id";
+ private static final @NonNull String STRING_ID = "_args";
+ private static final @NonNull String ENUM_ID = "_enumArgs";
+ private static final @NonNull String SEQUENCE_ID = "_seq";
+ private static final @NonNull String LENGTH_SEQ = "_len";
+ private static final @NonNull String VAR_FIELD_NAME = "SomeVariant";
private StructDefinition fixture;
private StructDefinition emptyStruct;
bytes[4] = 1;
bytes[8] = 2;
bytes[13] = 3;
- ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
- BitBuffer bb = new BitBuffer(byteBuffer);
+ BitBuffer bb = new BitBuffer(Util.testMemory(ByteBuffer.wrap(bytes)));
fixture = sDec.createDefinition(null, TEST_STRUCT_ID, bb);
EnumDefinition eDef = tagDec.createDefinition(fixture, TAG_ID, bb);
assertNotNull(eDef);
fixture.addField("a", IntegerDeclaration.UINT_64B_DECL);
IDefinitionScope definitionScope = createDefinitionScope();
String fieldName = "";
- BitBuffer bb = new BitBuffer(ByteBuffer.allocate(100));
+ ByteBuffer allocate = ByteBuffer.allocate(100);
+ if( allocate == null){
+ throw new IllegalStateException("Failed to allocate memory");
+ }
+ BitBuffer bb = new BitBuffer(allocate);
VariantDefinition result = fixture.createDefinition(definitionScope, fieldName, bb);
assertNotNull(result);
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.VariantDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
+import org.eclipse.linuxtools.ctf.core.tests.io.Util;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDeclaration;
import org.junit.Before;
sDec.addField(VAR_FIELD_NAME, varDec);
varDec.setTag(TAG_ID);
- ByteBuffer byteBuffer = ByteBuffer.allocate(100);
+ final ByteBuffer byteBuffer = Util.testMemory(ByteBuffer.allocate(100));
BitBuffer bb = new BitBuffer(byteBuffer);
byteBuffer.mark();
byteBuffer.putInt(1);
/**
* Default constructor, makes a big-endian buffer
*/
+ @SuppressWarnings("null")
public BitBuffer() {
- this(ByteBuffer.allocate(0), ByteOrder.BIG_ENDIAN);
+ this(ByteBuffer.allocateDirect(0), ByteOrder.BIG_ENDIAN);
}
/**
* @param buf
* the bytebuffer to read
*/
- public BitBuffer(ByteBuffer buf) {
+ public BitBuffer(@NonNull ByteBuffer buf) {
this(buf, ByteOrder.BIG_ENDIAN);
}
* @param order
* the byte order (big-endian, little-endian, network?)
*/
- public BitBuffer(ByteBuffer buf, ByteOrder order) {
- if (buf == null) {
- throw new IllegalArgumentException("Buffer cannot be null"); //$NON-NLS-1$
- }
+ public BitBuffer(@NonNull ByteBuffer buf, ByteOrder order) {
fBuffer = buf;
setByteOrder(order);
resetPosition();
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
+import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.util.UUID;
+ ((packetIndex.getPacketSizeBits() + 7) / 8);
}
- ByteBuffer getByteBufferAt(long position, long size) throws IOException {
- return fFileChannel.map(MapMode.READ_ONLY, position, size);
+ @NonNull
+ ByteBuffer getByteBufferAt(long position, long size) throws CTFReaderException, IOException {
+ MappedByteBuffer map = fFileChannel.map(MapMode.READ_ONLY, position, size);
+ if (map == null) {
+ throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$
+ }
+ return map;
}
- /**
- * @param fileSizeBytes
- * @param packetOffsetBytes
- * @param packetIndex
- * @return
- * @throws CTFReaderException
- */
+ @NonNull
private ByteBuffer createPacketBitBuffer(long fileSizeBytes,
long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
/*
FileChannel fc = fis.getChannel()) {
/* Map one memory page of 4 kiB */
byteBuffer = fc.map(MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
+ if( byteBuffer == null){
+ throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
+ }
} catch (IOException e) {
/* Shouldn't happen at this stage if every other check passed */
throw new CTFReaderException(e);