Skip to content

Commit 18ff126

Browse files
authored
improve test coverage of PageConfig#isNotModified() (#4853)
by (not) creating the timestamp file
1 parent 31fbf4b commit 18ff126

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/IndexTimestamp.java

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

2020
/*
21-
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.configuration;
2424

@@ -27,22 +27,28 @@
2727
import java.util.Date;
2828
import java.util.logging.Level;
2929
import java.util.logging.Logger;
30+
31+
import org.jetbrains.annotations.Nullable;
32+
import org.jetbrains.annotations.VisibleForTesting;
3033
import org.opengrok.indexer.logger.LoggerFactory;
3134

3235
public class IndexTimestamp {
3336
private Date lastModified;
3437

3538
private static final Logger LOGGER = LoggerFactory.getLogger(IndexTimestamp.class);
3639

40+
@VisibleForTesting
41+
public static final String TIMESTAMP_FILE_NAME = "timestamp";
42+
3743
/**
3844
* Get the date of the last index update.
3945
*
40-
* @return the time of the last index update.
46+
* @return the time of the last index update or {@code null}.
4147
*/
42-
public Date getDateForLastIndexRun() {
48+
public @Nullable Date getDateForLastIndexRun() {
4349
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
4450
if (lastModified == null) {
45-
File timestamp = new File(env.getDataRootFile(), "timestamp");
51+
File timestamp = new File(env.getDataRootFile(), TIMESTAMP_FILE_NAME);
4652
if (timestamp.exists()) {
4753
lastModified = new Date(timestamp.lastModified());
4854
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@
4949
import org.junit.jupiter.api.condition.OS;
5050
import org.junit.jupiter.params.ParameterizedTest;
5151
import org.junit.jupiter.params.provider.MethodSource;
52+
import org.junit.jupiter.params.provider.ValueSource;
5253
import org.opengrok.indexer.authorization.AuthControlFlag;
5354
import org.opengrok.indexer.authorization.AuthorizationFramework;
5455
import org.opengrok.indexer.authorization.AuthorizationPlugin;
5556
import org.opengrok.indexer.authorization.TestPlugin;
5657
import org.opengrok.indexer.condition.EnabledForRepository;
58+
import org.opengrok.indexer.configuration.IndexTimestamp;
5759
import org.opengrok.indexer.configuration.Project;
5860
import org.opengrok.indexer.configuration.RuntimeEnvironment;
5961
import org.opengrok.indexer.history.Annotation;
@@ -655,8 +657,9 @@ public String getPathInfo() {
655657
};
656658
}
657659

658-
@Test
659-
void testIsNotModifiedEtag() {
660+
@ParameterizedTest
661+
@ValueSource(booleans = {true, false})
662+
void testIsNotModifiedEtag(boolean createTimestamp) throws IOException {
660663
HttpServletRequest req = new DummyHttpServletRequest() {
661664
@Override
662665
public String getHeader(String name) {
@@ -672,6 +675,16 @@ public String getPathInfo() {
672675
}
673676
};
674677

678+
// The ETag value depends on the timestamp file.
679+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
680+
env.refreshDateForLastIndexRun();
681+
Path timestampPath = Path.of(env.getDataRootPath(), IndexTimestamp.TIMESTAMP_FILE_NAME);
682+
Files.deleteIfExists(timestampPath);
683+
if (createTimestamp) {
684+
Files.createFile(timestampPath);
685+
assertTrue(timestampPath.toFile().exists());
686+
}
687+
675688
PageConfig cfg = PageConfig.get(req);
676689
HttpServletResponse resp = mock(HttpServletResponse.class);
677690
assertFalse(cfg.isNotModified(req, resp));

0 commit comments

Comments
 (0)