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/*
1148N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
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;
1220N/Aimport java.util.ArrayList;
536N/Aimport org.junit.After;
536N/Aimport org.junit.AfterClass;
1416N/Aimport static org.junit.Assert.*;
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/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);
1214N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null, new ArrayList<String>(), false);
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();
696N/A configFile.delete();
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
552N/A assertTrue(instance.parseCmdLine(new String[]{}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "foo"}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-r", "foo"}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-d", "foo"}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-h", "foo"}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "foo"}));
552N/A assertTrue(instance.parseCmdLine(new String[]{"-R", configFile.getAbsolutePath()}));
536N/A
552N/A assertFalse(instance.parseCmdLine(new String[]{"-f"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-r"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-d"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-h"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-p"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-R"}));
552N/A assertFalse(instance.parseCmdLine(new String[]{"-R", "nonexisting-config-file"}));
536N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{
552N/A "-f", "foo",
552N/A "-r", "foo",
552N/A "-d", "foo",
552N/A "-d", "foo",
552N/A "-h", "foo",
552N/A "-p", "foo", "-R", configFile.getAbsolutePath()
552N/A }));
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());
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "Makefile"}));
536N/A assertTrue(instance.search());
536N/A assertEquals(1, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "main~"}));
550N/A assertTrue(instance.search());
1416N/A assertEquals("Search for main~ in testdata sources", 8, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "\"main troff\"~5"}));
550N/A assertTrue(instance.search());
550N/A assertEquals(0, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "Main OR main"}));
550N/A assertTrue(instance.search());
1416N/A assertEquals("Search for Main OR main in testdata sources", 8, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "\"main file\""}));
550N/A assertTrue(instance.search());
550N/A assertEquals(0, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "+main -file"}));
550N/A assertTrue(instance.search());
1416N/A assertEquals("search for main but not file", 8, instance.results.size());
550N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "main AND (file OR field)"}));
550N/A assertTrue(instance.search());
550N/A assertEquals(0, instance.results.size());
551N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "opengrok && something || else"}));
551N/A assertTrue(instance.search());
1148N/A assertEquals(6, instance.results.size());
551N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "op*ng?ok"}));
552N/A assertTrue(instance.search());
1148N/A assertEquals(5, instance.results.size());
552N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "\"op*n g?ok\""}));
551N/A assertTrue(instance.search());
551N/A assertEquals(0, instance.results.size());
551N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "title:[a TO b]"}));
552N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
552N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "title:{a TO c}"}));
551N/A assertTrue(instance.search());
551N/A assertEquals(0, instance.results.size());
551N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "\"contains some strange\""}));
551N/A assertTrue(instance.search());
552N/A assertEquals(1, instance.results.size());
551N/A
552N/A RuntimeEnvironment.getInstance().setAllowLeadingWildcard(true);
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "?akefile"}));
551N/A assertTrue(instance.search());
552N/A assertEquals(1, instance.results.size());
552N/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();
552N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-p", "path_that_can't_be_found"}));
549N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
549N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-d", "definition_that_can't_be_found"}));
549N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
549N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-r", "reference_that_can't_be_found"}));
549N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
549N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-h", "history_that_can't_be_found"}));
549N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
549N/A
552N/A assertTrue(instance.parseCmdLine(new String[]{"-f", "fulltext_that_can't_be_found"}));
549N/A assertTrue(instance.search());
552N/A assertEquals(0, instance.results.size());
552N/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();
552N/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
552N/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));
552N/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 }
1024N/A}