IndexerTest.java revision 1372
0N/A/*
0N/A * CDDL HEADER START
0N/A *
0N/A * The contents of this file are subject to the terms of the
137N/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/*
137N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
0N/A */
0N/Apackage org.opensolaris.opengrok.index;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileReader;
137N/Aimport java.io.IOException;
137N/Aimport java.io.StringWriter;
137N/Aimport java.util.ArrayList;
137N/Aimport java.util.List;
137N/Aimport org.junit.After;
137N/Aimport org.junit.AfterClass;
137N/Aimport org.junit.Before;
137N/Aimport org.junit.BeforeClass;
457N/Aimport org.junit.Test;
137N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
137N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
587N/Aimport org.opensolaris.opengrok.configuration.Project;
1327N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1327N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
1195N/Aimport org.opensolaris.opengrok.history.Repository;
0N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
0N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
0N/Aimport org.opensolaris.opengrok.util.Executor;
0N/Aimport org.opensolaris.opengrok.util.FileUtilities;
0N/Aimport org.opensolaris.opengrok.util.TestRepository;
0N/Aimport static org.junit.Assert.*;
0N/A
0N/A/**
0N/A *
0N/A * @author Trond Norbye
0N/A */
0N/Apublic class IndexerTest {
0N/A TestRepository repository;
0N/A
137N/A public IndexerTest() {
0N/A }
0N/A
0N/A @BeforeClass
0N/A public static void setUpClass() throws Exception {
137N/A assertTrue("No point in running indexer tests without valid ctags",
0N/A RuntimeEnvironment.getInstance().validateExuberantCtags());
137N/A }
0N/A
0N/A @AfterClass
457N/A public static void tearDownClass() throws Exception {
0N/A }
0N/A
0N/A @Before
137N/A public void setUp() throws IOException {
0N/A repository = new TestRepository();
0N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
0N/A }
0N/A
0N/A @After
137N/A public void tearDown() {
0N/A repository.destroy();
137N/A }
0N/A
0N/A /**
0N/A * Test of doIndexerExecution method, of class Indexer.
0N/A */
137N/A @Test
0N/A public void testIndexGeneration() throws Exception {
0N/A System.out.println("Generating index by using the class methods");
0N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
0N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
137N/A if (env.validateExuberantCtags()) {
0N/A env.setSourceRoot(repository.getSourceRoot());
137N/A env.setDataRoot(repository.getDataRoot());
0N/A env.setVerbose(true);
0N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null, new ArrayList<String>(), false);
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.");
137N/A }
0N/A }
0N/A
0N/A /**
0N/A * Test that rescanning for projects does not erase customization of
0N/A * existing projects. Bug #16006.
0N/A */
0N/A @Test
137N/A public void testRescanProjects() throws Exception {
0N/A // Generate one project that will be found in source.zip, and set
0N/A // some properties that we can verify after the rescan.
0N/A Project p1 = new Project();
0N/A p1.setPath("/java");
0N/A p1.setDescription("Project 1");
0N/A p1.setTabSize(3);
0N/A
137N/A // Generate one project that will not be found in source.zip, and that
0N/A // should not be in the list of projects after the rescan.
0N/A Project p2 = new Project();
0N/A p2.setPath("/this_path_does_not_exist");
0N/A p2.setDescription("Project 2");
0N/A
0N/A // Make the runtime environment aware of these two projects.
137N/A List<Project> projects = new ArrayList<Project>();
0N/A projects.add(p1);
0N/A projects.add(p2);
0N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
137N/A env.setProjects(projects);
137N/A
0N/A // Do a rescan of the projects, and only that (we don't care about
0N/A // the other aspects of indexing in this test case).
0N/A Indexer.getInstance().prepareIndexer(
0N/A env,
0N/A false, // don't search for repositories
0N/A true, // scan and add projects
0N/A null, // no default project
137N/A null, // don't write config file
0N/A false, // don't refresh history
0N/A false, // don't list files
0N/A false, // don't create dictionary
0N/A null, // subFiles - not needed since we don't list files
0N/A null, // repositories - not needed when not refreshing history
137N/A new ArrayList<String>(), // don't zap cache
0N/A false); // don't list repos
137N/A
0N/A List<Project> newProjects = env.getProjects();
0N/A
0N/A // p2 should not be in the project list anymore
0N/A for (Project p : newProjects) {
137N/A assertFalse("p2 not removed", p.getPath().equals(p2.getPath()));
0N/A }
137N/A
137N/A // p1 should be there
0N/A Project newP1 = null;
0N/A for (Project p : newProjects) {
0N/A if (p.getPath().equals(p1.getPath())) {
0N/A newP1 = p;
137N/A break;
0N/A }
137N/A }
0N/A assertNotNull("p1 not in list", newP1);
0N/A
0N/A // The properties of p1 should be preserved
137N/A assertEquals("project path", p1.getPath(), newP1.getPath());
0N/A assertEquals("project description",
137N/A p1.getDescription(), newP1.getDescription());
0N/A assertEquals("project tabsize", p1.getTabSize(), newP1.getTabSize());
137N/A }
0N/A
0N/A /**
460N/A * Test of doIndexerExecution method, of class Indexer.
460N/A */
460N/A @Test
137N/A public void testMain() throws IOException {
0N/A System.out.println("Generate index by using command line options");
0N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
460N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
460N/A if (env.validateExuberantCtags()) {
460N/A String[] argv = {"-S", "-P", "-H", "-Q", "off", "-s", repository.getSourceRoot(), "-d", repository.getDataRoot(), "-v"};
0N/A Indexer.main(argv);
0N/A } else {
0N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
0N/A }
137N/A }
0N/A
0N/A private class MyIndexChangeListener implements org.opensolaris.opengrok.index.IndexChangedListener {
0N/A List<String> files = new ArrayList<String>();
137N/A
137N/A @Override
0N/A public void fileAdd(String path, String analyzer) {
0N/A }
0N/A
0N/A @Override
0N/A public void fileAdded(String path, String analyzer) {
0N/A files.add(path);
137N/A }
0N/A
0N/A @Override
0N/A public void fileRemove(String path) {
0N/A }
0N/A @Override
137N/A public void fileUpdate(String path) {
0N/A }
0N/A
0N/A @Override
0N/A public void fileRemoved(String path) {
137N/A }
0N/A }
0N/A
521N/A @Test
521N/A public void testRFE2575() throws Exception {
521N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1195N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
521N/A env.setSourceRoot(repository.getSourceRoot());
521N/A env.setDataRoot(repository.getDataRoot());
521N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
521N/A
137N/A List<RepositoryInfo> repos = env.getRepositories();
0N/A Repository r = null;
0N/A for (RepositoryInfo ri : repos) {
0N/A if (ri.getDirectoryName().equals(repository.getSourceRoot()+"/rfe2575")) {
0N/A r = RepositoryFactory.getRepository(ri);
0N/A break;
137N/A }
0N/A }
137N/A
283N/A if (r != null && r.isWorking() && env.validateExuberantCtags()) {
0N/A Project project = new Project();
0N/A project.setPath("/rfe2575");
0N/A IndexDatabase idb = new IndexDatabase(project);
0N/A assertNotNull(idb);
0N/A MyIndexChangeListener listener = new MyIndexChangeListener();
0N/A idb.addIndexChangedListener(listener);
0N/A idb.update();
0N/A assertEquals(2, listener.files.size());
137N/A repository.purgeData();
0N/A RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(true);
0N/A idb = new IndexDatabase(project);
0N/A listener = new MyIndexChangeListener();
0N/A idb.addIndexChangedListener(listener);
0N/A idb.update();
0N/A assertEquals(1, listener.files.size());
0N/A RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
0N/A } else {
0N/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.");
0N/A }
1195N/A }
0N/A
137N/A @Test
459N/A public void testXref() throws IOException {
137N/A List<File> files = new ArrayList<File>();
137N/A FileUtilities.getAllFiles(new File(repository.getSourceRoot()), files, false);
137N/A for (File f : files) {
137N/A FileAnalyzerFactory factory = AnalyzerGuru.find(f.getAbsolutePath());
137N/A if (factory == null) {
587N/A continue;
587N/A }
587N/A FileReader in = new FileReader(f);
587N/A StringWriter out = new StringWriter();
1190N/A try {
587N/A AnalyzerGuru.writeXref(factory, in, out, null, null, null);
587N/A } catch (UnsupportedOperationException exp) {
587N/A // ignore
587N/A }
587N/A in.close();
587N/A out.close();
587N/A }
587N/A }
587N/A @Test
587N/A public void testBug3430() throws Exception {
587N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
587N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
587N/A env.setSourceRoot(repository.getSourceRoot());
1327N/A env.setDataRoot(repository.getDataRoot());
1327N/A
1327N/A if (env.validateExuberantCtags()) {
587N/A Project project = new Project();
587N/A project.setPath("/bug3430");
587N/A IndexDatabase idb = new IndexDatabase(project);
396N/A assertNotNull(idb);
MyIndexChangeListener listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
idb.update();
assertEquals(1, listener.files.size());
} else {
System.out.println("Skipping test. Could not find a ctags I could use in path.");
}
}
@Test
public void testBug11896() throws Exception {
boolean test = true;
if (FileUtilities.findProgInPath("mkfifo") == null) {
System.out.println("Error: mkfifo not found in PATH !\n");
test = false;
}
if (test) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
Executor executor;
executor = new Executor(new String[] {"mkdir", "-p", repository.getSourceRoot()+"/testBug11896"});
executor.exec(true);
executor = new Executor(new String[] {"mkfifo", repository.getSourceRoot()+"/testBug11896/FIFO"});
executor.exec(true);
if (env.validateExuberantCtags()) {
Project project = new Project();
project.setPath("/testBug11896");
IndexDatabase idb = new IndexDatabase(project);
assertNotNull(idb);
MyIndexChangeListener listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
System.out.println("Trying to index a special file - FIFO in this case.");
idb.update();
assertEquals(0, listener.files.size());
} else {
System.out.println("Skipping test. Could not find a ctags I could use in path.");
}
} else {
System.out.println("Skipping test for bug 11896. Could not find a mkfifo in path.");
}
}
}