Ejemplo n.º 1
0
        public virtual void TestDeletePartiallyWrittenFilesIfAbort()
        {
            Directory         dir    = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random()));

            iwConf.SetMaxBufferedDocs(RandomInts.NextIntBetween(Random(), 2, 30));
            iwConf.SetCodec(CompressingCodec.RandomInstance(Random()));
            // disable CFS because this test checks file names
            iwConf.SetMergePolicy(NewLogMergePolicy(false));
            iwConf.SetUseCompoundFile(false);
            RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, iwConf);

            Document validDoc = new Document();

            validDoc.Add(new IntField("id", 0, Field.Store.YES));
            iw.AddDocument(validDoc);
            iw.Commit();

            // make sure that #writeField will fail to trigger an abort
            Document  invalidDoc = new Document();
            FieldType fieldType  = new FieldType();

            fieldType.Stored = true;
            invalidDoc.Add(new FieldAnonymousInnerClassHelper(this, fieldType));

            Assert.Throws <ArgumentException>(() =>
            {
                try
                {
                    iw.AddDocument(invalidDoc);
                    iw.Commit();
                }
                finally
                {
                    int counter = 0;
                    foreach (string fileName in dir.ListAll())
                    {
                        if (fileName.EndsWith(".fdt") || fileName.EndsWith(".fdx"))
                        {
                            counter++;
                        }
                    }
                    // Only one .fdt and one .fdx files must have been found
                    Assert.AreEqual(2, counter);
                    iw.Dispose();
                    dir.Dispose();
                }
            });
        }