Search in sources :

Example 11 with QueryBuilder

use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method testIsEmpty.

/**
 * Tests for the isEmpty() method.
 * @throws org.apache.lucene.queryparser.classic.ParseException parse exception
 */
@Test
public void testIsEmpty() throws ParseException {
    String term = "qwerty";
    // Definition search should be used
    QueryBuilder qb = new QueryBuilder().setDefs(term);
    Context c = new Context(qb.build(), qb);
    assertFalse(c.isEmpty());
    // Symbol search should be used
    qb = new QueryBuilder().setRefs(term);
    c = new Context(qb.build(), qb);
    assertFalse(c.isEmpty());
    // Full search should be used
    qb = new QueryBuilder().setFreetext(term);
    c = new Context(qb.build(), qb);
    assertFalse(c.isEmpty());
    // History search should not be used
    qb = new QueryBuilder().setHist(term);
    c = new Context(qb.build(), qb);
    assertTrue(c.isEmpty());
    // Path search should not be used
    qb = new QueryBuilder().setPath(term);
    c = new Context(qb.build(), qb);
    assertTrue(c.isEmpty());
    // Combined search should be fine
    qb = new QueryBuilder().setHist(term).setFreetext(term);
    c = new Context(qb.build(), qb);
    assertFalse(c.isEmpty());
}
Also used : QueryBuilder(org.opengrok.indexer.search.QueryBuilder) Test(org.junit.jupiter.api.Test)

Example 12 with QueryBuilder

use of org.opengrok.indexer.search.QueryBuilder in project OpenGrok by OpenGrok.

the class ContextTest method testLongTruncatedLine.

/**
 * Test that a line with more than 100 characters after the first match is
 * truncated, and that … is appended to show that the line is
 * truncated. Bug 383.
 * @throws org.apache.lucene.queryparser.classic.ParseException parse exception
 */
@Test
public void testLongTruncatedLine() throws ParseException {
    StringBuilder sb = new StringBuilder();
    sb.append("search_for_me");
    while (sb.length() <= 100) {
        sb.append(" more words");
    }
    sb.append("should not be found");
    Reader in = new StringReader(sb.toString());
    StringWriter out = new StringWriter();
    QueryBuilder qb = new QueryBuilder().setFreetext("search_for_me");
    Context c = new Context(qb.build(), qb);
    boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
    assertTrue(match, "No match found");
    String s = out.toString();
    assertTrue(s.contains("<b>search_for_me</b>"), "Match not listed");
    assertFalse(s.contains("should not be found"), "Line not truncated");
    assertTrue(s.contains("&hellip;"), "Ellipsis not found");
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) Test(org.junit.jupiter.api.Test)

Aggregations

QueryBuilder (org.opengrok.indexer.search.QueryBuilder)12 StringReader (java.io.StringReader)7 StringWriter (java.io.StringWriter)7 Test (org.junit.jupiter.api.Test)7 CharArrayReader (java.io.CharArrayReader)4 Reader (java.io.Reader)4 Document (org.apache.lucene.document.Document)3 TopDocs (org.apache.lucene.search.TopDocs)3 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)3 ForbiddenSymlinkException (org.opengrok.indexer.util.ForbiddenSymlinkException)3 ArrayList (java.util.ArrayList)2 IndexReader (org.apache.lucene.index.IndexReader)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Query (org.apache.lucene.search.Query)2 Definitions (org.opengrok.indexer.analysis.Definitions)2 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1