|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved. |
| 22 | + */ |
| 23 | +package org.opengrok.indexer.search.context; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.AfterAll; |
| 26 | +import org.junit.jupiter.api.BeforeAll; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.opengrok.indexer.configuration.RuntimeEnvironment; |
| 29 | +import org.opengrok.indexer.history.HistoryGuru; |
| 30 | +import org.opengrok.indexer.history.RepositoryFactory; |
| 31 | +import org.opengrok.indexer.index.Indexer; |
| 32 | +import org.opengrok.indexer.search.SearchEngine; |
| 33 | +import org.opengrok.indexer.util.TestRepository; |
| 34 | +import org.w3c.dom.Document; |
| 35 | +import org.w3c.dom.NodeList; |
| 36 | + |
| 37 | +import javax.xml.parsers.DocumentBuilder; |
| 38 | +import javax.xml.parsers.DocumentBuilderFactory; |
| 39 | +import java.io.ByteArrayInputStream; |
| 40 | +import java.nio.file.Paths; |
| 41 | +import java.util.Arrays; |
| 42 | + |
| 43 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 45 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 46 | +import static org.opengrok.indexer.search.context.SearchAndContextFormatterTest.getFirstFragments; |
| 47 | + |
| 48 | +/** |
| 49 | + * Make sure that passages within search results are ordered strictly based on the line numbers. |
| 50 | + */ |
| 51 | +public class PassageScorerTest { |
| 52 | + private static RuntimeEnvironment env; |
| 53 | + private static TestRepository repository; |
| 54 | + |
| 55 | + @BeforeAll |
| 56 | + public static void setUpClass() throws Exception { |
| 57 | + repository = new TestRepository(); |
| 58 | + repository.create(HistoryGuru.class.getResource("/sources")); |
| 59 | + |
| 60 | + env = RuntimeEnvironment.getInstance(); |
| 61 | + env.setCtags(System.getProperty("org.opengrok.indexer.analysis.Ctags", "ctags")); |
| 62 | + env.setSourceRoot(repository.getSourceRoot()); |
| 63 | + env.setDataRoot(repository.getDataRoot()); |
| 64 | + RepositoryFactory.initializeIgnoredNames(env); |
| 65 | + env.setHistoryEnabled(false); |
| 66 | + |
| 67 | + assertTrue(Paths.get(env.getSourceRootPath(), "c", "sdt.h").toFile().exists()); |
| 68 | + |
| 69 | + Indexer.getInstance().doIndexerExecution(null, null); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterAll |
| 73 | + public static void tearDownClass() { |
| 74 | + repository.destroy(); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void testSearch() throws Exception { |
| 79 | + SearchEngine instance = new SearchEngine(); |
| 80 | + instance.setFreetext("DTRACE_PROBE4"); |
| 81 | + instance.setFile("sdt"); |
| 82 | + int noHits = instance.search(); |
| 83 | + assertTrue(noHits > 0, "noHits should be positive"); |
| 84 | + String[] frags = getFirstFragments(instance, env, new ContextArgs((short) 0, (short) 10)); |
| 85 | + assertNotNull(frags, "getFirstFragments() should return something"); |
| 86 | + assertEquals(1, frags.length, "frags should have one element"); |
| 87 | + |
| 88 | + // Create XML from the result and parse it, get the line numbers, compare. |
| 89 | + String docString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + |
| 90 | + "<document>\n" + |
| 91 | + frags[0] + |
| 92 | + "\n</document>"; |
| 93 | + |
| 94 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 95 | + assertNotNull(factory, "DocumentBuilderFactory is null"); |
| 96 | + |
| 97 | + DocumentBuilder builder = factory.newDocumentBuilder(); |
| 98 | + assertNotNull(builder, "DocumentBuilder is null"); |
| 99 | + |
| 100 | + final int[] lineNumbers = {58, 97, 155, 172, 189, 200, 232, 264, 297, 324}; |
| 101 | + |
| 102 | + Document document = builder.parse(new ByteArrayInputStream(docString.getBytes())); |
| 103 | + NodeList nl = document.getElementsByTagName("a"); |
| 104 | + assertEquals(lineNumbers.length, nl.getLength()); |
| 105 | + int[] lines = new int[nl.getLength()]; |
| 106 | + for (int i = 0; i < nl.getLength(); i++) { |
| 107 | + String href = nl.item(i).getAttributes().getNamedItem("href").getNodeValue(); |
| 108 | + assertNotNull(href); |
| 109 | + String lineNumStr = href.substring(href.indexOf("#") + 1); |
| 110 | + assertNotNull(lineNumStr); |
| 111 | + int lineNum = Integer.parseInt(lineNumStr); |
| 112 | + lines[i] = lineNum; |
| 113 | + } |
| 114 | + assertEquals(0, Arrays.compare(lineNumbers, lines)); |
| 115 | + |
| 116 | + instance.destroy(); |
| 117 | + } |
| 118 | +} |
0 commit comments