001: package net.sf.mockcreator.expectable;
002:
003: import java.util.Calendar;
004: import java.util.Date;
005:
006: import net.sf.mockcreator.MockCore;
007: import net.sf.mockcreator.Struct;
008: import net.sf.mockcreator.expectable.StructNoEquals;
009: import net.sf.mockcreator.expectable.ExpectDate;
010: import net.sf.mockcreator.expectable.ExpectFloat;
011: import net.sf.mockcreator.expectable.Ignore;
012: import net.sf.mockcreator.exceptions.MockException;
013: import net.sf.mockcreator.TestCase;
014:
015: public class ExpectablesTest extends TestCase {
016: MockExpectables mock;
017:
018: public ExpectablesTest(String name) {
019: super (name);
020: }
021:
022: public void setUp() throws Exception {
023: super .setUp();
024: mock = new MockExpectables();
025: }
026:
027: public void testIgnore() {
028: mock.acceptMap(new Ignore()).returns(
029: new Struct(1, 2, 3, "Hello"));
030:
031: Struct s = mock.map(new Struct(5, 6, 7, "FooBar"));
032: assertEquals(1, s.id);
033: assertEquals("Hello", s.name);
034: }
035:
036: public void testIgnoreOne() {
037: mock.acceptMap(new Ignore(), new Struct(777, 2, 3, "777"))
038: .returns(new Struct(1, 2, 3, "Hello"));
039:
040: Struct s = mock.map(new Struct(7, 1, 2, "Zoo"), new Struct(777,
041: 6, 7, "FooBar"));
042: assertEquals(1, s.id);
043: assertEquals("Hello", s.name);
044: }
045:
046: public void testNoEquals() {
047: StructNoEquals sne = new StructNoEquals();
048: sne.id = 555;
049: sne.name = "aName";
050: mock.expectZeroOrMoreGet().returns(sne);
051:
052: StructNoEquals s = mock.get(1);
053: assertEquals(555, s.id);
054: assertEquals("aName", s.name);
055: }
056:
057: public void testExpectDate() {
058: Calendar cal = Calendar.getInstance();
059: Date now = cal.getTime();
060: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 5);
061:
062: Date exp = cal.getTime();
063:
064: mock.acceptTimestamp(new ExpectDate(exp, 5000));
065: mock.acceptTimestamp(new ExpectDate(exp, 4000));
066:
067: mock.timestamp(now);
068:
069: try {
070: mock.timestamp(now);
071: fail();
072: } catch (MockException ex) {
073: }
074: }
075:
076: public void testExpectDateRange() {
077: Calendar cal = Calendar.getInstance();
078: Date upper = cal.getTime();
079: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 5);
080:
081: Date lower = cal.getTime();
082:
083: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 1);
084:
085: Date inside = cal.getTime();
086: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 10);
087:
088: Date outside = cal.getTime();
089:
090: try {
091: mock.acceptTimestamp(new ExpectDate(upper, lower));
092: fail();
093: } catch (IllegalArgumentException ex) {
094: }
095:
096: mock.acceptTimestamp(new ExpectDate(lower, upper));
097: mock.acceptTimestamp(new ExpectDate(lower, upper));
098:
099: mock.timestamp(inside);
100:
101: try {
102: mock.timestamp(outside);
103: fail();
104: } catch (MockException ex) {
105: }
106: }
107:
108: public void testExpectDateRangeAfter() {
109: Calendar cal = Calendar.getInstance();
110: Date now = cal.getTime();
111: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 10);
112:
113: Date future = cal.getTime();
114: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 20);
115:
116: Date past = cal.getTime();
117:
118: mock.acceptTimestamp(new ExpectDate(now, null));
119: mock.acceptTimestamp(new ExpectDate(now, null));
120:
121: mock.timestamp(future);
122:
123: try {
124: mock.timestamp(past);
125: fail();
126: } catch (MockException ex) {
127: }
128: }
129:
130: public void testExpectDateRangeBefore() {
131: Calendar cal = Calendar.getInstance();
132: Date now = cal.getTime();
133: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + 10);
134:
135: Date future = cal.getTime();
136: cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) - 20);
137:
138: Date past = cal.getTime();
139:
140: mock.acceptTimestamp(new ExpectDate(null, now));
141: mock.acceptTimestamp(new ExpectDate(null, now));
142:
143: mock.timestamp(past);
144:
145: try {
146: mock.timestamp(future);
147: fail();
148: } catch (MockException ex) {
149: }
150: }
151:
152: public void testExpectFloat() {
153: mock.acceptCalctype(new ExpectFloat(1.0f, 0.11f));
154: mock.acceptCalctype(new ExpectFloat(1.0f, 0.01f));
155:
156: mock.calctype(0.9f);
157:
158: try {
159: mock.calctype(0.9f);
160: fail();
161: } catch (MockException ex) {
162: }
163: }
164:
165: public void testExpectFloat2() {
166: mock.acceptCalctype(new ExpectFloat(1.0f, 0.11f));
167: mock.acceptCalctype(new ExpectFloat(1.0f, 0.01f));
168:
169: mock.calctype(1.1f);
170:
171: try {
172: mock.calctype(1.1f);
173: fail();
174: } catch (MockException ex) {
175: }
176: }
177:
178: public void testExpectFloatRange() {
179: mock.acceptCalctype(new ExpectFloat(new Float(1.0f), new Float(
180: 1.1f)));
181: mock.acceptCalctype(new ExpectFloat(new Float(1.0f), new Float(
182: 1.01f)));
183:
184: mock.calctype(1.05f);
185:
186: try {
187: mock.calctype(1.05f);
188: fail();
189: } catch (MockException ex) {
190: }
191: }
192:
193: public void testExpectDouble() {
194: mock.acceptCalcsum(new ExpectFloat(1.0, 0.1));
195: mock.acceptCalcsum(new ExpectFloat(1.0, 0.01));
196:
197: mock.calcsum(0.9);
198:
199: try {
200: mock.calcsum(0.9);
201: fail();
202: } catch (MockException ex) {
203: }
204: }
205:
206: public void testExpectDouble2() {
207: mock.acceptCalcsum(new ExpectFloat(1.0, 0.1));
208: mock.acceptCalcsum(new ExpectFloat(1.0, 0.01));
209:
210: mock.calcsum(1.1);
211:
212: try {
213: mock.calcsum(1.1);
214: fail();
215: } catch (MockException ex) {
216: }
217: }
218:
219: public void testExpectDoubleRange() {
220: mock.acceptCalcsum(new ExpectFloat(new Double(1.0), new Double(
221: 1.1)));
222: mock.acceptCalcsum(new ExpectFloat(new Double(1.0), new Double(
223: 1.01)));
224:
225: mock.calcsum(1.05);
226:
227: try {
228: mock.calcsum(1.05);
229: fail();
230: } catch (MockException ex) {
231: }
232: }
233:
234: public void testExpectInBlock() {
235: MockCore.startBlock();
236: mock.expectMap(new Struct(5, 6, 7, "hello")).returns(
237: new Struct(1, 1, 1, "1"));
238: mock.expectMap(new Struct(1, 2, 3, "bar")).returns(
239: new Struct(2, 2, 2, "2"));
240: MockCore.endBlock();
241:
242: Struct s1 = mock.map(new Struct(1, 2, 3, "bar"));
243: Struct s2 = mock.map(new Struct(5, 6, 7, "hello"));
244:
245: assertEquals(2, s1.id);
246: assertEquals(1, s2.id);
247: }
248:
249: public void testAcceptInBlock() {
250: MockCore.startBlock();
251: mock.acceptTimestamp(new ExpectDate(new Date(), 1000));
252: mock.acceptTimestamp(new ExpectDate(new Date(), 1000));
253: MockCore.endBlock();
254:
255: mock.timestamp(new Date());
256: mock.timestamp(new Date());
257: }
258: }
|