/*
* Copyright (c) 2012, Harald Kuhr
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name "TwelveMonkeys" nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.twelvemonkeys.util;
import org.junit.Ignore;
import org.junit.Test;
import java.util.*;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
/**
* CollectionUtilTest
*
* @author Harald Kuhr
* @author last modified by $Author: haraldk$
* @version $Id: CollectionUtilTest.java,v 1.0 24.01.12 17:39 haraldk Exp$
*/
public class CollectionUtilTest {
private static final Object[] stringObjects = new Object[] {"foo", "bar", "baz"};
private static final Object[] integerObjects = new Object[] {1, 2, 3};
@Test
public void testMergeArraysObject() {
Object[] merged = (Object[]) CollectionUtil.mergeArrays(stringObjects, integerObjects);
assertArrayEquals(new Object[] {"foo", "bar", "baz", 1, 2, 3}, merged);
}
@Test
public void testMergeArraysObjectOffset() {
Object[] merged = (Object[]) CollectionUtil.mergeArrays(stringObjects, 1, 2, integerObjects, 2, 1);
assertArrayEquals(new Object[] {"bar", "baz", 3}, merged);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectBadOffset() {
CollectionUtil.mergeArrays(stringObjects, 4, 2, integerObjects, 2, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectBadSecondOffset() {
CollectionUtil.mergeArrays(stringObjects, 1, 2, integerObjects, 4, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectBadLength() {
CollectionUtil.mergeArrays(stringObjects, 1, 4, integerObjects, 2, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectBadSecondLength() {
CollectionUtil.mergeArrays(stringObjects, 1, 2, integerObjects, 2, 2);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectNegativeOffset() {
CollectionUtil.mergeArrays(stringObjects, -1, 2, integerObjects, 2, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectNegativeSecondOffset() {
CollectionUtil.mergeArrays(stringObjects, 1, 2, integerObjects, -1, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectNegativeLength() {
CollectionUtil.mergeArrays(stringObjects, 1, -1, integerObjects, 2, 1);
}
@Test(expected = IndexOutOfBoundsException.class)
public void testMergeArraysObjectNegativeSecondLength() {
CollectionUtil.mergeArrays(stringObjects, 1, 2, integerObjects, 2, -1);
}
@Test
public void testMergeArraysObjectAssignable() {
Integer[] integers = {1, 2, 3}; // Integer assignable to Object
Object[] merged = (Object[]) CollectionUtil.mergeArrays(stringObjects, integers);
assertArrayEquals(new Object[] {"foo", "bar", "baz", 1, 2, 3}, merged);
}
@Test(expected = ArrayStoreException.class)
public void testMergeArraysObjectIllegalType() {
String[] strings = {"foo", "bar", "baz"};
Integer[] integers = {1, 2, 3}; // Integer not assignable to String
CollectionUtil.mergeArrays(strings, integers);
}
@Test(expected = ArrayStoreException.class)
public void testMergeArraysNativeIllegalType() {
char[] chars = {'a', 'b', 'c'};
int[] integers = {1, 2, 3}; // Integer not assignable to String
CollectionUtil.mergeArrays(chars, integers);
}
@Test
public void testMergeArraysNative() {
char[] chars = {'a', 'b', 'c'};
char[] more = {'x', 'y', 'z'};
char[] merged = (char[]) CollectionUtil.mergeArrays(chars, more);
assertArrayEquals(new char[] {'a', 'b', 'c', 'x', 'y', 'z'}, merged);
}
@Test
public void testSubArrayObject() {
String[] strings = CollectionUtil.subArray(new String[] {"foo", "bar", "baz", "xyzzy"}, 1, 2);
assertArrayEquals(new String[] {"bar", "baz"}, strings);
}
@Test
public void testSubArrayNative() {
int[] numbers = (int[]) CollectionUtil.subArray(new int[] {1, 2, 3, 4, 5}, 1, 3);
assertArrayEquals(new int[] {2, 3, 4}, numbers);
}
@Test(expected = IllegalArgumentException.class)
public void testEnumIteratorNull() {
CollectionUtil.iterator((Enumeration