Search in sources :

Example 31 with EnabledOnOs

use of org.junit.jupiter.api.condition.EnabledOnOs in project OpenGrok by OpenGrok.

the class FileHistoryCacheTest method testRenameFileThenDoIncrementalReindex.

/**
 * Check how incremental reindex behaves when indexing changesets that
 * rename+change file.
 *
 * The scenario goes as follows:
 * - create Mercurial repository
 * - perform full reindex
 * - add changesets which renamed and modify a file
 * - perform incremental reindex
 * - change+rename the file again
 * - incremental reindex
 */
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
@EnabledForRepository(MERCURIAL)
@Test
void testRenameFileThenDoIncrementalReindex() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    History updatedHistory;
    // The test expects support for renamed files.
    env.setHandleHistoryOfRenamedFiles(true);
    // Use tags for better coverage.
    env.setTagsEnabled(true);
    // 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);
    // Import changesets which rename one of the files in the repository.
    MercurialRepositoryTest.runHgCommand(reposRoot, "import", Paths.get(getClass().getResource("/history/hg-export-renamed.txt").toURI()).toString());
    // Perform incremental reindex.
    repo.createCache(cache, cache.getLatestCachedRevision(repo));
    // Verify size of complete history for the directory.
    updatedHistory = cache.get(reposRoot, repo, true);
    assertEquals(14, updatedHistory.getHistoryEntries().size());
    // Check changesets for the renames and changes of single file.
    File main2File = new File(reposRoot.toString() + File.separatorChar + "main2.c");
    updatedHistory = cache.get(main2File, repo, false);
    // Changesets e0-e3 were brought in by the import done above.
    HistoryEntry e0 = new HistoryEntry("13:e55a793086da", // whole minutes only
    new Date(1245447973L / 60 * 60 * 1000), "xyz", "Do something else", true);
    HistoryEntry e1 = new HistoryEntry("12:97b5392fec0d", // whole minutes only
    new Date(1393515253L / 60 * 60 * 1000), "Vladimir Kotal <Vladimir.Kotal@oracle.com>", "rename2", true);
    HistoryEntry e2 = new HistoryEntry("11:5c203a0bc12b", // whole minutes only
    new Date(1393515291L / 60 * 60 * 1000), "Vladimir Kotal <Vladimir.Kotal@oracle.com>", "rename1", true);
    HistoryEntry e3 = new HistoryEntry("10:1e392ef0b0ed", // whole minutes only
    new Date(1245446973L / 60 * 60 * 1000), "xyz", "Return failure when executed with no arguments", true);
    HistoryEntry e4 = new HistoryEntry("2:585a1b3f2efb", // whole minutes only
    new Date(1218571989L / 60 * 60 * 1000), "Trond Norbye <trond.norbye@sun.com>", "Add lint make target and fix lint warnings", true);
    HistoryEntry e5 = new HistoryEntry("1:f24a5fd7a85d", // whole minutes only
    new Date(1218571413L / 60 * 60 * 1000), "Trond Norbye <trond.norbye@sun.com>", "Created a small dummy program", 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);
    histConstruct.setHistoryEntries(entriesConstruct);
    assertEquals(6, updatedHistory.getHistoryEntries().size());
    assertSameEntries(histConstruct.getHistoryEntries(), updatedHistory.getHistoryEntries(), false);
    // Add some changes and rename the file again.
    MercurialRepositoryTest.runHgCommand(reposRoot, "import", Paths.get(getClass().getResource("/history/hg-export-renamed-again.txt").toURI()).toString());
    // Perform incremental reindex.
    repo.createCache(cache, cache.getLatestCachedRevision(repo));
    HistoryEntry e6 = new HistoryEntry("14:55c41cd4b348", // whole minutes only
    new Date(1489505558L / 60 * 60 * 1000), "Vladimir Kotal <Vladimir.Kotal@oracle.com>", "rename + cstyle", true);
    entriesConstruct = new LinkedList<>();
    entriesConstruct.add(e6);
    entriesConstruct.add(e0);
    entriesConstruct.add(e1);
    entriesConstruct.add(e2);
    entriesConstruct.add(e3);
    entriesConstruct.add(e4);
    entriesConstruct.add(e5);
    histConstruct.setHistoryEntries(entriesConstruct);
    // Check changesets for the renames and changes of single file.
    File main3File = new File(reposRoot.toString() + File.separatorChar + "main3.c");
    updatedHistory = cache.get(main3File, repo, false);
    assertEquals(7, updatedHistory.getHistoryEntries().size());
    assertSameEntries(histConstruct.getHistoryEntries(), updatedHistory.getHistoryEntries(), false);
}
Also used : TestRepository(org.opengrok.indexer.util.TestRepository) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) File(java.io.File) Date(java.util.Date) LinkedList(java.util.LinkedList) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 32 with EnabledOnOs

use of org.junit.jupiter.api.condition.EnabledOnOs 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).
 */
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
@EnabledForRepository(MERCURIAL)
@Test
void testRenamedFilePlusChangesBranched() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    History updatedHistory;
    // The test expects support for renamed files.
    env.setHandleHistoryOfRenamedFiles(true);
    // Use tags for better coverage.
    env.setTagsEnabled(true);
    // Branch the repo and add one changeset.
    runHgCommand(reposRoot, "unbundle", Paths.get(getClass().getResource("/history/hg-branch.bundle").toURI()).toString());
    // Import changesets which rename one of the files in the default branch.
    runHgCommand(reposRoot, "import", Paths.get(getClass().getResource("/history/hg-export-renamed.txt").toURI()).toString());
    // 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", Paths.get(getClass().getResource("/history/hg-export-renamed-branched.txt").toURI()).toString());
    // 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>", "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>", "branched", true);
    HistoryEntry e2 = new HistoryEntry("8:6a8c423f5624", // whole minutes only
    new Date(1362586899L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", "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>", "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>", "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>", "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>", "first change", true);
    HistoryEntry e7 = new HistoryEntry("3:2058725c1470", // whole minutes only
    new Date(1362586483L / 60 * 60 * 1000), "Vladimir Kotal <vlada@devnull.cz>", "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.opengrok.indexer.util.TestRepository) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) File(java.io.File) Date(java.util.Date) LinkedList(java.util.LinkedList) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 33 with EnabledOnOs

use of org.junit.jupiter.api.condition.EnabledOnOs in project OpenGrok by OpenGrok.

the class FileHistoryCacheTest method testStoreAndGet.

/**
 * Basic tests for the {@code store()} and {@code get()} methods.
 */
@Test
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
@EnabledForRepository(MERCURIAL)
void testStoreAndGet() throws Exception {
    File reposRoot = new File(repositories.getSourceRoot(), "mercurial");
    // The test expects support for renamed files.
    env.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(2, entries.size(), "Unexpected number of entries");
    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(6, entries.size(), "Unexpected number of entries");
    // 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", Paths.get(getClass().getResource("/history/hg-export.txt").toURI()).toString());
    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", "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", "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
    assertTrue(dir.list().length > 0);
    updatedHistory = cache.get(reposRoot, repo, true);
    assertSameEntries(updatedHistory.getHistoryEntries(), cache.get(reposRoot, repo, true).getHistoryEntries(), true);
}
Also used : TestRepository(org.opengrok.indexer.util.TestRepository) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) File(java.io.File) Date(java.util.Date) LinkedList(java.util.LinkedList) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 34 with EnabledOnOs

use of org.junit.jupiter.api.condition.EnabledOnOs in project OpenGrok by OpenGrok.

the class PathUtilsTest method shouldHandleLinksOfArbitraryDepthWithValidation.

@Test
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
public void shouldHandleLinksOfArbitraryDepthWithValidation() throws IOException, ForbiddenSymlinkException {
    // Create real directories
    File sourceRoot = createTemporaryDirectory("srcroot");
    assertTrue(sourceRoot.isDirectory(), "sourceRoot.isDirectory()");
    File realDir1 = createTemporaryDirectory("realdir1");
    assertTrue(realDir1.isDirectory(), "realDir1.isDirectory()");
    File realDir1b = new File(realDir1, "b");
    realDir1b.mkdir();
    assertTrue(realDir1b.isDirectory(), "realDir1b.isDirectory()");
    File realDir2 = createTemporaryDirectory("realdir2");
    assertTrue(realDir2.isDirectory(), "realDir2.isDirectory()");
    // Create symlink #1 underneath source root (srcroot/symlink1 -> realdir1)
    final String SYMLINK1 = "symlink1";
    File symlink1 = new File(sourceRoot, SYMLINK1);
    Files.createSymbolicLink(Paths.get(symlink1.getPath()), Paths.get(realDir1.getPath()));
    assertTrue(symlink1.exists(), "symlink1.exists()");
    // Create symlink #2 underneath realdir1/b (realdir1/b/symlink2 -> realdir2)
    final String SYMLINK2 = "symlink2";
    File symlink2 = new File(realDir1b, SYMLINK2);
    Files.createSymbolicLink(Paths.get(symlink2.getPath()), Paths.get(realDir2.getPath()));
    assertTrue(symlink2.exists(), "symlink2.exists()");
    // Assert symbolic path srcroot/symlink1/b/symlink2
    Path sympath = Paths.get(sourceRoot.getPath(), SYMLINK1, "b", SYMLINK2);
    assertTrue(Files.exists(sympath), "2-link path exists");
    // Test v. realDir1 canonical
    String realDir1Canon = realDir1.getCanonicalPath();
    String rel = relativeToCanonical(sympath.toString(), realDir1Canon);
    assertEquals("b/" + SYMLINK2, rel, "because links aren't validated");
    // Test v. realDir1 canonical with validation and no allowed links
    Set<String> allowedSymLinks = new HashSet<>();
    ForbiddenSymlinkException expex = null;
    try {
        relativeToCanonical(sympath.toString(), realDir1Canon, allowedSymLinks, null);
    } catch (ForbiddenSymlinkException e) {
        expex = e;
    }
    assertNotNull(expex, "because no links allowed, arg1 is returned fully");
    // Test v. realDir1 canonical with validation and an allowed link
    allowedSymLinks.add(symlink2.getPath());
    rel = relativeToCanonical(sympath.toString(), realDir1Canon, allowedSymLinks, null);
    assertEquals("b/" + SYMLINK2, rel, "because link is OKed");
}
Also used : Path(java.nio.file.Path) File(java.io.File) HashSet(java.util.HashSet) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Example 35 with EnabledOnOs

use of org.junit.jupiter.api.condition.EnabledOnOs in project OpenGrok by OpenGrok.

the class PageConfigTest method testGetSortedFilesDirsFirst.

@SuppressWarnings("ResultOfMethodCallIgnored")
@EnabledOnOs({ OS.LINUX, OS.MAC, OS.SOLARIS, OS.AIX, OS.OTHER })
@Test
void testGetSortedFilesDirsFirst() throws IOException {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    env.setListDirsFirst(true);
    // Cannot spy/mock final class.
    HttpServletRequest req = createRequest("/source", "/xref", "");
    PageConfig pageConfig = PageConfig.get(req);
    // Make sure the source root has just directories.
    File sourceRootFile = new File(repository.getSourceRoot());
    assertTrue(Arrays.stream(sourceRootFile.listFiles()).filter(File::isFile).collect(Collectors.toSet()).isEmpty());
    // Create regular file under source root.
    File file = new File(sourceRootFile, "foo.txt");
    assertTrue(file.createNewFile());
    assertTrue(file.isFile());
    // Make sure the regular file is last.
    List<String> entries = pageConfig.getSortedFiles(sourceRootFile.listFiles());
    assertNotNull(entries);
    assertFalse(entries.isEmpty());
    int numEntries = entries.size();
    assertEquals("foo.txt", entries.get(entries.size() - 1));
    // Create symbolic link to non-existent target.
    Path link = Path.of(sourceRootFile.getCanonicalPath(), "link");
    Path target = Paths.get("/nonexistent");
    Files.createSymbolicLink(link, target);
    // Check the symlink was sorted as file.
    entries = pageConfig.getSortedFiles(sourceRootFile.listFiles());
    assertNotNull(entries);
    assertFalse(entries.isEmpty());
    assertEquals(numEntries + 1, entries.size());
    assertEquals("link", entries.get(entries.size() - 1));
    // Cleanup.
    file.delete();
    link.toFile().delete();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequest(org.opengrok.indexer.web.DummyHttpServletRequest) Path(java.nio.file.Path) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) File(java.io.File) EnabledOnOs(org.junit.jupiter.api.condition.EnabledOnOs) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)36 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)36 File (java.io.File)14 Path (java.nio.file.Path)7 URI (java.net.URI)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)5 TestRepository (org.opengrok.indexer.util.TestRepository)5 URL (java.net.URL)3 URLClassLoader (java.net.URLClassLoader)3 Date (java.util.Date)3 LinkedList (java.util.LinkedList)3 StringBinding (javafx.beans.binding.StringBinding)3 DisplayName (org.junit.jupiter.api.DisplayName)3 EnabledIf (org.junit.jupiter.api.condition.EnabledIf)3 CommandLine (com.thoughtworks.go.util.command.CommandLine)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HashSet (java.util.HashSet)2 DummyHttpServletRequest (org.opengrok.indexer.web.DummyHttpServletRequest)2 NantTask (com.thoughtworks.go.config.NantTask)1