Index: lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/standard/TestNumericQueryParser.java =================================================================== --- lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/standard/TestNumericQueryParser.java (revision 1145808) +++ lucene/contrib/queryparser/src/test/org/apache/lucene/queryParser/standard/TestNumericQueryParser.java (working copy) @@ -81,34 +81,64 @@ static void init() { try { - LOCALE = randomLocale(random); - TIMEZONE = randomTimeZone(random); - DATE_STYLE = randomDateStyle(random); - TIME_STYLE = randomDateStyle(random); ANALYZER = new MockAnalyzer(random); qp = new StandardQueryParser(ANALYZER); - NUMBER_FORMAT = NumberFormat.getNumberInstance(LOCALE); - NUMBER_FORMAT.setMaximumFractionDigits((random.nextInt() & 20) + 1); - NUMBER_FORMAT.setMinimumFractionDigits((random.nextInt() & 20) + 1); - NUMBER_FORMAT.setMaximumIntegerDigits((random.nextInt() & 20) + 1); - NUMBER_FORMAT.setMinimumIntegerDigits((random.nextInt() & 20) + 4); // the loop checks for < 1000, this is a must! - // assumes localized date pattern will have at least year, month, day, hour, minute - SimpleDateFormat dateFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance( - DATE_STYLE, TIME_STYLE, LOCALE); - - // not all date patterns includes era, full year, timezone and second, so we add them here - dateFormat.applyPattern(dateFormat.toPattern() + " G s Z yyyy"); - dateFormat.setTimeZone(TIMEZONE); - DATE_FORMAT = new NumberDateFormat(dateFormat); - HashMap randomNumberMap = new HashMap(); + SimpleDateFormat dateFormat; + long randomDate; + boolean dateFormatSanityCheckPass; + do { + dateFormatSanityCheckPass = true; + LOCALE = randomLocale(random); + TIMEZONE = randomTimeZone(random); + DATE_STYLE = randomDateStyle(random); + TIME_STYLE = randomDateStyle(random); + + // assumes localized date pattern will have at least year, month, day, + // hour, minute + dateFormat = (SimpleDateFormat) DateFormat + .getDateTimeInstance(DATE_STYLE, TIME_STYLE, LOCALE); + + // not all date patterns includes era, full year, timezone and second, + // so we add them here + dateFormat.applyPattern(dateFormat.toPattern() + " G s Z yyyy"); + dateFormat.setTimeZone(TIMEZONE); + + DATE_FORMAT = new NumberDateFormat(dateFormat); + NUMBER_FORMAT = NumberFormat.getNumberInstance(LOCALE); + NUMBER_FORMAT.setMaximumFractionDigits((random.nextInt() & 20) + 1); + NUMBER_FORMAT.setMinimumFractionDigits((random.nextInt() & 20) + 1); + NUMBER_FORMAT.setMaximumIntegerDigits((random.nextInt() & 20) + 1); + NUMBER_FORMAT.setMinimumIntegerDigits((random.nextInt() & 20) + 4); // the loop checks for < 1000, this is a must! + + // make sure random date is at least one second from 0 + while ((randomDate = normalizeNumber(Math.abs(random.nextLong())) + .longValue()) < 1000) + ; + + // prune date value so it doesn't pass in insane values to some + // calendars. + randomDate = randomDate % 3400000000000l; + + // truncate to second + randomDate = (randomDate / 1000) * 1000; + + dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, + randomDate); + + dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, 0); + + dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, + -randomDate); + + } while (!dateFormatSanityCheckPass); + double randomDouble; long randomLong; int randomInt; float randomFloat; - long randomDate; while ((randomLong = normalizeNumber(Math.abs(random.nextLong())) .longValue()) == 0) @@ -123,17 +153,6 @@ .intValue()) == 0) ; - // make sure random date is at least one second from 0 - while ((randomDate = normalizeNumber(Math.abs(random.nextLong())) - .longValue()) < 1000) - ; - - // prune date value so it doesn't pass in insane values to some calendars. - randomDate = randomDate % 3400000000000l; - - // truncate to second - randomDate = (randomDate / 1000) * 1000; - randomNumberMap.put(NumericField.DataType.LONG.name(), randomLong); randomNumberMap.put(NumericField.DataType.INT.name(), randomInt); randomNumberMap.put(NumericField.DataType.FLOAT.name(), randomFloat); @@ -151,6 +170,10 @@ private static IndexReader reader = null; private static IndexSearcher searcher = null; + private static boolean checkDateFormatSanity(DateFormat dateFormat, long date) throws ParseException { + return date == dateFormat.parse(dateFormat.format(new Date(date))).getTime(); + } + @BeforeClass public static void beforeClass() throws Exception { init(); @@ -193,31 +216,6 @@ reader = writer.getReader(); searcher = newSearcher(reader); writer.close(); - - -// SimpleDateFormat df = new SimpleDateFormat( -// "yyyy.MM.dd G 'at' HH:mm:ss z", LOCALE.ENGLISH); -// assumes localized date pattern will have at least year, month, day, hour, minute - SimpleDateFormat df = (SimpleDateFormat) DateFormat.getDateTimeInstance( - randomDateStyle(random), randomDateStyle(random), LOCALE.ENGLISH); - System.out.println(df.toPattern()); - // most of date pattern do not include era, so we add it here. Also, - // sometimes second is not available, we make sure it's present too - df.applyPattern(df.toPattern() + " G s Z yyyy"); - df.setTimeZone(TIMEZONE); - System.out.println(TIMEZONE); - System.out.println(TIMEZONE); - System.out.println(TIMEZONE); - long l1 = 0; - long l2 = -30000; - String d1 = df.format(new Date(l1)); - String d2 = df.format(new Date(l2)); - long newL1 = df.parse(d1).getTime(); - long newL2 = df.parse(d2).getTime(); - - System.out.println(l1 + " => " + d1 + " => " + newL1); - System.out.println(l2 + " => " + d2 + " => " + newL2); - }