@Test public void testThatCaseIsPreservedInDecorateInstruction() throws Exception { Input input = (Input) LineParser.parseInput("in"); assertEquals(new DecorateInstruction("Some Deco"), LineParser.parse("DECORATE: Some Deco", input, null)); }
return new ValidationError("Empty input"); return parseInput(line.substring(0, line.length() - 2)); Object expr = parseTermExpression(instructionTerms); if (expr instanceof ValidationError) { return new ValidationError("Cannot parse line: " + line +" : " + ((ValidationError)expr).getMessage()); return parseBoostInstruction(line, lcLine, 4, BoostDirection.DOWN, querqyParserFactory); return parseBoostInstruction(line, lcLine, 2, BoostDirection.UP, querqyParserFactory); for (String token: synonymString.split("\\s+")) { if (token.length() > 0) { Term term = parseTerm(token); if (term.getMaxPlaceHolderRef() > 1) { return new ValidationError("Max. wild card reference is 1: " + line); return parseDecorateInstruction(line);
public static Term parseTerm(String s) { int len = s.length(); if (len == 1) { char ch = s.charAt(0); if (ch == WILDCARD) { throw new IllegalArgumentException("Missing prefix for wildcard " + WILDCARD); } return new Term(new char[] {ch}, 0, 1, null); } int pos = s.indexOf(':'); boolean fieldNamesPossible = (pos > 0 && pos < (len -1)); List<String> fieldNames = fieldNamesPossible ? parseFieldNames(s.substring(0, pos)) : null; String remaining = fieldNamesPossible ? s.substring(pos + 1).trim() : s; if (fieldNamesPossible && remaining.length() == 1 && remaining.charAt(0) == WILDCARD) { throw new IllegalArgumentException("Missing prefix for wildcard " + WILDCARD); } return (remaining.charAt(remaining.length() - 1) == WILDCARD) ? new PrefixTerm(remaining.toCharArray(), 0, remaining.length() - 1, fieldNames) : new Term(remaining.toCharArray(), 0, remaining.length(), fieldNames); }
@Test public void testInputWithWildcard() { assertTrue("parseInput must not allow wildcard in the middle", LineParser.parseInput("abc* def ghij*") instanceof ValidationError); }
static Object parseTermExpression(String s) { int len = s.length(); if (len == 1) { char ch = s.charAt(0); if (ch == WILDCARD) { return new ValidationError("Missing prefix for wildcard " + WILDCARD); } Term term = new Term(new char[] {ch}, 0, 1, null); return Arrays.asList(term); } List<Term> terms = new LinkedList<>(); for (String part : s.split("\\s+")) { if (part.length() > 0) { terms.add(parseTerm(part)); } } return terms; }
@SuppressWarnings("unchecked") public static Object parseInput(String s) { boolean requiresLeftBoundary = false; boolean requiresRightBoundary = false; s = s.trim(); if (s.length() > 0 && s.charAt(0) == BOUNDARY) { requiresLeftBoundary = true; s = s.substring(1).trim(); } if (s.length() > 0 && s.charAt(s.length() - 1) == BOUNDARY) { requiresRightBoundary = true; s = s.substring(0, s.length() - 1).trim(); } int pos = s.indexOf('*'); if (pos > -1) { if (pos < (s.length() -1)) { return new ValidationError(WILDCARD + " is only allowed at the end of the input: " + s); } else if (requiresRightBoundary) { return new ValidationError(WILDCARD + " cannot be combined with right boundary"); } } Object expr = parseTermExpression(s); return (expr instanceof ValidationError) ? expr : new Input((List<Term>) expr, requiresLeftBoundary, requiresRightBoundary); }
@Test public void testThatBoostInstructionWithSingleLetterTermIsAccepted() throws Exception { String line = "UP: x"; String lcLine = line.toLowerCase(); final Object instruction = LineParser .parseBoostInstruction(line, lcLine, 2, BoostDirection.UP, new WhiteSpaceQuerqyParserFactory()); assertTrue(instruction instanceof BoostInstruction); }
public void nextLine(String line) throws RuleParseException { line = stripLine(line); if (line.length() > 0) { Object lineObject = LineParser.parse(line, input, querqyParserFactory); if (lineObject instanceof Input) { putRule(); input = (Input) lineObject; instructions = new Instructions(); } else if (lineObject instanceof ValidationError) { throw new RuleParseException(lineNumber, ((ValidationError) lineObject).getMessage()); } else { instructions.add((Instruction) lineObject); } } }
@Test public void testThatWildcardCannotBeFollowedByRightBoundary() throws Exception { Object parseResult = LineParser.parseInput("a" + LineParser.WILDCARD + LineParser.BOUNDARY); assertEquals("Wildcard should not be allowed before right boundary", new ValidationError(LineParser.WILDCARD + " cannot be combined with right boundary"), parseResult); }
@Test public void testThatWildcardOnlyTermIsNotAllowedWithFieldName() throws Exception { try { LineParser.parseTerm("f1:*"); fail("Wildcard-only term must not be allowed with fieldname"); } catch (IllegalArgumentException e) { // expected } }
@Test public void testParseTermExpressionDoesNotAllowAWildCardOnly() { assertTrue("parseTermExpression must not allow single wild card", LineParser.parseTermExpression("*") instanceof ValidationError); }
@Test public void testThatBoostInstructionWithSingleLetterTermAndBoostFactorIsAccepted() throws Exception { String line = "UP(5): x"; String lcLine = line.toLowerCase(); final Object instruction = LineParser .parseBoostInstruction(line, lcLine, 2, BoostDirection.UP, new WhiteSpaceQuerqyParserFactory()); assertTrue(instruction instanceof BoostInstruction); }
@SuppressWarnings("unchecked") @Test public void testDecorationForEmptyInput() throws Exception { RulesCollectionBuilder builder = new TrieMapRulesCollectionBuilder(false); DecorateInstruction deco = new DecorateInstruction("deco1"); builder.addRule((Input) LineParser.parseInput(LineParser.BOUNDARY + "" + LineParser.BOUNDARY), new Instructions(Arrays.asList((Instruction) deco))); RulesCollection rules = builder.build(); CommonRulesRewriter rewriter = new CommonRulesRewriter(rules); ExpandedQuery query = makeQuery(""); Map<String, Object> context = new HashMap<>(); rewriter.rewrite(query, context); assertThat((Set<Object>) context.get(DecorateInstruction.CONTEXT_KEY), contains( equalTo((Object) "deco1") )); }
@Test public void testThatWildcardOnlyTermIsNotAllowed() throws Exception { try { LineParser.parseTerm("*"); fail("Wildcard-only term must not be allowed"); } catch (IllegalArgumentException e) { // expected } }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionSingleLetter() { assertThat((List<Term>) LineParser.parseTermExpression("a"), contains(term("a"))); }
@Test public void testThatPlayholdersAreParsedForBoostInstruction() throws Exception { String line = "UP(500): 3$1"; String lcLine = line.toLowerCase(); final Object instruction = LineParser .parseBoostInstruction(line, lcLine, 2, BoostDirection.UP, new WhiteSpaceQuerqyParserFactory()); assertTrue(instruction instanceof BoostInstruction); assertTrue(((BoostInstruction) instruction).hasPlaceHolderInBoostQuery()); }
@Test public void testThatBoundariesAreParsedInOtherwiseEmptyInput() throws Exception { Object parseResult = LineParser.parseInput(LineParser.BOUNDARY + "" + LineParser.BOUNDARY); assertTrue(parseResult instanceof Input); Input input = (Input) parseResult; assertTrue(input.requiresLeftBoundary()); assertTrue(input.requiresRightBoundary()); }
@Test public void testThatWildcardOnlyTermIsNotAllowedWithFieldNames() throws Exception { try { LineParser.parseTerm("{f1,f2}:*"); fail("Wildcard-only term must not be allowed with fieldname"); } catch (IllegalArgumentException e) { // expected } }
@SuppressWarnings("unchecked") @Test public void testParseTermExpressionSingleTerm() { assertThat((List<Term>) LineParser.parseTermExpression("abc"), contains(term("abc"))); }
@Test public void testThatBoundariesAreParsedInInput() throws Exception { Object parseResult = LineParser.parseInput(LineParser.BOUNDARY + "a" + LineParser.BOUNDARY); assertTrue(parseResult instanceof Input); Input input = (Input) parseResult; assertTrue(input.requiresLeftBoundary()); assertTrue(input.requiresRightBoundary()); }