|
24 | 24 | package org.opengrok.indexer.search; |
25 | 25 |
|
26 | 26 | import java.io.File; |
| 27 | +import java.io.FileOutputStream; |
| 28 | +import java.util.ArrayList; |
27 | 29 | import java.util.Collections; |
| 30 | +import java.util.List; |
28 | 31 | import java.util.TreeSet; |
29 | 32 |
|
30 | 33 | import org.junit.jupiter.api.AfterAll; |
|
36 | 39 | import org.opengrok.indexer.util.TestRepository; |
37 | 40 |
|
38 | 41 | import org.opengrok.indexer.history.RepositoryFactory; |
| 42 | +import org.opengrok.indexer.web.SortOrder; |
39 | 43 |
|
| 44 | +import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
40 | 45 | import static org.junit.jupiter.api.Assertions.assertEquals; |
41 | 46 | import static org.junit.jupiter.api.Assertions.assertFalse; |
42 | 47 | import static org.junit.jupiter.api.Assertions.assertNull; |
@@ -148,6 +153,108 @@ void testGetQuery() throws Exception { |
148 | 153 | instance.getQuery()); |
149 | 154 | } |
150 | 155 |
|
| 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 | + "/bazaar/header.h@2", |
| 173 | + "/bazaar/main.c@5", |
| 174 | + "/cvs_test/cvsrepo/main.c@7", |
| 175 | + "/git/header.h@2", |
| 176 | + "/git/main.c@5", |
| 177 | + "/mercurial/header.h@2", |
| 178 | + "/mercurial/main.c@5", |
| 179 | + "/rcs_test/header.h@2", |
| 180 | + "/rcs_test/main.c@5", |
| 181 | + "/teamware/header.h@2", |
| 182 | + "/teamware/main.c@5" |
| 183 | + }; |
| 184 | + |
| 185 | + assertArrayEquals(results.toArray(), expectedResults); |
| 186 | + |
| 187 | + instance.destroy(); |
| 188 | + } |
| 189 | + |
| 190 | + @Test |
| 191 | + void testSortOrderLastModified() { |
| 192 | + SearchEngine instance = new SearchEngine(); |
| 193 | + instance.setFile("main.c OR header.h"); |
| 194 | + instance.setFreetext("arguments OR stdio"); |
| 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 | + "/bazaar/header.h@2", |
| 207 | + "/bazaar/main.c@5", |
| 208 | + "/cvs_test/cvsrepo/main.c@7", |
| 209 | + "/git/header.h@2", |
| 210 | + "/git/main.c@5", |
| 211 | + "/mercurial/header.h@2", |
| 212 | + "/mercurial/main.c@5", |
| 213 | + "/rcs_test/header.h@2", |
| 214 | + "/rcs_test/main.c@5", |
| 215 | + "/teamware/header.h@2", |
| 216 | + "/teamware/main.c@5" |
| 217 | + }; |
| 218 | + |
| 219 | + assertArrayEquals(results.toArray(), expectedResults); |
| 220 | + |
| 221 | + instance.destroy(); |
| 222 | + } |
| 223 | + |
| 224 | + @Test |
| 225 | + void testSortOrderByPath() { |
| 226 | + SearchEngine instance = new SearchEngine(); |
| 227 | + instance.setFile("main.c OR header.h"); |
| 228 | + instance.setFreetext("arguments OR stdio"); |
| 229 | + instance.setSortOrder(SortOrder.BY_PATH); |
| 230 | + int hitsCount = instance.search(); |
| 231 | + List<Hit> hits = new ArrayList<>(); |
| 232 | + instance.results(0, hitsCount, hits); |
| 233 | + assertTrue(hits.size() > 1, "Should return at least 2 hits for RELEVANCY sort to check order"); |
| 234 | + |
| 235 | + List<String> results = new ArrayList<>(); |
| 236 | + for (Hit hit : hits) { |
| 237 | + results.add(hit.getPath() + "@" + hit.getLineno()); |
| 238 | + } |
| 239 | + final String[] expectedResults = { |
| 240 | + "/bazaar/header.h@2", |
| 241 | + "/bazaar/main.c@5", |
| 242 | + "/cvs_test/cvsrepo/main.c@7", |
| 243 | + "/git/header.h@2", |
| 244 | + "/git/main.c@5", |
| 245 | + "/mercurial/header.h@2", |
| 246 | + "/mercurial/main.c@5", |
| 247 | + "/rcs_test/header.h@2", |
| 248 | + "/rcs_test/main.c@5", |
| 249 | + "/teamware/header.h@2", |
| 250 | + "/teamware/main.c@5" |
| 251 | + }; |
| 252 | + |
| 253 | + assertArrayEquals(results.toArray(), expectedResults); |
| 254 | + |
| 255 | + instance.destroy(); |
| 256 | + } |
| 257 | + |
151 | 258 | /* see https://github.com/oracle/opengrok/issues/2030 |
152 | 259 | @Test |
153 | 260 | void testSearch() { |
|
0 commit comments