Skip to content

Commit

Permalink
SONARXML-220 Fix quality flaws after moving from Java 8 to Java 17 (#327
Browse files Browse the repository at this point in the history
)
  • Loading branch information
alban-auzeill authored Jan 9, 2025
1 parent 5d3914c commit f75a0d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
13 changes: 7 additions & 6 deletions its/plugin/src/test/java/com/sonar/it/xml/SonarLintTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ static void prepare() {
@Test
void simpleXml() throws Exception {
// Rule S1778 is part of SonarWay (characters before prolog)
ClientInputFile inputFile = prepareInputFile("foo.xml",
"<!-- Ohlala, there is a comment before prolog! -->\n"
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<foo>\n"
+ " <bar value='boom' />\n"
+ "</foo>\n");
ClientInputFile inputFile = prepareInputFile("foo.xml", """
<!-- Ohlala, there is a comment before prolog! -->
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<bar value='boom' />
</foo>
""");

List<Issue> issues = new ArrayList<>();
StandaloneAnalysisConfiguration configuration = StandaloneAnalysisConfiguration.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ private static void visitNode(Node node, Set<Integer> linesOfCode, Set<Integer>
case Node.COMMENT_NODE:
addNotEmptyLines(commentLines, node.getTextContent(), range);
break;
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
case Node.TEXT_NODE, Node.CDATA_SECTION_NODE:
addNotEmptyLines(linesOfCode, node.getTextContent(), range);
break;
case Node.DOCUMENT_TYPE_NODE:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ private void highlightNode(Node node) {
addHighlighting(XmlFile.startLocation((CDATASection) node), TypeOfText.KEYWORD);
addHighlighting(XmlFile.endLocation((CDATASection) node), TypeOfText.KEYWORD);
break;
case Node.COMMENT_NODE:
case Node.DOCUMENT_TYPE_NODE:
case Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE:
addHighlighting(XmlFile.nodeLocation(node), TypeOfText.STRUCTURED_COMMENT);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.SonarProduct;
Expand Down Expand Up @@ -84,7 +83,7 @@ public void execute(SensorContext context) {
boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT;

ProgressReport progressReport = new ProgressReport("Report about progress of XML Analyzer", TimeUnit.SECONDS.toMillis(10));
progressReport.start(inputFiles.stream().map(InputFile::toString).collect(Collectors.toList()));
progressReport.start(inputFiles.stream().map(InputFile::toString).toList());

boolean cancelled = false;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class XmlHighlightingTest {
private XmlFile xmlFile;

@BeforeEach
void setUp() throws Exception {
void setUp() {
context = SensorContextTester.create(tmpFolder.getRoot());
fileSystem = context.fileSystem();
}
Expand Down Expand Up @@ -113,12 +113,12 @@ void testHighlightTagWithDoubleQuoteAttribute() throws Exception {

@Test
void testHighlightMultilineTagWithAttributes() throws Exception {
highlight(
"<tag att1='value1' \n"
+ " att2\n"
+ " = 'value2' att3=\n"
+ "'value3' att4='multiline \n"
+ " \" attribute'> </tag>");
highlight("""
<tag att1='value1'\s
att2
= 'value2' att3=
'value3' att4='multiline\s
" attribute'> </tag>""");
// <tag
assertHighlighting(1, 0, 1, 4, TypeOfText.KEYWORD);

Expand Down Expand Up @@ -180,22 +180,18 @@ void testCDATA() throws Exception {

@Test
void testCDATAMultiline() throws Exception {
highlight(
"<tag><![CDATA[foo\n"
+ "bar\n"
+ "]]></tag>");
highlight("""
<tag><![CDATA[foo
bar
]]></tag>""");
assertHighlighting(1, 5, 1, 14, TypeOfText.KEYWORD);
assertHighlighting(3, 0, 3, 3, TypeOfText.KEYWORD);
}

@Test
void testBigCDATA() throws Exception {
StringBuilder sb = new StringBuilder();
int length = 100000;
for (int i = 0; i < length; i++) {
sb.append("a");
}
String cdataContent = sb.toString();
String cdataContent = "a".repeat(length);
highlight("<tag><![CDATA[" + cdataContent + "]]></tag>");
assertHighlighting(5, 14, TypeOfText.KEYWORD);
int expectedCDataEndOffset = 14 + cdataContent.length();
Expand Down Expand Up @@ -251,12 +247,11 @@ void testHighlightTagWithNamespace() throws Exception {

@Test
void testHighlightingTagWithNameSpaceMultipleLine() throws Exception {
highlight(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
+ "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
// ...
+ "</project>");
highlight("""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
</project>""");
// xmlns:xsi
assertHighlighting(2, 51, 2, 60, TypeOfText.CONSTANT);
// "http://www.w3.org/2001/XMLSchema-instance"
Expand Down Expand Up @@ -287,11 +282,11 @@ void testXMLHeader() throws Exception {

@Test
void testCharBeforeProlog() throws Exception {
highlightFromFile("char_before_prolog.xml",
"\n"
+ "\n"
+ "\n"
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <tag/>");
highlightFromFile("char_before_prolog.xml", """
<?xml version="1.0" encoding="UTF-8" ?> <tag/>""");
// <?xml
assertHighlighting(4, 0, 4, 5, TypeOfText.KEYWORD);
// ?>
Expand Down

0 comments on commit f75a0d5

Please sign in to comment.