Skip to content

Commit 35d7af4

Browse files
committed
Add test
Signed-off-by: Bernát Gábor <gaborjbernat@gmail.com>
1 parent f1de723 commit 35d7af4

File tree

2 files changed

+103
-15
lines changed

2 files changed

+103
-15
lines changed

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

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

2626
import java.io.File;
27+
import java.io.FileOutputStream;
28+
import java.util.ArrayList;
2729
import java.util.Collections;
30+
import java.util.List;
2831
import java.util.TreeSet;
2932

3033
import org.junit.jupiter.api.AfterAll;
@@ -36,7 +39,9 @@
3639
import org.opengrok.indexer.util.TestRepository;
3740

3841
import org.opengrok.indexer.history.RepositoryFactory;
42+
import org.opengrok.indexer.web.SortOrder;
3943

44+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4045
import static org.junit.jupiter.api.Assertions.assertEquals;
4146
import static org.junit.jupiter.api.Assertions.assertFalse;
4247
import static org.junit.jupiter.api.Assertions.assertNull;
@@ -148,6 +153,69 @@ void testGetQuery() throws Exception {
148153
instance.getQuery());
149154
}
150155

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