|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opengrok.indexer.index; |
| 24 | + |
| 25 | +import org.apache.lucene.document.Document; |
| 26 | +import org.apache.lucene.document.TextField; |
| 27 | +import org.junit.jupiter.api.AfterEach; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.params.ParameterizedTest; |
| 30 | +import org.junit.jupiter.params.provider.ValueSource; |
| 31 | +import org.mockito.ArgumentCaptor; |
| 32 | +import org.mockito.Mockito; |
| 33 | +import org.opengrok.indexer.analysis.AbstractAnalyzer; |
| 34 | +import org.opengrok.indexer.analysis.AnalyzerGuru; |
| 35 | +import org.opengrok.indexer.configuration.CommandTimeoutType; |
| 36 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 37 | +import org.opengrok.indexer.history.HistoryGuru; |
| 38 | +import org.opengrok.indexer.history.Repository; |
| 39 | +import org.opengrok.indexer.history.RepositoryFactory; |
| 40 | +import org.opengrok.indexer.search.QueryBuilder; |
| 41 | +import org.opengrok.indexer.util.NullWriter; |
| 42 | +import org.opengrok.indexer.util.TestRepository; |
| 43 | + |
| 44 | +import java.io.File; |
| 45 | +import java.io.Writer; |
| 46 | +import java.net.URL; |
| 47 | +import java.nio.file.Path; |
| 48 | +import java.util.HashMap; |
| 49 | + |
| 50 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 51 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 52 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 53 | +import static org.mockito.Mockito.atLeast; |
| 54 | +import static org.mockito.Mockito.verify; |
| 55 | + |
| 56 | +class AnalyzerGuruDocumentTest { |
| 57 | + private RuntimeEnvironment env; |
| 58 | + |
| 59 | + private static TestRepository testRepository; |
| 60 | + |
| 61 | + @BeforeEach |
| 62 | + void setUpClass() throws Exception { |
| 63 | + env = RuntimeEnvironment.getInstance(); |
| 64 | + |
| 65 | + testRepository = new TestRepository(); |
| 66 | + URL resourceURL = HistoryGuru.class.getResource("/repositories"); |
| 67 | + assertNotNull(resourceURL); |
| 68 | + testRepository.create(resourceURL); |
| 69 | + |
| 70 | + env.setSourceRoot(testRepository.getSourceRoot()); |
| 71 | + env.setDataRoot(testRepository.getDataRoot()); |
| 72 | + env.setHistoryEnabled(true); |
| 73 | + env.setProjectsEnabled(true); |
| 74 | + RepositoryFactory.initializeIgnoredNames(env); |
| 75 | + |
| 76 | + // Restore the project and repository information. |
| 77 | + env.setProjects(new HashMap<>()); |
| 78 | + env.setRepositories(testRepository.getSourceRoot()); |
| 79 | + HistoryGuru.getInstance().invalidateRepositories(env.getRepositories(), CommandTimeoutType.INDEXER); |
| 80 | + env.generateProjectRepositoriesMap(); |
| 81 | + } |
| 82 | + |
| 83 | + @AfterEach |
| 84 | + void tearDownClass() throws Exception { |
| 85 | + testRepository.destroy(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * {@link AnalyzerGuru#populateDocument(Document, File, String, AbstractAnalyzer, Writer)} should populate |
| 90 | + * the history of the document only if the repository related to the file allows for it. |
| 91 | + */ |
| 92 | + @ParameterizedTest |
| 93 | + @ValueSource(booleans = {false, true}) |
| 94 | + void testPopulateDocumentHistory(boolean historyEnabled) throws Exception { |
| 95 | + AnalyzerGuru analyzerGuru = new AnalyzerGuru(); |
| 96 | + Document doc = Mockito.mock(Document.class); |
| 97 | + Path filePath = Path.of(env.getSourceRootPath(), "git", "main.c"); |
| 98 | + File file = filePath.toFile(); |
| 99 | + assertTrue(file.exists()); |
| 100 | + HistoryGuru histGuru = HistoryGuru.getInstance(); |
| 101 | + Repository repository = histGuru.getRepository(file); |
| 102 | + assertNotNull(repository); |
| 103 | + repository.setHistoryEnabled(historyEnabled); |
| 104 | + String relativePath = env.getPathRelativeToSourceRoot(file); |
| 105 | + analyzerGuru.populateDocument(doc, file, relativePath, |
| 106 | + IndexDatabase.getAnalyzerFor(file, relativePath), new NullWriter()); |
| 107 | + ArgumentCaptor<TextField> argument = ArgumentCaptor.forClass(TextField.class); |
| 108 | + verify(doc, atLeast(1)).add(argument.capture()); |
| 109 | + assertEquals(historyEnabled, |
| 110 | + argument.getAllValues().stream().anyMatch(e -> e.name().equals(QueryBuilder.HIST))); |
| 111 | + } |
| 112 | +} |
0 commit comments