163c58231SAdam Hornáček /* 263c58231SAdam Hornáček * CDDL HEADER START 363c58231SAdam Hornáček * 463c58231SAdam Hornáček * The contents of this file are subject to the terms of the 563c58231SAdam Hornáček * Common Development and Distribution License (the "License"). 663c58231SAdam Hornáček * You may not use this file except in compliance with the License. 763c58231SAdam Hornáček * 863c58231SAdam Hornáček * See LICENSE.txt included in this distribution for the specific 963c58231SAdam Hornáček * language governing permissions and limitations under the License. 1063c58231SAdam Hornáček * 1163c58231SAdam Hornáček * When distributing Covered Code, include this CDDL HEADER in each 1263c58231SAdam Hornáček * file and include the License file at LICENSE.txt. 1363c58231SAdam Hornáček * If applicable, add the following below this CDDL HEADER, with the 1463c58231SAdam Hornáček * fields enclosed by brackets "[]" replaced with your own identifying 1563c58231SAdam Hornáček * information: Portions Copyright [yyyy] [name of copyright owner] 1663c58231SAdam Hornáček * 1763c58231SAdam Hornáček * CDDL HEADER END 1863c58231SAdam Hornáček */ 1963c58231SAdam Hornáček 2063c58231SAdam Hornáček /* 21*2f7dccc7SAdam Hornacek * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved. 2220463cfeSChris Fraire * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>. 2363c58231SAdam Hornáček */ 2463c58231SAdam Hornáček package org.opengrok.suggest.query; 2563c58231SAdam Hornáček 2663c58231SAdam Hornáček import org.apache.lucene.index.Term; 277ef04fd1SAdam Hornacek import org.junit.jupiter.api.Test; 2863c58231SAdam Hornáček import org.opengrok.suggest.query.customized.CustomPhraseQuery; 2963c58231SAdam Hornáček 3063c58231SAdam Hornáček import java.lang.reflect.Field; 3163c58231SAdam Hornáček import java.util.Arrays; 3263c58231SAdam Hornáček 337ef04fd1SAdam Hornacek import static org.junit.jupiter.api.Assertions.assertEquals; 3463c58231SAdam Hornáček 3563c58231SAdam Hornáček public class SuggesterPhraseQueryTest { 3663c58231SAdam Hornáček 3763c58231SAdam Hornáček @Test prefixQueryTest()3863c58231SAdam Hornáček public void prefixQueryTest() { 3963c58231SAdam Hornáček SuggesterPhraseQuery q = new SuggesterPhraseQuery("test", "ident", 4063c58231SAdam Hornáček Arrays.asList("one", "two", "tident"), 0); 4163c58231SAdam Hornáček 4263c58231SAdam Hornáček SuggesterPrefixQuery prefixQuery = (SuggesterPrefixQuery) q.getSuggesterQuery(); 4363c58231SAdam Hornáček 4463c58231SAdam Hornáček assertEquals("t", prefixQuery.getPrefix().text()); 4563c58231SAdam Hornáček } 4663c58231SAdam Hornáček 4763c58231SAdam Hornáček @Test phraseQueryTest()4863c58231SAdam Hornáček public void phraseQueryTest() throws Exception { 4963c58231SAdam Hornáček SuggesterPhraseQuery q = new SuggesterPhraseQuery("test", "ident", 5063c58231SAdam Hornáček Arrays.asList("one", "two", "tident"), 0); 5163c58231SAdam Hornáček 5263c58231SAdam Hornáček CustomPhraseQuery query = q.getPhraseQuery(); 5363c58231SAdam Hornáček 5420463cfeSChris Fraire assertEquals(2, query.getOffset()); 5563c58231SAdam Hornáček 5663c58231SAdam Hornáček Term[] terms = getTerms(query); 5763c58231SAdam Hornáček 5863c58231SAdam Hornáček assertEquals("one", terms[0].text()); 5963c58231SAdam Hornáček assertEquals("two", terms[1].text()); 6063c58231SAdam Hornáček } 6163c58231SAdam Hornáček getTerms(final CustomPhraseQuery query)6263c58231SAdam Hornáček private Term[] getTerms(final CustomPhraseQuery query) throws Exception { 6363c58231SAdam Hornáček Field f = CustomPhraseQuery.class.getDeclaredField("terms"); 6463c58231SAdam Hornáček f.setAccessible(true); 6563c58231SAdam Hornáček return (Term[]) f.get(query); 6663c58231SAdam Hornáček } 6763c58231SAdam Hornáček 6863c58231SAdam Hornáček } 69