Skip to content

Commit 3876c1a

Browse files
authored
add test for PageConfig#findDataFiles() (#4861)
1 parent 6f60ee6 commit 3876c1a

File tree

3 files changed

+117
-7
lines changed

3 files changed

+117
-7
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/util/IOUtils.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
2222
* Copyright (c) 2011, Trond Norbye.
2323
* Portions Copyright (c) 2017, 2021, Chris Fraire <[email protected]>.
2424
*/
@@ -44,6 +44,8 @@
4444
import java.util.Map;
4545
import java.util.logging.Level;
4646
import java.util.logging.Logger;
47+
48+
import org.jetbrains.annotations.NotNull;
4749
import org.opengrok.indexer.logger.LoggerFactory;
4850

4951
/**
@@ -83,21 +85,21 @@ public static void close(Closeable c) {
8385
public static void removeRecursive(Path path) throws IOException {
8486
Files.walkFileTree(path, new SimpleFileVisitor<>() {
8587
@Override
86-
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
88+
public @NotNull FileVisitResult visitFile(Path file, @NotNull BasicFileAttributes attrs)
8789
throws IOException {
8890
Files.delete(file);
8991
return FileVisitResult.CONTINUE;
9092
}
9193

9294
@Override
93-
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
95+
public @NotNull FileVisitResult visitFileFailed(Path file, @NotNull IOException exc) throws IOException {
9496
// Try to delete the file anyway.
9597
Files.delete(file);
9698
return FileVisitResult.CONTINUE;
9799
}
98100

99101
@Override
100-
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
102+
public @NotNull FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
101103
if (exc == null) {
102104
Files.delete(dir);
103105
return FileVisitResult.CONTINUE;

opengrok-web/src/main/java/org/opengrok/web/PageConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,7 @@ private File checkFileResolve(File dir, String name, boolean compressed) {
12901290
* {@code ".gz"} to the filename. If that fails or an uncompressed version of the
12911291
* file is younger than its compressed version, the uncompressed file gets used.
12921292
*
1293-
* @param filenames filenames to lookup.
1293+
* @param filenames filenames to lookup, relative to source root.
12941294
* @return an empty array if the related directory does not exist or the
12951295
* given list is {@code null} or empty, otherwise an array, which may
12961296
* contain {@code null} entries (when the related file could not be found)

opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@
6464
import org.opengrok.indexer.history.RepositoryFactory;
6565
import org.opengrok.indexer.index.Indexer;
6666
import org.opengrok.indexer.search.QueryBuilder;
67+
import org.opengrok.indexer.util.IOUtils;
6768
import org.opengrok.indexer.util.TestRepository;
6869
import org.opengrok.indexer.web.DummyHttpServletRequest;
6970
import org.opengrok.indexer.web.QueryParameters;
7071
import org.opengrok.indexer.web.SortOrder;
7172

73+
import static org.junit.jupiter.api.Assertions.assertAll;
7274
import static org.junit.jupiter.api.Assertions.assertEquals;
7375
import static org.junit.jupiter.api.Assertions.assertFalse;
7476
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -367,6 +369,10 @@ void testGetLatestRevisionViaIndex() throws Exception {
367369
String rev = LatestRevisionUtil.getLastRevFromIndex(new File(repository.getSourceRoot(), filePath));
368370
assertNotNull(rev);
369371
assertEquals(HASH_AA35C258, rev);
372+
373+
// cleanup
374+
env.releaseIndexSearchers();
375+
IOUtils.removeRecursive(env.getDataRootFile().toPath());
370376
}
371377

372378
@Test
@@ -683,7 +689,7 @@ public String getPathInfo() {
683689

684690
@ParameterizedTest
685691
@ValueSource(booleans = {true, false})
686-
void testIsNotModifiedEtag(boolean createTimestamp) throws IOException {
692+
void testIsNotModifiedEtag(boolean createTimestamp) throws Exception {
687693
HttpServletRequest req = new DummyHttpServletRequest() {
688694
@Override
689695
public String getHeader(String name) {
@@ -699,8 +705,25 @@ public String getPathInfo() {
699705
}
700706
};
701707

702-
// The ETag value depends on the timestamp file.
708+
// Need data root to be populated for the isNotModified() check, so index first.
703709
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
710+
env.setSourceRoot(repository.getSourceRoot());
711+
env.setDataRoot(repository.getDataRoot());
712+
env.setProjectsEnabled(true);
713+
env.setHistoryEnabled(true);
714+
RepositoryFactory.initializeIgnoredNames(env);
715+
716+
Indexer indexer = Indexer.getInstance();
717+
indexer.prepareIndexer(
718+
env,
719+
true, // search for repositories
720+
true, // scan and add projects
721+
// don't create dictionary
722+
null, // subFiles - needed when refreshing history partially
723+
null); // repositories - needed when refreshing history partially
724+
indexer.doIndexerExecution(null, null);
725+
726+
// The ETag value depends on the timestamp file.
704727
env.refreshDateForLastIndexRun();
705728
Path timestampPath = Path.of(env.getDataRootPath(), IndexTimestamp.TIMESTAMP_FILE_NAME);
706729
Files.deleteIfExists(timestampPath);
@@ -713,6 +736,10 @@ public String getPathInfo() {
713736
HttpServletResponse resp = mock(HttpServletResponse.class);
714737
assertFalse(cfg.isNotModified(req, resp));
715738
verify(resp).setHeader(eq(HttpHeaders.ETAG), startsWith("W/"));
739+
740+
// cleanup
741+
env.releaseIndexSearchers();
742+
IOUtils.removeRecursive(env.getDataRootFile().toPath());
716743
}
717744

718745
@Test
@@ -924,4 +951,85 @@ public String getContextPath() {
924951
cfg.addScript(scriptName);
925952
assertFalse(cfg.getScripts().getScriptNames().contains(scriptName));
926953
}
954+
955+
@Test
956+
void testFindDataFilesNullOrEmpty() {
957+
HttpServletRequest req = new DummyHttpServletRequest() {
958+
@Override
959+
public String getPathInfo() {
960+
return "path";
961+
}
962+
};
963+
964+
PageConfig cfg = PageConfig.get(req);
965+
assertAll(() -> {
966+
File[] files = cfg.findDataFiles(null);
967+
assertNotNull(files);
968+
assertEquals(0, files.length);
969+
},
970+
() -> {
971+
File[] files = cfg.findDataFiles(new ArrayList<>());
972+
assertNotNull(files);
973+
assertEquals(0, files.length);
974+
});
975+
}
976+
977+
@Test
978+
void testFindDataFilesNonExistent() {
979+
HttpServletRequest req = new DummyHttpServletRequest() {
980+
@Override
981+
public String getPathInfo() {
982+
return "path";
983+
}
984+
};
985+
986+
PageConfig cfg = PageConfig.get(req);
987+
var filenames = List.of("nonexistent1", "nonexistent2");
988+
File[] files = cfg.findDataFiles(filenames);
989+
assertNotNull(files);
990+
assertEquals(2, files.length);
991+
assertTrue(Arrays.stream(files).allMatch(Objects::isNull));
992+
}
993+
994+
@ParameterizedTest
995+
@ValueSource(booleans = {true, false})
996+
void testFindDataFiles(boolean isCompressed) throws Exception {
997+
HttpServletRequest req = new DummyHttpServletRequest() {
998+
@Override
999+
public String getPathInfo() {
1000+
return "mercurial";
1001+
}
1002+
};
1003+
1004+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
1005+
env.setSourceRoot(repository.getSourceRoot());
1006+
env.setDataRoot(repository.getDataRoot());
1007+
env.setProjectsEnabled(true);
1008+
env.setHistoryEnabled(true);
1009+
env.setCompressXref(isCompressed);
1010+
RepositoryFactory.initializeIgnoredNames(env);
1011+
1012+
Indexer indexer = Indexer.getInstance();
1013+
indexer.prepareIndexer(
1014+
env,
1015+
true, // search for repositories
1016+
true, // scan and add projects
1017+
// don't create dictionary
1018+
null, // subFiles - needed when refreshing history partially
1019+
null); // repositories - needed when refreshing history partially
1020+
indexer.doIndexerExecution(null, null);
1021+
1022+
PageConfig cfg = PageConfig.get(req);
1023+
var filenames = List.of("main.c", "nonexistent", "Makefile");
1024+
File[] files = cfg.findDataFiles(filenames);
1025+
assertNotNull(files);
1026+
assertEquals(filenames.size(), files.length);
1027+
assertTrue(files[0].exists());
1028+
assertNull(files[1]);
1029+
assertTrue(files[2].exists());
1030+
1031+
// cleanup
1032+
env.releaseIndexSearchers();
1033+
IOUtils.removeRecursive(env.getDataRootFile().toPath());
1034+
}
9271035
}

0 commit comments

Comments
 (0)