Beispiel #1
0
        public virtual void  TestDirectInstantiation()
        {
            System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(AppSettings.Get("tempDir", System.IO.Path.GetTempPath()));

            int sz = 2;

            Directory[] dirs = new Directory[sz];

            dirs[0] = new SimpleFSDirectory(path, null);
            // dirs[1] = new NIOFSDirectory(path, null);
            System.Console.WriteLine("Skipping NIOFSDirectory() test under Lucene.Net");
            dirs[1] = new MMapDirectory(path, null);

            for (int i = 0; i < sz; i++)
            {
                Directory dir = dirs[i];
                dir.EnsureOpen();
                System.String fname       = "foo." + i;
                System.String lockname    = "foo" + i + ".lck";
                IndexOutput   out_Renamed = dir.CreateOutput(fname, null);
                out_Renamed.WriteByte((byte)i);
                out_Renamed.Close();

                for (int j = 0; j < sz; j++)
                {
                    Directory d2 = dirs[j];
                    d2.EnsureOpen();
                    Assert.IsTrue(d2.FileExists(fname, null));
                    Assert.AreEqual(1, d2.FileLength(fname, null));

                    // don't test read on MMapDirectory, since it can't really be
                    // closed and will cause a failure to delete the file.
                    if (d2 is MMapDirectory)
                    {
                        continue;
                    }

                    IndexInput input = d2.OpenInput(fname, null);
                    Assert.AreEqual((byte)i, input.ReadByte(null));
                    input.Close();
                }

                // delete with a different dir
                dirs[(i + 1) % sz].DeleteFile(fname, null);

                for (int j = 0; j < sz; j++)
                {
                    Directory d2 = dirs[j];
                    Assert.IsFalse(d2.FileExists(fname, null));
                }

                Lock lock_Renamed = dir.MakeLock(lockname);
                Assert.IsTrue(lock_Renamed.Obtain());

                for (int j = 0; j < sz; j++)
                {
                    Directory d2    = dirs[j];
                    Lock      lock2 = d2.MakeLock(lockname);
                    try
                    {
                        Assert.IsFalse(lock2.Obtain(1));
                    }
                    catch (LockObtainFailedException)
                    {
                        // OK
                    }
                }

                lock_Renamed.Release();

                // now lock with different dir
                lock_Renamed = dirs[(i + 1) % sz].MakeLock(lockname);
                Assert.IsTrue(lock_Renamed.Obtain());
                lock_Renamed.Release();
            }

            for (int i = 0; i < sz; i++)
            {
                Directory dir = dirs[i];
                dir.EnsureOpen();
                dir.Close();
                Assert.IsFalse(dir.isOpen_ForNUnit);
            }
        }