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.Date;
26 import java.util.Locale;
27 import java.util.Calendar;
28 import java.util.GregorianCalendar;
29
30 import junit.framework.TestCase;
31
32 import org.apache.log4j.Logger;
33
34 import net.sf.jour.timer.NativeTimer;
35 import net.sf.jour.util.TimeUtil;
36
37 /***
38 * TODO Add docs
39 *
40 * Created on 08.12.2004
41 * Contributing Author(s):
42 *
43 * Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
44 * Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
45 *
46 * @author vlads
47 * @version $Revision: 1.1 $ ($Author: vlads $) $Date: 2004/12/09 07:26:42 $
48 */
49 public class TimeFilterTest extends TestCase {
50
51 protected static final Logger log = Logger.getLogger(TimeFilterTest.class);
52
53 public static String date2text(Calendar c) {
54 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
55 return dateFormat.format(c.getTime());
56 }
57
58 public static String today() {
59 Calendar now = new GregorianCalendar();
60 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
61 return dateFormat.format(now.getTime());
62 }
63
64 public static Date txt2date(String textDate) throws Exception {
65 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
66 try {
67 return dateFormat.parse(textDate);
68 } catch (ParseException e) {
69 log.error("Wrong date" + textDate);
70 throw e;
71 }
72 }
73
74 private void verify(String pattern, String text, boolean expect) throws Exception {
75 log.debug("verify [" + pattern + "] [" + text + "]");
76 verify(pattern, txt2date(text).getTime(), expect);
77 }
78
79 private void verify(String pattern, double time, boolean expect) throws Exception {
80 TimeListFilter f = new TimeListFilter();
81 f.addPatterns(pattern);
82 if (TimeUtil.debug && log.isDebugEnabled()) {
83 f.debug();
84 }
85 assertEquals(pattern + " " + TimeUtil.timeStamp2dateString(time), expect, f.match(time));
86 }
87
88 public void testCurentTime() throws Exception {
89 Calendar now = new GregorianCalendar();
90 double t1 = NativeTimer.getTime();
91 now.add(Calendar.MINUTE, -1);
92 StringBuffer ptr = new StringBuffer();
93 ptr.append(date2text(now));
94 ptr.append(" - ");
95 now.add(Calendar.MINUTE, 2);
96 ptr.append(date2text(now));
97 log.debug("NativeTime:" + TimeUtil.timeStamp2dateString(t1));
98 verify(ptr.toString(), t1, true);
99 now.add(Calendar.MINUTE, -1);
100
101 now = new GregorianCalendar();
102 t1 = NativeTimer.getTime();
103 now.add(Calendar.SECOND, -2);
104
105 ptr = new StringBuffer();
106 ptr.append(date2text(now));
107 ptr.append(" - ");
108 now.add(Calendar.SECOND, +4);
109 ptr.append(date2text(now));
110 log.debug("NativeTime:" + TimeUtil.timeStamp2dateString(t1));
111 verify(ptr.toString(), t1, true);
112 }
113
114 public void testTimePatterns() throws Exception {
115 verify("00:00:00.000 - 24:00:00.000", System.currentTimeMillis(), true);
116 verify("00:00:00 - 24:00:00", System.currentTimeMillis(), true);
117 verify("00:00:00-01:00:00;01:00:00-24:00:00", System.currentTimeMillis(), true);
118 verify("00:00:00-23:00:00;23:00:00-24:00:00", System.currentTimeMillis(), true);
119
120 String today = today();
121 verify("00:01:00 - 00:01:50", today + " 00:01:01", true);
122 verify("00:01:00 - 00:01:50", today + " 00:01:51", false);
123
124 verify("00:00:00.000 - 00:00:01.009", today + " 00:00:01", true);
125
126
127
128 verify("12-08-2004", "2004-12-08 00:00:01", true);
129
130 verify("2004-12-08", "2004-12-08 23:59:59", true);
131
132 verify("12/08/2004", "2004-12-08 05:00:01", true);
133
134 verify("12-31-2004", "2004-12-08 00:00:01", false);
135
136 verify("12-08-2004 00:00:00 - 12-08-2004 23:59:59", "2004-12-08 05:00:01", true);
137 }
138
139
140
141
142 public void testCurentTimeAgain() throws Exception {
143 testCurentTime();
144 }
145 }