001: package org.vraptor.reflection;
002:
003: import java.lang.reflect.Array;
004:
005: import org.vraptor.reflection.ArrayDealer;
006: import org.vraptor.reflection.SettingException;
007:
008: import junit.framework.TestCase;
009:
010: public class ArrayDealerTest extends TestCase {
011:
012: private static final String VALUE = "value";
013:
014: public void testResizesButIsBiggerThanNeeded()
015: throws SettingException {
016: String[] array = createArray(1);
017: Object result = new ArrayDealer().resizeAndSet(array, 0, false,
018: String.class);
019: assertEquals(array, result);
020: }
021:
022: public void testResizesTooFast() {
023: String[] array = createArray(0);
024: try {
025: new ArrayDealer()
026: .resizeAndSet(array, 1, true, String.class);
027: fail();
028: } catch (SettingException e) {
029: // ok
030: }
031: }
032:
033: public void testResizesIt() throws SettingException {
034: String[] array = createArray(0);
035: Object result = new ArrayDealer().resizeAndSet(array, 0, true,
036: String.class);
037: assertEquals(Array.getLength(result), 1);
038: }
039:
040: public void testResizesItTwiceInARow() throws SettingException {
041: String[] array = createArray(0);
042: Object result = new ArrayDealer().resizeAndSet(array, 0, true,
043: String.class);
044: result = new ArrayDealer().resizeAndSet(result, 1, true,
045: String.class);
046: assertEquals(Array.getLength(result), 2);
047: }
048:
049: public void testNotAllowedToCreate() {
050: String[] array = createArray(0);
051: try {
052: new ArrayDealer().resizeAndSet(array, 1, false,
053: String.class);
054: fail();
055: } catch (SettingException e) {
056: }
057: }
058:
059: private String[] createArray(int i) {
060: String[] ar = new String[i];
061: for (int j = 0; j < i; j++) {
062: ar[j] = j + "/" + i;
063: }
064: return ar;
065: }
066:
067: public void testFixedBiggerThanShould() {
068: String[] array = createArray(1);
069: try {
070: new ArrayDealer().resizeAndSet(array, 0, VALUE);
071: fail();
072: } catch (SettingException e) {
073: // ok
074: }
075: }
076:
077: public void testFixedResizeTooFast() {
078: String[] array = createArray(0);
079: try {
080: new ArrayDealer().resizeAndSet(array, 1, VALUE);
081: fail();
082: } catch (SettingException e) {
083: // ok
084: }
085: }
086:
087: public void testFixedResizeIt() throws SettingException {
088: String[] array = createArray(0);
089: Object result = new ArrayDealer().resizeAndSet(array, 0, VALUE);
090: assertEquals(Array.getLength(result), 1);
091: }
092:
093: public void testFixedResizeItTwiceInARow() throws SettingException {
094: String[] array = createArray(0);
095: Object result = new ArrayDealer().resizeAndSet(array, 0, true,
096: String.class);
097: result = new ArrayDealer().resizeAndSet(result, 1, VALUE);
098: assertEquals(Array.getLength(result), 2);
099: }
100:
101: public void testNullArray() throws SettingException {
102: String[] array = null;
103: Object result = new ArrayDealer().resizeAndSet(array, 0, true,
104: String.class);
105: assertEquals(Array.getLength(result), 1);
106: }
107:
108: public void testFixedNotAllowedToCreate() {
109: String[] array = createArray(0);
110: try {
111: new ArrayDealer().resizeAndSet(array, 1, VALUE);
112: fail();
113: } catch (SettingException e) {
114: // ok
115: }
116: }
117:
118: public void testTriesToInstantiateWhenForbidden() {
119: try {
120: new ArrayDealer()
121: .resizeAndSet(null, 1, false, String.class);
122: fail();
123: } catch (SettingException e) {
124: // ok
125: }
126: }
127:
128: }
|