Skip to content

Commit f75a0d5

Browse files
SONARXML-220 Fix quality flaws after moving from Java 8 to Java 17 (#327)
1 parent 5d3914c commit f75a0d5

File tree

5 files changed

+32
-39
lines changed

5 files changed

+32
-39
lines changed

its/plugin/src/test/java/com/sonar/it/xml/SonarLintTest.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,13 @@ static void prepare() {
6868
@Test
6969
void simpleXml() throws Exception {
7070
// Rule S1778 is part of SonarWay (characters before prolog)
71-
ClientInputFile inputFile = prepareInputFile("foo.xml",
72-
"<!-- Ohlala, there is a comment before prolog! -->\n"
73-
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
74-
+ "<foo>\n"
75-
+ " <bar value='boom' />\n"
76-
+ "</foo>\n");
71+
ClientInputFile inputFile = prepareInputFile("foo.xml", """
72+
<!-- Ohlala, there is a comment before prolog! -->
73+
<?xml version="1.0" encoding="UTF-8"?>
74+
<foo>
75+
<bar value='boom' />
76+
</foo>
77+
""");
7778

7879
List<Issue> issues = new ArrayList<>();
7980
StandaloneAnalysisConfiguration configuration = StandaloneAnalysisConfiguration.builder()

sonar-xml-plugin/src/main/java/org/sonar/plugins/xml/LineCounter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ private static void visitNode(Node node, Set<Integer> linesOfCode, Set<Integer>
8686
case Node.COMMENT_NODE:
8787
addNotEmptyLines(commentLines, node.getTextContent(), range);
8888
break;
89-
case Node.TEXT_NODE:
90-
case Node.CDATA_SECTION_NODE:
89+
case Node.TEXT_NODE, Node.CDATA_SECTION_NODE:
9190
addNotEmptyLines(linesOfCode, node.getTextContent(), range);
9291
break;
9392
case Node.DOCUMENT_TYPE_NODE:

sonar-xml-plugin/src/main/java/org/sonar/plugins/xml/XmlHighlighting.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ private void highlightNode(Node node) {
6161
addHighlighting(XmlFile.startLocation((CDATASection) node), TypeOfText.KEYWORD);
6262
addHighlighting(XmlFile.endLocation((CDATASection) node), TypeOfText.KEYWORD);
6363
break;
64-
case Node.COMMENT_NODE:
65-
case Node.DOCUMENT_TYPE_NODE:
64+
case Node.COMMENT_NODE, Node.DOCUMENT_TYPE_NODE:
6665
addHighlighting(XmlFile.nodeLocation(node), TypeOfText.STRUCTURED_COMMENT);
6766
break;
6867
default:

sonar-xml-plugin/src/main/java/org/sonar/plugins/xml/XmlSensor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323
import java.util.concurrent.TimeUnit;
24-
import java.util.stream.Collectors;
2524
import org.slf4j.Logger;
2625
import org.slf4j.LoggerFactory;
2726
import org.sonar.api.SonarProduct;
@@ -84,7 +83,7 @@ public void execute(SensorContext context) {
8483
boolean isSonarLintContext = context.runtime().getProduct() == SonarProduct.SONARLINT;
8584

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

8988
boolean cancelled = false;
9089
try {

sonar-xml-plugin/src/test/java/org/sonar/plugins/xml/XmlHighlightingTest.java

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class XmlHighlightingTest {
5151
private XmlFile xmlFile;
5252

5353
@BeforeEach
54-
void setUp() throws Exception {
54+
void setUp() {
5555
context = SensorContextTester.create(tmpFolder.getRoot());
5656
fileSystem = context.fileSystem();
5757
}
@@ -113,12 +113,12 @@ void testHighlightTagWithDoubleQuoteAttribute() throws Exception {
113113

114114
@Test
115115
void testHighlightMultilineTagWithAttributes() throws Exception {
116-
highlight(
117-
"<tag att1='value1' \n"
118-
+ " att2\n"
119-
+ " = 'value2' att3=\n"
120-
+ "'value3' att4='multiline \n"
121-
+ " \" attribute'> </tag>");
116+
highlight("""
117+
<tag att1='value1'\s
118+
att2
119+
= 'value2' att3=
120+
'value3' att4='multiline\s
121+
" attribute'> </tag>""");
122122
// <tag
123123
assertHighlighting(1, 0, 1, 4, TypeOfText.KEYWORD);
124124

@@ -180,22 +180,18 @@ void testCDATA() throws Exception {
180180

181181
@Test
182182
void testCDATAMultiline() throws Exception {
183-
highlight(
184-
"<tag><![CDATA[foo\n"
185-
+ "bar\n"
186-
+ "]]></tag>");
183+
highlight("""
184+
<tag><![CDATA[foo
185+
bar
186+
]]></tag>""");
187187
assertHighlighting(1, 5, 1, 14, TypeOfText.KEYWORD);
188188
assertHighlighting(3, 0, 3, 3, TypeOfText.KEYWORD);
189189
}
190190

191191
@Test
192192
void testBigCDATA() throws Exception {
193-
StringBuilder sb = new StringBuilder();
194193
int length = 100000;
195-
for (int i = 0; i < length; i++) {
196-
sb.append("a");
197-
}
198-
String cdataContent = sb.toString();
194+
String cdataContent = "a".repeat(length);
199195
highlight("<tag><![CDATA[" + cdataContent + "]]></tag>");
200196
assertHighlighting(5, 14, TypeOfText.KEYWORD);
201197
int expectedCDataEndOffset = 14 + cdataContent.length();
@@ -251,12 +247,11 @@ void testHighlightTagWithNamespace() throws Exception {
251247

252248
@Test
253249
void testHighlightingTagWithNameSpaceMultipleLine() throws Exception {
254-
highlight(
255-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
256-
+ "<project xmlns=\"http://maven.apache.org/POM/4.0.0\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
257-
+ "xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">\n"
258-
// ...
259-
+ "</project>");
250+
highlight("""
251+
<?xml version="1.0" encoding="UTF-8"?>
252+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
253+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
254+
</project>""");
260255
// xmlns:xsi
261256
assertHighlighting(2, 51, 2, 60, TypeOfText.CONSTANT);
262257
// "http://www.w3.org/2001/XMLSchema-instance"
@@ -287,11 +282,11 @@ void testXMLHeader() throws Exception {
287282

288283
@Test
289284
void testCharBeforeProlog() throws Exception {
290-
highlightFromFile("char_before_prolog.xml",
291-
"\n"
292-
+ "\n"
293-
+ "\n"
294-
+ "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <tag/>");
285+
highlightFromFile("char_before_prolog.xml", """
286+
287+
288+
289+
<?xml version="1.0" encoding="UTF-8" ?> <tag/>""");
295290
// <?xml
296291
assertHighlighting(4, 0, 4, 5, TypeOfText.KEYWORD);
297292
// ?>

0 commit comments

Comments
 (0)