Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.infinispan.lucene.cachestore;
  
 
 import java.io.File;
Helper class which will contain common using methods.

Author(s):
Anna Manukyan
 
 public class TestHelper {
   
Verifies that entries exist/not exist in the index.

Parameters:
rootDir index root directory.
indexName index name.
termsAdded number of terms to be verified.
inverted flag to define which term should exist which not.
Throws:
java.io.IOException
 
    public static void verifyIndex(File rootDirString indexNameint termsAddedboolean invertedthrows IOException {
       File indexDir = new File(rootDirindexName);
       Directory directory = FSDirectory.open(indexDir);
       try {
          verifyOnDirectory(directorytermsAddedinverted);
       }
       finally {
          directory.close();
       }
    }

   
Verifies that entries exist/not exist in the index.

Parameters:
directory index root directory.
termsAdded number of terms to be verified.
inverted flag to define which term should exist which not.
Throws:
java.io.IOException
 
    public static void verifyOnDirectory(Directory directoryint termsAddedboolean invertedthrows IOException {
       IndexReader indexReader = IndexReader.open(directory);
       IndexSearcher searcher = new IndexSearcher(indexReader);
       try {
          for (int i = 0; i <= termsAddedi++) {
             String term = String.valueOf(i);
             final Query queryToFind;
             final Query queryNotToFind;
             if (i % 2 == 0 ^ inverted) {
                queryToFind = new TermQuery(new Term("main"term));
                queryNotToFind = new TermQuery(new Term("secondaryField"term));
             }
             else {
                queryToFind = new TermQuery(new Term("secondaryField"term));
                queryNotToFind = new TermQuery(new Term("main"term));
             }
             TopDocs docs = searcher.search(queryToFindnull, 2);
             AssertJUnit.assertEquals("String '" + term + "' should exist but was not found in index", 1, docs.totalHits);
             docs = searcher.search(queryNotToFindnull, 1);
             AssertJUnit.assertEquals("String '" + term + "' should NOT exist but was found in index", 0, docs.totalHits);
          }
       }
       finally {
          indexReader.close();
       }
    }

   
Creates terms and inserts them into the index.

Parameters:
rootDir index root directory.
indexName the name of the index.
termsToAdd number of terms to be added.
invert flag which identifies which terms should be inserted which not.
Throws:
java.io.IOException
 
    public static void createIndex(File rootDirString indexNameint termsToAddboolean invertthrows IOException {
       File indexDir = new File(rootDirindexName);
       FSDirectory directory = FSDirectory.open(indexDir);
       try {
          CacheTestSupport.initializeDirectory(directory);
          IndexWriter iwriter = LuceneSettings.openWriter(directory, 100000);
          try {
             for (int i = 0; i <= termsToAddi++) {
                Document doc = new Document();
               String term = String.valueOf(i);
               //For even values of i we add to "main" field
               if (i % 2 == 0 ^ invert) {
                  doc.add(new Field("main"term....));
               }
               else {
                  doc.add(new Field("secondaryField"term....));
               }
               iwriter.addDocument(doc);
            }
            iwriter.commit();
         }
         finally {
            iwriter.close();
         }
      }
      finally {
         directory.close();
      }
   }

   
Creates root directory with subdirectories based on the provided directory name and it's sub directory name.

Parameters:
parentDir the path of the root directory.
rootDirectoryName the path of the sub directory.
Returns:
the created file.
   public static File createRootDir(final String parentDirfinal String rootDirectoryName) {
      File rootDir = new File(new File(parentDir).getAbsoluteFile(), rootDirectoryName);
      boolean directoriesCreated = rootDir.mkdir();
      assert directoriesCreated : "couldn't create directory for test";
      rootDir.deleteOnExit();
      return rootDir;
   }

   
Returns the list of file names in the specified directory.

Parameters:
rootDir the root directory.
indexName the name of the subdirectory.
Returns:
the array of the names of the files.
   public static String[] getFileNamesFromDir(File rootDirString indexName) {
      File indexDir = new File(rootDir.getAbsoluteFile(), indexName);
      assert indexDir.exists();
      String[] fileNames = indexDir.list();
      assert fileNames.length > 0;
      return fileNames;
   }
New to GrepCode? Check out our FAQ X