Skip to content

Commit 41ef174

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

File tree

2 files changed

+136
-15
lines changed

2 files changed

+136
-15
lines changed

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

Lines changed: 102 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,103 @@ void testGetQuery() throws Exception {
148153
instance.getQuery());
149154
}
150155

156+
@Test
157+
void testSortOrderByRelevancy() {
158+
SearchEngine instance = new SearchEngine();
159+
instance.setFile("main.c OR header.h");
160+
instance.setFreetext("arguments OR stdio");
161+
instance.setSortOrder(SortOrder.RELEVANCY);
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+
"/mercurial/header.h@2",
173+
"/bazaar/header.h@2",
174+
"/teamware/header.h@2",
175+
"/git/header.h@2",
176+
"/rcs_test/header.h@2",
177+
"/bazaar/main.c@5",
178+
"/rcs_test/main.c@5",
179+
"/teamware/main.c@5",
180+
"/mercurial/main.c@5",
181+
"/git/main.c@5",
182+
"/cvs_test/cvsrepo/main.c@7"
183+
};
184+
185+
assertArrayEquals(expectedResults, results.toArray());
186+
187+
instance.destroy();
188+
}
189+
190+
@Test
191+
void testSortOrderLastModified() {
192+
SearchEngine instance = new SearchEngine();
193+
instance.setFile("main.c");
194+
instance.setFreetext("arguments");
195+
instance.setSortOrder(SortOrder.LASTMODIFIED);
196+
int hitsCount = instance.search();
197+
List<Hit> hits = new ArrayList<>();
198+
instance.results(0, hitsCount, hits);
199+
assertTrue(hits.size() > 1, "Should return at least 2 hits for RELEVANCY sort to check order");
200+
201+
List<String> results = new ArrayList<>();
202+
for (Hit hit : hits) {
203+
results.add(hit.getPath() + "@" + hit.getLineno());
204+
}
205+
final String[] expectedResults = {
206+
"/teamware/main.c@5",
207+
"/rcs_test/main.c@5",
208+
"/mercurial/main.c@5",
209+
"/git/main.c@5",
210+
"/cvs_test/cvsrepo/main.c@7",
211+
"/bazaar/main.c@5"
212+
};
213+
214+
assertArrayEquals(expectedResults, results.toArray());
215+
216+
instance.destroy();
217+
}
218+
219+
@Test
220+
void testSortOrderByPath() {
221+
SearchEngine instance = new SearchEngine();
222+
instance.setFile("main.c OR header.h");
223+
instance.setFreetext("arguments OR stdio");
224+
instance.setSortOrder(SortOrder.BY_PATH);
225+
int hitsCount = instance.search();
226+
List<Hit> hits = new ArrayList<>();
227+
instance.results(0, hitsCount, hits);
228+
assertTrue(hits.size() > 1, "Should return at least 2 hits for RELEVANCY sort to check order");
229+
230+
List<String> results = new ArrayList<>();
231+
for (Hit hit : hits) {
232+
results.add(hit.getPath() + "@" + hit.getLineno());
233+
}
234+
final String[] expectedResults = {
235+
"/bazaar/header.h@2",
236+
"/bazaar/main.c@5",
237+
"/cvs_test/cvsrepo/main.c@7",
238+
"/git/header.h@2",
239+
"/git/main.c@5",
240+
"/mercurial/header.h@2",
241+
"/mercurial/main.c@5",
242+
"/rcs_test/header.h@2",
243+
"/rcs_test/main.c@5",
244+
"/teamware/header.h@2",
245+
"/teamware/main.c@5"
246+
};
247+
248+
assertArrayEquals(expectedResults, results.toArray());
249+
250+
instance.destroy();
251+
}
252+
151253
/* see https://github.com/oracle/opengrok/issues/2030
152254
@Test
153255
void testSearch() {

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

Lines changed: 34 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,43 @@ 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 both sorting by creation time, path and relevancy
116+
List<Path> allPaths;
114117
try (Stream<Path> stream = Files.walk(src)) {
115-
stream.forEach(sourceFile -> {
116-
if (sourceFile.equals(src)) {
117-
return;
118+
allPaths = stream.filter(p -> !p.equals(src)).sorted().toList();
119+
}
120+
// Set base time to now, and go back in time for each subsequent path
121+
java.time.Instant baseTime = java.time.Instant.now();
122+
for (int i = 0; i < allPaths.size(); i++) {
123+
Path sourcePath = allPaths.get(i);
124+
Path destRelativePath = getDestinationRelativePath(src, sourcePath);
125+
Path destPath = dest.resolve(destRelativePath);
126+
var fileTime = java.nio.file.attribute.FileTime.from(baseTime.plusSeconds(i * 60L));
127+
if (Files.isDirectory(sourcePath)) {
128+
// Create directory and set timestamps
129+
if (!Files.exists(destPath)) {
130+
Files.createDirectories(destPath);
118131
}
119132
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);
133+
Files.setLastModifiedTime(destPath, fileTime);
134+
Files.setAttribute(destPath, "basic:creationTime", fileTime);
135+
} catch (Exception ignored) {
136+
// Not all filesystems support creationTime
137+
}
138+
} else {
139+
// Ensure parent directory exists before copying file
140+
Path parentDir = destPath.getParent();
141+
if (parentDir != null && !Files.exists(parentDir)) {
142+
Files.createDirectories(parentDir);
131143
}
132-
});
144+
Files.copy(sourcePath, destPath, REPLACE_EXISTING, COPY_ATTRIBUTES);
145+
Files.setLastModifiedTime(destPath, fileTime);
146+
try {
147+
Files.setAttribute(destPath, "basic:creationTime", fileTime);
148+
} catch (Exception ignored) {
149+
// Not all filesystems support creationTime
150+
}
151+
}
133152
}
134153
}
135154

0 commit comments

Comments
 (0)