Skip to content

Commit 8fd284e

Browse files
committed
Add test
Signed-off-by: Bernát Gábor <[email protected]>
1 parent f1de723 commit 8fd284e

File tree

2 files changed

+102
-15
lines changed

2 files changed

+102
-15
lines changed

opengrok-indexer/src/test/java/org/opengrok/indexer/search/SearchEngineTest.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
package org.opengrok.indexer.search;
2525

2626
import java.io.File;
27+
import java.util.ArrayList;
2728
import java.util.Collections;
29+
import java.util.List;
2830
import java.util.TreeSet;
2931

3032
import org.junit.jupiter.api.AfterAll;
@@ -36,7 +38,9 @@
3638
import org.opengrok.indexer.util.TestRepository;
3739

3840
import org.opengrok.indexer.history.RepositoryFactory;
41+
import org.opengrok.indexer.web.SortOrder;
3942

43+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4044
import static org.junit.jupiter.api.Assertions.assertEquals;
4145
import static org.junit.jupiter.api.Assertions.assertFalse;
4246
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -148,6 +152,69 @@ void testGetQuery() throws Exception {
148152
instance.getQuery());
149153
}
150154

155+
@Test
156+
void testSortOrderLastModified() {
157+
SearchEngine instance = new SearchEngine();
158+
instance.setFile("main.c");
159+
instance.setFreetext("arguments");
160+
instance.setSortOrder(SortOrder.LASTMODIFIED);
161+
int hitsCount = instance.search();
162+
List<Hit> hits = new ArrayList<>();
163+
instance.results(0, hitsCount, hits);
164+
assertTrue(hits.size() > 1, "Should return at least 2 hits for RELEVANCY sort to check order");
165+
166+
List<String> results = new ArrayList<>();
167+
for (Hit hit : hits) {
168+
results.add(hit.getPath() + "@" + hit.getLineno());
169+
}
170+
final String[] expectedResults = {
171+
"/teamware/main.c@5",
172+
"/rcs_test/main.c@5",
173+
"/mercurial/main.c@5",
174+
"/git/main.c@5",
175+
"/cvs_test/cvsrepo/main.c@7",
176+
"/bazaar/main.c@5"
177+
};
178+
179+
assertArrayEquals(expectedResults, results.toArray());
180+
181+
instance.destroy();
182+
}
183+
184+
@Test
185+
void testSortOrderByPath() {
186+
SearchEngine instance = new SearchEngine();
187+
instance.setFile("main.c OR header.h");
188+
instance.setFreetext("arguments OR stdio");
189+
instance.setSortOrder(SortOrder.BY_PATH);
190+
int hitsCount = instance.search();
191+
List<Hit> hits = new ArrayList<>();
192+
instance.results(0, hitsCount, hits);
193+
assertTrue(hits.size() > 1, "Should return at least 2 hits for RELEVANCY sort to check order");
194+
195+
List<String> results = new ArrayList<>();
196+
for (Hit hit : hits) {
197+
results.add(hit.getPath() + "@" + hit.getLineno());
198+
}
199+
final String[] expectedResults = {
200+
"/bazaar/header.h@2",
201+
"/bazaar/main.c@5",
202+
"/cvs_test/cvsrepo/main.c@7",
203+
"/git/header.h@2",
204+
"/git/main.c@5",
205+
"/mercurial/header.h@2",
206+
"/mercurial/main.c@5",
207+
"/rcs_test/header.h@2",
208+
"/rcs_test/main.c@5",
209+
"/teamware/header.h@2",
210+
"/teamware/main.c@5"
211+
};
212+
213+
assertArrayEquals(expectedResults, results.toArray());
214+
215+
instance.destroy();
216+
}
217+
151218
/* see https://github.com/oracle/opengrok/issues/2030
152219
@Test
153220
void testSearch() {

opengrok-indexer/src/test/java/org/opengrok/indexer/util/TestRepository.java

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Files;
3434
import java.nio.file.Path;
3535
import java.util.LinkedHashMap;
36+
import java.util.List;
3637
import java.util.Map;
3738
import java.util.stream.Stream;
3839

@@ -111,25 +112,44 @@ public void create(@NotNull final URL url) throws IOException, URISyntaxExceptio
111112
* @throws IOException on error
112113
*/
113114
public void copyDirectory(Path src, Path dest) throws IOException {
115+
// Create a deterministic order of paths for creation time, so last modified time indexing is stable in tests
116+
// note we cannot use Files.copy(sourceFile, destPath, REPLACE_EXISTING, COPY_ATTRIBUTES)
117+
// as the original creation time is the user checkout and not different accross files
118+
List<Path> allPaths;
114119
try (Stream<Path> stream = Files.walk(src)) {
115-
stream.forEach(sourceFile -> {
116-
if (sourceFile.equals(src)) {
117-
return;
120+
allPaths = stream.filter(p -> !p.equals(src)).sorted().toList();
121+
}
122+
// Set base time to now, and go ahead in time for each subsequent path by 1 minute
123+
java.time.Instant baseTime = java.time.Instant.now();
124+
for (int i = 0; i < allPaths.size(); i++) {
125+
Path sourcePath = allPaths.get(i);
126+
Path destRelativePath = getDestinationRelativePath(src, sourcePath);
127+
Path destPath = dest.resolve(destRelativePath);
128+
var fileTime = java.nio.file.attribute.FileTime.from(baseTime.plusSeconds(i * 60L));
129+
if (Files.isDirectory(sourcePath)) {
130+
if (!Files.exists(destPath)) {
131+
Files.createDirectories(destPath);
118132
}
119133
try {
120-
Path destRelativePath = getDestinationRelativePath(src, sourceFile);
121-
Path destPath = dest.resolve(destRelativePath);
122-
if (Files.isDirectory(sourceFile)) {
123-
if (!Files.exists(destPath)) {
124-
Files.createDirectory(destPath);
125-
}
126-
return;
127-
}
128-
Files.copy(sourceFile, destPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
129-
} catch (Exception e) {
130-
throw new RuntimeException(e);
134+
Files.setLastModifiedTime(destPath, fileTime);
135+
Files.setAttribute(destPath, "basic:creationTime", fileTime);
136+
} catch (Exception ignored) {
137+
// Not all filesystems support creationTime
138+
}
139+
} else {
140+
// Ensure parent directory exists before copying file
141+
Path parentDir = destPath.getParent();
142+
if (parentDir != null && !Files.exists(parentDir)) {
143+
Files.createDirectories(parentDir);
131144
}
132-
});
145+
Files.copy(sourcePath, destPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
146+
Files.setLastModifiedTime(destPath, fileTime);
147+
try {
148+
Files.setAttribute(destPath, "basic:creationTime", fileTime);
149+
} catch (Exception ignored) {
150+
// Not all filesystems support creationTime
151+
}
152+
}
133153
}
134154
}
135155

0 commit comments

Comments
 (0)