public void TestDetectClose()
 {
     Directory dir = new IsolatedStorageDirectory(IndexDir);
     dir.Close();
     try
     {
         dir.CreateOutput("test");
         Assert.Fail("did not hit expected exception");
     }
     catch (AlreadyClosedException ace)
     {
     }
 }
        public void TestDirectoryFilter()
        {
            using (IsolatedStorageDirectory dir = new IsolatedStorageDirectory(IndexDir))
            {
                System.String name = "file";

                dir.CreateOutput(name).Close();
                Assert.IsTrue(dir.FileExists(name));
                Assert.IsTrue(new System.Collections.ArrayList(dir.ListAll()).Contains(name));
                dir.DeleteFile(name);
                Assert.IsFalse(dir.FileExists(name));
            }
        }
 public void TestLock()
 {
     using (var dir = new IsolatedStorageDirectory(IndexDir))
     {
         IndexWriter writer1 = new IndexWriter(dir, new StandardAnalyzer());
         try
         {
             IndexWriter writer2 = new IndexWriter(dir, new StandardAnalyzer());
             Assert.Fail("Unexpected lock error");
         }
         catch
         {
         }
         finally
         {
             writer1.Close();
         }
     }
 }
        public void TestIndexAndSearch()
        {
            GenerateRandomDocs(10, 5000);

            using (var dir = new IsolatedStorageDirectory(IndexDir))
            {
                Random rgen = new Random();
                using (IndexReader reader = IndexReader.Open(dir, true))
                {
                    IndexSearcher sfs = new IndexSearcher(reader);
                    QueryParser parser = new MultiFieldQueryParser(new string[] { "category", "title", "body" }, new StandardAnalyzer());

                    for (int i = 0; i < 100; i++)
                    {
                        Query q = parser.Parse("some stuff " + RandomField(3, rgen));
                        TopDocs hits = sfs.Search(q, 200);
                        for (int j = 0; j < hits.ScoreDocs.Length; j++)
                        {
                            Document doc = reader.Document(hits.ScoreDocs[j].Doc);
                            Fieldable f = doc.GetField("title");
                        }
                    }
                }
            }
        }
        private void GenerateRandomDocs(int numCats, int numDocs)
        {
            using (var dir = new IsolatedStorageDirectory(IndexDir))
            {
                Random rgen = new Random();
                string[] categories = Enumerable.Range(0, numCats).Select(x => RandomString(4, rgen)).ToArray();
                IEnumerable<Document> docs = Enumerable.Range(0, numDocs).Select(x => RandomDocument(categories[rgen.Next(0, numCats - 1)], rgen));

                using (IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(), true))
                {
                    System.Threading.Tasks.Parallel.ForEach(docs, d =>
                    {
                        writer.AddDocument(d); //multi-access to writer
                    });

                    Assert.AreEqual(docs.Count(), writer.MaxDoc(), "Unexpected error in \"writer.AddDocument\"");
                }
            }
        }
 public static void Remove(string dirName)
 {
     var dir = new IsolatedStorageDirectory(dirName);
     dir.Remove();
     dir.Close();
 }