Skip to content

Commit 0b043ae

Browse files
author
Vincent Potucek
committed
Pull #2304: test DefaultModelProcessor#read
1 parent 6be7a12 commit 0b043ae

File tree

2 files changed

+374
-29
lines changed

2 files changed

+374
-29
lines changed

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

Lines changed: 27 additions & 29 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.
@@ -94,50 +97,45 @@ public Path locateExistingPom(Path projectDirectory) {
9497
return pom;
9598
}
9699

100+
private Path doLocateExistingPom(Path project) {
101+
if (project == null) {
102+
project = Paths.get(System.getProperty("user.dir"));
103+
} else if (Files.isDirectory(project)) {
104+
Path pom = project.resolve("pom.xml");
105+
return Files.isRegularFile(pom) ? pom : null;
106+
}
107+
return project;
108+
}
109+
97110
@Override
98111
public Model read(XmlReaderRequest request) throws IOException {
99-
Objects.requireNonNull(request, "source cannot be null");
100-
Path pomFile = request.getPath();
112+
Path pomFile = requireNonNull(request, "source cannot be null").getPath();
101113
if (pomFile != null) {
102-
Path projectDirectory = pomFile.getParent();
103114
List<ModelParserException> exceptions = new ArrayList<>();
104115
for (ModelParser parser : modelParsers) {
105116
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);
117+
Optional<Model> parent = parent(request, parser, pomFile.getParent());
118+
if (parent.isPresent()) {
119+
return parent.get().withPomFile(pomFile);
110120
}
111121
} catch (ModelParserException e) {
112122
exceptions.add(e);
113123
}
114124
}
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);
125+
throwErrorsSuppressed(exceptions);
123126
}
127+
return modelXmlFactory.read(request);
124128
}
125129

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-
}
130+
private static Optional<Model> parent(XmlReaderRequest request, ModelParser parser, Path projectDirectory) {
131+
return parser.locateAndParse(projectDirectory, Map.of(STRICT, request.isStrict()));
138132
}
139133

140-
private Model doRead(XmlReaderRequest request) throws IOException {
141-
return modelXmlFactory.read(request);
134+
private static void throwErrorsSuppressed(List<ModelParserException> exceptions) throws IOException {
135+
if (!exceptions.isEmpty()) {
136+
IOException ex = new IOException();
137+
exceptions.forEach(ex::addSuppressed);
138+
throw ex;
139+
}
142140
}
143141
}

0 commit comments

Comments
 (0)