예제 #1
0
        private static bool checkDateFormatSanity(NumberDateFormat dateFormat, long date, TimeZoneInfo timeZone)
        {
            IFormatProvider provider = dateFormat.FormatProvider ?? CultureInfo.CurrentCulture;

            if (IsOutOfBounds(date, provider))
            {
                return(false);
            }

            string         format = dateFormat.GetDateFormat();
            DateTimeOffset offset = DateTimeOffsetUtil.FromUnixTimeMilliseconds(Convert.ToInt64(date));

            offset = TimeZoneInfo.ConvertTime(offset, timeZone);
            string formattedDate = offset.ToString(format, provider);

            return(DateTimeOffset.TryParseExact(formattedDate, format, provider, DateTimeStyles.None, out DateTimeOffset _));
        }
예제 #2
0
        public void BeforeClass()
        {
            ANALYZER = new MockAnalyzer(Random());

            qp = new StandardQueryParser(ANALYZER);

            HashMap <String, /*Number*/ object> randomNumberMap = new HashMap <string, object>();

            /*SimpleDateFormat*/
            string dateFormat;
            long   randomDate;
            bool   dateFormatSanityCheckPass;
            int    count = 0;

            do
            {
                if (count > 100)
                {
                    fail("This test has problems to find a sane random DateFormat/NumberFormat. Stopped trying after 100 iterations.");
                }

                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(DATE_STYLE, TIME_STYLE, LOCALE)
                {
                    TimeZone = TIMEZONE
                };
                dateFormat = DATE_FORMAT.GetDateFormat();

                do
                {
                    randomDate = Random().nextLong();

                    // prune date value so it doesn't pass in insane values to some
                    // calendars.
                    randomDate = randomDate % 3400000000000L;

                    // truncate to second
                    randomDate = (randomDate / 1000L) * 1000L;

                    // only positive values
                    randomDate = Math.Abs(randomDate);
                } while (randomDate == 0L);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, randomDate);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, 0);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat,
                                                                   -randomDate);

                count++;
            } while (!dateFormatSanityCheckPass);

            //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) + 1);

            NUMBER_FORMAT = new NumberFormat(LOCALE);

            double randomDouble;
            long   randomLong;
            int    randomInt;
            float  randomFloat;

            while ((randomLong = Convert.ToInt64(NormalizeNumber(Math.Abs(Random().nextLong()))
                                                 )) == 0L)
            {
                ;
            }
            while ((randomDouble = Convert.ToDouble(NormalizeNumber(Math.Abs(Random().NextDouble()))
                                                    )) == 0.0)
            {
                ;
            }
            while ((randomFloat = Convert.ToSingle(NormalizeNumber(Math.Abs(Random().nextFloat()))
                                                   )) == 0.0f)
            {
                ;
            }
            while ((randomInt = Convert.ToInt32(NormalizeNumber(Math.Abs(Random().nextInt())))) == 0)
            {
                ;
            }

            randomNumberMap.Put(FieldType.NumericType.LONG.ToString(), randomLong);
            randomNumberMap.Put(FieldType.NumericType.INT.ToString(), randomInt);
            randomNumberMap.Put(FieldType.NumericType.FLOAT.ToString(), randomFloat);
            randomNumberMap.Put(FieldType.NumericType.DOUBLE.ToString(), randomDouble);
            randomNumberMap.Put(DATE_FIELD_NAME, randomDate);

            RANDOM_NUMBER_MAP = Collections.UnmodifiableMap(randomNumberMap);

            directory = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory,
                                                             NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))
                                                             .SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000))
                                                             .SetMergePolicy(NewLogMergePolicy()));

            Document doc = new Document();
            HashMap <String, NumericConfig> numericConfigMap = new HashMap <String, NumericConfig>();
            HashMap <String, Field>         numericFieldMap  = new HashMap <String, Field>();

            qp.NumericConfigMap = (numericConfigMap);

            foreach (FieldType.NumericType type in Enum.GetValues(typeof(FieldType.NumericType)))
            {
                numericConfigMap.Put(type.ToString(), new NumericConfig(PRECISION_STEP,
                                                                        NUMBER_FORMAT, type));

                FieldType ft2 = new FieldType(IntField.TYPE_NOT_STORED);
                ft2.NumericTypeValue     = (type);
                ft2.Stored               = (true);
                ft2.NumericPrecisionStep = (PRECISION_STEP);
                ft2.Freeze();
                Field field;

                switch (type)
                {
                case FieldType.NumericType.INT:
                    field = new IntField(type.ToString(), 0, ft2);
                    break;

                case FieldType.NumericType.FLOAT:
                    field = new FloatField(type.ToString(), 0.0f, ft2);
                    break;

                case FieldType.NumericType.LONG:
                    field = new LongField(type.ToString(), 0L, ft2);
                    break;

                case FieldType.NumericType.DOUBLE:
                    field = new DoubleField(type.ToString(), 0.0, ft2);
                    break;

                default:
                    fail();
                    field = null;
                    break;
                }
                numericFieldMap.Put(type.ToString(), field);
                doc.Add(field);
            }

            numericConfigMap.Put(DATE_FIELD_NAME, new NumericConfig(PRECISION_STEP,
                                                                    DATE_FORMAT, FieldType.NumericType.LONG));
            FieldType ft = new FieldType(LongField.TYPE_NOT_STORED);

            ft.Stored = (true);
            ft.NumericPrecisionStep = (PRECISION_STEP);
            LongField dateField = new LongField(DATE_FIELD_NAME, 0L, ft);

            numericFieldMap.Put(DATE_FIELD_NAME, dateField);
            doc.Add(dateField);

            foreach (NumberType numberType in Enum.GetValues(typeof(NumberType)))
            {
                setFieldValues(numberType, numericFieldMap);
                if (VERBOSE)
                {
                    Console.WriteLine("Indexing document: " + doc);
                }
                writer.AddDocument(doc);
            }

            reader   = writer.Reader;
            searcher = NewSearcher(reader);
            writer.Dispose();
        }
예제 #3
0
        public void BeforeClass()
        {
            ANALYZER = new MockAnalyzer(Random());

            qp = new StandardQueryParser(ANALYZER);

            HashMap<String, /*Number*/object> randomNumberMap = new HashMap<string, object>();

            /*SimpleDateFormat*/
            string dateFormat;
            long randomDate;
            bool dateFormatSanityCheckPass;
            int count = 0;
            do
            {
                if (count > 100)
                {
                    fail("This test has problems to find a sane random DateFormat/NumberFormat. Stopped trying after 100 iterations.");
                }

                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(DATE_STYLE, TIME_STYLE, LOCALE)
                {
                    TimeZone = TIMEZONE
                };
                dateFormat = DATE_FORMAT.GetDateFormat();

                do
                {
                    randomDate = Random().nextLong();

                    // prune date value so it doesn't pass in insane values to some
                    // calendars.
                    randomDate = randomDate % 3400000000000L;

                    // truncate to second
                    randomDate = (randomDate / 1000L) * 1000L;

                    // only positive values
                    randomDate = Math.Abs(randomDate);
                } while (randomDate == 0L);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, randomDate);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat, 0);

                dateFormatSanityCheckPass &= checkDateFormatSanity(dateFormat,
                          -randomDate);

                count++;
            } while (!dateFormatSanityCheckPass);

            //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) + 1);

            NUMBER_FORMAT = new NumberFormat(LOCALE);

            double randomDouble;
            long randomLong;
            int randomInt;
            float randomFloat;

            while ((randomLong = Convert.ToInt64(NormalizeNumber(Math.Abs(Random().nextLong()))
                )) == 0L)
                ;
            while ((randomDouble = Convert.ToDouble(NormalizeNumber(Math.Abs(Random().NextDouble()))
                )) == 0.0)
                ;
            while ((randomFloat = Convert.ToSingle(NormalizeNumber(Math.Abs(Random().nextFloat()))
                )) == 0.0f)
                ;
            while ((randomInt = Convert.ToInt32(NormalizeNumber(Math.Abs(Random().nextInt())))) == 0)
                ;

            randomNumberMap.Put(FieldType.NumericType.LONG.ToString(), randomLong);
            randomNumberMap.Put(FieldType.NumericType.INT.ToString(), randomInt);
            randomNumberMap.Put(FieldType.NumericType.FLOAT.ToString(), randomFloat);
            randomNumberMap.Put(FieldType.NumericType.DOUBLE.ToString(), randomDouble);
            randomNumberMap.Put(DATE_FIELD_NAME, randomDate);

            RANDOM_NUMBER_MAP = Collections.UnmodifiableMap(randomNumberMap);

            directory = NewDirectory();
            RandomIndexWriter writer = new RandomIndexWriter(Random(), directory,
                NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()))
                    .SetMaxBufferedDocs(TestUtil.NextInt(Random(), 50, 1000))
                    .SetMergePolicy(NewLogMergePolicy()));

            Document doc = new Document();
            HashMap<String, NumericConfig> numericConfigMap = new HashMap<String, NumericConfig>();
            HashMap<String, Field> numericFieldMap = new HashMap<String, Field>();
            qp.NumericConfigMap = (numericConfigMap);

            foreach (FieldType.NumericType type in Enum.GetValues(typeof(FieldType.NumericType)))
            {
                numericConfigMap.Put(type.ToString(), new NumericConfig(PRECISION_STEP,
                    NUMBER_FORMAT, type)); 

                FieldType ft2 = new FieldType(IntField.TYPE_NOT_STORED);
                ft2.NumericTypeValue = (type);
                ft2.Stored = (true);
                ft2.NumericPrecisionStep = (PRECISION_STEP);
                ft2.Freeze();
                Field field;

                switch (type)
                {
                    case FieldType.NumericType.INT:
                        field = new IntField(type.ToString(), 0, ft2);
                        break;
                    case FieldType.NumericType.FLOAT:
                        field = new FloatField(type.ToString(), 0.0f, ft2);
                        break;
                    case FieldType.NumericType.LONG:
                        field = new LongField(type.ToString(), 0L, ft2);
                        break;
                    case FieldType.NumericType.DOUBLE:
                        field = new DoubleField(type.ToString(), 0.0, ft2);
                        break;
                    default:
                        fail();
                        field = null;
                        break;
                }
                numericFieldMap.Put(type.ToString(), field);
                doc.Add(field);
            }

            numericConfigMap.Put(DATE_FIELD_NAME, new NumericConfig(PRECISION_STEP,
                DATE_FORMAT, FieldType.NumericType.LONG));
            FieldType ft = new FieldType(LongField.TYPE_NOT_STORED);
            ft.Stored = (true);
            ft.NumericPrecisionStep = (PRECISION_STEP);
            LongField dateField = new LongField(DATE_FIELD_NAME, 0L, ft);
            numericFieldMap.Put(DATE_FIELD_NAME, dateField);
            doc.Add(dateField);

            foreach (NumberType numberType in Enum.GetValues(typeof(NumberType)))
            {
                setFieldValues(numberType, numericFieldMap);
                if (VERBOSE) Console.WriteLine("Indexing document: " + doc);
                writer.AddDocument(doc);
            }

            reader = writer.Reader;
            searcher = NewSearcher(reader);
            writer.Dispose();

        }