Search in sources :

Example 6 with ConditionalRun

use of org.opensolaris.opengrok.condition.ConditionalRun in project OpenGrok by OpenGrok.

the class FileHistoryCacheTest method testStoreAndGet.

/**
 * Basic tests for the {@code store()} and {@code get()} methods.
 *
 * @throws java.lang.Exception
 */
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
@Test
public void testStoreAndGet() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    // The test expects support for renamed files.
    RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
    Repository repo = RepositoryFactory.getRepository(reposRoot);
    History historyToStore = repo.getHistory(reposRoot);
    cache.store(historyToStore, repo);
    // test reindex
    History historyNull = new History();
    cache.store(historyNull, repo);
    // test get history for single file
    File makefile = new File(reposRoot, "Makefile");
    assertTrue(makefile.exists());
    History retrievedHistory = cache.get(makefile, repo, true);
    List<HistoryEntry> entries = retrievedHistory.getHistoryEntries();
    assertEquals("Unexpected number of entries", 2, entries.size());
    final String TROND = "Trond Norbye <trond.norbye@sun.com>";
    Iterator<HistoryEntry> entryIt = entries.iterator();
    HistoryEntry e1 = entryIt.next();
    assertEquals(TROND, e1.getAuthor());
    assertEquals("2:585a1b3f2efb", e1.getRevision());
    assertEquals(0, e1.getFiles().size());
    HistoryEntry e2 = entryIt.next();
    assertEquals(TROND, e2.getAuthor());
    assertEquals("1:f24a5fd7a85d", e2.getRevision());
    assertEquals(0, e2.getFiles().size());
    assertFalse(entryIt.hasNext());
    // test get history for renamed file
    File novel = new File(reposRoot, "novel.txt");
    assertTrue(novel.exists());
    retrievedHistory = cache.get(novel, repo, true);
    entries = retrievedHistory.getHistoryEntries();
    assertEquals("Unexpected number of entries", 6, entries.size());
    // test get history for directory
    // Need to refresh history to store since the file lists were stripped
    // from it in the call to cache.store() above.
    historyToStore = repo.getHistory(reposRoot);
    History dirHistory = cache.get(reposRoot, repo, true);
    assertSameEntries(historyToStore.getHistoryEntries(), dirHistory.getHistoryEntries(), true);
    // test incremental update
    MercurialRepositoryTest.runHgCommand(reposRoot, "import", getClass().getResource("hg-export.txt").getPath());
    repo.createCache(cache, cache.getLatestCachedRevision(repo));
    History updatedHistory = cache.get(reposRoot, repo, true);
    HistoryEntry newEntry1 = new HistoryEntry("10:1e392ef0b0ed", // whole minutes only
    new Date(1245446973L / 60 * 60 * 1000), "xyz", null, "Return failure when executed with no arguments", true);
    newEntry1.addFile("/mercurial/main.c");
    HistoryEntry newEntry2 = new HistoryEntry("11:bbb3ce75e1b8", // whole minutes only
    new Date(1245447973L / 60 * 60 * 1000), "xyz", null, "Do something else", true);
    newEntry2.addFile("/mercurial/main.c");
    LinkedList<HistoryEntry> updatedEntries = new LinkedList<>(updatedHistory.getHistoryEntries());
    // The history for retrieved for the whole directory so it will contain
    // lists of files so we need to set isdir to true.
    assertSameEntry(newEntry2, updatedEntries.removeFirst(), true);
    assertSameEntry(newEntry1, updatedEntries.removeFirst(), true);
    assertSameEntries(historyToStore.getHistoryEntries(), updatedEntries, true);
    // test clearing of cache
    File dir = new File(cache.getRepositoryHistDataDirname(repo));
    assertTrue(dir.isDirectory());
    cache.clear(repo);
    // We cannot call cache.get() here since it would read the history anew.
    // Instead check that the data directory does not exist anymore.
    assertFalse(dir.exists());
    cache.store(historyToStore, repo);
    // check that the data directory is non-empty
    assertEquals(true, dir.list().length > 0);
    updatedHistory = cache.get(reposRoot, repo, true);
    assertSameEntries(updatedHistory.getHistoryEntries(), cache.get(reposRoot, repo, true).getHistoryEntries(), true);
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) File(java.io.File) Date(java.util.Date) LinkedList(java.util.LinkedList) ConditionalRun(org.opensolaris.opengrok.condition.ConditionalRun) Test(org.junit.Test)

Example 7 with ConditionalRun

use of org.opensolaris.opengrok.condition.ConditionalRun in project OpenGrok by OpenGrok.

the class FileHistoryCacheTest method testRenamedFilePlusChangesBranched.

/**
 * Make sure generating incremental history index in branched repository
 * with renamed file produces correct history for the renamed file
 * (i.e. there should not be history entries from the default branch made
 * there after the branch was created).
 *
 * @throws Exception
 */
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
@Test
public void testRenamedFilePlusChangesBranched() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    History updatedHistory;
    // The test expects support for renamed files.
    RuntimeEnvironment.getInstance().setHandleHistoryOfRenamedFiles(true);
    // Use tags for better coverage.
    RuntimeEnvironment.getInstance().setTagsEnabled(true);
    // Branch the repo and add one changeset.
    runHgCommand(reposRoot, "unbundle", getClass().getResource("hg-branch.bundle").getPath());
    // Import changesets which rename one of the files in the default branch.
    runHgCommand(reposRoot, "import", getClass().getResource("hg-export-renamed.txt").getPath());
    // Switch to the newly created branch.
    runHgCommand(reposRoot, "update", "mybranch");
    // Generate history index.
    // It is necessary to call getRepository() only after tags were enabled
    // to produce list of tags.
    Repository repo = RepositoryFactory.getRepository(reposRoot);
    History historyToStore = repo.getHistory(reposRoot);
    cache.store(historyToStore, repo);
    /* quick sanity check */
    updatedHistory = cache.get(reposRoot, repo, true);
    assertEquals(11, updatedHistory.getHistoryEntries().size());
    // Import changesets which rename the file in the new branch.
    runHgCommand(reposRoot, "import", getClass().getResource("hg-export-renamed-branched.txt").getPath());
    // Perform incremental reindex.
    repo.createCache(cache, cache.getLatestCachedRevision(repo));
    /* overall history check */
    updatedHistory = cache.get(reposRoot, repo, false);
    assertEquals(12, updatedHistory.getHistoryEntries().size());
    // Check complete list of history entries for the renamed file.
    File testFile = new File(reposRoot.toString() + File.separatorChar + "blog.txt");
    updatedHistory = cache.get(testFile, repo, false);
    HistoryEntry e0 = new HistoryEntry("15:709c7a27f9fa", // whole minutes only
    new Date(1489160275L / 60 * 60 * 1000), "Vladimir Kotal <Vladimir.Kotal@oracle.com>", null, "novels are so last century. Let's write a blog !", true);
    HistoryEntry e1 = new HistoryEntry("10:c4518ca0c841", // whole minutes only
    new Date(1415483555L / 60 * 60 * 1000), "Vladimir Kotal <Vladimir.Kotal@oracle.com>", null, "branched", true);
    HistoryEntry e2 = new HistoryEntry("8:6a8c423f5624", // whole minutes only
    new Date(1362586899L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", null, "first words of the novel", true);
    HistoryEntry e3 = new HistoryEntry("7:db1394c05268", // whole minutes only
    new Date(1362586862L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", "start_of_novel", "book sounds too boring, let's do a novel !", true);
    HistoryEntry e4 = new HistoryEntry("6:e386b51ddbcc", // whole minutes only
    new Date(1362586839L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", null, "stub of chapter 1", true);
    HistoryEntry e5 = new HistoryEntry("5:8706402863c6", // whole minutes only
    new Date(1362586805L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", null, "I decided to actually start writing a book based on the first plaintext file.", true);
    HistoryEntry e6 = new HistoryEntry("4:e494d67af12f", // whole minutes only
    new Date(1362586747L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", null, "first change", true);
    HistoryEntry e7 = new HistoryEntry("3:2058725c1470", // whole minutes only
    new Date(1362586483L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", null, "initial checking of text files", true);
    History histConstruct = new History();
    LinkedList<HistoryEntry> entriesConstruct = new LinkedList<>();
    entriesConstruct.add(e0);
    entriesConstruct.add(e1);
    entriesConstruct.add(e2);
    entriesConstruct.add(e3);
    entriesConstruct.add(e4);
    entriesConstruct.add(e5);
    entriesConstruct.add(e6);
    entriesConstruct.add(e7);
    histConstruct.setHistoryEntries(entriesConstruct);
    assertSameEntries(histConstruct.getHistoryEntries(), updatedHistory.getHistoryEntries(), false);
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) File(java.io.File) Date(java.util.Date) LinkedList(java.util.LinkedList) ConditionalRun(org.opensolaris.opengrok.condition.ConditionalRun) Test(org.junit.Test)

Example 8 with ConditionalRun

use of org.opensolaris.opengrok.condition.ConditionalRun in project OpenGrok by OpenGrok.

the class FileHistoryCacheTest method testStoreAndGetNotRenamed.

/**
 * Basic tests for the {@code store()} method on cache with disabled
 * handling of renamed files.
 *
 * @throws java.lang.Exception
 */
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
@Test
public void testStoreAndGetNotRenamed() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    Repository repo = RepositoryFactory.getRepository(reposRoot);
    History historyToStore = repo.getHistory(reposRoot);
    cache.store(historyToStore, repo);
    // This makes sure that the file which contains the latest revision
    // has indeed been created.
    assertEquals("9:8b340409b3a8", cache.getLatestCachedRevision(repo));
    // test reindex
    History historyNull = new History();
    cache.store(historyNull, repo);
    assertEquals("9:8b340409b3a8", cache.getLatestCachedRevision(repo));
}
Also used : TestRepository(org.opensolaris.opengrok.util.TestRepository) File(java.io.File) ConditionalRun(org.opensolaris.opengrok.condition.ConditionalRun) Test(org.junit.Test)

Example 9 with ConditionalRun

use of org.opensolaris.opengrok.condition.ConditionalRun in project OpenGrok by OpenGrok.

the class IndexerRepoTest method testSymlinks.

/**
 * Test that symlinked directories from source root get their relative
 * path set correctly in RepositoryInfo objects.
 * @throws org.opensolaris.opengrok.index.IndexerException
 * @throws java.io.IOException
 */
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
@Test
public void testSymlinks() throws IndexerException, IOException {
    final String SYMLINK = "symlink";
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    // Set source root to pristine directory so that there is only one
    // repository to deal with (which makes this faster and easier to write)
    // and clone the mercurial repository outside of the source root.
    File realSource = FileUtilities.createTemporaryDirectory("real");
    File sourceRoot = FileUtilities.createTemporaryDirectory("src");
    MercurialRepositoryTest.runHgCommand(sourceRoot, "clone", repository.getSourceRoot() + File.separator + "mercurial", realSource.getPath());
    // Create symlink from source root to the real repository.
    String symlinkPath = sourceRoot.toString() + File.separator + SYMLINK;
    Files.createSymbolicLink(Paths.get(symlinkPath), Paths.get(realSource.getPath()));
    env.setSourceRoot(sourceRoot.toString());
    env.setDataRoot(repository.getDataRoot());
    // Need to have history cache enabled in order to perform scan of repositories.
    env.setHistoryEnabled(true);
    // Normally the Indexer would add the symlink automatically.
    env.setAllowedSymlinks(new HashSet<String>(Arrays.asList(symlinkPath)));
    // Do a rescan of the projects, and only that (we don't care about
    // the other aspects of indexing in this test case).
    Indexer.getInstance().prepareIndexer(env, // search for repositories
    true, // scan and add projects
    true, // no default project
    null, // don't list files
    false, // don't create dictionary
    false, // subFiles - not needed since we don't list files
    null, // repositories - not needed when not refreshing history
    null, // don't zap cache
    new ArrayList<>(), // don't list repos
    false);
    // Check the respository paths.
    List<RepositoryInfo> repos = env.getRepositories();
    assertEquals(repos.size(), 1);
    RepositoryInfo repo = repos.get(0);
    assertEquals("/" + SYMLINK, repo.getDirectoryNameRelative());
    String epath = sourceRoot.toString() + File.separator + SYMLINK;
    String apath = repo.getDirectoryName();
    assertTrue("Should match (with macOS leeway):\n" + epath + "\nv.\n" + apath, epath.equals(apath) || apath.equals("/private" + epath));
    // Check that history exists for a file in the repository.
    File repoRoot = new File(env.getSourceRootFile(), SYMLINK);
    File fileInRepo = new File(repoRoot, "main.c");
    assertTrue(fileInRepo.exists());
    assertTrue(HistoryGuru.getInstance().hasHistory(fileInRepo));
    assertTrue(HistoryGuru.getInstance().hasCacheForFile(fileInRepo));
    // cleanup
    IOUtils.removeRecursive(realSource.toPath());
    IOUtils.removeRecursive(sourceRoot.toPath());
}
Also used : RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) RepositoryInfo(org.opensolaris.opengrok.history.RepositoryInfo) File(java.io.File) ConditionalRun(org.opensolaris.opengrok.condition.ConditionalRun) Test(org.junit.Test) MercurialRepositoryTest(org.opensolaris.opengrok.history.MercurialRepositoryTest)

Example 10 with ConditionalRun

use of org.opensolaris.opengrok.condition.ConditionalRun in project OpenGrok by OpenGrok.

the class IndexerTest method testRemoveFileOnFileChange.

/**
 * Test that reindex after changing a file does not wipe out history index
 * for this file. This is important for the incremental history indexing.
 * @throws Exception
 */
@Test
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
public void testRemoveFileOnFileChange() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    if (!env.validateExuberantCtags()) {
        System.out.println("Skipping test due to no ctags");
    }
    TestRepository testrepo = new TestRepository();
    testrepo.create(HistoryGuru.class.getResourceAsStream("repositories.zip"));
    env.setSourceRoot(testrepo.getSourceRoot());
    env.setDataRoot(testrepo.getDataRoot());
    env.setRepositories(testrepo.getSourceRoot());
    // create index
    Project project = new Project("mercurial", "/mercurial");
    IndexDatabase idb = new IndexDatabase(project);
    assertNotNull(idb);
    RemoveIndexChangeListener listener = new RemoveIndexChangeListener();
    idb.addIndexChangedListener(listener);
    idb.update(parallelizer);
    Assert.assertEquals(5, listener.filesToAdd.size());
    listener.reset();
    // Change a file so that it gets picked up by the indexer.
    Thread.sleep(1100);
    File bar = new File(testrepo.getSourceRoot() + File.separator + "mercurial", "bar.txt");
    Assert.assertTrue(bar.exists());
    FileWriter fw = new FileWriter(bar, true);
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("foo\n");
    bw.close();
    fw.close();
    // reindex
    idb.update(parallelizer);
    // Make sure that the file was actually processed.
    assertEquals(1, listener.removedFiles.size());
    assertEquals(1, listener.filesToAdd.size());
    assertEquals("/mercurial/bar.txt", listener.removedFiles.peek());
    testrepo.destroy();
}
Also used : Project(org.opensolaris.opengrok.configuration.Project) TestRepository(org.opensolaris.opengrok.util.TestRepository) RuntimeEnvironment(org.opensolaris.opengrok.configuration.RuntimeEnvironment) FileWriter(java.io.FileWriter) HistoryGuru(org.opensolaris.opengrok.history.HistoryGuru) File(java.io.File) BufferedWriter(java.io.BufferedWriter) ConditionalRun(org.opensolaris.opengrok.condition.ConditionalRun) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 ConditionalRun (org.opensolaris.opengrok.condition.ConditionalRun)12 File (java.io.File)9 TestRepository (org.opensolaris.opengrok.util.TestRepository)6 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 RuntimeEnvironment (org.opensolaris.opengrok.configuration.RuntimeEnvironment)3 ArrayList (java.util.ArrayList)2 Term (org.apache.lucene.index.Term)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 PhraseQuery (org.apache.lucene.search.PhraseQuery)2 TermQuery (org.apache.lucene.search.TermQuery)2 MercurialRepositoryTest (org.opensolaris.opengrok.history.MercurialRepositoryTest)2 BufferedWriter (java.io.BufferedWriter)1 FileWriter (java.io.FileWriter)1 StringWriter (java.io.StringWriter)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Project (org.opensolaris.opengrok.configuration.Project)1 Annotation (org.opensolaris.opengrok.history.Annotation)1 HistoryGuru (org.opensolaris.opengrok.history.HistoryGuru)1