| 
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,103 @@ 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 | +            "/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 | + | 
151 | 253 |     /* see https://github.com/oracle/opengrok/issues/2030  | 
152 | 254 |     @Test  | 
153 | 255 |     void testSearch() {  | 
 | 
0 commit comments