1   /*
2    * Jour - java profiler and monitoring library
3    *
4    * Copyright (C) 2004 Jour team
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Library General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Library General Public License for more details.
15   *
16   * You should have received a copy of the GNU Library General Public
17   * License along with this library; if not, write to the
18   * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19   * Boston, MA  02111-1307, USA.
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   * TODO Add docs
36   *
37   * Created on 08.12.2004
38   * Contributing Author(s):
39   *
40   *   Misha Lifschitz <mishalifschitz at users.sourceforge.net> (Inital implementation)
41   *   Vlad Skarzhevskyy <vlads at users.sourceforge.net> (Inital implementation)
42   *
43   * @author vlads
44   * @version $Revision: 46 $ ($Author: vlads $)  $Date: 2007-08-28 14:30:40 -0400 (Tue, 28 Aug 2007) $
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 	    // One day
125 	    //"MM-dd-yyyy",
126 	    verify("12-08-2004", "2004-12-08 00:00:01", true);
127 	    //"yyyy-MM-dd",
128 	    verify("2004-12-08", "2004-12-08 23:59:59", true);
129 		//"MM/dd/yyyy",
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 }