Search in sources :

Example 6 with EnabledForRepository

use of org.opengrok.indexer.condition.EnabledForRepository in project OpenGrok by OpenGrok.

the class HistoryContextTest method testGetContext4args.

@Test
@EnabledForRepository(MERCURIAL)
public void testGetContext4args() throws Exception {
    String path = "/mercurial/Makefile";
    Path file = Paths.get(repositories.getSourceRoot(), "mercurial", "Makefile");
    String parent = file.getParent().toString();
    String base = file.getFileName().toString();
    // Construct a query equivalent to hist:dummy
    TermQuery q1 = new TermQuery(new Term("hist", "dummy"));
    StringWriter sw = new StringWriter();
    assertTrue(new HistoryContext(q1).getContext(parent, base, path, sw, null), parent + " has context");
    assertTrue(sw.toString().contains("Created a small <b>dummy</b> program"));
    // Construct a query equivalent to hist:"dummy program"
    PhraseQuery.Builder q2 = new PhraseQuery.Builder();
    q2.add(new Term("hist", "dummy"));
    q2.add(new Term("hist", "program"));
    sw = new StringWriter();
    assertTrue(new HistoryContext(q2.build()).getContext(parent, base, path, sw, null), parent + " has context");
    assertTrue(sw.toString().contains("Created a small <b>dummy program</b>"));
    // Search for a term that doesn't exist
    TermQuery q3 = new TermQuery(new Term("hist", "term_does_not_exist"));
    sw = new StringWriter();
    assertFalse(new HistoryContext(q3).getContext(parent, base, path, sw, null), parent + " has no context");
    assertEquals("", sw.toString());
    // Search for term with multiple hits - hist:small OR hist:target
    BooleanQuery.Builder q4 = new BooleanQuery.Builder();
    q4.add(new TermQuery(new Term("hist", "small")), Occur.SHOULD);
    q4.add(new TermQuery(new Term("hist", "target")), Occur.SHOULD);
    sw = new StringWriter();
    assertTrue(new HistoryContext(q4.build()).getContext(parent, base, path, sw, null), parent + " has context");
    String result = sw.toString();
    assertTrue(result.contains("Add lint make <b>target</b> and fix lint warnings"));
    assertTrue(result.contains("Created a <b>small</b> dummy program"));
}
Also used : Path(java.nio.file.Path) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StringWriter(java.io.StringWriter) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term) Test(org.junit.jupiter.api.Test) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 7 with EnabledForRepository

use of org.opengrok.indexer.condition.EnabledForRepository in project OpenGrok by OpenGrok.

the class RepositoryFactoryTest method testMercurialRepositoryWhenDisabled.

@EnabledForRepository(RepositoryInstalled.Type.MERCURIAL)
@Test
public void testMercurialRepositoryWhenDisabled() throws IllegalAccessException, InvocationTargetException, ForbiddenSymlinkException, InstantiationException, NoSuchMethodException, IOException {
    env.setDisabledRepositories(new HashSet<>(Collections.singletonList("MercurialRepository")));
    File root = new File(repository.getSourceRoot(), "mercurial");
    env.setSourceRoot(root.getAbsolutePath());
    assertNull(RepositoryFactory.getRepository(root), "should not get repository for mercurial/ if disabled");
    List<Class<? extends Repository>> clazzes = RepositoryFactory.getRepositoryClasses();
    assertFalse(clazzes.contains(MercurialRepository.class), "should not contain MercurialRepository");
}
Also used : EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) TestRepository(org.opengrok.indexer.util.TestRepository) File(java.io.File) Test(org.junit.jupiter.api.Test) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 8 with EnabledForRepository

use of org.opengrok.indexer.condition.EnabledForRepository in project OpenGrok by OpenGrok.

the class HistoryGuruTest method testAddSubRepository.

@Test
@EnabledForRepository(MERCURIAL)
void testAddSubRepository() {
    HistoryGuru instance = HistoryGuru.getInstance();
    // Clone a Mercurial repository underneath a Mercurial repository.
    File hgRoot = new File(repository.getSourceRoot(), "mercurial");
    assertTrue(hgRoot.exists());
    assertTrue(hgRoot.isDirectory());
    MercurialRepositoryTest.runHgCommand(hgRoot, "clone", hgRoot.getAbsolutePath(), "subrepo");
    Collection<RepositoryInfo> addedRepos = instance.addRepositories(Collections.singleton(Paths.get(repository.getSourceRoot(), "mercurial").toString()));
    assertEquals(2, addedRepos.size());
}
Also used : File(java.io.File) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 9 with EnabledForRepository

use of org.opengrok.indexer.condition.EnabledForRepository 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.
 */
@Test
@EnabledForRepository(MERCURIAL)
void testRemoveFileOnFileChange() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    String path = "/mercurial/bar.txt";
    TestRepository testrepo = new TestRepository();
    testrepo.create(HistoryGuru.class.getResource("/repositories"));
    env.setSourceRoot(testrepo.getSourceRoot());
    env.setDataRoot(testrepo.getDataRoot());
    env.setRepositories(testrepo.getSourceRoot());
    // Create history cache.
    Indexer.getInstance().prepareIndexer(env, true, true, false, null, List.of("mercurial"));
    File historyFile = new File(env.getDataRootPath(), TandemPath.join("historycache" + path, ".gz"));
    assertTrue(historyFile.exists(), String.format("history cache for %s has to exist", path));
    // create index
    Project project = new Project("mercurial", "/mercurial");
    IndexDatabase idb = new IndexDatabase(project);
    assertNotNull(idb);
    RemoveIndexChangeListener listener = new RemoveIndexChangeListener(path);
    idb.addIndexChangedListener(listener);
    idb.update();
    assertEquals(5, listener.filesToAdd.size());
    listener.reset();
    // Change a file so that it gets picked up by the indexer.
    File bar = new File(testrepo.getSourceRoot() + File.separator + "mercurial", "bar.txt");
    assertTrue(bar.exists());
    try (BufferedWriter bw = new BufferedWriter(new FileWriter(bar, true))) {
        bw.write("foo\n");
    }
    // reindex
    idb.update();
    // 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.opengrok.indexer.configuration.Project) TestRepository(org.opengrok.indexer.util.TestRepository) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) FileWriter(java.io.FileWriter) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) File(java.io.File) BufferedWriter(java.io.BufferedWriter) Test(org.junit.jupiter.api.Test) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Example 10 with EnabledForRepository

use of org.opengrok.indexer.condition.EnabledForRepository in project OpenGrok by OpenGrok.

the class IndexerTest method testSetRepositories.

@Test
@EnabledForRepository(MERCURIAL)
void testSetRepositories() throws Exception {
    RuntimeEnvironment env = RuntimeEnvironment.getInstance();
    TestRepository testrepo = new TestRepository();
    testrepo.create(HistoryGuru.class.getResource("/repositories"));
    env.setSourceRoot(testrepo.getSourceRoot());
    env.setRepositories(testrepo.getSourceRoot());
    assertEquals(7, env.getRepositories().size());
    String[] repoNames = { "mercurial", "git" };
    env.setRepositories(Arrays.stream(repoNames).map(t -> Paths.get(env.getSourceRootPath(), t).toString()).distinct().toArray(String[]::new));
    assertEquals(2, env.getRepositories().size());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) BeforeEach(org.junit.jupiter.api.BeforeEach) Repository(org.opengrok.indexer.history.Repository) RepositoryFactory(org.opengrok.indexer.history.RepositoryFactory) Arrays(java.util.Arrays) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) Project(org.opengrok.indexer.configuration.Project) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Executor(org.opengrok.indexer.util.Executor) TandemPath(org.opengrok.indexer.util.TandemPath) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) RepositoryInfo(org.opengrok.indexer.history.RepositoryInfo) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) FileUtilities(org.opengrok.indexer.util.FileUtilities) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Path(java.nio.file.Path) TestRepository(org.opengrok.indexer.util.TestRepository) CommandTimeoutType(org.opengrok.indexer.configuration.CommandTimeoutType) EnabledIf(org.junit.jupiter.api.condition.EnabledIf) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository) AnalyzerGuru(org.opengrok.indexer.analysis.AnalyzerGuru) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) StringWriter(java.io.StringWriter) FileWriter(java.io.FileWriter) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) MERCURIAL(org.opengrok.indexer.condition.RepositoryInstalled.Type.MERCURIAL) FileReader(java.io.FileReader) Queue(java.util.Queue) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Collections(java.util.Collections) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) AnalyzerFactory(org.opengrok.indexer.analysis.AnalyzerFactory) TestRepository(org.opengrok.indexer.util.TestRepository) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) HistoryGuru(org.opengrok.indexer.history.HistoryGuru) Test(org.junit.jupiter.api.Test) EnabledForRepository(org.opengrok.indexer.condition.EnabledForRepository)

Aggregations

Test (org.junit.jupiter.api.Test)21 EnabledForRepository (org.opengrok.indexer.condition.EnabledForRepository)21 File (java.io.File)19 TestRepository (org.opengrok.indexer.util.TestRepository)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)12 LinkedList (java.util.LinkedList)5 EnabledOnOs (org.junit.jupiter.api.condition.EnabledOnOs)5 Path (java.nio.file.Path)3 Date (java.util.Date)3 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)3 BufferedWriter (java.io.BufferedWriter)2 FileWriter (java.io.FileWriter)2 StringWriter (java.io.StringWriter)2 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 Project (org.opengrok.indexer.configuration.Project)2 HistoryGuru (org.opengrok.indexer.history.HistoryGuru)2