1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.filter;
22
23 import java.text.ParseException;
24 import java.text.SimpleDateFormat;
25 import java.util.Calendar;
26 import java.util.Date;
27 import java.util.GregorianCalendar;
28 import java.util.Locale;
29
30 import junit.framework.TestCase;
31 import net.sf.jour.log.Logger;
32 import net.sf.jour.util.TimeUtil;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public class TimeFilterTest extends TestCase {
47
48 protected static final Logger log = Logger.getLogger();
49
50 public static String date2text(Calendar c) {
51 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
52 return dateFormat.format(c.getTime());
53 }
54
55 public static String today() {
56 Calendar now = new GregorianCalendar();
57 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
58 return dateFormat.format(now.getTime());
59 }
60
61 public static Date txt2date(String textDate) throws Exception {
62 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
63 try {
64 return dateFormat.parse(textDate);
65 } catch (ParseException e) {
66 log.error("Wrong date" + textDate);
67 throw e;
68 }
69 }
70
71 private void verify(String pattern, String text, boolean expect) throws Exception {
72 log.debug("verify [" + pattern + "] [" + text + "]");
73 verify(pattern, txt2date(text).getTime(), expect);
74 }
75
76 private void verify(String pattern, double time, boolean expect) throws Exception {
77 TimeListFilter f = new TimeListFilter();
78 f.addPatterns(pattern);
79 if (TimeUtil.debug && log.isDebugEnabled()) {
80 log.debug("for " + (long)time);
81 f.debug();
82 }
83 assertEquals(pattern + " for {" + TimeUtil.timeStamp2dateString(time) + "}", expect, f.match(time));
84 }
85
86 public void testCurentTime() throws Exception {
87 Calendar now = new GregorianCalendar();
88 double t1 = System.currentTimeMillis();
89 now.add(Calendar.MINUTE, -2);
90 StringBuffer ptr = new StringBuffer();
91 ptr.append(date2text(now));
92 ptr.append(" - ");
93 now.add(Calendar.MINUTE, 2);
94 ptr.append(date2text(now));
95 log.debug("NativeTime:" + TimeUtil.timeStamp2dateString(t1));
96 verify(ptr.toString(), t1, true);
97 now.add(Calendar.MINUTE, -1);
98
99 now = new GregorianCalendar();
100 t1 = System.currentTimeMillis();
101 now.add(Calendar.SECOND, -2);
102
103 ptr = new StringBuffer();
104 ptr.append(date2text(now));
105 ptr.append(" - ");
106 now.add(Calendar.SECOND, +4);
107 ptr.append(date2text(now));
108 log.debug("NativeTime:" + TimeUtil.timeStamp2dateString(t1));
109 verify(ptr.toString(), t1, true);
110 }
111
112 public void testTimePatterns() throws Exception {
113 verify("00:00:00.000 - 24:00:00.000", System.currentTimeMillis(), true);
114 verify("00:00:00 - 24:00:00", System.currentTimeMillis(), true);
115 verify("00:00:00-01:00:00;01:00:00-24:00:00", System.currentTimeMillis(), true);
116 verify("00:00:00-23:00:00;23:00:00-24:00:00", System.currentTimeMillis(), true);
117
118 String today = today();
119 verify("00:01:00 - 00:01:50", today + " 00:01:01", true);
120 verify("00:01:00 - 00:01:50", today + " 00:01:51", false);
121
122 verify("00:00:00.000 - 00:00:01.009", today + " 00:00:01", true);
123
124
125
126 verify("12-08-2004", "2004-12-08 00:00:01", true);
127
128 verify("2004-12-08", "2004-12-08 23:59:59", true);
129
130 verify("12/08/2004", "2004-12-08 05:00:01", true);
131
132 verify("12-31-2004", "2004-12-08 00:00:01", false);
133
134 verify("12-08-2004 00:00:00 - 12-08-2004 23:59:59", "2004-12-08 05:00:01", true);
135 }
136 }