001: package org.vraptor.reflection;
002:
003: import java.util.ArrayList;
004: import java.util.HashSet;
005: import java.util.Set;
006:
007: import org.vraptor.reflection.CollectionDealer;
008: import org.vraptor.reflection.SettingException;
009:
010: import junit.framework.TestCase;
011:
012: public class CollectionDealerTest extends TestCase {
013:
014: public void testArrayListExactSize() throws SettingException {
015: ArrayList<String> array = createArrayList(1);
016: Object result = new CollectionDealer().resizeAndSet(array, 0,
017: false, String.class);
018: assertEquals(array.size(), 1);
019: assertEquals(array, result);
020: }
021:
022: public void testArrayListBiggerThanNeeded() throws SettingException {
023: ArrayList<String> array = createArrayList(3);
024: Object result = new CollectionDealer().resizeAndSet(array, 0,
025: false, String.class);
026: assertEquals(array.size(), 3);
027: assertEquals(array, result);
028: }
029:
030: public void testArrayListResizeIt() throws SettingException {
031: ArrayList<String> array = createArrayList(0);
032: ArrayList result = (ArrayList) new CollectionDealer()
033: .resizeAndSet(array, 0, true, String.class);
034: assertEquals(array.size(), 1);
035: assertEquals(array, result);
036: }
037:
038: public void testArrayListResizeTooMuch() {
039: ArrayList<String> array = createArrayList(0);
040: try {
041: new CollectionDealer().resizeAndSet(array, 1, true,
042: String.class);
043: fail();
044: } catch (SettingException e) {
045: }
046: }
047:
048: public void testArrayListNotAllowedToCreate()
049: throws SettingException {
050: try {
051: ArrayList<String> array = createArrayList(0);
052: new CollectionDealer().resizeAndSet(array, 1, false,
053: String.class);
054: fail("Should be unable to create objects");
055: } catch (SettingException e) {
056: }
057: }
058:
059: private ArrayList<String> createArrayList(int i) {
060: ArrayList<String> ar = new ArrayList<String>();
061: for (int j = 0; j < i; j++) {
062: ar.add(j + "/" + i);
063: }
064: return ar;
065: }
066:
067: public void testSetExactSize() throws SettingException {
068: Set<String> array = createSet(1);
069: Object result = new CollectionDealer().resizeAndSet(array, 0,
070: false, String.class);
071: assertEquals(array.size(), 1);
072: assertEquals(array, result);
073: }
074:
075: public void testSetBiggerThanNeeded() throws SettingException {
076: Set<String> array = createSet(3);
077: Object result = new CollectionDealer().resizeAndSet(array, 0,
078: false, String.class);
079: assertEquals(array.size(), 3);
080: assertEquals(array, result);
081: }
082:
083: public void testSetResizeIt() throws SettingException {
084: Set<String> array = createSet(0);
085: Set result = (Set) new CollectionDealer().resizeAndSet(array,
086: 0, true, String.class);
087: assertEquals(array.size(), 1);
088: assertEquals(array, result);
089: }
090:
091: public void testSetNotAllowedToCreate() {
092: try {
093: Set<String> array = createSet(0);
094: new CollectionDealer().resizeAndSet(array, 1, false,
095: String.class);
096: fail();
097: } catch (SettingException e) {
098: // ok
099: }
100: }
101:
102: private Set<String> createSet(int i) {
103: Set<String> ar = new HashSet<String>();
104: for (int j = 0; j < i; j++) {
105: ar.add(j + "/" + i);
106: }
107: return ar;
108: }
109:
110: }
|