Java Code Examples for org.apache.lucene.util.LuceneTestCase

The following examples show how to use org.apache.lucene.util.LuceneTestCase. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: lucene-solr   Source File: SimplePrimaryNode.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexWriter initWriter(int id, Random random, Path indexPath, boolean doCheckIndexOnClose) throws IOException {
  Directory dir = SimpleReplicaNode.getDirectory(random, id, indexPath, doCheckIndexOnClose);

  MockAnalyzer analyzer = new MockAnalyzer(random);
  analyzer.setMaxTokenLength(TestUtil.nextInt(random, 1, IndexWriter.MAX_TERM_LENGTH));
  IndexWriterConfig iwc = LuceneTestCase.newIndexWriterConfig(random, analyzer);

  MergePolicy mp = iwc.getMergePolicy();
  //iwc.setInfoStream(new PrintStreamInfoStream(System.out));

  // Force more frequent merging so we stress merge warming:
  if (mp instanceof TieredMergePolicy) {
    TieredMergePolicy tmp = (TieredMergePolicy) mp;
    tmp.setSegmentsPerTier(3);
    tmp.setMaxMergeAtOnce(3);
  } else if (mp instanceof LogMergePolicy) {
    LogMergePolicy lmp = (LogMergePolicy) mp;
    lmp.setMergeFactor(3);
  }

  IndexWriter writer = new IndexWriter(dir, iwc);

  TestUtil.reduceOpenFiles(writer);
  return writer;
}
 
Example 2
Source Project: lucene-solr   Source File: PayloadHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Sets up a RAM-resident Directory, and adds documents (using English.intToEnglish()) with two fields: field and multiField
 * and analyzes them using the PayloadAnalyzer
 * @param similarity The Similarity class to use in the Searcher
 * @param numDocs The num docs to add
 * @return An IndexSearcher
 */
// TODO: randomize
public IndexSearcher setUp(Random random, Similarity similarity, int numDocs) throws IOException {
  Directory directory = new MockDirectoryWrapper(random, new ByteBuffersDirectory());
  PayloadAnalyzer analyzer = new PayloadAnalyzer();

  // TODO randomize this
  IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
      analyzer).setSimilarity(similarity));
  // writer.infoStream = System.out;
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    doc.add(new TextField(FIELD, English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(MULTI_FIELD, English.intToEnglish(i) + "  " + English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES));
    writer.addDocument(doc);
  }
  writer.forceMerge(1);
  reader = DirectoryReader.open(writer);
  writer.close();

  IndexSearcher searcher = LuceneTestCase.newSearcher(LuceneTestCase.getOnlyLeafReader(reader));
  searcher.setSimilarity(similarity);
  return searcher;
}
 
Example 3
Source Project: lucene-solr   Source File: VirusCheckingFS.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void delete(Path path) throws IOException {

  // Fake but deterministic and hopefully portable like-randomness:
  long hash = state.incrementAndGet() * path.getFileName().hashCode();
  
  if (enabled // test infra disables when it's "really" time to delete after test is done, so it can reclaim temp dirs
      && Files.exists(path) // important that we NOT delay a NoSuchFileException until later
      && path.getFileName().toString().equals(IndexWriter.WRITE_LOCK_NAME) == false // life is particularly difficult if the virus checker hits our lock file
      && (hash % 5) == 1) {
    if (LuceneTestCase.VERBOSE) {
      System.out.println("NOTE: VirusCheckingFS now refusing to delete " + path);
    }
    throw new AccessDeniedException("VirusCheckingFS is randomly refusing to delete file \"" + path + "\"");
  }
  super.delete(path);
}
 
Example 4
Source Project: lucene-solr   Source File: QueryUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Given an IndexSearcher, returns a new IndexSearcher whose IndexReader
 * is a MultiReader containing the Reader of the original IndexSearcher,
 * as well as several "empty" IndexReaders -- some of which will have
 * deleted documents in them.  This new IndexSearcher should
 * behave exactly the same as the original IndexSearcher.
 * @param s the searcher to wrap
 * @param edge if negative, s will be the first sub; if 0, s will be in the middle, if positive s will be the last sub
 */
public static IndexSearcher wrapUnderlyingReader(Random random, final IndexSearcher s, final int edge)
  throws IOException {

  IndexReader r = s.getIndexReader();

  // we can't put deleted docs before the nested reader, because
  // it will throw off the docIds
  IndexReader[] readers = new IndexReader[] {
    edge < 0 ? r : new MultiReader(),
    new MultiReader(),
    new MultiReader(edge < 0 ? emptyReader(4) : new MultiReader(),
        new MultiReader(),
        0 == edge ? r : new MultiReader()),
    0 < edge ? new MultiReader() : emptyReader(7),
    new MultiReader(),
    new MultiReader(0 < edge ? new MultiReader() : emptyReader(5),
        new MultiReader(),
        0 < edge ? r : new MultiReader())
  };

  IndexSearcher out = LuceneTestCase.newSearcher(new MultiReader(readers));
  out.setSimilarity(s.getSimilarity());
  return out;
}
 
Example 5
Source Project: lucene-solr   Source File: CheckHits.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a query matches the an expected set of documents using Hits.
 *
 * <p>
 * Note that when using the Hits API, documents will only be returned
 * if they have a positive normalized score.
 * </p>
 * @param query the query to test
 * @param searcher the searcher to test the query against
 * @param defaultFieldName used for displaing the query in assertion messages
 * @param results a list of documentIds that must match the query
 * @see #checkHitCollector
 */
public static void checkHits(
      Random random,
      Query query,
      String defaultFieldName,
      IndexSearcher searcher,
      int[] results)
        throws IOException {

  ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;

  Set<Integer> correct = new TreeSet<>();
  for (int i = 0; i < results.length; i++) {
    correct.add(Integer.valueOf(results[i]));
  }

  Set<Integer> actual = new TreeSet<>();
  for (int i = 0; i < hits.length; i++) {
    actual.add(Integer.valueOf(hits[i].doc));
  }

  assertEquals(query.toString(defaultFieldName), correct, actual);

  QueryUtils.check(random, query,searcher, LuceneTestCase.rarely(random));
}
 
Example 6
Source Project: lucene-solr   Source File: RandomIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private RandomIndexWriter(Random r, Directory dir, IndexWriterConfig c, boolean closeAnalyzer, boolean useSoftDeletes) throws IOException {
  // TODO: this should be solved in a different way; Random should not be shared (!).
  this.r = new Random(r.nextLong());
  if (useSoftDeletes) {
    c.setSoftDeletesField("___soft_deletes");
    softDeletesRatio = 1.d / (double)1 + r.nextInt(10);
  } else {
    softDeletesRatio = 0d;
  }
  w = mockIndexWriter(dir, c, r);
  config = w.getConfig();
  flushAt = TestUtil.nextInt(r, 10, 1000);
  if (closeAnalyzer) {
    analyzer = w.getAnalyzer();
  } else {
    analyzer = null;
  }
  if (LuceneTestCase.VERBOSE) {
    System.out.println("RIW dir=" + dir);
  }

  // Make sure we sometimes test indices that don't get
  // any forced merges:
  doRandomForceMerge = !(c.getMergePolicy() instanceof NoMergePolicy) && r.nextBoolean();
}
 
Example 7
Source Project: lucene-solr   Source File: RandomIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
private void maybeFlushOrCommit() throws IOException {
  LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, config);
  if (docCount++ == flushAt) {
    if (r.nextBoolean()) {
      flushAllBuffersSequentially();
    } else if (r.nextBoolean()) {
      if (LuceneTestCase.VERBOSE) {
        System.out.println("RIW.add/updateDocument: now doing a flush at docCount=" + docCount);
      }
      w.flush();
    } else {
      if (LuceneTestCase.VERBOSE) {
        System.out.println("RIW.add/updateDocument: now doing a commit at docCount=" + docCount);
      }
      w.commit();
    }
    flushAt += TestUtil.nextInt(r, (int) (flushAtFactor * 10), (int) (flushAtFactor * 1000));
    if (flushAtFactor < 2e6) {
      // gradually but exponentially increase time b/w flushes
      flushAtFactor *= 1.05;
    }
  }
}
 
Example 8
Source Project: lucene-solr   Source File: CustomAnalyzerStrField.java    License: Apache License 2.0 6 votes vote down vote up
public CustomAnalyzerStrField() {
  Random r = LuceneTestCase.random();

  // two arg constructor
  Analyzer a2 = new TokenizerChain
    (new KeywordTokenizerFactory(new HashMap<>()),
     r.nextBoolean() ? null : new TokenFilterFactory[0]);
  
  // three arg constructor
  Analyzer a3 = new TokenizerChain
    (r.nextBoolean() ? null : new CharFilterFactory[0],
     new KeywordTokenizerFactory(new HashMap<>()),
     r.nextBoolean() ? null : new TokenFilterFactory[0]);

  if (r.nextBoolean()) {
    indexAnalyzer = a2;
    queryAnalyzer = a3;
  } else {
    queryAnalyzer = a2;
    indexAnalyzer = a3;
  }
}
 
Example 9
Source Project: lucene-solr   Source File: RandomIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Updates a document.
 * @see IndexWriter#updateDocument(Term, Iterable)
 */
public <T extends IndexableField> long updateDocument(Term t, final Iterable<T> doc) throws IOException {
  LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, config);
  final long seqNo;
  if (useSoftDeletes()) {
    if (r.nextInt(5) == 3) {
      seqNo = w.softUpdateDocuments(t, Arrays.asList(doc), new NumericDocValuesField(config.getSoftDeletesField(), 1));
    } else {
      seqNo = w.softUpdateDocument(t, doc, new NumericDocValuesField(config.getSoftDeletesField(), 1));
    }
  } else {
    if (r.nextInt(5) == 3) {
      seqNo = w.updateDocuments(t, Arrays.asList(doc));
    } else {
      seqNo = w.updateDocument(t, doc);
    }
  }
  maybeFlushOrCommit();

  return seqNo;
}
 
Example 10
Source Project: lucene-solr   Source File: RandomIndexWriter.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Close this writer.
 * @see IndexWriter#close()
 */
@Override
public void close() throws IOException {
  boolean success = false;
  try {
    if (w.isClosed() == false) {
      LuceneTestCase.maybeChangeLiveIndexWriterConfig(r, config);
    }
    // if someone isn't using getReader() API, we want to be sure to
    // forceMerge since presumably they might open a reader on the dir.
    if (getReaderCalled == false && r.nextInt(8) == 2 && w.isClosed() == false) {
      doRandomForceMerge();
      if (config.getCommitOnClose() == false) {
        // index may have changed, must commit the changes, or otherwise they are discarded by the call to close()
        w.commit();
      }
    }
    success = true;
  } finally {
    if (success) {
      IOUtils.close(w, analyzer);
    } else {
      IOUtils.closeWhileHandlingException(w, analyzer);
    }
  }
}
 
Example 11
Source Project: lucene-solr   Source File: RandomPostingsTester.java    License: Apache License 2.0 6 votes vote down vote up
public static SeedPostings getSeedPostings(String term, long seed, IndexOptions options, boolean allowPayloads) {
  int minDocFreq, maxDocFreq;
  if (term.startsWith("big_")) {
    minDocFreq = LuceneTestCase.RANDOM_MULTIPLIER * 50000;
    maxDocFreq = LuceneTestCase.RANDOM_MULTIPLIER * 70000;
  } else if (term.startsWith("medium_")) {
    minDocFreq = LuceneTestCase.RANDOM_MULTIPLIER * 3000;
    maxDocFreq = LuceneTestCase.RANDOM_MULTIPLIER * 6000;
  } else if (term.startsWith("low_")) {
    minDocFreq = LuceneTestCase.RANDOM_MULTIPLIER;
    maxDocFreq = LuceneTestCase.RANDOM_MULTIPLIER * 40;
  } else {
    minDocFreq = 1;
    maxDocFreq = 3;
  }

  return new SeedPostings(seed, minDocFreq, maxDocFreq, options, allowPayloads);
}
 
Example 12
Source Project: lucene-solr   Source File: RandomPostingsTester.java    License: Apache License 2.0 6 votes vote down vote up
public void testTerms(final Fields fieldsSource, final EnumSet<Option> options,
                      final IndexOptions maxTestOptions,
                      final IndexOptions maxIndexOptions,
                      final boolean alwaysTestMax) throws Exception {

  if (options.contains(Option.THREADS)) {
    int numThreads = LuceneTestCase.TEST_NIGHTLY ? TestUtil.nextInt(random, 2, 5) : 2;
    Thread[] threads = new Thread[numThreads];
    for(int threadUpto=0;threadUpto<numThreads;threadUpto++) {
      threads[threadUpto] = new TestThread(new Random(random.nextLong()), this, fieldsSource, options, maxTestOptions, maxIndexOptions, alwaysTestMax);
      threads[threadUpto].start();
    }
    for(int threadUpto=0;threadUpto<numThreads;threadUpto++) {
      threads[threadUpto].join();
    }
  } else {
    testTermsOneThread(random, fieldsSource, options, maxTestOptions, maxIndexOptions, alwaysTestMax);
  }
}
 
Example 13
Source Project: lucene-solr   Source File: RandomPostingsTester.java    License: Apache License 2.0 6 votes vote down vote up
public void testFields(Fields fields) throws Exception {
  Iterator<String> iterator = fields.iterator();
  while (iterator.hasNext()) {
    iterator.next();
    try {
      iterator.remove();
      throw new AssertionError("Fields.iterator() allows for removal");
    } catch (UnsupportedOperationException expected) {
      // expected;
    }
  }
  assertFalse(iterator.hasNext());
  LuceneTestCase.expectThrows(NoSuchElementException.class, () -> {
    iterator.next();
  });
}
 
Example 14
Source Project: lucene-solr   Source File: RandomPostingsTester.java    License: Apache License 2.0 6 votes vote down vote up
/** Indexes all fields/terms at the specified
 *  IndexOptions, and fully tests at that IndexOptions. */
public void testFull(Codec codec, Path path, IndexOptions options, boolean withPayloads) throws Exception {
  Directory dir = LuceneTestCase.newFSDirectory(path);

  // TODO test thread safety of buildIndex too
  FieldsProducer fieldsProducer = buildIndex(codec, dir, options, withPayloads, true);

  testFields(fieldsProducer);

  IndexOptions[] allOptions = IndexOptions.values();
  int maxIndexOption = Arrays.asList(allOptions).indexOf(options);

  for(int i=0;i<=maxIndexOption;i++) {
    testTerms(fieldsProducer, EnumSet.allOf(Option.class), allOptions[i], options, true);
    if (withPayloads) {
      // If we indexed w/ payloads, also test enums w/o accessing payloads:
      testTerms(fieldsProducer, EnumSet.complementOf(EnumSet.of(Option.PAYLOADS)), allOptions[i], options, true);
    }
  }

  fieldsProducer.close();
  dir.close();
}
 
Example 15
Source Project: lucene-solr   Source File: MockFSDirectoryFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Directory create(String path, LockFactory lockFactory, DirContext dirContext) throws IOException {
  // we pass NoLockFactory, because the real lock factory is set later by injectLockFactory:
  Directory dir = LuceneTestCase.newFSDirectory(new File(path).toPath(), lockFactory);
  // we can't currently do this check because of how
  // Solr has to reboot a new Directory sometimes when replicating
  // or rolling back - the old directory is closed and the following
  // test assumes it can open an IndexWriter when that happens - we
  // have a new Directory for the same dir and still an open IW at 
  // this point
  
  Directory cdir = reduce(dir);
  cdir = reduce(cdir);
  cdir = reduce(cdir);
  
  if (cdir instanceof MockDirectoryWrapper) {
    ((MockDirectoryWrapper)cdir).setAssertNoUnrefencedFilesOnClose(false);
  }
  return dir;
}
 
Example 16
Source Project: lucene-solr   Source File: CoreParserTestIndexData.java    License: Apache License 2.0 6 votes vote down vote up
CoreParserTestIndexData(Analyzer analyzer) throws Exception {
  BufferedReader d = new BufferedReader(new InputStreamReader(
      TestCoreParser.class.getResourceAsStream("reuters21578.txt"), StandardCharsets.US_ASCII));
  dir = LuceneTestCase.newDirectory();
  IndexWriter writer = new IndexWriter(dir, LuceneTestCase.newIndexWriterConfig(analyzer));
  String line = d.readLine();
  while (line != null) {
    int endOfDate = line.indexOf('\t');
    String date = line.substring(0, endOfDate).trim();
    String content = line.substring(endOfDate).trim();
    Document doc = new Document();
    doc.add(LuceneTestCase.newTextField("date", date, Field.Store.YES));
    doc.add(LuceneTestCase.newTextField("contents", content, Field.Store.YES));
    doc.add(new IntPoint("date3", Integer.parseInt(date)));
    writer.addDocument(doc);
    line = d.readLine();
  }
  d.close();
  writer.close();
  reader = DirectoryReader.open(dir);
  searcher = LuceneTestCase.newSearcher(reader, false);
}
 
Example 17
Source Project: lucene-solr   Source File: TestSearcherManager.java    License: Apache License 2.0 6 votes vote down vote up
public void testEvilSearcherFactory() throws Exception {
  final Random random = random();
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random, dir);
  w.commit();

  final IndexReader other = DirectoryReader.open(dir);

  final SearcherFactory theEvilOne = new SearcherFactory() {
    @Override
    public IndexSearcher newSearcher(IndexReader ignored, IndexReader previous) {
      return LuceneTestCase.newSearcher(other);
    }
    };

  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(dir, theEvilOne);
  });
  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(w.w, random.nextBoolean(), false, theEvilOne);
  });
  w.close();
  other.close();
  dir.close();
}
 
Example 18
public void testEvilSearcherFactory() throws Exception {
  final Directory dir = newDirectory();
  final RandomIndexWriter w = new RandomIndexWriter(random(), dir);
  w.commit();

  final IndexReader other = DirectoryReader.open(dir);

  final SearcherFactory theEvilOne = new SearcherFactory() {
    @Override
    public IndexSearcher newSearcher(IndexReader ignored, IndexReader previous) {
      return LuceneTestCase.newSearcher(other);
    }
    };

  expectThrows(IllegalStateException.class, () -> {
    new SearcherManager(w.w, false, false, theEvilOne);
  });

  w.close();
  other.close();
  dir.close();
}
 
Example 19
Source Project: lucene-solr   Source File: TestStressIndexing2.java    License: Apache License 2.0 6 votes vote down vote up
public static void indexSerial(Random random, Map<String,Document> docs, Directory dir) throws IOException {
  IndexWriter w = new IndexWriter(dir, LuceneTestCase.newIndexWriterConfig(random, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));

  // index all docs in a single thread
  Iterator<Document> iter = docs.values().iterator();
  while (iter.hasNext()) {
    Document d = iter.next();
    ArrayList<IndexableField> fields = new ArrayList<>();
    fields.addAll(d.getFields());
    // put fields in same order each time
    Collections.sort(fields, fieldNameComparator);
    
    Document d1 = new Document();
    for (int i=0; i<fields.size(); i++) {
      d1.add(fields.get(i));
    }
    w.addDocument(d1);
    // System.out.println("indexing "+d1);
  }
  
  w.close();
}
 
Example 20
Source Project: lucene-solr   Source File: TestDirectoryReaderReopen.java    License: Apache License 2.0 6 votes vote down vote up
public static void createIndex(Random random, Directory dir, boolean multiSegment) throws IOException {
  IndexWriter w = new IndexWriter(dir, LuceneTestCase.newIndexWriterConfig(random, new MockAnalyzer(random))
      .setMergePolicy(new LogDocMergePolicy()));
  
  for (int i = 0; i < 100; i++) {
    w.addDocument(createDocument(i, 4));
    if (multiSegment && (i % 10) == 0) {
      w.commit();
    }
  }
  
  if (!multiSegment) {
    w.forceMerge(1);
  }
  
  w.close();

  DirectoryReader r = DirectoryReader.open(dir);
  if (multiSegment) {
    assertTrue(r.leaves().size() > 1);
  } else {
    assertTrue(r.leaves().size() == 1);
  }
  r.close();
}
 
Example 21
Source Project: lucene-solr   Source File: TestByteBuffersDataOutput.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testLargeArrayAdd() {
  ByteBuffersDataOutput o = new ByteBuffersDataOutput();
  int MB = 1024 * 1024;
  final byte [] bytes;
  if (LuceneTestCase.TEST_NIGHTLY) {
    bytes = randomBytesOfLength(5 * MB, 15 * MB);
  } else {
    bytes = randomBytesOfLength(MB/2, MB);
  }
  int offset = randomIntBetween(0, 100);
  int len = bytes.length - offset;
  o.writeBytes(bytes, offset, len);
  assertEquals(len, o.size());
  Assert.assertArrayEquals(ArrayUtil.copyOfSubArray(bytes, offset, offset + len), o.toArrayCopy());
}
 
Example 22
Source Project: lucene-solr   Source File: TestCoreAdmin.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigSet() throws Exception {

  SolrClient client = getSolrAdmin();
  File testDir = createTempDir(LuceneTestCase.getTestClass().getSimpleName()).toFile();
  File newCoreInstanceDir = new File(testDir, "newcore");
  cores.getAllowPaths().add(testDir.toPath()); // Allow the test dir

  CoreAdminRequest.Create req = new CoreAdminRequest.Create();
  req.setCoreName("corewithconfigset");
  req.setInstanceDir(newCoreInstanceDir.getAbsolutePath());
  req.setConfigSet("configset-2");

  CoreAdminResponse response = req.process(client);
  assertThat((String) response.getResponse().get("core"), is("corewithconfigset"));

  try (SolrCore core = cores.getCore("corewithconfigset")) {
    assertThat(core, is(notNullValue()));
  }

}
 
Example 23
public void test() throws Exception {
  File tmpdir = createTempDir(LuceneTestCase.getTestClass().getSimpleName()).toFile();
  createFile(tmpdir, "a.txt", "a line one\na line two\na line three".getBytes(StandardCharsets.UTF_8), false);
  createFile(tmpdir, "b.txt", "b line one\nb line two".getBytes(StandardCharsets.UTF_8), false);
  createFile(tmpdir, "c.txt", "c line one\nc line two\nc line three\nc line four".getBytes(StandardCharsets.UTF_8), false);
  
  String config = generateConfig(tmpdir);
  LocalSolrQueryRequest request = lrf.makeRequest(
      "command", "full-import", "dataConfig", config,
      "clean", "true", "commit", "true", "synchronous", "true", "indent", "true");
  h.query("/dataimport", request);
  
  assertQ(req("*:*"), "//*[@numFound='9']");
  assertQ(req("id:?\\ line\\ one"), "//*[@numFound='3']");
  assertQ(req("id:a\\ line*"), "//*[@numFound='3']");
  assertQ(req("id:b\\ line*"), "//*[@numFound='2']");
  assertQ(req("id:c\\ line*"), "//*[@numFound='4']");    
}
 
Example 24
Source Project: lucene-solr   Source File: EmbeddedSolrServerTestBase.java    License: Apache License 2.0 6 votes vote down vote up
public static String legacyExampleCollection1SolrHome() throws IOException {
  final String sourceHome = ExternalPaths.SOURCE_HOME;
  if (sourceHome == null)
    throw new IllegalStateException("No source home! Cannot create the legacy example solr home directory.");

  final File tempSolrHome = LuceneTestCase.createTempDir().toFile();
  FileUtils.copyFileToDirectory(new File(sourceHome, "server/solr/solr.xml"), tempSolrHome);
  final File collectionDir = new File(tempSolrHome, DEFAULT_CORE_NAME);
  FileUtils.forceMkdir(collectionDir);
  final File configSetDir = new File(sourceHome, "server/solr/configsets/sample_techproducts_configs/conf");
  FileUtils.copyDirectoryToDirectory(configSetDir, collectionDir);

  final Properties props = new Properties();
  props.setProperty("name", DEFAULT_CORE_NAME);

  try (Writer writer = new OutputStreamWriter(FileUtils.openOutputStream(new File(collectionDir, "core.properties")),
      "UTF-8");) {
    props.store(writer, null);
  }

  return tempSolrHome.getAbsolutePath();
}
 
Example 25
Source Project: lucene-solr   Source File: HighlighterTest.java    License: Apache License 2.0 5 votes vote down vote up
void start() throws Exception {
  if (LuceneTestCase.VERBOSE) System.out.println("Run QueryScorer");
  run();
  if (LuceneTestCase.VERBOSE) System.out.println("Run QueryTermScorer");
  mode = QUERY_TERM;
  run();
}
 
Example 26
Source Project: lucene-solr   Source File: TestPerfTasksParse.java    License: Apache License 2.0 5 votes vote down vote up
/** Test the parsing of example scripts **/
@SuppressWarnings("try")
public void testParseExamples() throws Exception {
  // hackedy-hack-hack
  boolean foundFiles = false;
  final Path examplesDir = Paths.get(getClass().getResource("/conf").toURI());
  try (DirectoryStream<Path> stream = Files.newDirectoryStream(examplesDir, "*.alg")) {
    for (Path path : stream) {
      Config config = new Config(Files.newBufferedReader(path, StandardCharsets.UTF_8));
      String contentSource = config.get("content.source", null);
      if (contentSource != null) { Class.forName(contentSource); }
      config.set("work.dir", createTempDir(LuceneTestCase.getTestClass().getSimpleName()).toAbsolutePath().toString());
      config.set("content.source", MockContentSource.class.getName());
      String dir = config.get("content.source", null);
      if (dir != null) { Class.forName(dir); }
      config.set("directory", "ByteBuffersDirectory");
      if (config.get("line.file.out", null) != null) {
        config.set("line.file.out", createTempFile("linefile", ".txt").toAbsolutePath().toString());
      }
      if (config.get("query.maker", null) != null) {
        Class.forName(config.get("query.maker", null));
        config.set("query.maker", MockQueryMaker.class.getName());
      }
      PerfRunData data = new PerfRunData(config);
      try (Algorithm algo = new Algorithm(data)) {}
      foundFiles = true;
    }
  }
  if (!foundFiles) {
    fail("could not find any .alg files!");
  }
}
 
Example 27
Source Project: lucene-solr   Source File: SpatialTestCase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
  super.setUp();
  directory = newDirectory();
  analyzer = new MockAnalyzer(random());
  indexWriter = new RandomIndexWriter(random(), directory, LuceneTestCase.newIndexWriterConfig(random(), analyzer));
  indexReader = indexWriter.getReader();
  indexSearcher = newSearcher(indexReader);
}
 
Example 28
Source Project: lucene-solr   Source File: SimpleReplicaNode.java    License: Apache License 2.0 5 votes vote down vote up
static Directory getDirectory(Random random, int id, Path path, boolean doCheckIndexOnClose) throws IOException {
  MockDirectoryWrapper dir = LuceneTestCase.newMockFSDirectory(path);
  
  dir.setAssertNoUnrefencedFilesOnClose(true);
  dir.setCheckIndexOnClose(doCheckIndexOnClose);

  // Corrupt any index files not referenced by current commit point; this is important (increases test evilness) because we may have done
  // a hard crash of the previous JVM writing to this directory and so MDW's corrupt-unknown-files-on-close never ran:
  Node.nodeMessage(System.out, id, "top: corrupt unknown files");
  dir.corruptUnknownFiles();

  return dir;
}
 
Example 29
Source Project: crate   Source File: MockBigArrays.java    License: Apache License 2.0 5 votes vote down vote up
AbstractArrayWrapper(boolean clearOnResize) {
    this.clearOnResize = clearOnResize;
    this.originalRelease = new AtomicReference<>();
    ACQUIRED_ARRAYS.put(this,
            TRACK_ALLOCATIONS ? new RuntimeException("Unreleased array from test: " + LuceneTestCase.getTestClass().getName())
                    : Boolean.TRUE);
}
 
Example 30
Source Project: lucene-solr   Source File: ShapeTestUtil.java    License: Apache License 2.0 5 votes vote down vote up
/** returns next pseudorandom polygon */
public static XYPolygon nextPolygon() {
  Random random = random();
  if (random.nextBoolean()) {
    return surpriseMePolygon(random);
  } else if (LuceneTestCase.TEST_NIGHTLY && random.nextInt(10) == 1) {
    // this poly is slow to create ... only do it 10% of the time:
    while (true) {
      int gons = TestUtil.nextInt(random, 4, 500);
      // So the poly can cover at most 50% of the earth's surface:
      double radius = random.nextDouble() * 0.5 * Float.MAX_VALUE + 1.0;
      try {
        return createRegularPolygon(nextFloat(random), nextFloat(random), radius, gons);
      } catch (IllegalArgumentException iae) {
        // something went wrong, try again
      }
    }
  }

  XYRectangle box = nextBox(random);
  if (random.nextBoolean()) {
    // box
    return boxPolygon(box);
  } else {
    // triangle
    return trianglePolygon(box);
  }
}