001: package net.sf.mockcreator;
002:
003: import java.util.Collections;
004: import java.util.HashMap;
005: import java.util.HashSet;
006: import java.util.Map;
007: import java.util.Set;
008:
009: import net.sf.mockcreator.utils.CompareByValue;
010: import net.sf.mockcreator.exceptions.MockException;
011:
012: public class ComplexTest extends TestCase {
013: MockComplex mock;
014:
015: public ComplexTest(String name) {
016: super (name);
017: }
018:
019: public void setUp() throws Exception {
020: super .setUp();
021: mock = new MockComplex();
022: }
023:
024: public void testGetArray_SetDummy() throws Exception {
025: mock.expectZeroOrMoreGetArr().returns(new int[] { 1, 2, 3 });
026: assertTrue(CompareByValue.equals(new int[] { 1, 2, 3 }, mock
027: .getArr(Collections.EMPTY_SET)));
028: }
029:
030: public void testGetArray_SetMultipleDummy() throws Exception {
031: mock.expectZeroOrMoreGetArr(set2D()).returns(
032: new int[] { 5, 6, 7 });
033: mock.expectZeroOrMoreGetArr().returns(new int[] { 1, 2, 3 });
034:
035: assertTrue(CompareByValue.equals(new int[] { 5, 6, 7 }, mock
036: .getArr(set2D())));
037: assertTrue(CompareByValue.equals(new int[] { 1, 2, 3 }, mock
038: .getArr(Collections.EMPTY_SET)));
039: }
040:
041: public void testGetArray_expect() throws Exception {
042: mock.expectGetArr(set2D()).returns(new int[] { 1, 2, 3 });
043: assertTrue(CompareByValue.equals(new int[] { 1, 2, 3 }, mock
044: .getArr(set2D())));
045: MockCore.verify();
046: }
047:
048: public void testGetArray_accept() throws Exception {
049: mock.acceptGetArr(set2D()).returns(new int[] { 1, 2, 3 });
050: assertTrue(CompareByValue.equals(new int[] { 1, 2, 3 }, mock
051: .getArr(set2D())));
052: MockCore.verify();
053: }
054:
055: private Set set2D() {
056: Set set = new HashSet();
057: set.add("FooBar");
058:
059: Set set2 = new HashSet();
060: set2.add("FooBaz");
061: set.add(set2);
062:
063: return set;
064: }
065:
066: public void testGetArray2D_SetDummy() throws Exception {
067: mock.expectZeroOrMoreGetArr2D().returns(arr2D(5));
068: assertTrue(CompareByValue.equals(arr2D(5), mock
069: .getArr2D(Collections.EMPTY_SET)));
070: }
071:
072: public void testGetArray2D_SetMultipleDummy() throws Exception {
073: mock.expectZeroOrMoreGetArr2D(set()).returns(arr2D(5));
074: mock.expectZeroOrMoreGetArr2D().returns(arr2D(6));
075:
076: assertTrue(CompareByValue
077: .equals(arr2D(5), mock.getArr2D(set())));
078: assertTrue(CompareByValue.equals(arr2D(6), mock
079: .getArr2D(Collections.EMPTY_SET)));
080: }
081:
082: public void testGetArray2D_expect() throws Exception {
083: mock.expectGetArr2D(set()).returns(arr2D(5));
084: assertTrue(CompareByValue
085: .equals(arr2D(5), mock.getArr2D(set())));
086: }
087:
088: public void testGetArray2D_expectMissed() throws Exception {
089: mock.expectGetSet2D(new int[] { 1, 2, 3 }).returns((Set) null);
090:
091: try {
092: mock.getSet2D(new int[] { 1, 2, 3 });
093: } catch (MockException ex) {
094: }
095: }
096:
097: public void testGetArray2D_accept() throws Exception {
098: mock.acceptGetArr2D(set()).returns(arr2D(8));
099: assertTrue(CompareByValue
100: .equals(arr2D(8), mock.getArr2D(set())));
101: }
102:
103: private Set set() {
104: Set set = new HashSet();
105: set.add("FooBar");
106: set.add(new int[][] { { 1, 2 }, { 3, 4 } });
107:
108: return set;
109: }
110:
111: private int[][] arr2D(int val) {
112: return new int[][] { { 1, 2 }, { 3, 4, val } };
113: }
114:
115: public void testGetBadSet() throws Exception {
116: mock.expectGetBad(badSet(true)).returns(1);
117: assertEquals(1, mock.getBad(badSet(false)));
118: }
119:
120: public void testGetBadMap() throws Exception {
121: mock.expectGetBad(badMap(false)).returns(8);
122: assertEquals(8, mock.getBad(badMap(true)));
123: }
124:
125: private Set badSet(boolean reversed) {
126: Set set = new HashSet();
127: set.add(new Complex.BadHash(1, reversed ? 1 : 0));
128: set.add(new Complex.BadHash(2, reversed ? 0 : 1));
129:
130: return set;
131: }
132:
133: private Map badMap(boolean reversed) {
134: Map map = new HashMap();
135: map.put(new Complex.BadHash(1, reversed ? 1 : 0),
136: new Complex.BadHash(10, 1));
137: map.put(new Complex.BadHash(2, reversed ? 0 : 1),
138: new Complex.BadHash(20, 2));
139:
140: return map;
141: }
142:
143: public void testEmptySet() throws Exception {
144: mock.expectGetBad(new HashSet()).returns(8);
145: assertEquals(8, mock.getBad(new HashSet()));
146: }
147:
148: public void testEmptyMap() throws Exception {
149: mock.expectGetBad(new HashMap()).returns(8);
150: assertEquals(8, mock.getBad(new HashMap()));
151: }
152:
153: public void testEmptyArray() throws Exception {
154: mock.expectGetSet(new int[][] {}).returns(new HashSet());
155: assertTrue(mock.getSet(new int[][] {}).isEmpty());
156: }
157: }
|