SearchTest.java revision 549
536N/A/*
536N/A * CDDL HEADER START
536N/A *
536N/A * The contents of this file are subject to the terms of the
536N/A * Common Development and Distribution License (the "License").
536N/A * You may not use this file except in compliance with the License.
536N/A *
536N/A * See LICENSE.txt included in this distribution for the specific
536N/A * language governing permissions and limitations under the License.
536N/A *
536N/A * When distributing Covered Code, include this CDDL HEADER in each
536N/A * file and include the License file at LICENSE.txt.
536N/A * If applicable, add the following below this CDDL HEADER, with the
536N/A * fields enclosed by brackets "[]" replaced with your own identifying
536N/A * information: Portions Copyright [yyyy] [name of copyright owner]
536N/A *
536N/A * CDDL HEADER END
536N/A */
536N/A
536N/A/*
536N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
536N/A * Use is subject to license terms.
536N/A */
536N/Apackage org.opensolaris.opengrok.search;
536N/A
536N/Aimport java.io.ByteArrayOutputStream;
536N/Aimport java.io.File;
536N/Aimport java.io.PrintStream;
536N/Aimport org.junit.After;
536N/Aimport org.junit.AfterClass;
536N/Aimport org.junit.Before;
536N/Aimport org.junit.BeforeClass;
536N/Aimport org.junit.Test;
536N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
536N/Aimport org.opensolaris.opengrok.index.Indexer;
536N/Aimport org.opensolaris.opengrok.index.IndexerTest;
536N/Aimport org.opensolaris.opengrok.util.TestRepository;
536N/Aimport static org.junit.Assert.*;
536N/A
536N/A/**
536N/A * Basic testing of the Search class
536N/A *
536N/A * @author Trond Norbye
536N/A */
536N/Apublic class SearchTest {
536N/A
536N/A static TestRepository repository;
536N/A static boolean skip = false;
537N/A static PrintStream err = System.err;
537N/A static File configFile;
536N/A
536N/A @BeforeClass
536N/A public static void setUpClass() throws Exception {
536N/A repository = new TestRepository();
536N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
536N/A
536N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
536N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
536N/A env.setSourceRoot(repository.getSourceRoot());
536N/A env.setDataRoot(repository.getDataRoot());
536N/A
536N/A if (env.validateExuberantCtags()) {
536N/A env.setSourceRoot(repository.getSourceRoot());
536N/A env.setDataRoot(repository.getDataRoot());
536N/A env.setVerbose(false);
536N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null);
536N/A Indexer.getInstance().doIndexerExecution(true, 1, null, null);
536N/A } else {
536N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
536N/A skip = true;
536N/A }
537N/A
537N/A configFile = File.createTempFile("configuration", ".xml");
537N/A env.writeConfiguration(configFile);
537N/A
537N/A RuntimeEnvironment.getInstance().readConfiguration(new File(configFile.getAbsolutePath()));
537N/A PrintStream stream = new PrintStream(new ByteArrayOutputStream());
537N/A System.setErr(stream);
536N/A }
536N/A
536N/A @AfterClass
536N/A public static void tearDownClass() throws Exception {
537N/A System.setErr(err);
536N/A repository.destroy();
536N/A }
536N/A
536N/A @Before
536N/A public void setUp() {
536N/A }
536N/A
536N/A @After
536N/A public void tearDown() {
536N/A }
536N/A
536N/A @Test
536N/A public void testParseCmdLine() {
536N/A if (skip) {
536N/A return;
536N/A }
536N/A Search instance = new Search();
536N/A
536N/A assertTrue(instance.parseCmdLine(new String[] {}));
536N/A assertTrue(instance.parseCmdLine(new String[] {"-f", "foo"}));
536N/A assertTrue(instance.parseCmdLine(new String[] {"-r", "foo"}));
536N/A assertTrue(instance.parseCmdLine(new String[] {"-d", "foo"}));
536N/A assertTrue(instance.parseCmdLine(new String[] {"-h", "foo"}));
536N/A assertTrue(instance.parseCmdLine(new String[] {"-p", "foo"}));
537N/A assertTrue(instance.parseCmdLine(new String[] {"-R", configFile.getAbsolutePath()}));
536N/A
536N/A assertFalse(instance.parseCmdLine(new String[] {"-f"}));
536N/A assertFalse(instance.parseCmdLine(new String[] {"-r"}));
536N/A assertFalse(instance.parseCmdLine(new String[] {"-d"}));
536N/A assertFalse(instance.parseCmdLine(new String[] {"-h"}));
536N/A assertFalse(instance.parseCmdLine(new String[] {"-p"}));
536N/A assertFalse(instance.parseCmdLine(new String[] {"-R"}));
537N/A assertFalse(instance.parseCmdLine(new String[] {"-R", "nonexisting-config-file"}));
536N/A
536N/A assertTrue(instance.parseCmdLine(new String[] {
536N/A "-f", "foo",
536N/A "-r", "foo",
536N/A "-d", "foo",
536N/A "-d", "foo",
536N/A "-h", "foo",
537N/A "-p", "foo", "-R", configFile.getAbsolutePath()}));
536N/A }
536N/A
536N/A /**
536N/A * Test of search method, of class Search.
536N/A */
536N/A @Test
536N/A public void testSearch() {
536N/A if (skip) {
536N/A return;
536N/A }
536N/A Search instance = new Search();
537N/A assertFalse(instance.search());
536N/A assertTrue(instance.parseCmdLine(new String[] {"-p", "Makefile"}));
536N/A assertTrue(instance.search());
536N/A assertEquals(1, instance.results.size());
549N/A}
549N/A
549N/A @Test
549N/A public void testSearchNotFound() {
549N/A if (skip) {
549N/A return;
549N/A }
549N/A Search instance = new Search();
549N/A
549N/A assertTrue(instance.parseCmdLine(new String[] {"-p", "path_that_can't_be_found"}));
549N/A assertTrue(instance.search());
549N/A assertEquals(0, instance.results.size());
549N/A
549N/A assertTrue(instance.parseCmdLine(new String[] {"-d", "definition_that_can't_be_found"}));
549N/A assertTrue(instance.search());
549N/A assertEquals(0, instance.results.size());
549N/A
549N/A assertTrue(instance.parseCmdLine(new String[] {"-r", "reference_that_can't_be_found"}));
549N/A assertTrue(instance.search());
549N/A assertEquals(0, instance.results.size());
549N/A
549N/A assertTrue(instance.parseCmdLine(new String[] {"-h", "history_that_can't_be_found"}));
549N/A assertTrue(instance.search());
549N/A assertEquals(0, instance.results.size());
549N/A
549N/A assertTrue(instance.parseCmdLine(new String[] {"-f", "fulltext_that_can't_be_found"}));
549N/A assertTrue(instance.search());
549N/A assertEquals(0, instance.results.size());
549N/A }
536N/A
536N/A @Test
536N/A public void testDumpResults() {
536N/A if (skip) {
536N/A return;
536N/A }
536N/A Search instance = new Search();
537N/A assertTrue(instance.parseCmdLine(new String[] {"-p", "Non-existing-makefile-Makefile"}));
537N/A assertTrue(instance.search());
537N/A assertEquals(0, instance.results.size());
537N/A instance.dumpResults();
537N/A
536N/A assertTrue(instance.parseCmdLine(new String[] {"-p", "Makefile"}));
536N/A assertTrue(instance.search());
536N/A
536N/A PrintStream out = System.out;
536N/A ByteArrayOutputStream array = new ByteArrayOutputStream();
536N/A System.setOut(new PrintStream(array));
536N/A instance.dumpResults();
536N/A System.out.flush();
536N/A assertTrue(array.toString().indexOf("Makefile: [...]") != -1);
536N/A System.setOut(out);
536N/A }
536N/A
536N/A /**
536N/A * Test of main method, of class Search.
536N/A */
536N/A @Test
536N/A public void testMain() {
536N/A if (skip) {
536N/A return;
536N/A }
536N/A PrintStream out = System.out;
536N/A ByteArrayOutputStream array = new ByteArrayOutputStream();
536N/A System.setOut(new PrintStream(array));
536N/A Search.main(new String[] {"-p", "Makefile"});
536N/A System.out.flush();
536N/A assertTrue(array.toString().indexOf("Makefile: [...]") != -1);
536N/A System.setOut(out);
536N/A }
536N/A}