IndexerTest.java revision 678
670N/A/*
670N/A * CDDL HEADER START
670N/A *
670N/A * The contents of this file are subject to the terms of the
670N/A * Common Development and Distribution License (the "License").
670N/A * You may not use this file except in compliance with the License.
670N/A *
670N/A * See LICENSE.txt included in this distribution for the specific
670N/A * language governing permissions and limitations under the License.
670N/A *
670N/A * When distributing Covered Code, include this CDDL HEADER in each
670N/A * file and include the License file at LICENSE.txt.
670N/A * If applicable, add the following below this CDDL HEADER, with the
670N/A * fields enclosed by brackets "[]" replaced with your own identifying
670N/A * information: Portions Copyright [yyyy] [name of copyright owner]
670N/A *
670N/A * CDDL HEADER END
670N/A */
670N/A
670N/A/*
670N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
670N/A * Use is subject to license terms.
670N/A */
670N/Apackage org.opensolaris.opengrok.index;
670N/A
670N/Aimport java.io.File;
670N/Aimport java.io.FileInputStream;
670N/Aimport java.io.IOException;
670N/Aimport java.io.InputStream;
670N/Aimport java.io.StringWriter;
670N/Aimport java.util.ArrayList;
670N/Aimport java.util.List;
670N/Aimport org.junit.After;
670N/Aimport org.junit.AfterClass;
670N/Aimport org.junit.Before;
670N/Aimport org.junit.BeforeClass;
670N/Aimport org.junit.Test;
670N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
670N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
670N/Aimport org.opensolaris.opengrok.configuration.Project;
670N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
670N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
670N/Aimport org.opensolaris.opengrok.history.Repository;
670N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
670N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
670N/Aimport org.opensolaris.opengrok.util.FileUtilities;
670N/Aimport org.opensolaris.opengrok.util.TestRepository;
670N/Aimport static org.junit.Assert.*;
670N/A
670N/A/**
670N/A *
670N/A * @author Trond Norbye
670N/A */
670N/Apublic class IndexerTest {
670N/A TestRepository repository;
670N/A
670N/A public IndexerTest() {
670N/A }
670N/A
670N/A @BeforeClass
670N/A public static void setUpClass() throws Exception {
670N/A }
670N/A
670N/A @AfterClass
670N/A public static void tearDownClass() throws Exception {
670N/A }
670N/A
670N/A @Before
670N/A public void setUp() throws IOException {
repository = new TestRepository();
repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
}
@After
public void tearDown() {
repository.destroy();
}
/**
* Test of doIndexerExecution method, of class Indexer.
*/
@Test
public void testIndexGeneration() throws Exception {
System.out.println("Generating index by using the class methods");
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", "-p", "/c", "-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())) {
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());
} else {
System.out.println("Skipping test. 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;
}
InputStream in = new FileInputStream(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.");
}
}
}