1065N/A/*
1065N/A * CDDL HEADER START
1065N/A *
1065N/A * The contents of this file are subject to the terms of the
1065N/A * Common Development and Distribution License (the "License").
1065N/A * You may not use this file except in compliance with the License.
1065N/A *
1065N/A * See LICENSE.txt included in this distribution for the specific
1065N/A * language governing permissions and limitations under the License.
1065N/A *
1065N/A * When distributing Covered Code, include this CDDL HEADER in each
1065N/A * file and include the License file at LICENSE.txt.
1065N/A * If applicable, add the following below this CDDL HEADER, with the
1065N/A * fields enclosed by brackets "[]" replaced with your own identifying
1065N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1065N/A *
1065N/A * CDDL HEADER END
1065N/A */
1065N/A
1065N/A/*
1065N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
1065N/A */
1065N/A
1065N/Apackage org.opensolaris.opengrok.search.context;
1065N/A
1065N/Aimport java.io.File;
1065N/Aimport java.io.StringWriter;
1065N/Aimport java.util.ArrayList;
1065N/Aimport org.apache.lucene.index.Term;
1065N/Aimport org.apache.lucene.search.BooleanClause.Occur;
1065N/Aimport org.apache.lucene.search.BooleanQuery;
1065N/Aimport org.apache.lucene.search.PhraseQuery;
1065N/Aimport org.apache.lucene.search.TermQuery;
1065N/Aimport org.junit.AfterClass;
1065N/Aimport org.junit.BeforeClass;
1065N/Aimport org.junit.Test;
1065N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
1065N/Aimport org.opensolaris.opengrok.search.Hit;
1065N/Aimport org.opensolaris.opengrok.util.TestRepository;
1065N/Aimport static org.junit.Assert.*;
1065N/A
1065N/A/**
1065N/A * Unit tests for the {@code HistoryContext} class.
1065N/A */
1065N/Apublic class HistoryContextTest {
1065N/A
1065N/A private static TestRepository repositories;
1065N/A
1065N/A @BeforeClass
1065N/A public static void setUpClass() throws Exception {
1065N/A repositories = new TestRepository();
1065N/A repositories.create(HistoryContextTest.class.getResourceAsStream(
1065N/A "/org/opensolaris/opengrok/history/repositories.zip"));
1065N/A HistoryGuru.getInstance().addRepositories(repositories.getSourceRoot());
1065N/A }
1065N/A
1065N/A @AfterClass
1065N/A public static void tearDownClass() throws Exception {
1065N/A repositories.destroy();
1065N/A repositories = null;
1065N/A }
1065N/A
1065N/A @Test
1065N/A public void testIsEmpty() {
1065N/A TermQuery q1 = new TermQuery(new Term("refs", "isEmpty"));
1065N/A TermQuery q2 = new TermQuery(new Term("defs", "isEmpty"));
1065N/A TermQuery q3 = new TermQuery(new Term("hist", "isEmpty"));
1065N/A BooleanQuery q4 = new BooleanQuery();
1065N/A q4.add(q1, Occur.MUST);
1065N/A q4.add(q2, Occur.MUST);
1065N/A BooleanQuery q5 = new BooleanQuery();
1065N/A q5.add(q2, Occur.MUST);
1065N/A q5.add(q3, Occur.MUST);
1065N/A
1065N/A // The queries that don't contain a "hist" term are considered empty.
1065N/A assertTrue(new HistoryContext(q1).isEmpty());
1065N/A assertTrue(new HistoryContext(q2).isEmpty());
1065N/A assertFalse(new HistoryContext(q3).isEmpty());
1065N/A assertTrue(new HistoryContext(q4).isEmpty());
1065N/A assertFalse(new HistoryContext(q5).isEmpty());
1065N/A }
1065N/A
1065N/A @Test
1065N/A public void testGetContext_3args() throws Exception {
1065N/A String path = "/mercurial/Makefile";
1065N/A String filename = repositories.getSourceRoot() + path;
1065N/A
1065N/A // Construct a query equivalent to hist:dummy
1065N/A TermQuery q1 = new TermQuery(new Term("hist", "dummy"));
1065N/A ArrayList<Hit> hits = new ArrayList<Hit>();
1065N/A assertTrue(new HistoryContext(q1).getContext(filename, path, hits));
1065N/A assertEquals(1, hits.size());
1065N/A assertTrue(hits.get(0).getLine().contains(
1065N/A "Created a small <b>dummy</b> program"));
1065N/A
1065N/A // Construct a query equivalent to hist:"dummy program"
1065N/A PhraseQuery q2 = new PhraseQuery();
1065N/A q2.add(new Term("hist", "dummy"));
1065N/A q2.add(new Term("hist", "program"));
1065N/A hits.clear();
1065N/A assertTrue(new HistoryContext(q2).getContext(filename, path, hits));
1065N/A assertEquals(1, hits.size());
1065N/A assertTrue(hits.get(0).getLine().contains(
1065N/A "Created a small <b>dummy program</b>"));
1065N/A
1065N/A // Search for a term that doesn't exist
1065N/A TermQuery q3 = new TermQuery(new Term("hist", "term_does_not_exist"));
1065N/A hits.clear();
1065N/A assertFalse(new HistoryContext(q3).getContext(filename, path, hits));
1065N/A assertEquals(0, hits.size());
1065N/A
1065N/A // Search for term with multiple hits - hist:small OR hist:target
1065N/A BooleanQuery q4 = new BooleanQuery();
1065N/A q4.add(new TermQuery(new Term("hist", "small")), Occur.SHOULD);
1065N/A q4.add(new TermQuery(new Term("hist", "target")), Occur.SHOULD);
1065N/A hits.clear();
1065N/A assertTrue(new HistoryContext(q4).getContext(filename, path, hits));
1065N/A assertEquals(2, hits.size());
1065N/A assertTrue(hits.get(0).getLine().contains(
1065N/A "Add lint make <b>target</b> and fix lint warnings"));
1065N/A assertTrue(hits.get(1).getLine().contains(
1065N/A "Created a <b>small</b> dummy program"));
1065N/A }
1065N/A
1065N/A @Test
1065N/A public void testGetContext_4args() throws Exception {
1065N/A String path = "/mercurial/Makefile";
1065N/A File file = new File(repositories.getSourceRoot() + path);
1065N/A String parent = file.getParent();
1065N/A String base = file.getName();
1065N/A
1065N/A // Construct a query equivalent to hist:dummy
1065N/A TermQuery q1 = new TermQuery(new Term("hist", "dummy"));
1065N/A StringWriter sw = new StringWriter();
1113N/A assertTrue(new HistoryContext(q1).getContext(parent, base, path, sw, null));
1065N/A assertTrue(sw.toString().contains(
1065N/A "Created a small <b>dummy</b> program"));
1065N/A
1065N/A // Construct a query equivalent to hist:"dummy program"
1065N/A PhraseQuery q2 = new PhraseQuery();
1065N/A q2.add(new Term("hist", "dummy"));
1065N/A q2.add(new Term("hist", "program"));
1065N/A sw = new StringWriter();
1113N/A assertTrue(new HistoryContext(q2).getContext(parent, base, path, sw, null));
1065N/A assertTrue(sw.toString().contains(
1065N/A "Created a small <b>dummy program</b>"));
1065N/A
1065N/A // Search for a term that doesn't exist
1065N/A TermQuery q3 = new TermQuery(new Term("hist", "term_does_not_exist"));
1065N/A sw = new StringWriter();
1113N/A assertFalse(new HistoryContext(q3).getContext(parent, base, path, sw, null));
1065N/A assertEquals("", sw.toString());
1065N/A
1065N/A // Search for term with multiple hits - hist:small OR hist:target
1065N/A BooleanQuery q4 = new BooleanQuery();
1065N/A q4.add(new TermQuery(new Term("hist", "small")), Occur.SHOULD);
1065N/A q4.add(new TermQuery(new Term("hist", "target")), Occur.SHOULD);
1065N/A sw = new StringWriter();
1113N/A assertTrue(new HistoryContext(q4).getContext(parent, base, path, sw, null));
1065N/A String result = sw.toString();
1065N/A assertTrue(result.contains(
1065N/A "Add lint make <b>target</b> and fix lint warnings"));
1065N/A assertTrue(result.contains(
1065N/A "Created a <b>small</b> dummy program"));
1065N/A }
1065N/A
1065N/A}