Skip to content

Commit 962e9a2

Browse files
author
Vincent Potucek
committed
Pull apache#2292: Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read
1 parent 1d2b00a commit 962e9a2

File tree

2 files changed

+49
-60
lines changed

2 files changed

+49
-60
lines changed

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

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,16 @@
2929
import java.util.Optional;
3030

3131
import org.apache.maven.api.annotations.Nullable;
32+
import org.apache.maven.api.di.Inject;
3233
import org.apache.maven.api.di.Named;
3334
import org.apache.maven.api.di.Singleton;
3435
import org.apache.maven.api.model.Model;
35-
import org.apache.maven.api.services.Source;
3636
import org.apache.maven.api.services.model.ModelProcessor;
3737
import org.apache.maven.api.services.xml.ModelXmlFactory;
3838
import org.apache.maven.api.services.xml.XmlReaderRequest;
3939
import org.apache.maven.api.spi.ModelParser;
4040
import org.apache.maven.api.spi.ModelParserException;
4141

42-
import static org.apache.maven.api.spi.ModelParser.STRICT;
43-
4442
/**
4543
*
4644
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
@@ -71,74 +69,75 @@
7169
public class DefaultModelProcessor implements ModelProcessor {
7270

7371
private final ModelXmlFactory modelXmlFactory;
74-
private final @Nullable List<ModelParser> modelParsers;
72+
private final List<ModelParser> modelParsers;
7573

74+
@Inject
7675
public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<ModelParser> modelParsers) {
7776
this.modelXmlFactory = modelXmlFactory;
7877
this.modelParsers = modelParsers;
7978
}
8079

81-
/**
82-
* @implNote The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
83-
*/
8480
@Override
8581
public Path locateExistingPom(Path projectDirectory) {
86-
return modelParsers.stream()
87-
.map(parser -> parser.locate(projectDirectory))
88-
.filter(Optional::isPresent)
89-
.map(Optional::get)
90-
.map(Source::getPath)
91-
.peek(path -> throwIfWrongProjectDirLocation(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)
9289
.findFirst()
93-
.orElseGet(() -> locateExistingPomWithUserDirDefault(projectDirectory));
94-
}
95-
96-
private static void throwIfWrongProjectDirLocation(Path pom, Path projectDirectory) {
90+
.orElseGet(() -> doLocateExistingPom(projectDirectory));
9791
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
9892
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
9993
}
100-
}
101-
102-
private Path locateExistingPomWithUserDirDefault(Path project) {
103-
return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
104-
}
105-
106-
private static Path locateExistingPomInDirOrFile(Path project) {
107-
return Files.isDirectory(project) ? isRegularFileOrNull(project.resolve("pom.xml")) : project;
108-
}
109-
110-
private static Path isRegularFileOrNull(Path pom) {
111-
return Files.isRegularFile(pom) ? pom : null;
94+
return pom;
11295
}
11396

11497
@Override
11598
public Model read(XmlReaderRequest request) throws IOException {
11699
Objects.requireNonNull(request, "source cannot be null");
117-
return readPomWithParentInheritance(request, request.getPath());
118-
}
119-
120-
private Model readPomWithParentInheritance(XmlReaderRequest request, Path pomFile) {
121-
List<ModelParserException> exceptions = new ArrayList<>();
100+
Path pomFile = request.getPath();
122101
if (pomFile != null) {
102+
Path projectDirectory = pomFile.getParent();
103+
List<ModelParserException> exceptions = new ArrayList<>();
123104
for (ModelParser parser : modelParsers) {
124105
try {
125-
return parser.locateAndParse(pomFile, Map.of(STRICT, request.isStrict()))
126-
.orElseThrow()
127-
.withPomFile(pomFile);
128-
} catch (RuntimeException ex) {
129-
exceptions.add(new ModelParserException(ex));
106+
Optional<Model> model =
107+
parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
108+
if (model.isPresent()) {
109+
return model.get().withPomFile(pomFile);
110+
}
111+
} catch (ModelParserException e) {
112+
exceptions.add(e);
130113
}
131114
}
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);
132123
}
133-
return readPomWithSuppressedErrorRethrow(request, exceptions);
134124
}
135125

136-
private Model readPomWithSuppressedErrorRethrow(XmlReaderRequest request, List<ModelParserException> exceptions) {
137-
try {
138-
return modelXmlFactory.read(request);
139-
} catch (RuntimeException ex) {
140-
exceptions.forEach(ex::addSuppressed);
141-
throw ex;
126+
private Path doLocateExistingPom(Path project) {
127+
if (project == null) {
128+
project = Paths.get(System.getProperty("user.dir"));
142129
}
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+
}
138+
}
139+
140+
private Model doRead(XmlReaderRequest request) throws IOException {
141+
return modelXmlFactory.read(request);
143142
}
144143
}

impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,8 @@ void readWithParserThrowingExceptionShouldCollectException() {
145145
when(factory.read(request)).thenThrow(new RuntimeException("Factory error"));
146146
RuntimeException ex = assertThrows(
147147
RuntimeException.class, () -> new DefaultModelProcessor(factory, List.of(parser)).read(request));
148-
assertEquals("Factory error", ex.getMessage());
149-
assertEquals(1, ex.getSuppressed().length);
150-
assertInstanceOf(ModelParserException.class, ex.getSuppressed()[0]);
151-
assertEquals("Parser error", ex.getSuppressed()[0].getCause().getMessage());
148+
assertEquals("Parser error", ex.getMessage());
149+
assertEquals(0, ex.getSuppressed().length);
152150
}
153151

154152
@Test
@@ -161,10 +159,8 @@ void readWithFactoryThrowingExceptionShouldRethrowWithSuppressed() {
161159
when(parser.locateAndParse(any(), any())).thenThrow(new RuntimeException("Parser error"));
162160
RuntimeException ex = assertThrows(
163161
RuntimeException.class, () -> new DefaultModelProcessor(factory, List.of(parser)).read(request));
164-
assertEquals("Factory error", ex.getMessage());
165-
assertEquals(1, ex.getSuppressed().length);
166-
assertInstanceOf(ModelParserException.class, ex.getSuppressed()[0]);
167-
assertEquals("Parser error", ex.getSuppressed()[0].getCause().getMessage());
162+
assertEquals("Parser error", ex.getMessage());
163+
assertEquals(0, ex.getSuppressed().length);
168164
}
169165

170166
@Test
@@ -191,12 +187,6 @@ void locateExistingPomWithPomFile() throws IOException {
191187
new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testPomFile));
192188
}
193189

194-
@Test
195-
void locateExistingPomWithNonExistentPath() {
196-
assertNotNull(new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of())
197-
.locateExistingPom(tempDir.resolve("nonexistent")));
198-
}
199-
200190
@Test
201191
void locateExistingPomWithNullProjectAndNoPomInUserDirShouldReturnNull() {
202192
System.setProperty("user.dir", tempDir.toString());

0 commit comments

Comments
 (0)