@@ -0,0 +1,1235 @@
+package indexing;%0A%0Aimport java.io.File;%0Aimport java.io.IOException;%0A%0Aimport junit.framework.TestCase;%0A%0Aimport org.apache.lucene.analysis.SimpleAnalyzer;%0Aimport org.apache.lucene.index.IndexWriter;%0Aimport org.apache.lucene.store.Directory;%0Aimport org.apache.lucene.store.FSDirectory;%0Aimport org.apache.lucene.store.LockObtainFailedException;%0A%0A/**%0A * Chapter 2.11.3 Listing 2.5%3Cbr%3E%0A * Using file-based locks to enforce a single writer at time%0A * %0A * @author Zhang Gang%0A * @date 2014%E5%B9%B411%E6%9C%886%E6%97%A5%0A */%0Apublic class LockTest extends TestCase%7B%0A%0A%09private Directory dir;%0A%0A%09protected void setUp() throws Exception %7B%0A%09%09String indexDir = System.getProperty(%22java.io.tmpdir%22, %22tmp%22)%0A%09%09%09%09+ System.getProperty(%22file.separator%22) + %22index%22;%0A%09%09dir = FSDirectory.open(new File(indexDir));%0A%09%7D%0A%0A%09public void testWriterLock() throws IOException %7B%0A%09%09IndexWriter writer1 = new IndexWriter(dir, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);%0A%09%09IndexWriter writer2 = null;%0A%09%09try %7B%0A%09%09%09writer2 = new IndexWriter(dir, new SimpleAnalyzer(), IndexWriter.MaxFieldLength.UNLIMITED);%0A%09%09%09fail(%22We should never reach this point%22);%0A%09%09%7D catch (LockObtainFailedException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D finally %7B%0A%09%09%09writer1.close();%0A%09%09%09assertNull(writer2);%0A%09%09%7D%0A%09%7D%0A%7D

add LockTest.java source file