IndexerTest.java revision 921
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/*
352N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
352N/A * Use is subject to license terms.
352N/A */
352N/Apackage org.opensolaris.opengrok.index;
352N/A
560N/Aimport java.io.File;
560N/Aimport java.io.FileReader;
352N/Aimport java.io.IOException;
560N/Aimport java.io.StringWriter;
560N/Aimport java.util.ArrayList;
480N/Aimport java.util.List;
480N/Aimport org.junit.After;
352N/Aimport org.junit.AfterClass;
352N/Aimport org.junit.Before;
352N/Aimport org.junit.BeforeClass;
352N/Aimport org.junit.Test;
352N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
560N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
560N/Aimport org.opensolaris.opengrok.configuration.Project;
560N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
480N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
352N/Aimport org.opensolaris.opengrok.history.Repository;
480N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
560N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
377N/Aimport org.opensolaris.opengrok.util.Executor;
480N/Aimport org.opensolaris.opengrok.util.FileUtilities;
352N/Aimport org.opensolaris.opengrok.util.TestRepository;
352N/Aimport static org.junit.Assert.*;
352N/A
352N/A/**
352N/A *
352N/A * @author Trond Norbye
377N/A */
352N/Apublic class IndexerTest {
352N/A TestRepository repository;
352N/A
352N/A public IndexerTest() {
352N/A }
352N/A
352N/A @BeforeClass
352N/A public static void setUpClass() throws Exception {
352N/A assertTrue("No point in running indexer tests without valid ctags",
352N/A RuntimeEnvironment.getInstance().validateExuberantCtags());
352N/A }
352N/A
352N/A @AfterClass
367N/A public static void tearDownClass() throws Exception {
377N/A }
377N/A
352N/A @Before
352N/A public void setUp() throws IOException {
352N/A repository = new TestRepository();
352N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
377N/A }
352N/A
352N/A @After
352N/A public void tearDown() {
352N/A repository.destroy();
352N/A }
352N/A
352N/A /**
365N/A * Test of doIndexerExecution method, of class Indexer.
352N/A */
352N/A @Test
352N/A public void testIndexGeneration() throws Exception {
489N/A System.out.println("Generating index by using the class methods");
377N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
352N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
352N/A if (env.validateExuberantCtags()) {
352N/A env.setSourceRoot(repository.getSourceRoot());
352N/A env.setDataRoot(repository.getDataRoot());
352N/A env.setVerbose(true);
352N/A Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null);
352N/A Indexer.getInstance().doIndexerExecution(true, 1, null, null);
352N/A } else {
365N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
365N/A }
365N/A }
365N/A
365N/A /**
365N/A * Test of doIndexerExecution method, of class Indexer.
365N/A */
365N/A @Test
365N/A public void testMain() throws IOException {
489N/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()) {
365N/A String[] argv = { "-S", "-P", "-p", "/c", "-H", "-Q", "off", "-s", repository.getSourceRoot(), "-d", repository.getDataRoot(), "-v"};
365N/A Indexer.main(argv);
480N/A } else {
480N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
480N/A }
480N/A }
480N/A
480N/A private class MyIndexChangeListener implements org.opensolaris.opengrok.index.IndexChangedListener {
480N/A List<String> files = new ArrayList<String>();
480N/A
480N/A public void fileAdded(String path, String analyzer) {
480N/A files.add(path);
480N/A }
480N/A
480N/A public void fileRemoved(String path) {
480N/A files.add(path);
480N/A }
480N/A }
480N/A
489N/A @Test
480N/A public void testRFE2575() throws Exception {
489N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
480N/A env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
480N/A env.setSourceRoot(repository.getSourceRoot());
480N/A env.setDataRoot(repository.getDataRoot());
480N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
480N/A
480N/A List<RepositoryInfo> repos = env.getRepositories();
480N/A Repository r = null;
480N/A for (RepositoryInfo ri : repos) {
480N/A if (ri.getDirectoryName().equals(repository.getSourceRoot()+"/rfe2575")) {
480N/A r = RepositoryFactory.getRepository(ri);
480N/A break;
480N/A }
480N/A }
480N/A
480N/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);
560N/A idb.update();
560N/A assertEquals(2, listener.files.size());
560N/A repository.purgeData();
560N/A RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(true);
560N/A idb = new IndexDatabase(project);
560N/A listener = new MyIndexChangeListener();
560N/A idb.addIndexChangedListener(listener);
560N/A idb.update();
560N/A assertEquals(1, listener.files.size());
560N/A RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
560N/A } else {
560N/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.");
560N/A }
560N/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());
352N/A if (factory == null) {
continue;
}
FileReader in = new FileReader(f);
StringWriter out = new StringWriter();
try {
AnalyzerGuru.writeXref(factory, in, out, null, null);
} catch (UnsupportedOperationException exp) {
// ignore
}
in.close();
out.close();
}
}
@Test
public void testBug3430() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
if (env.validateExuberantCtags()) {
Project project = new Project();
project.setPath("/bug3430");
IndexDatabase idb = new IndexDatabase(project);
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;
Executor executor = new Executor(new String[] {"mkfifo"});
executor.exec(true);
String output = executor.getErrorString();
if (output == null || output.indexOf("mkfifo") == -1 || output.indexOf("command not found") > -1) {
System.out.println("Error: No mkfifo found in PATH!\n");
test = false;
}
if (test) {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
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.");
}
}
}