|
| 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.history; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.ValueSource; |
| 29 | +import org.opengrok.indexer.configuration.CommandTimeoutType; |
| 30 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 31 | +import org.opengrok.indexer.util.IOUtils; |
| 32 | +import org.opengrok.indexer.util.TestRepository; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.net.URL; |
| 36 | +import java.nio.file.Path; |
| 37 | +import java.util.Collection; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 44 | + |
| 45 | +class HistoryGuruVsRepositoryCacheTest { |
| 46 | + private RuntimeEnvironment env; |
| 47 | + |
| 48 | + private static TestRepository testRepository; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUpClass() throws Exception { |
| 52 | + env = RuntimeEnvironment.getInstance(); |
| 53 | + |
| 54 | + testRepository = new TestRepository(); |
| 55 | + URL resourceURL = HistoryGuru.class.getResource("/repositories"); |
| 56 | + assertNotNull(resourceURL); |
| 57 | + testRepository.create(resourceURL); |
| 58 | + |
| 59 | + env.setSourceRoot(testRepository.getSourceRoot()); |
| 60 | + env.setDataRoot(testRepository.getDataRoot()); |
| 61 | + env.setHistoryEnabled(true); |
| 62 | + env.setProjectsEnabled(true); |
| 63 | + RepositoryFactory.initializeIgnoredNames(env); |
| 64 | + |
| 65 | + // Restore the project and repository information. |
| 66 | + env.setProjects(new HashMap<>()); |
| 67 | + env.setRepositories(testRepository.getSourceRoot()); |
| 68 | + HistoryGuru.getInstance().invalidateRepositories(env.getRepositories(), CommandTimeoutType.INDEXER); |
| 69 | + env.generateProjectRepositoriesMap(); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterEach |
| 73 | + void tearDownClass() throws Exception { |
| 74 | + testRepository.destroy(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that {@link HistoryGuru#createHistoryCache(Collection)} honors |
| 79 | + * the {@link Repository#isHistoryCacheEnabled()} setting. |
| 80 | + */ |
| 81 | + @ParameterizedTest |
| 82 | + @ValueSource(booleans = {false, true}) |
| 83 | + void testCreateCacheVsRepository(boolean historyCacheEnabled) throws Exception { |
| 84 | + Path filePath = Path.of(env.getSourceRootPath(), "git", "main.c"); |
| 85 | + File file = filePath.toFile(); |
| 86 | + assertTrue(file.exists()); |
| 87 | + HistoryGuru histGuru = HistoryGuru.getInstance(); |
| 88 | + Repository repository = histGuru.getRepository(file); |
| 89 | + assertNotNull(repository); |
| 90 | + |
| 91 | + repository.setHistoryCacheEnabled(historyCacheEnabled); |
| 92 | + histGuru.createHistoryCache(List.of(repository.getDirectoryNameRelative())); |
| 93 | + |
| 94 | + HistoryCache historyCache = histGuru.getHistoryCache(); |
| 95 | + assertNotNull(historyCache); |
| 96 | + assertEquals(historyCacheEnabled, historyCache.hasCacheForFile(file)); |
| 97 | + |
| 98 | + // cleanup |
| 99 | + IOUtils.removeRecursive(Path.of(env.getDataRootPath())); |
| 100 | + } |
| 101 | +} |
0 commit comments