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());
}
}
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>"));
}
}
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 … 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("…"));
}
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());
}
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>"));
}
Aggregations