IndexerTest.java revision 1470
58N/A/*
58N/A * CDDL HEADER START
58N/A *
58N/A * The contents of this file are subject to the terms of the
58N/A * Common Development and Distribution License (the "License").
58N/A * You may not use this file except in compliance with the License.
58N/A *
58N/A * See LICENSE.txt included in this distribution for the specific
58N/A * language governing permissions and limitations under the License.
58N/A *
58N/A * When distributing Covered Code, include this CDDL HEADER in each
58N/A * file and include the License file at LICENSE.txt.
58N/A * If applicable, add the following below this CDDL HEADER, with the
58N/A * fields enclosed by brackets "[]" replaced with your own identifying
58N/A * information: Portions Copyright [yyyy] [name of copyright owner]
58N/A *
58N/A * CDDL HEADER END
58N/A */
58N/A
58N/A/*
1252N/A * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
58N/A */
58N/Apackage org.opensolaris.opengrok.index;
58N/A
234N/Aimport static org.junit.Assert.assertEquals;
234N/Aimport static org.junit.Assert.assertFalse;
234N/Aimport static org.junit.Assert.assertNotNull;
234N/Aimport static org.junit.Assert.assertTrue;
639N/A
639N/Aimport java.io.File;
234N/Aimport java.io.FileReader;
234N/Aimport java.io.IOException;
234N/Aimport java.io.StringWriter;
234N/Aimport java.util.ArrayList;
639N/Aimport java.util.List;
639N/A
58N/Aimport org.junit.After;
1185N/Aimport org.junit.Before;
667N/Aimport org.junit.BeforeClass;
1185N/Aimport org.junit.Test;
1016N/Aimport org.opensolaris.opengrok.analysis.AnalyzerGuru;
58N/Aimport org.opensolaris.opengrok.analysis.FileAnalyzerFactory;
1185N/Aimport org.opensolaris.opengrok.analysis.XrefWriter;
1016N/Aimport org.opensolaris.opengrok.configuration.Configuration;
1185N/Aimport org.opensolaris.opengrok.configuration.Project;
664N/Aimport org.opensolaris.opengrok.configuration.RuntimeEnvironment;
1026N/Aimport org.opensolaris.opengrok.history.HistoryGuru;
112N/Aimport org.opensolaris.opengrok.history.Repository;
1195N/Aimport org.opensolaris.opengrok.history.RepositoryFactory;
58N/Aimport org.opensolaris.opengrok.history.RepositoryInfo;
58N/Aimport org.opensolaris.opengrok.util.Executor;
77N/Aimport org.opensolaris.opengrok.util.FileUtilities;
77N/Aimport org.opensolaris.opengrok.util.TestRepository;
77N/A
77N/A/**
58N/A *
418N/A * @author Trond Norbye
58N/A */
773N/Apublic class IndexerTest {
773N/A TestRepository repository;
58N/A
773N/A @SuppressWarnings("javadoc")
773N/A @BeforeClass
773N/A public static void setUpClass() {
773N/A assertTrue("No point in running indexer tests without valid ctags",
58N/A RuntimeEnvironment.validateExuberantCtags());
773N/A }
773N/A
773N/A @SuppressWarnings("javadoc")
773N/A @Before
58N/A public void setUp() throws IOException {
58N/A repository = new TestRepository();
58N/A repository.create(IndexerTest.class.getResourceAsStream("source.zip"));
664N/A }
58N/A
65N/A @SuppressWarnings("javadoc")
894N/A @After
77N/A public void tearDown() {
99N/A repository.destroy();
99N/A }
1115N/A
1115N/A /**
125N/A * Test of doIndexerExecution method, of class Indexer.
112N/A * @throws Exception
1026N/A */
129N/A @Test
1100N/A public void testIndexGeneration() throws Exception {
129N/A System.out.println("Generating index by using the class methods");
129N/A Configuration cfg = RuntimeEnvironment.getConfig();
318N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
318N/A Configuration.CTAGS_CMD_FALLBACK));
144N/A if (RuntimeEnvironment.validateExuberantCtags()) {
173N/A cfg.setSourceRoot(repository.getSourceRoot());
253N/A cfg.setDataRoot(repository.getDataRoot());
296N/A cfg.setVerbose(true);
335N/A Indexer.prepareIndexer(true, true, "/c", null, false, false, false,
480N/A null, null, new ArrayList<String>(), false);
816N/A Indexer.doIndexerExecution(true, 1, null, null);
816N/A } else {
833N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
833N/A }
1185N/A }
1016N/A
1123N/A /**
1125N/A * Test that rescanning for projects does not erase customization of
1218N/A * existing projects. Bug #16006.
1185N/A * @throws Exception
993N/A */
1185N/A @SuppressWarnings({ "static-method", "null" })
1185N/A @Test
1190N/A public void testRescanProjects() throws Exception {
1185N/A // Generate one project that will be found in source.zip, and set
1185N/A // some properties that we can verify after the rescan.
1252N/A Project p1 = new Project();
1185N/A p1.setPath("/java");
1185N/A p1.setDescription("Project 1");
1185N/A p1.setTabSize(3);
1185N/A
1185N/A // Generate one project that will not be found in source.zip, and that
1185N/A // should not be in the list of projects after the rescan.
1185N/A Project p2 = new Project();
1185N/A p2.setPath("/this_path_does_not_exist");
1185N/A p2.setDescription("Project 2");
1185N/A
1252N/A // Make the runtime environment aware of these two projects.
1185N/A List<Project> projects = new ArrayList<Project>();
1185N/A projects.add(p1);
1185N/A projects.add(p2);
1185N/A Configuration cfg = RuntimeEnvironment.getConfig();
1185N/A cfg.setProjects(projects);
1185N/A
993N/A // Do a rescan of the projects, and only that (we don't care about
993N/A // the other aspects of indexing in this test case).
993N/A Indexer.prepareIndexer(
1185N/A false, // don't search for repositories
993N/A true, // scan and add projects
993N/A null, // no default project
937N/A null, // don't write config file
58N/A false, // don't refresh history
58N/A false, // don't list files
816N/A false, // don't create dictionary
58N/A null, // subFiles - not needed since we don't list files
58N/A null, // repositories - not needed when not refreshing history
773N/A new ArrayList<String>(), // don't zap cache
58N/A false); // don't list repos
664N/A
58N/A List<Project> newProjects = cfg.getProjects();
850N/A
1118N/A // p2 should not be in the project list anymore
870N/A for (Project p : newProjects) {
870N/A assertFalse("p2 not removed", p.getPath().equals(p2.getPath()));
99N/A }
1115N/A
101N/A // p1 should be there
106N/A Project newP1 = null;
112N/A for (Project p : newProjects) {
1026N/A if (p.getPath().equals(p1.getPath())) {
129N/A newP1 = p;
129N/A break;
129N/A }
875N/A }
318N/A assertNotNull("p1 not in list", newP1);
144N/A
173N/A // The properties of p1 should be preserved
253N/A assertEquals("project path", p1.getPath(), newP1.getPath());
296N/A assertEquals("project description",
335N/A p1.getDescription(), newP1.getDescription());
480N/A assertEquals("project tabsize", p1.getTabSize(), newP1.getTabSize());
816N/A }
816N/A
993N/A /**
1016N/A * Test of doIndexerExecution method, of class Indexer.
1185N/A */
1185N/A @Test
58N/A public void testMain() {
937N/A System.out.println("Generate index by using command line options");
1185N/A Configuration cfg = RuntimeEnvironment.getConfig();
1185N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
1185N/A Configuration.CTAGS_CMD_FALLBACK));
1190N/A if (RuntimeEnvironment.validateExuberantCtags()) {
1185N/A String[] argv = { "-S", "-P", "-H", "-Q", "off", "-s",
1185N/A repository.getSourceRoot(), "-d", repository.getDataRoot(), "-v" };
1185N/A Indexer.main(argv);
1185N/A } else {
1185N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
1185N/A }
1185N/A }
1185N/A
1185N/A class MyIndexChangeListener implements IndexChangedListener {
1185N/A List<String> files = new ArrayList<String>();
1185N/A
1185N/A @Override
1185N/A public void fileAdd(String path, String analyzer) {
1185N/A // dummy
1190N/A }
1185N/A @Override
1185N/A public void fileAdded(String path, String analyzer) {
1185N/A files.add(path);
1185N/A }
1190N/A @Override
58N/A public void fileRemove(String path) {
58N/A // dummy
58N/A }
937N/A @Override
58N/A public void fileUpdate(String path) {
58N/A // dummy
58N/A }
937N/A @Override
816N/A public void fileRemoved(String path) {
816N/A // dummy
816N/A }
816N/A }
816N/A
816N/A @SuppressWarnings("javadoc")
816N/A @Test
816N/A public void testRFE2575() throws Exception {
816N/A Configuration cfg = RuntimeEnvironment.getConfig();
816N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
816N/A Configuration.CTAGS_CMD_FALLBACK));
816N/A cfg.setSourceRoot(repository.getSourceRoot());
816N/A cfg.setDataRoot(repository.getDataRoot());
816N/A HistoryGuru.getInstance().addRepositories(repository.getSourceRoot());
816N/A
773N/A List<RepositoryInfo> repos = cfg.getRepositories();
773N/A Repository r = null;
773N/A for (RepositoryInfo ri : repos) {
773N/A if (ri.getDirectoryName().equals(repository.getSourceRoot() + "/rfe2575")) {
773N/A r = RepositoryFactory.getRepository(ri);
773N/A break;
58N/A }
58N/A }
58N/A
773N/A if (r != null && r.isWorking()
773N/A && RuntimeEnvironment.validateExuberantCtags())
773N/A {
773N/A Project project = new Project();
773N/A project.setPath("/rfe2575");
58N/A IndexDatabase idb = new IndexDatabase(project);
58N/A assertNotNull(idb);
58N/A MyIndexChangeListener listener = new MyIndexChangeListener();
773N/A idb.addIndexChangedListener(listener);
773N/A idb.update();
773N/A assertEquals(2, listener.files.size());
773N/A repository.purgeData();
773N/A cfg.setIndexVersionedFilesOnly(true);
773N/A idb = new IndexDatabase(project);
773N/A listener = new MyIndexChangeListener();
773N/A idb.addIndexChangedListener(listener);
773N/A idb.update();
58N/A assertEquals(1, listener.files.size());
58N/A cfg.setIndexVersionedFilesOnly(false);
58N/A } else {
773N/A System.out.println("Skipping test. Repository for rfe2575 not found"
773N/A + " or could not find a ctags or an sccs I could use in path.");
773N/A }
773N/A }
773N/A
773N/A @SuppressWarnings("javadoc")
773N/A @Test
58N/A public void testXref() throws IOException {
58N/A List<File> files = new ArrayList<File>();
58N/A FileUtilities.getAllFiles(new File(repository.getSourceRoot()), files, false);
773N/A for (File f : files) {
773N/A FileAnalyzerFactory factory = AnalyzerGuru.find(f.getAbsolutePath());
773N/A if (factory == null) {
773N/A continue;
773N/A }
773N/A FileReader in = new FileReader(f);
773N/A StringWriter out = new StringWriter();
773N/A try {
773N/A AnalyzerGuru.writeXref(factory, in,
773N/A new XrefWriter(out), null, null, null);
773N/A } catch (UnsupportedOperationException exp) {
773N/A // ignore
773N/A }
773N/A in.close();
773N/A out.close();
773N/A }
773N/A }
773N/A
773N/A @SuppressWarnings("javadoc")
773N/A @Test
773N/A public void testBug3430() throws Exception {
773N/A Configuration cfg = RuntimeEnvironment.getConfig();
773N/A cfg.setCtags(System.getProperty(Configuration.CTAGS_CMD_PROPERTY_KEY,
937N/A Configuration.CTAGS_CMD_FALLBACK));
58N/A cfg.setSourceRoot(repository.getSourceRoot());
58N/A cfg.setDataRoot(repository.getDataRoot());
58N/A
937N/A if (RuntimeEnvironment.validateExuberantCtags()) {
58N/A Project project = new Project();
58N/A project.setPath("/bug3430");
58N/A IndexDatabase idb = new IndexDatabase(project);
937N/A assertNotNull(idb);
58N/A MyIndexChangeListener listener = new MyIndexChangeListener();
58N/A idb.addIndexChangedListener(listener);
58N/A idb.update();
937N/A assertEquals(1, listener.files.size());
58N/A } else {
58N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
58N/A }
937N/A }
58N/A
58N/A @SuppressWarnings("javadoc")
58N/A @Test
937N/A public void testBug11896() throws Exception {
58N/A if (FileUtilities.findProgInPath("mkfifo") == null) {
58N/A System.out.println("Skipping test for bug 11896. Could not find a mkfifo in path.");
58N/A return;
937N/A }
664N/A
58N/A Configuration cfg = RuntimeEnvironment.getConfig();
58N/A cfg.setSourceRoot(repository.getSourceRoot());
937N/A cfg.setDataRoot(repository.getDataRoot());
664N/A Executor executor;
58N/A
58N/A executor = new Executor(new String[] { "mkdir", "-p",
937N/A repository.getSourceRoot() + "/testBug11896" });
58N/A executor.exec(true);
58N/A
58N/A executor = new Executor(new String[] { "mkfifo",
937N/A repository.getSourceRoot() + "/testBug11896/FIFO" });
1185N/A executor.exec(true);
1252N/A
1252N/A if (RuntimeEnvironment.validateExuberantCtags()) {
1252N/A Project project = new Project();
1252N/A project.setPath("/testBug11896");
1185N/A IndexDatabase idb = new IndexDatabase(project);
1185N/A assertNotNull(idb);
58N/A MyIndexChangeListener listener = new MyIndexChangeListener();
58N/A idb.addIndexChangedListener(listener);
58N/A System.out.println("Trying to index a special file - FIFO in this case.");
937N/A idb.update();
65N/A assertEquals(0, listener.files.size());
65N/A } else {
65N/A System.out.println("Skipping test. Could not find a ctags I could use in path.");
937N/A }
65N/A }
65N/A}
65N/A