Skip to content

Commit

Permalink
Do not log ClassCastException in MavenParser in case of malformed P…
Browse files Browse the repository at this point in the history
…OM (#3701)

* [#3699] WIP, Wrote the test and the fix

(cherry picked from commit b8545e1)

* [#3699] Small optimisations

* Test ParseError by directly invoking the MavenParser

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
api-from-the-ion and timtebeek authored Nov 17, 2023
1 parent 172657a commit eee01dd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
14 changes: 10 additions & 4 deletions rewrite-maven/src/main/java/org/openrewrite/maven/MavenParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,18 @@ public Stream<SourceFile> parseInputs(Iterable<Input> sources, @Nullable Path re
pom.getProperties().put("project.basedir", baseDir);
pom.getProperties().put("basedir", baseDir);

Xml.Document xml = (Xml.Document) new MavenXmlParser()
SourceFile sourceFile = new MavenXmlParser()
.parseInputs(singletonList(source), relativeTo, ctx)
.iterator().next();

projectPoms.put(xml, pom);
projectPomsByPath.put(pomPath, pom);
if (sourceFile instanceof Xml.Document) {
Xml.Document xml = (Xml.Document) sourceFile;

projectPoms.put(xml, pom);
projectPomsByPath.put(pomPath, pom);
} else {
parsed.add(sourceFile);
}
} catch (Throwable t) {
ctx.getOnError().accept(t);
parsed.add(ParseError.build(this, source, relativeTo, ctx, t));
Expand Down Expand Up @@ -185,7 +191,7 @@ public Builder mavenConfig(@Nullable Path mavenConfig) {
if (mavenConfig != null && mavenConfig.toFile().exists()) {
try {
String mavenConfigText = new String(Files.readAllBytes(mavenConfig));
Matcher matcher = Pattern.compile("(?:$|\\s)-P\\s+([^\\s]+)").matcher(mavenConfigText);
Matcher matcher = Pattern.compile("(?:$|\\s)-P\\s+(\\S+)").matcher(mavenConfigText);
if (matcher.find()) {
String[] profiles = matcher.group(1).split(",");
return activeProfiles(profiles);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openrewrite.maven.internal.MavenParsingException;
import org.openrewrite.maven.tree.*;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.tree.ParseError;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -853,7 +854,6 @@ void mirrorsAndAuth() throws IOException {
var password = "password";
try (MockWebServer mockRepo = new MockWebServer()) {
mockRepo.setDispatcher(new Dispatcher() {
@SuppressWarnings("NullableProblems")
@Override
public MockResponse dispatch(RecordedRequest request) {
MockResponse resp = new MockResponse();
Expand Down Expand Up @@ -2237,4 +2237,26 @@ void exclusions() {
))
);
}

@Test
void malformedPom() {
String malformedPomXml = """
<project>
<groupId>com.mycompany.app</groupId>
<artifactId>my-app</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>&gt;
</dependency>
</dependencies>
</project>
""";
assertThat(MavenParser.builder().build().parse(malformedPomXml))
.singleElement()
.isInstanceOf(ParseError.class);
}
}

0 comments on commit eee01dd

Please sign in to comment.