Skip to content

Commit 56789e1

Browse files
committed
test: replace serialized fixture with in-memory round trip
Signed-off-by: 付典 <fudianchn@gmail.com>
2 parents 0998415 + 7b08430 commit 56789e1

3 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎src/main/java/net/sf/jsqlparser/parser/ASTNodeAccessImpl.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
public class ASTNodeAccessImpl implements ASTNodeAccess {
1919

20+
private static final long serialVersionUID = 3920262240331742793L;
21+
2022
private transient Node node;
2123

2224
@Override
@@ -31,8 +33,8 @@ public void setASTNode(Node node) {
3133

3234
/**
3335
* The comments preceding this node's first token, in source order, with the original token
34-
* images (line comments keep their line terminator). Empty for nodes built programmatically,
35-
* which carry no AST.
36+
* images. Line comments do not include their line terminator; callers must add a line break
37+
* when prepending them to SQL. Empty for nodes built programmatically, which carry no AST.
3638
*/
3739
public List<String> getLeadingComments() {
3840
Node node = getASTNode();

‎src/test/java/net/sf/jsqlparser/parser/LeadingCommentsTest.java‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
import static org.junit.jupiter.api.Assertions.assertEquals;
1313
import static org.junit.jupiter.api.Assertions.assertFalse;
14-
import static org.junit.jupiter.api.Assertions.assertTrue;
1514

1615
import java.util.List;
1716
import net.sf.jsqlparser.JSQLParserException;
1817
import net.sf.jsqlparser.statement.Statements;
1918
import net.sf.jsqlparser.statement.select.PlainSelect;
2019
import net.sf.jsqlparser.statement.select.Select;
2120
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.params.ParameterizedTest;
22+
import org.junit.jupiter.params.provider.ValueSource;
2223

2324
public class LeadingCommentsTest {
2425

@@ -29,12 +30,20 @@ public void blockCommentsBeforeStatementInSourceOrder() throws JSQLParserExcepti
2930
assertEquals(List.of("/* keep orderby */", "/*+ FULL(t) */"), select.getLeadingComments());
3031
}
3132

33+
@ParameterizedTest
34+
@ValueSource(strings = {"\n", "\r\n", "\r"})
35+
public void lineCommentsExcludeLineTerminators(String lineTerminator)
36+
throws JSQLParserException {
37+
Select select =
38+
(Select) CCJSqlParserUtil.parse("-- paging hint" + lineTerminator + "SELECT 1");
39+
assertEquals(List.of("-- paging hint"), select.getLeadingComments());
40+
}
41+
3242
@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"));
43+
public void mixedCommentsRetainExactImagesAndSourceOrder() throws JSQLParserException {
44+
Select select = (Select) CCJSqlParserUtil
45+
.parse("/* a */ -- b\r\n// c\r/* d */ SELECT 1");
46+
assertEquals(List.of("/* a */", "-- b", "// c", "/* d */"), select.getLeadingComments());
3847
}
3948

4049
@Test

‎src/test/java/net/sf/jsqlparser/statement/SerializationTest.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.ObjectInputStream;
1616
import java.io.ObjectOutputStream;
1717
import java.math.BigDecimal;
18+
import java.util.List;
1819
import java.util.stream.Stream;
1920
import net.sf.jsqlparser.JSQLParserException;
2021
import net.sf.jsqlparser.expression.SpannerInterleaveIn;
@@ -34,6 +35,17 @@
3435
import org.junit.jupiter.params.provider.ValueSource;
3536

3637
public class SerializationTest {
38+
@Test
39+
void serializeSelectWithLeadingComments() throws Exception {
40+
PlainSelect original = (PlainSelect) CCJSqlParserUtil
41+
.parse("/* keep orderby */ SELECT id FROM mytable");
42+
Assertions.assertEquals(List.of("/* keep orderby */"), original.getLeadingComments());
43+
PlainSelect copy = roundTrip(original);
44+
Assertions.assertEquals("SELECT id FROM mytable", copy.toString());
45+
Assertions.assertNull(copy.getASTNode());
46+
Assertions.assertEquals(List.of(), copy.getLeadingComments());
47+
}
48+
3749
@Test
3850
void serializeWithItem() throws JSQLParserException, IOException, ClassNotFoundException {
3951
String sqlStr =

0 commit comments

Comments
 (0)