Search in sources :

Example 6 with QueryBuilder

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

the class ContextTest method testGetContext.

/**
     * Helper method for testing various paths through the getContext() method.
     *
     * @param limit true if limited, quick context scan should be used
     * @param hitList true if output should be written to a list instead of a
     * writer
     */
private void testGetContext(boolean limit, boolean hitList) throws ParseException {
    StringReader in = new StringReader("abc def ghi\n");
    StringWriter out = hitList ? null : new StringWriter();
    List<Hit> hits = hitList ? new ArrayList<>() : null;
    RuntimeEnvironment.getInstance().setQuickContextScan(limit);
    // Search freetext for the term "def"
    QueryBuilder qb = new QueryBuilder().setFreetext("def");
    Context c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    String expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "abc <b>def</b> ghi</a><br/>";
    String actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    // Search with definitions
    Definitions defs = new Definitions();
    defs.addTag(1, "def", "type", "text");
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    in = new StringReader("abc def ghi\nbah def foobar");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    //test case - if this is def search, don't show false results (defs
    // weren't defined)
    assertEquals(expectedOutput, actualOutput);
    // Search with no input (will search definitions)
    in = null;
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "text" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "text</a> <i>type</i><br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    defs = new Definitions();
    defs.addTag(2, "def", "type", "text");
    in = new StringReader("abc1 def ghi\nabc def ghi\nabc3 def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("2", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#2\"><span class=\"l\">2</span> " + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    // Search with no results
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setFreetext("no_match");
    c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(0, hits.size());
    } else {
        assertEquals("", out.toString());
    }
    // History search (should not show source context)
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setHist("abc");
    c = new Context(qb.build(), qb.getQueries());
    assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(0, hits.size());
    } else {
        assertEquals("", out.toString());
    }
}
Also used : Hit(org.opensolaris.opengrok.search.Hit) StringWriter(java.io.StringWriter) Definitions(org.opensolaris.opengrok.analysis.Definitions) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder)

Example 7 with QueryBuilder

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

the class ContextTest method searchContextTestHelper.

/**
     * Helper method for testing presence of expected words in search context
     *
     * @param searchInText Context of document we are searching in.
     * @param queryString Definition of search query.
     * @param expectWordInContext Word expected to be found by 'queryString' in
     * 'searchInText' and to be included in context. Set null if context is
     * expected to be empty.
     * @throws ParseException
     */
private void searchContextTestHelper(String searchInText, String queryString, String expectWordInContext) throws ParseException {
    Reader in = new StringReader(searchInText);
    StringWriter out = new StringWriter();
    QueryBuilder qb = new QueryBuilder().setFreetext(queryString);
    Context c = new Context(qb.build(), qb.getQueries());
    boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
    if (expectWordInContext == null) {
        assertFalse("Match found", match);
    } else {
        assertTrue("No match found", match);
        String s = out.toString();
        assertTrue("Searched word '" + expectWordInContext + "' not in context", s.contains("<b>" + expectWordInContext + "</b>"));
    }
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder)

Example 8 with QueryBuilder

use of org.opensolaris.opengrok.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 &hellip; is appended to show that the line is
     * truncated. Bug 383.
     *
     * @throws org.apache.lucene.queryparser.classic.ParseException
     */
@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.getQueries());
    boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
    assertTrue("No match found", match);
    String s = out.toString();
    assertTrue("Match not listed", s.contains("<b>search_for_me</b>"));
    assertFalse("Line not truncated", s.contains("should not be found"));
    assertTrue("Ellipsis not found", s.contains("&hellip;"));
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Example 9 with QueryBuilder

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

the class ContextTest method bug16848.

/**
     * Verify that the matching lines are shown in their original form and not
     * lower-cased (bug #16848).
     *
     * @throws java.lang.Exception
     */
@Test
public void bug16848() throws Exception {
    StringReader in = new StringReader("Mixed case: abc AbC dEf\n");
    StringWriter out = new StringWriter();
    QueryBuilder qb = new QueryBuilder().setFreetext("mixed");
    Context c = new Context(qb.build(), qb.getQueries());
    assertTrue(c.getContext(in, out, "", "", "", null, false, qb.isDefSearch(), null));
    assertEquals("<a class=\"s\" href=\"#0\"><span class=\"l\">0</span> " + "<b>Mixed</b> case: abc AbC dEf</a><br/>", out.toString());
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Example 10 with QueryBuilder

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

the class ContextTest method testAllLinkWithLongLines.

/**
     * Test that we get the [all...] link if a very long line crosses the buffer
     * boundary. Bug 383.
     *
     * @throws org.apache.lucene.queryparser.classic.ParseException
     */
@Test
public void testAllLinkWithLongLines() throws ParseException {
    // Create input which consists of one single line longer than
    // Context.MAXFILEREAD.
    StringBuilder sb = new StringBuilder();
    sb.append("search_for_me");
    while (sb.length() <= Context.MAXFILEREAD) {
        sb.append(" more words");
    }
    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.getQueries());
    boolean match = c.getContext(in, out, "", "", "", null, true, qb.isDefSearch(), null);
    assertTrue("No match found", match);
    String s = out.toString();
    assertTrue("No [all...] link", s.contains(">[all...]</a>"));
}
Also used : StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) CharArrayReader(java.io.CharArrayReader) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryBuilder(org.opensolaris.opengrok.search.QueryBuilder) Test(org.junit.Test)

Aggregations

QueryBuilder (org.opensolaris.opengrok.search.QueryBuilder)10 StringReader (java.io.StringReader)7 StringWriter (java.io.StringWriter)7 Test (org.junit.Test)7 CharArrayReader (java.io.CharArrayReader)4 Reader (java.io.Reader)4 ArrayList (java.util.ArrayList)1 Document (org.apache.lucene.document.Document)1 IndexReader (org.apache.lucene.index.IndexReader)1 IndexableField (org.apache.lucene.index.IndexableField)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Query (org.apache.lucene.search.Query)1 TopDocs (org.apache.lucene.search.TopDocs)1 Definitions (org.opensolaris.opengrok.analysis.Definitions)1 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)1 Hit (org.opensolaris.opengrok.search.Hit)1