apche org.common.lang.arrayutils类的ut测试

apche commons包是对jdk包的扩展,提供了功能更加强大的api,对程序的编写十分有用。今天对本包下的lang包下的ArrayUtils类进行了测试,总体印象是数组的基本操作:将数组转化成map型数组,往数组中添加元素,删除元素,复制元素,反转数组,求子数组,基本类型数组和对象型数组的转化,检索元素是否存在该数组,指定位置取数组中的值等,十分齐全。一下是具体的UT测试。
  UT测试用到的JUint test 4.:



package commons.lang;

import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.ArrayUtils;
import org.junit.Test;

/**
 * 
 * @Progect_name: ApacheCommonTest
 * @Class_description: 测试apache commons.lang包下的: ArrayUtils
 * @Author: LinYiSong
 * @Date: 2010-11-6~
 * @Vesion:
 */
public class ArrayUtilsTest {
	int[] data1={1,2,3};
	int[] data2={4,5,6};
	/**
	 * 比较数组是否相等
	 */
	@Test
	public void testIsEquals() {
		// 字符串数组
		String[] str = { "aa", "bbbccc" };
		String[] str1 = { "bbbccc", "aa" };
		String[] str2 = { "aa", "bbbccc" };
		assertThat(ArrayUtils.isEquals(str, str1), is(false));
		assertThat(ArrayUtils.isEquals(str, str2), is(true));
		// double 数组 考虑到精度
		double[] d1 = { 1.2, 12.3 };
		double[] d2 = { 1.20, 12.300 };
		assertThat(ArrayUtils.isEquals(d1, d2), is(true));
		//自定义对象 数组 自定义对象要提供equals和hashCode方法 
		User user1=new User(1,"12");
		User user2=new User(1,"12");
		assertThat(user1.equals(user2),is(true));
		User[] users1={user1,user2};
		User[] users2={user2,user1};
		assertThat(ArrayUtils.isEquals(users1, users2), is(true));
	}
	
	@Test
	public void testToMap(){
		/** 二维数组,[1][2] **/
		/**正常情况**/
//		String[][] strs={{"1","2"},{"3","4"},{"4","3"}};
//		Map map=ArrayUtils.toMap(strs);
//		Set keys=map.keySet();
//		Iterator i=keys.iterator();
//		while(i.hasNext()){
//			Object key=i.next();
//			System.out.println(key+":"+map.get(key));
//		}
//		/**
//		  	3:4
//			1:2
//			4:3
//		 * 
//		 */
		/**[2]中少1**/
//		String[][] strs={{"1"},{"3","4"},{"4","3"}};
//		Map map=ArrayUtils.toMap(strs);
//		Set keys=map.keySet();
//		Iterator i=keys.iterator();
//		while(i.hasNext()){
//			Object key=i.next();
//			System.out.println(key+":"+map.get(key));
//		}
		/**
		 *结果报错 
		 */
		/**[2]中多1个**/
//		String[][] strs={{"1","2"},{"3","4"},{"4","3","5"}};
//		Map map=ArrayUtils.toMap(strs);
//		Set keys=map.keySet();
//		Iterator i=keys.iterator();
//		while(i.hasNext()){
//			Object key=i.next();
//			System.out.println(key+":"+map.get(key));
//		}
//		/**
//		 * 结果:
//			3:4
//			1:2
//			4:3
//			舍弃第二个后多余的
//		 */
		/**[2]中发生冲突**/
		String[][] strs={{"1","2"},{"3","4"},{"3","5","5"}};
		Map map=ArrayUtils.toMap(strs);
		Set keys=map.keySet();
		Iterator i=keys.iterator();
		while(i.hasNext()){
			Object key=i.next();
			System.out.println(key+":"+map.get(key));
		}
//		/**结果:
//			3:5
//		    1:2
//		     会覆盖相同key的值
//		 */
	}
	/**
	 * 测试add方法,以添加boolean型数据为例
boolean[] add(boolean[] array, boolean element) 
        Copies the given array and adds the given element at the end of the new array. 
static boolean[] add(boolean[] array, int index, boolean element) 
        Inserts the specified element at the specified position in the array. 
	 */
	@Test
	public void testAdd(){
		boolean[] bools={true,false,true};
		bools=ArrayUtils.add(bools, true);
		boolean[] result1={true,false,true,true};
		assertThat(bools,is(result1));
		
		bools=ArrayUtils.add(bools,0,false);
		boolean[] result2={false,true,false,true,true};
		assertThat(bools,is(result2));
		//越界出错
//		bools=ArrayUtils.add(bools,-1,false);
//		bools=ArrayUtils.add(bools,6,false);

	}
	/**
	 * 测试addAll类以int类型为例
 static int[] addAll(int[] array1, int[] array2) 
          Adds all the elements of the given arrays into a new array. 
	 */
	@Test
	public void testAddAll(){
		
		int[] data3={1,2,3,4,5,6};
		assertThat(ArrayUtils.addAll(data1, data2),is(data3));	
	}
	/**
	 * 测试clone方法,以int为例
	 */
	@Test
	public void testClone(){
		int[] data2={1,2,3};
		assertThat(ArrayUtils.clone(data1),is(data2));
	}
	/**
	 * 测试contains,以double为例
	 */
	@Test
	public void testContains(){
		double[] array={1.2,1,3,1.4};
		assertThat(ArrayUtils.contains(array, 1.4),is(true));
	}
	/**
	 * 测试getLength,以int的型数组为例
 If the input array is null, 0 is returned.

 ArrayUtils.getLength(null)            = 0
 ArrayUtils.getLength([])              = 0
 ArrayUtils.getLength([null])          = 1
 ArrayUtils.getLength([true, false])   = 2
 ArrayUtils.getLength([1, 2, 3])       = 3
 ArrayUtils.getLength(["a", "b", "c"]) = 3
	 */
	@Test
	public void testGetLength(){
		//与array.length相同
		assertThat(ArrayUtils.getLength(data1),is(3));
		//特殊情况
		assertThat(ArrayUtils.getLength(null),is(0));
	}
	/**
	 * hashCode,为数组算出hashcode
	 */
	@Test
	public void testHashCode(){
		assertThat(ArrayUtils.hashCode(data1),is(862547));
	}
	/**
	 * indexof:存在数组中则返回在数组中第一次出现的位置,不在则返回-1
	 */
	@Test
	public void testIndexOf(){
		int[] data2={1,1,1};
		assertThat(ArrayUtils.indexOf(data1, 1),is(0));
		/**不在数组中**/
		assertThat(ArrayUtils.indexOf(data1, 4),is(-1));
		assertThat(ArrayUtils.indexOf(data2, 1),is(0));
	}
	/**
	 * 测试IsEmpty,数组为null或没有数据时被认定为空
	 */
	@Test
	public void testIsEmpty(){
		int[] data2=null;
		int[] data3={};
		assertThat(ArrayUtils.isEmpty(data1),is(false));
		assertThat(ArrayUtils.isEmpty(data2),is(true));
		assertThat(ArrayUtils.isEmpty(data3),is(true));
	}
	/**
	 * isSameLength:调用getLength后比较两数组的大小
	 */
	@Test
	public void testIsSameLength(){
		int[] data3={};
		int[] data4=null;
		assertThat(ArrayUtils.isSameLength(data1,data2),is(true));
		assertThat(ArrayUtils.isSameLength(data1,data3),is(false));
		assertThat(ArrayUtils.isSameLength(data4,data3),is(true));
	}
	/**
	 * 从数组后开始找数组中是否存在,给定的数
	 */
	@Test
	public void testLastIndexOf(){
		int[] data2={1,1,1};
		assertThat(ArrayUtils.lastIndexOf(data1,3),is(2));
		assertThat(ArrayUtils.lastIndexOf(data1,4),is(-1));
		assertThat(ArrayUtils.lastIndexOf(data2,1),is(2));
		
	 }
	/**
	 *测试remove指定数组的位置移除数据,如果下标越界会抛出异常
	 * remove(char[] array, int index) 
          Removes the element at the specified position from the specified array.
	 */
	@Test
	public void testRemove(){
		int[] temp={1,2};
		assertThat(ArrayUtils.remove(data1, 2),is(temp));
//		assertThat(ArrayUtils.remove(data1, 4),is(temp));
//		assertThat(ArrayUtils.remove(data1,-1),is(temp));
	}
	/**
	 * removeElement,根据指定的元素移除数组中的数,
	 * 如果数组中不存在则数组保持不变
	 */
	@Test
	public void testRemoveElement(){
		int[] temp={1,2};
		assertThat(ArrayUtils.removeElement(data1, 3),is(temp));
		assertThat(ArrayUtils.removeElement(data1, 4),is(data1));
		
	}
	/**
	 *reverse,将原数组反转,原数组发生改变,所以返回值为void
	 */
	@Test
	public void testReverse(){
		int[] temp={3,2,1};
		ArrayUtils.reverse(data1);
		assertThat(data1,is(temp));
	}
	/**
	 * subarray,截取子数组
	 * public static int[] subarray(int[] array,
                             int startIndexInclusive,
                             int endIndexExclusive)
          startIndexInclusive,从该位置开始截数组,包含此位置
          endIndexExclusive,到该位置停止截取数组,是在该位置前,不截取此位置
          startIndexInclusive,和    endIndexExclusive可以超过原数组的边界,
                    如果超过会选取与原队列相交的部分,截取不到是返回长度为0的数组              
	 */
	@Test
	public void testSubarray(){
		int[] temp={1,2};
		int[] temp1={1};
		int[] temp2={3};
		int[] temp3={};
		assertThat(ArrayUtils.subarray(data1,0,2),is(temp));
		assertThat(ArrayUtils.subarray(data1,0,1),is(temp1));
		assertThat(ArrayUtils.subarray(data1,-1,1),is(temp1));
		
		assertThat(ArrayUtils.subarray(data1,2,3),is(temp2));
		assertThat(ArrayUtils.subarray(data1,2,5),is(temp2));
		assertThat(ArrayUtils.subarray(data1,5,5),is(temp3));
	}
	/**
	 *toObject 将基本类型的数组转化成对象型的数组
	 */
	@Test
	public void testToObject(){
		Integer[] temp={1,2,3};
		assertThat(ArrayUtils.toObject(data1),is(temp));
	}
	
	/**
	 * toPrimitive,将对象型数组转化成基本类型数组
	 */
	@Test
	public void testToPrimitive(){
		Integer[] temp={1,2,3};
		assertThat(ArrayUtils.toPrimitive(temp),is(data1));
	}
	/**
	 * 将数组转化成String,将数组转化成java源代码的形式
	 * The format is that of Java source code, for example {a,b}.
	 */
	@Test
	public void testToString1(){
		String str="{1,2,3}";
		int[] temp=null;
		assertThat(ArrayUtils.toString(data1),is(str));
		assertThat(ArrayUtils.toString(temp),is("{}"));
		
	}
	/**
	 * 将数组转化成String,将数组转化成java源代码的形式,可以添加当数组为空时的返回值
	 * public static String toString(Object array,
                              String stringIfNull)
	 */
	@Test
	public void testToString2(){
		String str="{1,2,3}";
		int[] temp=null;
		assertThat(ArrayUtils.toString(data1),is(str));
		assertThat(ArrayUtils.toString(temp,"数组为空"),is("数组为空"));
	}
}


你可能感兴趣的:(apache,jdk,JUnit)