IndexerTest.java revision 1024
1668N/A/*
1668N/A * CDDL HEADER START
1381N/A *
142N/A * The contents of this file are subject to the terms of the
142N/A * Common Development and Distribution License (the "License").
142N/A * You may not use this file except in compliance with the License.
142N/A *
142N/A * See LICENSE.txt included in this distribution for the specific
142N/A * language governing permissions and limitations under the License.
142N/A *
142N/A * When distributing Covered Code, include this CDDL HEADER in each
142N/A * file and include the License file at LICENSE.txt.
142N/A * If applicable, add the following below this CDDL HEADER, with the
142N/A * fields enclosed by brackets "[]" replaced with your own identifying
142N/A * information: Portions Copyright [yyyy] [name of copyright owner]
142N/A *
1381N/A * CDDL HEADER END
142N/A */
142N/A
142N/A/*
479N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
479N/A * Use is subject to license terms.
479N/A */
142N/Apackage org.opensolaris.opengrok.index;
142N/A
142N/Aimport java.io.File;
142N/Aimport java.io.FileReader;
1381N/Aimport java.io.IOException;
1381N/Aimport java.io.StringWriter;
1381N/Aimport java.util.ArrayList;
1381N/Aimport java.util.List;
142N/Aimport org.junit.After;
142N/Aimport org.junit.AfterClass;
142N/Aimport org.junit.Before;
142N/Aimport org.junit.BeforeClass;
479N/Aimport org.junit.Test;
479N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
479N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
142N/Aimport org.opensolaris.opengrok.configuration.Project;
142N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
142N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
479N/Aimport org.opensolaris.opengrok.history.Repository;
479N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
479N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
142N/Aimport org.opensolaris.opengrok.util.Executor;
142N/Aimport org.opensolaris.opengrok.util.FileUtilities;
142N/Aimport org.opensolaris.opengrok.util.TestRepository;
142N/Aimport static org.junit.Assert.*;
479N/A
479N/A/**
479N/A *
142N/A * @author Trond Norbye
142N/A */
142N/Apublic class IndexerTest {
142N/A TestRepository repository;
142N/A
479N/A public IndexerTest() {
479N/A }
1668N/A
1668N/A @BeforeClass
1668N/A public static void setUpClass() throws Exception {
1668N/A assertTrue("No point in running indexer tests without valid ctags",
1668N/A RuntimeEnvironment.getInstance().validateExuberantCtags());
1668N/A }
1668N/A
1668N/A @AfterClass
1668N/A public static void tearDownClass() throws Exception {
1668N/A }
1668N/A
1668N/A @Before
1668N/A public void setUp() throws IOException {
479N/A repository = new TestRepository();
142N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
142N/A }
142N/A
142N/A @After
142N/A public void tearDown() {
142N/A repository.destroy();
142N/A }
142N/A
142N/A /**
142N/A * Test of doIndexerExecution method, of class Indexer.
142N/A */
142N/A @Test
142N/A public void testIndexGeneration() throws Exception {
142N/A System.out.println("Generating index by using the class methods");
142N/A RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
if (env.validateExuberantCtags()) {
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
env.setVerbose(true);
Indexer.getInstance().prepareIndexer(env, true, true, "/c", null, false, false, false, null, null);
Indexer.getInstance().doIndexerExecution(true, 1, null, null);
} else {
System.out.println("Skipping test. Could not find a ctags I could use in path.");
}
}
/**
* Test of doIndexerExecution method, of class Indexer.
*/
@Test
public void testMain() throws IOException {
System.out.println("Generate index by using command line options");
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
if (env.validateExuberantCtags()) {
String[] argv = {"-S", "-P", "-H", "-Q", "off", "-s", repository.getSourceRoot(), "-d", repository.getDataRoot(), "-v"};
Indexer.main(argv);
} else {
System.out.println("Skipping test. Could not find a ctags I could use in path.");
}
}
private class MyIndexChangeListener implements org.opensolaris.opengrok.index.IndexChangedListener {
List<String> files = new ArrayList<String>();
public void fileAdded(String path, String analyzer) {
files.add(path);
}
public void fileRemoved(String path) {
files.add(path);
}
}
@Test
public void testRFE2575() throws Exception {
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
env.setCtags(System.getProperty("org.opensolaris.opengrok.configuration.ctags", "ctags"));
env.setSourceRoot(repository.getSourceRoot());
env.setDataRoot(repository.getDataRoot());
HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
List<RepositoryInfo> repos = env.getRepositories();
Repository r = null;
for (RepositoryInfo ri : repos) {
if (ri.getDirectoryName().equals(repository.getSourceRoot()+"/rfe2575")) {
r = RepositoryFactory.getRepository(ri);
break;
}
}
if (r != null && r.isWorking() && env.validateExuberantCtags()) {
Project project = new Project();
project.setPath("/rfe2575");
IndexDatabase idb = new IndexDatabase(project);
assertNotNull(idb);
MyIndexChangeListener listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
idb.update();
assertEquals(2, listener.files.size());
repository.purgeData();
RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(true);
idb = new IndexDatabase(project);
listener = new MyIndexChangeListener();
idb.addIndexChangedListener(listener);
idb.update();
assertEquals(1, listener.files.size());
RuntimeEnvironment.getInstance().setIndexVersionedFilesOnly(false);
} else {
System.out.println("Skipping test. Repository for rfe2575 not found or could not find a ctags or an sccs I could use in path.");
}
}
@Test
public void testXref() throws IOException {
List<File> files = new ArrayList<File>();
FileUtilities.getAllFiles(new File(repository.getSourceRoot()), files, false);
for (File f : files) {
FileAnalyzerFactory factory = AnalyzerGuru.find(f.getAbsolutePath());
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.");
}
}
}