Clover Coverage Report
Coverage timestamp: Fri May 9 2008 10:54:27 EST
201   523   57   7.73
30   348   0.28   5.2
26     2.19  
5    
 
  TestLockFactory       Line # 38 144 35 91.1% 0.9111111
  TestLockFactory.WriterThread       Line # 378 24 9 56.2% 0.5625
  TestLockFactory.SearcherThread       Line # 429 24 7 66.7% 0.6666667
  TestLockFactory.MockLockFactory       Line # 472 6 3 88.9% 0.8888889
  TestLockFactory.MockLockFactory.MockLock       Line # 492 3 3 66.7% 0.6666667
 
  (13)
 
1    package org.apache.lucene.store;
2   
3    /**
4    * Copyright 2004 The Apache Software Foundation
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10    * http://www.apache.org/licenses/LICENSE-2.0
11    *
12    * Unless required by applicable law or agreed to in writing, software
13    * distributed under the License is distributed on an "AS IS" BASIS,
14    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    * See the License for the specific language governing permissions and
16    * limitations under the License.
17    */
18   
19    import junit.framework.TestCase;
20   
21    import java.util.Hashtable;
22    import java.util.Enumeration;
23   
24    import java.io.IOException;
25    import java.io.File;
26   
27    import org.apache.lucene.document.Document;
28    import org.apache.lucene.document.Field;
29    import org.apache.lucene.index.IndexReader;
30    import org.apache.lucene.index.IndexWriter;
31    import org.apache.lucene.search.IndexSearcher;
32    import org.apache.lucene.search.Query;
33    import org.apache.lucene.index.Term;
34    import org.apache.lucene.search.TermQuery;
35    import org.apache.lucene.search.Hits;
36    import org.apache.lucene.analysis.WhitespaceAnalyzer;
37   
 
38    public class TestLockFactory extends TestCase {
39   
40    // Verify: we can provide our own LockFactory implementation, the right
41    // methods are called at the right time, locks are created, etc.
42   
 
43  1 toggle public void testCustomLockFactory() throws IOException {
44  1 Directory dir = new RAMDirectory();
45  1 MockLockFactory lf = new MockLockFactory();
46  1 dir.setLockFactory(lf);
47   
48    // Lock prefix should have been set:
49  1 assertTrue("lock prefix was not set by the RAMDirectory", lf.lockPrefixSet);
50   
51  1 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
52   
53    // add 100 documents (so that commit lock is used)
54  101 for (int i = 0; i < 100; i++) {
55  100 addDoc(writer);
56    }
57   
58    // Both write lock and commit lock should have been created:
59  1 assertEquals("# of unique locks created (after instantiating IndexWriter)",
60    2, lf.locksCreated.size());
61  1 assertTrue("# calls to makeLock <= 2 (after instantiating IndexWriter)",
62    lf.makeLockCount > 2);
63   
64  3 for(Enumeration e = lf.locksCreated.keys(); e.hasMoreElements();) {
65  2 String lockName = (String) e.nextElement();
66  2 MockLockFactory.MockLock lock = (MockLockFactory.MockLock) lf.locksCreated.get(lockName);
67  2 assertTrue("# calls to Lock.obtain is 0 (after instantiating IndexWriter)",
68    lock.lockAttempts > 0);
69    }
70   
71  1 writer.close();
72    }
73   
74    // Verify: we can use the NoLockFactory with RAMDirectory w/ no
75    // exceptions raised:
76    // Verify: NoLockFactory allows two IndexWriters
 
77  1 toggle public void testRAMDirectoryNoLocking() throws IOException {
78  1 Directory dir = new RAMDirectory();
79  1 dir.setLockFactory(NoLockFactory.getNoLockFactory());
80   
81  1 assertTrue("RAMDirectory.setLockFactory did not take",
82    NoLockFactory.class.isInstance(dir.getLockFactory()));
83   
84  1 IndexWriter writer = new IndexWriter(dir, new WhitespaceAnalyzer(), true);
85   
86    // Create a 2nd IndexWriter. This is normally not allowed but it should run through since we're not
87    // using any locks:
88  1 IndexWriter writer2 = null;
89  1 try {
90  1 writer2 = new IndexWriter(dir, new WhitespaceAnalyzer(), false);
91    } catch (Exception e) {
92  0