diff --git a/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java b/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java index 305105277ae..0e25fc9fcc7 100644 --- a/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java +++ b/opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java @@ -18,7 +18,7 @@ */ /* - * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved. */ package org.opengrok.indexer.configuration; @@ -27,6 +27,9 @@ import java.util.Date; import java.util.logging.Level; import java.util.logging.Logger; + +import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.VisibleForTesting; import org.opengrok.indexer.logger.LoggerFactory; public class IndexTimestamp { @@ -34,15 +37,18 @@ public class IndexTimestamp { private static final Logger LOGGER = LoggerFactory.getLogger(IndexTimestamp.class); + @VisibleForTesting + public static final String TIMESTAMP_FILE_NAME = "timestamp"; + /** * Get the date of the last index update. * - * @return the time of the last index update. + * @return the time of the last index update or {@code null}. */ - public Date getDateForLastIndexRun() { + public @Nullable Date getDateForLastIndexRun() { RuntimeEnvironment env = RuntimeEnvironment.getInstance(); if (lastModified == null) { - File timestamp = new File(env.getDataRootFile(), "timestamp"); + File timestamp = new File(env.getDataRootFile(), TIMESTAMP_FILE_NAME); if (timestamp.exists()) { lastModified = new Date(timestamp.lastModified()); } diff --git a/opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java b/opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java index e0dd781ac01..5f30fb2f9fa 100644 --- a/opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java +++ b/opengrok-web/src/test/java/org/opengrok/web/PageConfigTest.java @@ -49,11 +49,13 @@ import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.opengrok.indexer.authorization.AuthControlFlag; import org.opengrok.indexer.authorization.AuthorizationFramework; import org.opengrok.indexer.authorization.AuthorizationPlugin; import org.opengrok.indexer.authorization.TestPlugin; import org.opengrok.indexer.condition.EnabledForRepository; +import org.opengrok.indexer.configuration.IndexTimestamp; import org.opengrok.indexer.configuration.Project; import org.opengrok.indexer.configuration.RuntimeEnvironment; import org.opengrok.indexer.history.Annotation; @@ -655,8 +657,9 @@ public String getPathInfo() { }; } - @Test - void testIsNotModifiedEtag() { + @ParameterizedTest + @ValueSource(booleans = {true, false}) + void testIsNotModifiedEtag(boolean createTimestamp) throws IOException { HttpServletRequest req = new DummyHttpServletRequest() { @Override public String getHeader(String name) { @@ -672,6 +675,16 @@ public String getPathInfo() { } }; + // The ETag value depends on the timestamp file. + RuntimeEnvironment env = RuntimeEnvironment.getInstance(); + env.refreshDateForLastIndexRun(); + Path timestampPath = Path.of(env.getDataRootPath(), IndexTimestamp.TIMESTAMP_FILE_NAME); + Files.deleteIfExists(timestampPath); + if (createTimestamp) { + Files.createFile(timestampPath); + assertTrue(timestampPath.toFile().exists()); + } + PageConfig cfg = PageConfig.get(req); HttpServletResponse resp = mock(HttpServletResponse.class); assertFalse(cfg.isNotModified(req, resp));