FileDocCategorySizeDatePackage
TestFSDirectory.javaAPI DocApache Lucene 1.94916Mon Feb 20 09:19:34 GMT 2006org.apache.lucene.index.store

TestFSDirectory

public class TestFSDirectory extends TestCase
Test to illustrate the problem found when trying to open an IndexWriter in a situation where the the property org.apache.lucene.lockDir was not set and the one specified by java.io.tmpdir had been set to a non-existent path. What I observed is that this combination of conditions resulted in a NullPointerException being thrown in the create() method in FSDirectory, where files.length is de-referenced, but files is null.
author
Michael Goddard

Fields Summary
public static final String
FILE_SEP
public static final String
NON_EXISTENT_DIRECTORY
public static final String
TEST_INDEX_DIR
private String
orgApacheLuceneLockDir
private File
shouldBeADirectory
Constructors Summary
Methods Summary
private static java.lang.StringopenIndexWriter()
Open an IndexWriter
Catch any (expected) IOException
Close the IndexWriter

        IndexWriter iw = null;
        String ret = null;
        try {
            iw = new IndexWriter(TEST_INDEX_DIR, new StandardAnalyzer(), true);
        } catch (IOException e) {
            ret = e.toString();
            e.printStackTrace();
        } catch (NullPointerException e) {
            ret = e.toString();
            e.printStackTrace();
        } finally {
            if (iw != null) {
                try {
                    iw.close();
                } catch (IOException ioe) {
                    // ignore this
                }
            }
        }
        return ret;
    
private static voidrmDir(java.io.File dirName)

        if (dirName.exists()) {
            if (dirName.isDirectory()) {
                File[] contents = dirName.listFiles();
                for (int i = 0; i < contents.length; i++)
                    rmDir(contents[i]);
                dirName.delete();
            } else {
                dirName.delete();
            }
        }
	
public voidtearDown()


       
        if (orgApacheLuceneLockDir != null)
            System.setProperty("org.apache.lucene.lockDir",
                    orgApacheLuceneLockDir);
        if (shouldBeADirectory != null && shouldBeADirectory.exists()) {
            try {
                shouldBeADirectory.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        File deletableIndex = new File(TEST_INDEX_DIR);
        if (deletableIndex.exists())
            try {
                rmDir(deletableIndex);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
public voidtestNonExistentTmpDir()
What happens if the Lucene lockDir doesn't exist?

throws
Exception

        orgApacheLuceneLockDir = System.setProperty(
                "org.apache.lucene.lockDir", NON_EXISTENT_DIRECTORY);
        String exceptionClassName = openIndexWriter();
        if (exceptionClassName == null
                || exceptionClassName.equals("java.io.IOException"))
            assertTrue(true);
        else
            fail("Caught an unexpected Exception");
    
public voidtestTmpDirIsPlainFile()
What happens if the Lucene lockDir is a regular file instead of a directory?

throws
Exception

        shouldBeADirectory = new File(NON_EXISTENT_DIRECTORY);
        shouldBeADirectory.createNewFile();
        String exceptionClassName = openIndexWriter();
        if (exceptionClassName == null
                || exceptionClassName.equals("java.io.IOException"))
            assertTrue(true);
        else
            fail("Caught an unexpected Exception");