Skip to content

Commit ebe1e37

Browse files
author
Vincent Potucek
committed
Pull apache#2292: Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read
1 parent 3994d80 commit ebe1e37

File tree

2 files changed

+87
-8
lines changed

2 files changed

+87
-8
lines changed

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,11 @@ private Path doLocateExistingPom(Path project) {
127127
if (project == null) {
128128
project = Paths.get(System.getProperty("user.dir"));
129129
}
130-
if (Files.isDirectory(project)) {
130+
else if (Files.isDirectory(project)) {
131131
Path pom = project.resolve("pom.xml");
132132
return Files.isRegularFile(pom) ? pom : null;
133-
} else if (Files.isRegularFile(project)) {
134-
return project;
135-
} else {
136-
return null;
137133
}
134+
return project;
138135
}
139136

140137
private Model doRead(XmlReaderRequest request) throws IOException {

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

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@
3636

3737
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
3838
import static org.junit.jupiter.api.Assertions.*;
39-
import static org.mockito.Mockito.any;
40-
import static org.mockito.Mockito.mock;
41-
import static org.mockito.Mockito.when;
39+
import static org.mockito.Mockito.*;
4240

4341
class DefaultModelProcessorTest {
4442

@@ -249,7 +247,91 @@ void readWithParserThrowingModelParserExceptionShouldAddAsSuppressed() throws Ex
249247
// In this case, since factory succeeds, no exception is thrown
250248
}
251249

250+
@Test
251+
void readWithParserReturningEmptyShouldTryNextParser() throws Exception {
252+
ModelXmlFactory factory = mock(ModelXmlFactory.class);
253+
ModelParser parser1 = mock(ModelParser.class);
254+
ModelParser parser2 = mock(ModelParser.class);
255+
XmlReaderRequest request = mock(XmlReaderRequest.class);
256+
Path pomPath = Path.of("project/pom.xml");
257+
when(request.getPath()).thenReturn(pomPath);
258+
when(request.isStrict()).thenReturn(true);
259+
260+
Model expectedModel = mock(Model.class);
261+
when(expectedModel.withPomFile(pomPath)).thenReturn(expectedModel);
262+
263+
// First parser returns empty
264+
when(parser1.locateAndParse(any(), any())).thenReturn(Optional.empty());
265+
// Second parser returns model
266+
when(parser2.locateAndParse(any(), any())).thenReturn(Optional.of(expectedModel));
267+
268+
Model result = new DefaultModelProcessor(factory, List.of(parser1, parser2)).read(request);
269+
270+
assertSame(expectedModel, result);
271+
}
272+
273+
@Test
274+
void readWithAllParsersReturningEmptyShouldFallbackToFactory() throws Exception {
275+
ModelXmlFactory factory = mock(ModelXmlFactory.class);
276+
ModelParser parser1 = mock(ModelParser.class);
277+
ModelParser parser2 = mock(ModelParser.class);
278+
XmlReaderRequest request = mock(XmlReaderRequest.class);
279+
Path pomPath = Path.of("project/pom.xml");
280+
when(request.getPath()).thenReturn(pomPath);
281+
when(request.isStrict()).thenReturn(true);
282+
283+
Model expectedModel = mock(Model.class);
284+
285+
// Both parsers return empty
286+
when(parser1.locateAndParse(any(), any())).thenReturn(Optional.empty());
287+
when(parser2.locateAndParse(any(), any())).thenReturn(Optional.empty());
288+
// Factory returns model
289+
when(factory.read(request)).thenReturn(expectedModel);
290+
291+
Model result = new DefaultModelProcessor(factory, List.of(parser1, parser2)).read(request);
292+
293+
assertSame(expectedModel, result);
294+
}
252295

296+
@Test
297+
void readWithParserReturningModelShouldUseThatModel() throws Exception {
298+
ModelXmlFactory factory = mock(ModelXmlFactory.class);
299+
ModelParser parser = mock(ModelParser.class);
300+
XmlReaderRequest request = mock(XmlReaderRequest.class);
301+
Path pomPath = Path.of("project/pom.xml");
302+
when(request.getPath()).thenReturn(pomPath);
303+
when(request.isStrict()).thenReturn(true);
304+
305+
Model expectedModel = mock(Model.class);
306+
when(expectedModel.withPomFile(pomPath)).thenReturn(expectedModel);
307+
when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(expectedModel));
308+
309+
Model result = new DefaultModelProcessor(factory, List.of(parser)).read(request);
310+
311+
assertSame(expectedModel, result);
312+
// Verify factory was not called
313+
verify(factory, never()).read((Path) any());
314+
}
315+
316+
@Test
317+
void readWithParserReturningModelShouldSetPomFile() throws Exception {
318+
ModelXmlFactory factory = mock(ModelXmlFactory.class);
319+
ModelParser parser = mock(ModelParser.class);
320+
XmlReaderRequest request = mock(XmlReaderRequest.class);
321+
Path pomPath = Path.of("project/pom.xml");
322+
when(request.getPath()).thenReturn(pomPath);
323+
when(request.isStrict()).thenReturn(true);
324+
325+
Model originalModel = mock(Model.class);
326+
Model modelWithPom = mock(Model.class);
327+
when(originalModel.withPomFile(pomPath)).thenReturn(modelWithPom);
328+
when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(originalModel));
329+
330+
Model result = new DefaultModelProcessor(factory, List.of(parser)).read(request);
331+
332+
assertSame(modelWithPom, result);
333+
verify(originalModel).withPomFile(pomPath);
334+
}
253335

254336

255337
}

0 commit comments

Comments
 (0)