001: package net.sf.mockcreator.expectable;
002:
003: import java.util.Date;
004:
005: import net.sf.mockcreator.TestCase;
006: import net.sf.mockcreator.expectable.ExpectDate;
007: import net.sf.mockcreator.exceptions.MockException;
008:
009: public class ExpectDateTest extends TestCase {
010:
011: public ExpectDateTest(String name) {
012: super (name);
013: }
014:
015: public void testMillisecondsNull() {
016: try {
017: new ExpectDate(null, 1000);
018: fail();
019: } catch (IllegalArgumentException ex) {
020: // okay
021: }
022: }
023:
024: public void testMillisecondsNegative() {
025: try {
026: new ExpectDate(null, -1);
027: fail();
028: } catch (IllegalArgumentException ex) {
029: // okay
030: }
031: }
032:
033: public void testRangeBackwards() {
034: try {
035: Date d1 = new Date();
036: Date d2 = new Date(d1.getTime() + 100);
037: new ExpectDate(d2, d1);
038: fail();
039: } catch (IllegalArgumentException ex) {
040: // okay
041: }
042: }
043:
044: public void testRangeSame() {
045: Date d1 = new Date();
046: ExpectDate ed = new ExpectDate(d1, d1);
047: assertTrue(ed.isExpected(d1));
048:
049: Date d2 = new Date(d1.getTime() + 1);
050: assertFalse(ed.isExpected(d2));
051: d2 = new Date(d1.getTime() - 1);
052: assertFalse(ed.isExpected(d2));
053:
054: assertFalse(ed.isExpected(null));
055: }
056:
057: public void testRangeBothNull() {
058: try {
059: new ExpectDate(null, null);
060: fail();
061: } catch (IllegalArgumentException ex) {
062: // okay
063: }
064: }
065:
066: public void testMillisecondsRange() {
067: Date now = new Date();
068: Date old = new Date(now.getTime() - 1000);
069: Date fut = new Date(now.getTime() + 1000);
070:
071: ExpectDate ed = new ExpectDate(now, 1000);
072: assertTrue(ed.isExpected(old));
073: assertTrue(ed.isExpected(fut));
074:
075: old = new Date(now.getTime() - 1001);
076: fut = new Date(now.getTime() + 1001);
077: assertFalse(ed.isExpected(old));
078: assertFalse(ed.isExpected(fut));
079:
080: assertFalse(ed.isExpected(null));
081: }
082:
083: public void testFullRange() {
084: Date now = new Date();
085: Date old = new Date(now.getTime() - 1000);
086: Date fut = new Date(now.getTime() + 1000);
087:
088: ExpectDate ed = new ExpectDate(old, fut);
089: assertTrue(ed.isExpected(old));
090: assertTrue(ed.isExpected(now));
091: assertTrue(ed.isExpected(fut));
092:
093: old = new Date(now.getTime() - 1001);
094: fut = new Date(now.getTime() + 1001);
095: assertFalse(ed.isExpected(old));
096: assertFalse(ed.isExpected(fut));
097:
098: assertFalse(ed.isExpected(null));
099: }
100:
101: public void testFromRange() {
102: Date now = new Date();
103: Date old = new Date(now.getTime() - 10000);
104: Date fut = new Date(now.getTime() + 1000);
105:
106: ExpectDate ed = new ExpectDate(null, fut);
107: assertTrue(ed.isExpected(old));
108: assertTrue(ed.isExpected(now));
109: assertTrue(ed.isExpected(fut));
110:
111: fut = new Date(now.getTime() + 1001);
112: assertFalse(ed.isExpected(fut));
113:
114: assertFalse(ed.isExpected(null));
115: }
116:
117: public void testTillRange() {
118: Date now = new Date();
119: Date old = new Date(now.getTime() - 1000);
120: Date fut = new Date(now.getTime() + 10000);
121:
122: ExpectDate ed = new ExpectDate(old, null);
123: assertTrue(ed.isExpected(old));
124: assertTrue(ed.isExpected(now));
125: assertTrue(ed.isExpected(fut));
126:
127: old = new Date(now.getTime() - 1001);
128: assertFalse(ed.isExpected(old));
129:
130: assertFalse(ed.isExpected(null));
131: }
132:
133: public void testIsExpectedNonDate() {
134: ExpectDate ed = new ExpectDate(new Date(), null);
135: assertFalse(ed.isExpected("String"));
136: ed.toString();
137: }
138:
139: public void testSQLDate() {
140: Date now = new Date();
141: ExpectDate ed = new ExpectDate(now, 1000);
142:
143: java.sql.Date sql = new java.sql.Date(now.getTime() - 500);
144: assertTrue(ed.isExpected(sql));
145:
146: sql = new java.sql.Date(now.getTime() - 1500);
147: assertFalse(ed.isExpected(sql));
148: }
149: }
|