IndexerTest.java revision 1384
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.AfterClass;
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;
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
352N/A public IndexerTest() {
352N/A }
352N/A
352N/A @BeforeClass
352N/A public static void setUpClass() throws Exception {
861N/A assertTrue("No point in running indexer tests without valid ctags",
861N/A RuntimeEnvironment.getInstance().validateExuberantCtags());
352N/A }
352N/A
352N/A @AfterClass
352N/A public static void tearDownClass() throws Exception {
352N/A }
352N/A
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
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.
352N/A */
352N/A @Test
352N/A public void testIndexGeneration() throws Exception {
365N/A System.out.println("Generating index by using the class methods");
352N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
352N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
352N/A if (env.validateExuberantCtags()) {
489N/A env.setSourceRoot(repository.getSourceRoot());
377N/A env.setDataRoot(repository.getDataRoot());
352N/A env.setVerbose(true);
1214N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null, new ArrayList<String>(), false);
352N/A Indexer.getInstance().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.
1092N/A */
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);
1092N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1092N/A env.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).
1092N/A Indexer.getInstance().prepareIndexer(
1092N/A env,
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
1092N/A List<Project> newProjects = env.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
365N/A public void testMain() throws IOException {
365N/A System.out.println("Generate index by using command line options");
365N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
365N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
365N/A if (env.validateExuberantCtags()) {
1024N/A String[] argv = {"-S", "-P", "-H", "-Q", "off", "-s", 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
480N/A private class MyIndexChangeListener implements org.opensolaris.opengrok.index.IndexChangedListener {
480N/A List<String> files = new ArrayList<String>();
480N/A
1054N/A @Override
1054N/A public void fileAdd(String path, String analyzer) {
1054N/A }
1054N/A
1054N/A @Override
480N/A public void fileAdded(String path, String analyzer) {
480N/A files.add(path);
480N/A }
480N/A
1054N/A @Override
1054N/A public void fileRemove(String path) {
1054N/A }
1054N/A @Override
1054N/A public void fileUpdate(String path) {
1054N/A }
1054N/A
1054N/A @Override
1054N/A public void fileRemoved(String path) {
480N/A }
480N/A }
480N/A
480N/A @Test
678N/A public void testRFE2575() throws Exception {
480N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
480N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
489N/A env.setSourceRoot(repository.getSourceRoot());
480N/A env.setDataRoot(repository.getDataRoot());
489N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
480N/A
665N/A List<RepositoryInfo> repos = env.getRepositories();
665N/A Repository r = null;
665N/A for (RepositoryInfo ri : repos) {
807N/A if (ri.getDirectoryName().equals(repository.getSourceRoot()+"/rfe2575")) {
665N/A r = RepositoryFactory.getRepository(ri);
665N/A break;
665N/A }
665N/A }
592N/A
592N/A if (r != null && r.isWorking() && env.validateExuberantCtags()) {
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();
480N/A RuntimeEnvironment.getInstance().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());
807N/A RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
480N/A } else {
807N/A System.out.println("Skipping test. Repository for rfe2575 not found or could not find a ctags or an sccs I could use in path.");
480N/A }
480N/A }
560N/A
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 {
1384N/A AnalyzerGuru.writeXref(factory, in, 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 }
593N/A @Test
678N/A public void testBug3430() throws Exception {
593N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
593N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
593N/A env.setSourceRoot(repository.getSourceRoot());
593N/A env.setDataRoot(repository.getDataRoot());
593N/A
593N/A if (env.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
876N/A @Test
876N/A public void testBug11896() throws Exception {
876N/A
1372N/A boolean test = true;
1372N/A if (FileUtilities.findProgInPath("mkfifo") == null) {
1372N/A System.out.println("Error: mkfifo not found in PATH !\n");
1372N/A test = false;
1372N/A }
876N/A
876N/A if (test) {
876N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
876N/A env.setSourceRoot(repository.getSourceRoot());
876N/A env.setDataRoot(repository.getDataRoot());
1372N/A Executor executor;
1023N/A
876N/A executor = new Executor(new String[] {"mkdir", "-p", repository.getSourceRoot()+"/testBug11896"});
876N/A executor.exec(true);
876N/A
876N/A executor = new Executor(new String[] {"mkfifo", repository.getSourceRoot()+"/testBug11896/FIFO"});
876N/A executor.exec(true);
876N/A
876N/A if (env.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());
876N/A } else {
876N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
876N/A }
876N/A } else {
876N/A System.out.println("Skipping test for bug 11896. Could not find a mkfifo in path.");
876N/A }
876N/A }
593N/A}