blob: 97f602e883acf07229b90435f9a1153a79195c3a [file] [log] [blame]
/*
* Copyright (c) 2002-2014 Pivotal Software, Inc. All Rights Reserved.
* This product is protected by U.S. and international copyright
* and intellectual property laws. Pivotal products are covered by
* more patents listed at http://www.pivotal.io/patents.
*/
package com.gemstone.gemfire.internal.util;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import com.gemstone.gemfire.test.junit.categories.UnitTest;
/**
* The ArrayUtilsJUnitTest class... </p>
*
* @author jblum
* @see ?
* @since 7.x
*/
@Category(UnitTest.class)
public class ArrayUtilsJUnitTest {
@Test
@SuppressWarnings("null")
public void testGetElementAtIndex() {
final Object[] array = { "test", "testing", "tested" };
assertEquals("test", ArrayUtils.getElementAtIndex(array, 0, null));
assertEquals("testing", ArrayUtils.getElementAtIndex(array, 1, null));
assertEquals("tested", ArrayUtils.getElementAtIndex(array, 2, null));
}
@Test
public void testGetElementAtIndexThrowingArrayIndexOutOfBoundsException() {
assertEquals("test", ArrayUtils.getElementAtIndex(new Object[0], 0, "test"));
}
@Test
public void testGetElementAtIndexThrowingArrayIndexOutOfBoundsExceptionOnNonEmptyArray() {
assertEquals("defaultValue", ArrayUtils.getElementAtIndex(new Object[] { "test" }, 1, "defaultValue"));
}
@Test
public void testGetFirst() {
assertEquals("first", ArrayUtils.getFirst("first", "second", "third"));
assertEquals("null", ArrayUtils.getFirst("null", "nil", null));
assertEquals("test", ArrayUtils.getFirst("test"));
assertNull(ArrayUtils.getFirst((Object[]) null));
assertNull(ArrayUtils.getFirst(new Object[0]));
assertNull(ArrayUtils.getFirst(null, null, null));
}
@Test
public void testToString() {
final Object[] array = { "test", "testing", "tested" };
assertEquals("[test, testing, tested]", ArrayUtils.toString(array));
}
@Test
public void testToStringWithEmptyArray() {
assertEquals("[]", ArrayUtils.toString((new Object[0])));
}
@Test
public void testToStringWithNullArray() {
assertEquals("[]", ArrayUtils.toString((Object[]) null));
}
@Test
public void testGetIntegerArray() {
final Integer[] array = ArrayUtils.toIntegerArray(new int[] { 0, 1, 2, 4, 8 });
assertNotNull(array);
assertEquals(5, array.length);
assertEquals(0, array[0].intValue());
assertEquals(1, array[1].intValue());
assertEquals(2, array[2].intValue());
assertEquals(4, array[3].intValue());
assertEquals(8, array[4].intValue());
}
@Test
public void testGetIntegerArrayWithEmptyArray() {
final Integer[] array = ArrayUtils.toIntegerArray(new int[0]);
assertNotNull(array);
assertEquals(0, array.length);
}
@Test
@SuppressWarnings("null")
public void testGetIntegerArrayWithNullArray() {
final Integer[] array = ArrayUtils.toIntegerArray(null);
assertNotNull(array);
assertEquals(0, array.length);
}
@Test
public void testFromBytesToByteArray() {
int count = 0;
final byte[][] array = new byte[10][5];
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
array[i][j] = (byte)++count;
}
}
assertEquals(50, count);
count = 0;
final Byte[][] byteArray = ArrayUtils.toByteArray(array);
for (int i = 0; i < byteArray.length; i++) {
for (int j = 0; j < byteArray[i].length; j++) {
assertEquals((byte)++count, byteArray[i][j].byteValue());
}
}
assertEquals(50, count);
}
@Test
public void testFromByteArrayToBytes() {
int count = 100;
final Byte[][] byteArray = new Byte[5][10];
for (int i = 0; i < byteArray.length; i++) {
for (int j = 0; j < byteArray[i].length; j++) {
byteArray[i][j] = (byte)--count;
}
}
assertEquals(50, count);
count = 100;
final byte[][] array = ArrayUtils.toBytes(byteArray);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
assertEquals((byte)--count, array[i][j]);
}
}
assertEquals(50, count);
}
@Test
public void testFromEmptyBytesToByteArray() {
final byte[][] array = new byte[0][0];
assertArrayEquals(new Byte[0][0], ArrayUtils.toByteArray(array));
}
@Test
public void testFromNullBytesToByteArray() {
final byte[][] array = null;
assertNull(ArrayUtils.toByteArray(array));
}
@Test
public void testFromEmptyByteArrayToBytes() {
final Byte[][] byteArray = new Byte[0][0];
assertArrayEquals(new byte[0][0], ArrayUtils.toBytes(byteArray));
}
@Test
public void testFromNullByteArrayToBytes() {
final Byte[][] byteArray = null;
assertNull(ArrayUtils.toBytes(byteArray));
}
}