SearchEngineTest.java revision 697
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
0N/A * Common Development and Distribution License (the "License").
0N/A * You may not use this file except in compliance with the License.
0N/A *
0N/A * See LICENSE.txt included in this distribution for the specific
0N/A * language governing permissions and limitations under the License.
0N/A *
0N/A * When distributing Covered Code, include this CDDL HEADER in each
0N/A * file and include the License file at LICENSE.txt.
0N/A * If applicable, add the following below this CDDL HEADER, with the
0N/A * fields enclosed by brackets "[]" replaced with your own identifying
0N/A * information: Portions Copyright [yyyy] [name of copyright owner]
0N/A *
0N/A * CDDL HEADER END
0N/A */
0N/A
0N/A/*
0N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
0N/A * Use is subject to license terms.
0N/A */
0N/Apackage org.opensolaris.opengrok.search;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.List;
0N/Aimport org.junit.After;
0N/Aimport org.junit.AfterClass;
0N/Aimport org.junit.Before;
0N/Aimport org.junit.BeforeClass;
0N/Aimport org.junit.Test;
0N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
0N/Aimport org.opensolaris.opengrok.index.Indexer;
0N/Aimport org.opensolaris.opengrok.index.IndexerTest;
0N/Aimport org.opensolaris.opengrok.util.TestRepository;
0N/Aimport static org.junit.Assert.*;
0N/A
0N/A/**
0N/A * Do basic testing of the SearchEngine
0N/A *
0N/A * @author Trond Norbye
0N/A */
0N/Apublic class SearchEngineTest {
0N/A
0N/A static TestRepository repository;
0N/A static boolean skip = false;
0N/A static File configFile;
0N/A
0N/A @BeforeClass
0N/A public static void setUpClass() throws Exception {
0N/A repository = new TestRepository();
0N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
0N/A
0N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
0N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
0N/A env.setSourceRoot(repository.getSourceRoot());
0N/A env.setDataRoot(repository.getDataRoot());
0N/A
0N/A if (env.validateExuberantCtags()) {
0N/A env.setSourceRoot(repository.getSourceRoot());
0N/A env.setDataRoot(repository.getDataRoot());
0N/A env.setVerbose(false);
0N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null);
0N/A Indexer.getInstance().doIndexerExecution(true, 1, null, null);
0N/A } else {
0N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
0N/A skip = true;
0N/A }
0N/A
0N/A configFile = File.createTempFile("configuration", ".xml");
0N/A env.writeConfiguration(configFile);
0N/A RuntimeEnvironment.getInstance().readConfiguration(new File(configFile.getAbsolutePath()));
0N/A }
0N/A
0N/A @AfterClass
0N/A public static void tearDownClass() throws Exception {
0N/A repository.destroy();
0N/A configFile.delete();
0N/A }
0N/A
0N/A @Before
0N/A public void setUp() {
0N/A }
0N/A
0N/A @After
0N/A public void tearDown() {
0N/A }
0N/A
0N/A @Test
0N/A public void testIsValidQuery() {
0N/A SearchEngine instance = new SearchEngine();
0N/A assertFalse(instance.isValidQuery());
0N/A instance.setFile("foo");
0N/A assertTrue(instance.isValidQuery());
0N/A }
0N/A
0N/A @Test
0N/A public void testDefinition() {
0N/A SearchEngine instance = new SearchEngine();
0N/A assertNull(instance.getDefinition());
0N/A String defs = "This is a definition";
0N/A instance.setDefinition(defs);
0N/A assertEquals(defs, instance.getDefinition());
0N/A }
0N/A
0N/A @Test
public void testFile() {
SearchEngine instance = new SearchEngine();
assertNull(instance.getFile());
String file = "This is a File";
instance.setFile(file);
assertEquals(file, instance.getFile());
}
@Test
public void testFreetext() {
SearchEngine instance = new SearchEngine();
assertNull(instance.getFreetext());
String freetext = "This is just a piece of text";
instance.setFreetext(freetext);
assertEquals(freetext, instance.getFreetext());
}
@Test
public void testHistory() {
SearchEngine instance = new SearchEngine();
assertNull(instance.getHistory());
String hist = "This is a piece of history";
instance.setHistory(hist);
assertEquals(hist, instance.getHistory());
}
@Test
public void testSymbol() {
SearchEngine instance = new SearchEngine();
assertNull(instance.getSymbol());
String sym = "This is a symbol";
instance.setSymbol(sym);
assertEquals(sym, instance.getSymbol());
}
@Test
public void testGetQuery() throws Exception {
SearchEngine instance = new SearchEngine();
instance.setHistory("Once upon a time");
instance.setFile("Makefile");
instance.setDefinition("std::string");
instance.setSymbol("toString");
instance.setFreetext("OpenGrok");
assertTrue(instance.isValidQuery());
assertEquals("+full:opengrok +defs:\"std string\" +refs:toString +path:makefile +(+hist:once +hist:upon +hist:time)",
instance.getQuery());
}
@Test
public void testSearch() {
if (skip) {
return;
}
SearchEngine instance = new SearchEngine();
instance.setFile("Makefile");
assertEquals(1, instance.search());
List<Hit> hits = new ArrayList<Hit>();
hits.clear();
instance.more(0, 1, hits);
assertEquals(1, hits.size());
instance.setFile("main~");
assertEquals(6, instance.search());
hits.clear();
instance.more(0, 3, hits);
assertEquals(3, hits.size());
instance.more(3, 6, hits);
assertEquals(6, hits.size());
instance.setFile("\"main troff\"~5");
assertEquals(0, instance.search());
instance.setFile("Main OR main");
assertEquals(6, instance.search());
instance.setFile("main file");
assertEquals(0, instance.search());
instance.setFile("+main -file");
assertEquals(6, instance.search());
instance.setFile("main AND (file OR field)");
assertEquals(0, instance.search());
instance.setFreetext("opengrok && something || else");
assertEquals(4, instance.search());
instance.setFreetext("op*ng?ok");
assertEquals(3, instance.search());
instance.setFreetext("\"op*n g?ok\"");
assertEquals(0, instance.search());
instance.setFreetext("title:[a TO b]");
assertEquals(0, instance.search());
instance.setFreetext("title:{a TO c}");
assertEquals(0, instance.search());
instance.setFreetext("\"contains some strange\"");
assertEquals(1, instance.search());
RuntimeEnvironment.getInstance().setAllowLeadingWildcard(true);
instance.setFile("?akefile");
assertEquals(1, instance.search());
}
}