352N/A/*
352N/A * CDDL HEADER START
352N/A *
352N/A * The contents of this file are subject to the terms of the
352N/A * Common Development and Distribution License (the "License").
352N/A * You may not use this file except in compliance with the License.
352N/A *
352N/A * See LICENSE.txt included in this distribution for the specific
352N/A * language governing permissions and limitations under the License.
352N/A *
352N/A * When distributing Covered Code, include this CDDL HEADER in each
352N/A * file and include the License file at LICENSE.txt.
352N/A * If applicable, add the following below this CDDL HEADER, with the
352N/A * fields enclosed by brackets "[]" replaced with your own identifying
352N/A * information: Portions Copyright [yyyy] [name of copyright owner]
352N/A *
352N/A * CDDL HEADER END
352N/A */
352N/A
352N/A/*
1372N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
352N/A */
352N/Apackage org.opensolaris.opengrok.index;
352N/A
1384N/Aimport static org.junit.Assert.assertEquals;
1384N/Aimport static org.junit.Assert.assertFalse;
1384N/Aimport static org.junit.Assert.assertNotNull;
1384N/Aimport static org.junit.Assert.assertTrue;
1384N/A
560N/Aimport java.io.File;
921N/Aimport java.io.FileReader;
352N/Aimport java.io.IOException;
560N/Aimport java.io.StringWriter;
480N/Aimport java.util.ArrayList;
480N/Aimport java.util.List;
1384N/A
352N/Aimport org.junit.After;
352N/Aimport org.junit.Before;
352N/Aimport org.junit.BeforeClass;
352N/Aimport org.junit.Test;
560N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
560N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
1384N/Aimport org.opensolaris.opengrok.analysis.XrefWriter;
1470N/Aimport org.opensolaris.opengrok.configuration.Configuration;
480N/Aimport org.opensolaris.opengrok.configuration.Project;
352N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
480N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
592N/Aimport org.opensolaris.opengrok.history.Repository;
665N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
665N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
876N/Aimport org.opensolaris.opengrok.util.Executor;
560N/Aimport org.opensolaris.opengrok.util.FileUtilities;
377N/Aimport org.opensolaris.opengrok.util.TestRepository;
352N/A
352N/A/**
352N/A *
352N/A * @author Trond Norbye
352N/A */
352N/Apublic class IndexerTest {
377N/A TestRepository repository;
352N/A
1470N/A @SuppressWarnings("javadoc")
1470N/A @BeforeClass
1470N/A public static void setUpClass() {
1470N/A assertTrue("No point in running indexer tests without valid ctags",
1470N/A RuntimeEnvironment.validateExuberantCtags());
352N/A }
352N/A
1470N/A @SuppressWarnings("javadoc")
352N/A @Before
367N/A public void setUp() throws IOException {
377N/A repository = new TestRepository();
377N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
352N/A }
352N/A
1470N/A @SuppressWarnings("javadoc")
352N/A @After
352N/A public void tearDown() {
377N/A repository.destroy();
352N/A }
352N/A
352N/A /**
352N/A * Test of doIndexerExecution method, of class Indexer.
1470N/A * @throws Exception
352N/A */
352N/A @Test
352N/A public void testIndexGeneration() throws Exception {
365N/A System.out.println("Generating index by using the class methods");
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
1470N/A Configuration.CTAGS_CMD_FALLBACK));
1470N/A if (RuntimeEnvironment.validateExuberantCtags()) {
1470N/A cfg.setSourceRoot(repository.getSourceRoot());
1470N/A cfg.setDataRoot(repository.getDataRoot());
1470N/A cfg.setVerbose(true);
1470N/A Indexer.prepareIndexer(true, true, "/c", null, false, false, false,
1470N/A null, null, new ArrayList<String>(), false);
1470N/A Indexer.doIndexerExecution(true, 1, null, null);
352N/A } else {
352N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
352N/A }
352N/A }
352N/A
365N/A /**
1092N/A * Test that rescanning for projects does not erase customization of
1092N/A * existing projects. Bug #16006.
1470N/A * @throws Exception
1092N/A */
1470N/A @SuppressWarnings({ "static-method", "null" })
1092N/A @Test
1092N/A public void testRescanProjects() throws Exception {
1092N/A // Generate one project that will be found in source.zip, and set
1092N/A // some properties that we can verify after the rescan.
1092N/A Project p1 = new Project();
1092N/A p1.setPath("/java");
1092N/A p1.setDescription("Project 1");
1092N/A p1.setTabSize(3);
1092N/A
1092N/A // Generate one project that will not be found in source.zip, and that
1092N/A // should not be in the list of projects after the rescan.
1092N/A Project p2 = new Project();
1092N/A p2.setPath("/this_path_does_not_exist");
1092N/A p2.setDescription("Project 2");
1092N/A
1092N/A // Make the runtime environment aware of these two projects.
1092N/A List<Project> projects = new ArrayList<Project>();
1092N/A projects.add(p1);
1092N/A projects.add(p2);
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setProjects(projects);
1092N/A
1092N/A // Do a rescan of the projects, and only that (we don't care about
1092N/A // the other aspects of indexing in this test case).
1470N/A Indexer.prepareIndexer(
1092N/A false, // don't search for repositories
1092N/A true, // scan and add projects
1092N/A null, // no default project
1092N/A null, // don't write config file
1092N/A false, // don't refresh history
1092N/A false, // don't list files
1092N/A false, // don't create dictionary
1092N/A null, // subFiles - not needed since we don't list files
1185N/A null, // repositories - not needed when not refreshing history
1214N/A new ArrayList<String>(), // don't zap cache
1185N/A false); // don't list repos
1092N/A
1470N/A List<Project> newProjects = cfg.getProjects();
1092N/A
1092N/A // p2 should not be in the project list anymore
1092N/A for (Project p : newProjects) {
1092N/A assertFalse("p2 not removed", p.getPath().equals(p2.getPath()));
1092N/A }
1092N/A
1092N/A // p1 should be there
1092N/A Project newP1 = null;
1092N/A for (Project p : newProjects) {
1092N/A if (p.getPath().equals(p1.getPath())) {
1092N/A newP1 = p;
1092N/A break;
1092N/A }
1092N/A }
1092N/A assertNotNull("p1 not in list", newP1);
1092N/A
1092N/A // The properties of p1 should be preserved
1092N/A assertEquals("project path", p1.getPath(), newP1.getPath());
1092N/A assertEquals("project description",
1092N/A p1.getDescription(), newP1.getDescription());
1092N/A assertEquals("project tabsize", p1.getTabSize(), newP1.getTabSize());
1092N/A }
1092N/A
1092N/A /**
365N/A * Test of doIndexerExecution method, of class Indexer.
365N/A */
365N/A @Test
1470N/A public void testMain() {
365N/A System.out.println("Generate index by using command line options");
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
1470N/A Configuration.CTAGS_CMD_FALLBACK));
1470N/A if (RuntimeEnvironment.validateExuberantCtags()) {
1470N/A String[] argv = { "-S", "-P", "-H", "-Q", "off", "-s",
1470N/A repository.getSourceRoot(), "-d", repository.getDataRoot(), "-v" };
365N/A Indexer.main(argv);
365N/A } else {
365N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
365N/A }
365N/A }
480N/A
1470N/A class MyIndexChangeListener implements IndexChangedListener {
480N/A List<String> files = new ArrayList<String>();
480N/A
1054N/A @Override
1470N/A public void fileAdd(String path, String analyzer) {
1470N/A // dummy
1054N/A }
1054N/A @Override
480N/A public void fileAdded(String path, String analyzer) {
480N/A files.add(path);
480N/A }
1054N/A @Override
1054N/A public void fileRemove(String path) {
1470N/A // dummy
1054N/A }
1054N/A @Override
1054N/A public void fileUpdate(String path) {
1470N/A // dummy
1054N/A }
1054N/A @Override
1470N/A public void fileRemoved(String path) {
1470N/A // dummy
480N/A }
480N/A }
480N/A
1470N/A @SuppressWarnings("javadoc")
480N/A @Test
678N/A public void testRFE2575() throws Exception {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
1470N/A Configuration.CTAGS_CMD_FALLBACK));
1470N/A cfg.setSourceRoot(repository.getSourceRoot());
1470N/A cfg.setDataRoot(repository.getDataRoot());
489N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
480N/A
1470N/A List<RepositoryInfo> repos = cfg.getRepositories();
665N/A Repository r = null;
665N/A for (RepositoryInfo ri : repos) {
1470N/A if (ri.getDirectoryName().equals(repository.getSourceRoot() + "/rfe2575")) {
665N/A r = RepositoryFactory.getRepository(ri);
665N/A break;
665N/A }
665N/A }
592N/A
1470N/A if (r != null && r.isWorking()
1470N/A && RuntimeEnvironment.validateExuberantCtags())
1470N/A {
480N/A Project project = new Project();
480N/A project.setPath("/rfe2575");
480N/A IndexDatabase idb = new IndexDatabase(project);
480N/A assertNotNull(idb);
480N/A MyIndexChangeListener listener = new MyIndexChangeListener();
480N/A idb.addIndexChangedListener(listener);
480N/A idb.update();
480N/A assertEquals(2, listener.files.size());
480N/A repository.purgeData();
1470N/A cfg.setIndexVersionedFilesOnly(true);
480N/A idb = new IndexDatabase(project);
480N/A listener = new MyIndexChangeListener();
480N/A idb.addIndexChangedListener(listener);
480N/A idb.update();
480N/A assertEquals(1, listener.files.size());
1470N/A cfg.setIndexVersionedFilesOnly(false);
480N/A } else {
1470N/A System.out.println("Skipping test. Repository for rfe2575 not found"
1470N/A + " or could not find a ctags or an sccs I could use in path.");
480N/A }
480N/A }
1470N/A
1470N/A @SuppressWarnings("javadoc")
560N/A @Test
560N/A public void testXref() throws IOException {
560N/A List<File> files = new ArrayList<File>();
560N/A FileUtilities.getAllFiles(new File(repository.getSourceRoot()), files, false);
560N/A for (File f : files) {
560N/A FileAnalyzerFactory factory = AnalyzerGuru.find(f.getAbsolutePath());
560N/A if (factory == null) {
560N/A continue;
560N/A }
921N/A FileReader in = new FileReader(f);
560N/A StringWriter out = new StringWriter();
560N/A try {
1470N/A AnalyzerGuru.writeXref(factory, in,
1470N/A new XrefWriter(out), null, null, null);
560N/A } catch (UnsupportedOperationException exp) {
560N/A // ignore
560N/A }
560N/A in.close();
560N/A out.close();
560N/A }
560N/A }
1470N/A
1470N/A @SuppressWarnings("javadoc")
593N/A @Test
678N/A public void testBug3430() throws Exception {
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
1470N/A Configuration.CTAGS_CMD_FALLBACK));
1470N/A cfg.setSourceRoot(repository.getSourceRoot());
1470N/A cfg.setDataRoot(repository.getDataRoot());
593N/A
1470N/A if (RuntimeEnvironment.validateExuberantCtags()) {
593N/A Project project = new Project();
593N/A project.setPath("/bug3430");
593N/A IndexDatabase idb = new IndexDatabase(project);
593N/A assertNotNull(idb);
593N/A MyIndexChangeListener listener = new MyIndexChangeListener();
593N/A idb.addIndexChangedListener(listener);
593N/A idb.update();
593N/A assertEquals(1, listener.files.size());
593N/A } else {
593N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
593N/A }
593N/A }
876N/A
1470N/A @SuppressWarnings("javadoc")
876N/A @Test
876N/A public void testBug11896() throws Exception {
1470N/A if (FileUtilities.findProgInPath("mkfifo") == null) {
1470N/A System.out.println("Skipping test for bug 11896. Could not find a mkfifo in path.");
1470N/A return;
1470N/A }
876N/A
1470N/A Configuration cfg = RuntimeEnvironment.getConfig();
1470N/A cfg.setSourceRoot(repository.getSourceRoot());
1470N/A cfg.setDataRoot(repository.getDataRoot());
1372N/A Executor executor;
1470N/A
1470N/A executor = new Executor(new String[] { "mkdir", "-p",
1470N/A repository.getSourceRoot() + "/testBug11896" });
876N/A executor.exec(true);
876N/A
1470N/A executor = new Executor(new String[] { "mkfifo",
1470N/A repository.getSourceRoot() + "/testBug11896/FIFO" });
876N/A executor.exec(true);
876N/A
1470N/A if (RuntimeEnvironment.validateExuberantCtags()) {
876N/A Project project = new Project();
876N/A project.setPath("/testBug11896");
876N/A IndexDatabase idb = new IndexDatabase(project);
876N/A assertNotNull(idb);
876N/A MyIndexChangeListener listener = new MyIndexChangeListener();
876N/A idb.addIndexChangedListener(listener);
876N/A System.out.println("Trying to index a special file - FIFO in this case.");
876N/A idb.update();
876N/A assertEquals(0, listener.files.size());
1470N/A } else {
876N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
876N/A }
876N/A }
593N/A}