1 10 package org.mmbase.util; 11 import org.mmbase.util.dateparser.*; 12 13 import java.util.*; 14 import junit.framework.TestCase; 15 16 21 public class DateParserTest extends TestCase { 22 23 24 protected boolean sufficientlyEqual(Object value1, Object value2) { 25 if (value1 == null) return value2 == null; 26 if (value1 instanceof Date && value2 instanceof Date) { 27 return Math.abs(((Date) value1).getTime() - ((Date) value2).getTime()) < 200L; 29 } else { 30 return value1.equals(value2); 31 } 32 } 33 35 public void testNow() { 36 try { 37 assertTrue(sufficientlyEqual(DynamicDate.getInstance("now"), new Date())); 38 assertTrue(sufficientlyEqual(DynamicDate.getInstance("now - 5 minute"), new Date(System.currentTimeMillis() - 5 * 60 * 1000))); 39 assertTrue(sufficientlyEqual(DynamicDate.getInstance("now + 5 minute"), new Date(System.currentTimeMillis() + 5 * 60 * 1000))); 40 assertTrue(sufficientlyEqual(DynamicDate.getInstance("now + 5 hour"), new Date(System.currentTimeMillis() + 5 * 60 * 60 * 1000))); 41 } catch (Exception e) { 42 fail(e.getMessage()); 43 } 44 } 45 46 int NULL = -100000; 47 int IGN = -100001; 48 protected int[] getNow(int[] n) { 49 Calendar cal = Calendar.getInstance(); 50 if (n[0] != NULL && n[0] != IGN) cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + n[0]); 51 if (n[1] != NULL && n[1] != IGN) cal.set(Calendar.MONTH,cal.get(Calendar.MONTH) + n[1]); 52 if (n[2] != NULL && n[2] != IGN) cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n[2]); 53 if (n[3] != NULL && n[3] != IGN) cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + n[3]); 54 if (n[4] != NULL && n[4] != IGN) cal.set(Calendar.MINUTE, cal.get(Calendar.MINUTE) + n[4]); 55 if (n[5] != NULL && n[5] != IGN) cal.set(Calendar.SECOND, cal.get(Calendar.SECOND) + n[5]); 56 57 return new int[] { 58 n[0] == NULL ? 0 : (n[0] == IGN ? IGN : cal.get(Calendar.YEAR)), 59 n[1] == NULL ? 0 : (n[1] == IGN ? IGN : cal.get(Calendar.MONTH)), 60 n[2] == NULL ? 0 : (n[2] == IGN ? IGN : cal.get(Calendar.DAY_OF_MONTH)), 61 n[3] == NULL ? 0 : (n[3] == IGN ? IGN : cal.get(Calendar.HOUR_OF_DAY)), 62 n[4] == NULL ? 0 : (n[4] == IGN ? IGN : cal.get(Calendar.MINUTE)), 63 n[5] == NULL ? 0 : (n[5] == IGN ? IGN : cal.get(Calendar.SECOND))}; 64 } 65 protected Collection compare(Date date, int[] fields) { 66 List result = new ArrayList(); 67 Calendar cal = Calendar.getInstance(); 68 cal.setLenient(true); 69 cal.setTime(date); 70 if (fields[0] != IGN && cal.get(Calendar.YEAR) != fields[0]) result.add("year " + cal.get(Calendar.YEAR) + " != " + fields[0]); 71 if (fields[1] != IGN && cal.get(Calendar.MONTH) != fields[1]) result.add("month " + cal.get(Calendar.MONTH) + " != " + fields[1]); 72 if (fields[2] != IGN && cal.get(Calendar.DAY_OF_MONTH) != fields[2]) result.add("day of month " + cal.get(Calendar.DAY_OF_MONTH) + " != " + fields[2]); 73 if (fields[3] != IGN && cal.get(Calendar.HOUR_OF_DAY) != fields[3]) result.add("hour of day " + cal.get(Calendar.HOUR_OF_DAY) + " != " + fields[3]); 74 if (fields[4] != IGN && cal.get(Calendar.MINUTE) != fields[4]) result.add("minute " + cal.get(Calendar.MINUTE) + " != " + fields[4]); 75 if (fields[5] != IGN && cal.get(Calendar.SECOND) != fields[5]) result.add("second " + cal.get(Calendar.SECOND) + " != " + fields[5]); 76 return result; 77 78 } 79 80 protected void assertTrue(String date1, int[] date2) throws ParseException { 81 Date dyndate = DynamicDate.getInstance(date1); 82 int[] r = getNow(date2); 83 Collection result = compare(dyndate, r); 84 assertTrue(date1 + "->" + dyndate + " != " + new Date(r[0] - 1900, r[1], r[2], r[3], r[4], r[5]) + result, result.isEmpty()); 85 } 86 public void testDay() throws ParseException { 87 Date date = DynamicDate.getInstance("today"); 88 assertTrue("today", new int[] {0, 0, 0, NULL, NULL, NULL}); 89 assertTrue("today + 1 day", new int[] {0, 0, 1, NULL, NULL, NULL}); 90 assertTrue("tomorrow", new int[] {0, 0, 1, NULL, NULL, NULL}); 91 DynamicDate.getInstance("tomorrow 5 oclock"); 93 DynamicDate.getInstance("now 5 oclock"); 94 DynamicDate.getInstance("2006-01-10T06:12Z"); 95 DynamicDate.getInstance("2006-01-10 06:12"); 96 DynamicDate.getInstance("2006-01-10 5 oclock"); 98 DynamicDate.getInstance("friday"); 99 DynamicDate.getInstance("next friday"); 100 DynamicDate.getInstance("previous friday"); 101 } 102 103 protected void beginOfDay(Calendar cal) { 104 cal.set(Calendar.MILLISECOND, 0); 105 cal.set(Calendar.SECOND, 0); 106 cal.set(Calendar.MINUTE, 0); 107 cal.set(Calendar.HOUR_OF_DAY, 0); 108 } 109 public void testToday() { 110 try { 111 Calendar today = Calendar.getInstance(); 112 beginOfDay(today); 113 assertTrue(sufficientlyEqual(DynamicDate.getInstance("today"), today.getTime())); 114 } catch (Exception e) { 115 fail(e.getMessage()); 116 } 117 118 } 119 120 } 121 | Popular Tags |