01: package net.sf.mockcreator.expectable;
02:
03: import java.util.Date;
04:
05: /**
06: * Expects Date with given conditions. TODO: usage
07: */
08: public class ExpectDate implements ExpectableParameter {
09:
10: private Date from;
11: private Date till;
12:
13: /**
14: * Construct expectable by date and allowed error in milliseconds.
15: * Borders of the range are included.
16: *
17: * @param date
18: * Expected date
19: * @param millisDeviation
20: * Allowed distance from the date in millis.
21: */
22: public ExpectDate(Date date, long millisDeviation) {
23: if (millisDeviation < 0) {
24: throw new IllegalArgumentException("deviation is negative");
25: }
26:
27: if (date == null) {
28: throw new IllegalArgumentException("date is null");
29: }
30:
31: from = new Date(date.getTime() - millisDeviation);
32: till = new Date(date.getTime() + millisDeviation);
33: }
34:
35: /**
36: * Construct expectable by date range. Borders of the range are included.
37: *
38: * @param from
39: * Expected date lower bound inclusive.
40: * @param till
41: * Expected date upper bound inclusive.
42: */
43: public ExpectDate(Date from, Date till) {
44: if ((from == null) && (till == null)) {
45: throw new IllegalArgumentException("both dates are null");
46: }
47:
48: if ((from != null) && (till != null) && from.after(till)) {
49: throw new IllegalArgumentException("from is after till");
50: }
51:
52: this .from = from;
53: this .till = till;
54: }
55:
56: public boolean isExpected(Object actual) {
57: if (actual == null) {
58: return false;
59: }
60:
61: if (!(actual instanceof Date)) {
62: return false;
63: }
64:
65: Date act = (Date) actual;
66:
67: if (from != null) {
68: if (from.equals(act))
69: return true;
70: if (from.after(act))
71: return false;
72: }
73:
74: if (till != null) {
75: if (till.equals(act))
76: return true;
77: if (till.before(act))
78: return false;
79: }
80:
81: return true;
82: }
83:
84: public String toString() {
85: return "ExpectDate(" + from + ";" + till + ")";
86: }
87: }
|