QueryPrologLexer { public static Token getRestOfQueryToken(String input) { Token result = null; for (int i = 0; i < input.length();) { char c = input.charAt(i); switch (c) { case '#': String comment = readComment(input, i); i += comment.length() + 1; break; case 'p': case 'P': String prefix = readPrefix(input, i); if (prefix == null) { prefix = ""; } i = i + prefix.length() + 7; break; case 'b': case 'B': i += 4; break; case '<': String iri = readIRI(input, i); if (iri == null) { iri = ""; } i += iri.length() + 2; break; default: if (Character.isWhitespace(c)) { i++; } else { String restOfQuery = input.substring(i); result = (new Token(TokenType.REST_OF_QUERY, restOfQuery)); i += restOfQuery.length(); } break; } } return result; }  static List<Token> lex(String input); static Token getRestOfQueryToken(String input);  }
@Test public void testFinalTokenEmptyString() { try { Token t = QueryPrologLexer.getRestOfQueryToken(""); } catch (Exception e) { fail("lexer should not throw exception on malformed input"); } }
@Test public void testFinalTokenNoProlog1() { Token t = QueryPrologLexer.getRestOfQueryToken("SELECT * WHERE {?s ?p ?o} "); assertNotNull(t); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); }
@Test public void testFinalTokenWithComment() { Token t = QueryPrologLexer.getRestOfQueryToken("# COMMENT \n SELECT * WHERE {?s ?p ?o} "); assertNotNull(t); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testFinalTokenWithMultilineComment() { Token t = QueryPrologLexer .getRestOfQueryToken("# COMMENT \n# COMMENT (continued) \nSELECT * WHERE {?s ?p ?o} "); assertNotNull(t); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE {?s ?p ?o} ", t.s); }
@Test public void testFinalTokenWithMultilineComment2() { Token t = QueryPrologLexer .getRestOfQueryToken("#comment1\n#another comment\n" + "SELECT * WHERE { ?s ?p ?o } LIMIT 1"); assertNotNull(t); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); assertEquals("SELECT * WHERE { ?s ?p ?o } LIMIT 1", t.s); }
@Test public void testFinalTokenWithBaseAndComment() { Token t = QueryPrologLexer.getRestOfQueryToken("BASE <foobar> # COMMENT \nSELECT * WHERE {?s ?p ?o} "); assertNotNull(t); assertTrue(t.getType().equals(TokenType.REST_OF_QUERY)); }
@Test public void testFinalTokenSyntaxError() { try { Token t = QueryPrologLexer .getRestOfQueryToken("BASE <foobar # missing closing bracket \nSELECT * WHERE {?s ?p ?o} "); } catch (Exception e) { fail("malformed query should not make lexer fail"); } }
@Test public void testFinalTokenSyntaxErrorPrefix() { try { Token t = QueryPrologLexer.getRestOfQueryToken("PREFIX"); } catch (Exception e) { fail("Malformed query should not make lexer throw Exception"); } }