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