KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > util > CollectionUtilsTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.util;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.SortedSet JavaDoc;
25 import java.util.TreeSet JavaDoc;
26 import java.util.Vector JavaDoc;
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 /**
34  * @author tierney.matt
35  */

36 public class CollectionUtilsTest extends DozerTestBase {
37   private CollectionUtils utils = new CollectionUtils();
38   
39   public void testIsList() throws Exception JavaDoc {
40     Object JavaDoc[] values = new Object JavaDoc[] { new ArrayList JavaDoc(), new Vector JavaDoc(), new LinkedList JavaDoc() };
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 JavaDoc {
48     Object JavaDoc[] values = new Object JavaDoc[] { new TreeSet JavaDoc(), new HashSet JavaDoc(), new HashSet JavaDoc() };
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 JavaDoc {
56     Object JavaDoc[] values = new Object JavaDoc[] { new int[3], new InsideTestObject[2], new String JavaDoc[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 JavaDoc {
64     Object JavaDoc[] values = new Object JavaDoc[] { 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 JavaDoc {
72     Object JavaDoc[] values = new Object JavaDoc[] { new String JavaDoc[3], new Date JavaDoc[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 JavaDoc {
80     String JavaDoc sysTime = String.valueOf(System.currentTimeMillis());
81     String JavaDoc[] input = new String JavaDoc[] { "zer", "one", "two", "three", "four", sysTime, "six", "seven" };
82     Object JavaDoc result = utils.getValueFromCollection(input, 5);
83
84     assertEquals("invalid result", sysTime, result);
85   }
86
87   public void testLengthOfCollection() throws Exception JavaDoc {
88     String JavaDoc[] input = new String JavaDoc[] { "zer", "one", "two", "three", "four" };
89     assertEquals("invalid array size", input.length, utils.getLengthOfCollection(input));
90   }
91   
92   public void testCreateNewSet_Default() throws Exception JavaDoc {
93     Set JavaDoc result = utils.createNewSet(Set JavaDoc.class);
94     assertNotNull("new set should not be null", result);
95   }
96   
97   public void testCreateNewSet_SortedSetDefault() throws Exception JavaDoc {
98     Set JavaDoc result = utils.createNewSet(SortedSet JavaDoc.class);
99     assertNotNull("new set should not be null", result);
100     assertTrue("new set should be instance of SortedSet", result instanceof SortedSet JavaDoc);
101   }
102   
103   public void testCreateNewSet_FromExistingSet() throws Exception JavaDoc {
104     Set JavaDoc src = new HashSet JavaDoc();
105     src.add("test1");
106     src.add("test2");
107     Set JavaDoc result = utils.createNewSet(Set JavaDoc.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 JavaDoc {
113     int[] srcArray = new int[] {5, 10, 15};
114     
115     List JavaDoc 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 JavaDoc srcValue = new Integer JavaDoc((int)srcArray[i]);
120       Integer JavaDoc resultValue = (Integer JavaDoc) result.get(i);
121       assertEquals("invalid result entry value", srcValue, resultValue);
122     }
123   }
124   
125 }
126
Popular Tags