Skip to content

Commit 49d13d9

Browse files
author
Vincent Potucek
committed
Pull #2304: Modernize codebase with Java improvements - test DefaultModelProcessor#read
1 parent 6be7a12 commit 49d13d9

File tree

2 files changed

+443
-38
lines changed

2 files changed

+443
-38
lines changed

impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import org.apache.maven.api.spi.ModelParser;
4040
import org.apache.maven.api.spi.ModelParserException;
4141

42+
import static java.util.Objects.requireNonNull;
43+
import static org.apache.maven.api.spi.ModelParser.STRICT;
44+
4245
/**
4346
*
4447
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
@@ -77,67 +80,69 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
7780
this.modelParsers = modelParsers;
7881
}
7982

83+
/**
84+
* @implNote The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
85+
*/
8086
@Override
8187
public Path locateExistingPom(Path projectDirectory) {
82-
// Note that the ModelProcessor#locatePom never returns null
83-
// while the ModelParser#locatePom needs to return an existing path!
84-
Path pom = modelParsers.stream()
85-
.map(m -> m.locate(projectDirectory)
86-
.map(org.apache.maven.api.services.Source::getPath)
87-
.orElse(null))
88-
.filter(Objects::nonNull)
89-
.findFirst()
90-
.orElseGet(() -> doLocateExistingPom(projectDirectory));
88+
return throwIfWrongProjectDirLocation(
89+
projectDirectory,
90+
modelParsers.stream()
91+
.map(m -> m.locate(projectDirectory)
92+
.map(org.apache.maven.api.services.Source::getPath)
93+
.orElse(null))
94+
.filter(Objects::nonNull)
95+
.findFirst()
96+
.orElseGet(() -> locateExistingPomWithFallback(projectDirectory)));
97+
}
98+
99+
private static Path throwIfWrongProjectDirLocation(Path projectDirectory, Path pom) {
91100
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
92101
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
93102
}
94103
return pom;
95104
}
96105

106+
private Path locateExistingPomWithFallback(Path project) {
107+
if (project != null) {
108+
if (Files.isDirectory(project)) {
109+
Path pom = project.resolve("pom.xml");
110+
return Files.isRegularFile(pom) ? pom : null;
111+
}
112+
return Files.isRegularFile(project) ? project : null;
113+
}
114+
return locateExistingPomWithFallback(Paths.get(System.getProperty("user.dir"))); // check in user dir
115+
}
116+
97117
@Override
98118
public Model read(XmlReaderRequest request) throws IOException {
99-
Objects.requireNonNull(request, "source cannot be null");
100-
Path pomFile = request.getPath();
119+
Path pomFile = requireNonNull(request, "source cannot be null").getPath();
101120
if (pomFile != null) {
102-
Path projectDirectory = pomFile.getParent();
103121
List<ModelParserException> exceptions = new ArrayList<>();
104122
for (ModelParser parser : modelParsers) {
105123
try {
106-
Optional<Model> model =
107-
parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
108-
if (model.isPresent()) {
109-
return model.get().withPomFile(pomFile);
124+
Optional<Model> parent = parent(request, parser, pomFile.getParent());
125+
if (parent.isPresent()) {
126+
return parent.get().withPomFile(pomFile);
110127
}
111128
} catch (ModelParserException e) {
112129
exceptions.add(e);
113130
}
114131
}
115-
try {
116-
return doRead(request);
117-
} catch (IOException e) {
118-
exceptions.forEach(e::addSuppressed);
119-
throw e;
120-
}
121-
} else {
122-
return doRead(request);
132+
throwErrorsSuppressed(exceptions);
123133
}
134+
return modelXmlFactory.read(request);
124135
}
125136

126-
private Path doLocateExistingPom(Path project) {
127-
if (project == null) {
128-
project = Paths.get(System.getProperty("user.dir"));
129-
}
130-
if (Files.isDirectory(project)) {
131-
Path pom = project.resolve("pom.xml");
132-
return Files.isRegularFile(pom) ? pom : null;
133-
} else if (Files.isRegularFile(project)) {
134-
return project;
135-
} else {
136-
return null;
137-
}
137+
private static Optional<Model> parent(XmlReaderRequest request, ModelParser parser, Path projectDirectory) {
138+
return parser.locateAndParse(projectDirectory, Map.of(STRICT, request.isStrict()));
138139
}
139140

140-
private Model doRead(XmlReaderRequest request) throws IOException {
141-
return modelXmlFactory.read(request);
141+
private static void throwErrorsSuppressed(List<ModelParserException> exceptions) throws IOException {
142+
if (!exceptions.isEmpty()) {
143+
IOException ex = new IOException();
144+
exceptions.forEach(ex::addSuppressed);
145+
throw ex;
146+
}
142147
}
143148
}

0 commit comments

Comments
 (0)