1 16 package net.sf.dozer.util.mapping.util; 17 18 import java.util.ArrayList ; 19 import java.util.Date ; 20 import java.util.HashSet ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.Set ; 24 import java.util.SortedSet ; 25 import java.util.TreeSet ; 26 import java.util.Vector ; 27 28 import junit.framework.Assert; 29 import net.sf.dozer.util.mapping.DozerTestBase; 30 import net.sf.dozer.util.mapping.vo.InsideTestObject; 31 import net.sf.dozer.util.mapping.vo.SimpleObj; 32 33 36 public class CollectionUtilsTest extends DozerTestBase { 37 private CollectionUtils utils = new CollectionUtils(); 38 39 public void testIsList() throws Exception { 40 Object [] values = new Object [] { new ArrayList (), new Vector (), new LinkedList () }; 41 42 for (int i = 0; i < values.length; i++) { 43 assertTrue(utils.isList(values[i].getClass())); 44 } 45 } 46 47 public void testIsSet() throws Exception { 48 Object [] values = new Object [] { new TreeSet (), new HashSet (), new HashSet () }; 49 50 for (int i = 0; i < values.length; i++) { 51 assertTrue(utils.isSet(values[i].getClass())); 52 } 53 } 54 55 public void testIsArray() throws Exception { 56 Object [] values = new Object [] { new int[3], new InsideTestObject[2], new String [3] }; 57 58 for (int i = 0; i < values.length; i++) { 59 assertTrue(utils.isArray(values[i].getClass())); 60 } 61 } 62 63 public void testIsPrimitiveArray() throws Exception { 64 Object [] values = new Object [] { new int[3], new long[2], new boolean[3] }; 65 66 for (int i = 0; i < values.length; i++) { 67 assertTrue(utils.isPrimitiveArray(values[i].getClass())); 68 } 69 } 70 71 public void testIsPrimitiveArray_False() throws Exception { 72 Object [] values = new Object [] { new String [3], new Date [2], new SimpleObj[3] }; 73 74 for (int i = 0; i < values.length; i++) { 75 assertFalse(utils.isPrimitiveArray(values[i].getClass())); 76 } 77 } 78 79 public void testGetValueFromCollection() throws Exception { 80 String sysTime = String.valueOf(System.currentTimeMillis()); 81 String [] input = new String [] { "zer", "one", "two", "three", "four", sysTime, "six", "seven" }; 82 Object result = utils.getValueFromCollection(input, 5); 83 84 assertEquals("invalid result", sysTime, result); 85 } 86 87 public void testLengthOfCollection() throws Exception { 88 String [] input = new String [] { "zer", "one", "two", "three", "four" }; 89 assertEquals("invalid array size", input.length, utils.getLengthOfCollection(input)); 90 } 91 92 public void testCreateNewSet_Default() throws Exception { 93 Set result = utils.createNewSet(Set .class); 94 assertNotNull("new set should not be null", result); 95 } 96 97 public void testCreateNewSet_SortedSetDefault() throws Exception { 98 Set result = utils.createNewSet(SortedSet .class); 99 assertNotNull("new set should not be null", result); 100 assertTrue("new set should be instance of SortedSet", result instanceof SortedSet ); 101 } 102 103 public void testCreateNewSet_FromExistingSet() throws Exception { 104 Set src = new HashSet (); 105 src.add("test1"); 106 src.add("test2"); 107 Set result = utils.createNewSet(Set .class, src); 108 assertNotNull("new set should not be null", result); 109 assertEquals("new set should equal src set", src, result); 110 } 111 112 public void testConvertPrimitiveArrayToList() throws Exception { 113 int[] srcArray = new int[] {5, 10, 15}; 114 115 List result = utils.convertPrimitiveArrayToList(srcArray); 116 assertEquals("invalid result size", srcArray.length, result.size()); 117 118 for(int i = 0; i < srcArray.length; i++) { 119 Integer srcValue = new Integer ((int)srcArray[i]); 120 Integer resultValue = (Integer ) result.get(i); 121 assertEquals("invalid result entry value", srcValue, resultValue); 122 } 123 } 124 125 } 126 | Popular Tags |