Skip to content

Commit 0209ca0

Browse files
committed
add createHistoryCache() test
1 parent ccefc97 commit 0209ca0

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-1
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ private HistoryGuru() {
135135
repositoryLookup = RepositoryLookup.cached();
136136
}
137137

138+
@VisibleForTesting
139+
HistoryCache getHistoryCache() {
140+
return historyCache;
141+
}
142+
138143
/**
139144
* Set annotation cache to its default implementation.
140145
* @return {@link AnnotationCache} instance or {@code null} on error
@@ -1048,7 +1053,6 @@ public void storeHistory(File file, History history) {
10481053
}
10491054

10501055
private void createHistoryCache(Repository repository, String sinceRevision) throws CacheException, HistoryException {
1051-
// TODO: add test for this
10521056
if (!repository.isHistoryCacheEnabled()) {
10531057
LOGGER.log(Level.INFO,
10541058
"Skipping history cache creation for {0} and its subdirectories: history cache disabled",
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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

Comments
 (0)