|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2019 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.parser; |
| 11 | + |
| 12 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 13 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 14 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import net.sf.jsqlparser.JSQLParserException; |
| 18 | +import net.sf.jsqlparser.statement.Statements; |
| 19 | +import net.sf.jsqlparser.statement.select.PlainSelect; |
| 20 | +import net.sf.jsqlparser.statement.select.Select; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +public class LeadingCommentsTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + public void blockCommentsBeforeStatementInSourceOrder() throws JSQLParserException { |
| 27 | + Select select = (Select) CCJSqlParserUtil |
| 28 | + .parse("/* keep orderby */ /*+ FULL(t) */ SELECT * FROM mytable"); |
| 29 | + assertEquals(List.of("/* keep orderby */", "/*+ FULL(t) */"), select.getLeadingComments()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void lineCommentBeforeStatement() throws JSQLParserException { |
| 34 | + Select select = (Select) CCJSqlParserUtil.parse("-- paging hint\nSELECT 1"); |
| 35 | + List<String> comments = select.getLeadingComments(); |
| 36 | + assertEquals(1, comments.size()); |
| 37 | + assertTrue(comments.get(0).startsWith("-- paging hint")); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void commentInsideStatementIsNotLeading() throws JSQLParserException { |
| 42 | + Select select = (Select) CCJSqlParserUtil.parse("SELECT /* mid comment */ 1"); |
| 43 | + assertEquals(List.of(), select.getLeadingComments()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void statementWithoutComments() throws JSQLParserException { |
| 48 | + Select select = (Select) CCJSqlParserUtil.parse("SELECT * FROM mytable"); |
| 49 | + assertEquals(List.of(), select.getLeadingComments()); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void programmaticStatementHasNoLeadingComments() { |
| 54 | + assertEquals(List.of(), new PlainSelect().getLeadingComments()); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void toStringDropsLeadingCommentsAsBefore() throws JSQLParserException { |
| 59 | + Select select = (Select) CCJSqlParserUtil |
| 60 | + .parse("/* keep orderby */ SELECT * FROM mytable"); |
| 61 | + assertFalse(select.toString().contains("/*")); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void commentsSplitPerStatement() throws JSQLParserException { |
| 66 | + Statements statements = CCJSqlParserUtil |
| 67 | + .parseStatements("/* first */ SELECT 1; /* second */ SELECT 2"); |
| 68 | + assertEquals(List.of("/* first */"), ((Select) statements.get(0)).getLeadingComments()); |
| 69 | + assertEquals(List.of("/* second */"), ((Select) statements.get(1)).getLeadingComments()); |
| 70 | + } |
| 71 | +} |
0 commit comments