You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "Pankraz76 (via GitHub)" <gi...@apache.org> on 2025/05/02 20:00:02 UTC
[PR] Pull #2287: `Modernize codebase with Java improvements - functional DefaultModelProcessor#read` [maven]
Pankraz76 opened a new pull request, #2292:
URL: https://github.com/apache/maven/pull/2292
Pull #2287: `Modernize codebase with Java improvements - functional DefaultModelProcessor#read`
- https://github.com/apache/maven/pull/2277
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078310207
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
Review Comment:
good eye. yes its boilerplate:
<img width="1824" alt="image" src="https://github.com/user-attachments/assets/d37df953-1e83-4c00-9d27-a80d2f510652" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078302185
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
try {
- return doRead(request);
+ return readOnSelf(request);
} catch (IOException e) {
exceptions.forEach(e::addSuppressed);
throw e;
}
} else {
- return doRead(request);
+ return readOnSelf(request);
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
- } else if (Files.isRegularFile(project)) {
- return project;
- } else {
- return null;
}
+ return project;
}
- private Model doRead(XmlReaderRequest request) throws IOException {
+ private Model readOnSelf(XmlReaderRequest request) throws IOException {
Review Comment:
It's DRY anyway, with a lot of abstraction obfuscating the code. I usually overdo the layering. This might actually help bring some cohesion.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2877537605
wip reopen
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077768586
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -92,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
} else if (outputStream != null) {
new PluginDescriptorStaxWriter().write(outputStream, content);
} else {
- try (OutputStream os = Files.newOutputStream(path)) {
+ try (OutputStream ignored = Files.newOutputStream(path)) {
Review Comment:
no its not:
<img width="1158" alt="image" src="https://github.com/user-attachments/assets/bd256a25-1ccc-4faf-aa09-fa2fdfc251cb" />
one time usage:
<img width="1055" alt="image" src="https://github.com/user-attachments/assets/0f1b9dac-2d67-496b-861a-4139147d94e8" />
`Value 'outputStream' is always 'null' `
<img width="1198" alt="image" src="https://github.com/user-attachments/assets/485d2b7c-3a42-4f26-b421-3bfbb5e239b7" />
this was the idea maybe:
<img width="1003" alt="image" src="https://github.com/user-attachments/assets/25e5704f-b7df-4c32-89fb-ad75c9b4e171" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2858286975
is this any good?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2072361875
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,30 +95,20 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
- if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
- List<ModelParserException> exceptions = new ArrayList<>();
- for (ModelParser parser : modelParsers) {
- try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
- }
- } catch (ModelParserException e) {
- exceptions.add(e);
- }
- }
- try {
- return doRead(request);
- } catch (IOException e) {
- exceptions.forEach(e::addSuppressed);
- throw e;
- }
- } else {
- return doRead(request);
- }
+ return readOnSelfOrParent(request.getPath(), request);
+ }
+
+ private Model readOnSelfOrParent(Path pomFile, XmlReaderRequest request) throws IOException {
+ return pomFile == null
+ ? doRead(request)
+ : modelParsers.stream()
Review Comment:
extract concern into readParent() to hide impl. detail and give dedicated concern
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077415845
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,139 @@
+package org.apache.maven.impl.model;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.Location;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderException;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class DefaultModelProcessorTest {
Review Comment:
<img width="590" alt="image" src="https://github.com/user-attachments/assets/cb30cdff-f971-451a-978e-a5afafee6a92" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2860054029
> Adding checked exceptions to the API which relies on unchecked exceptions everywhere is problematic
Thanks for the feedback. But just to clarify — this reflects what the current implementation actually does, right?
I'm doing this to enforce the current API and approach, so I’d suggest we state things as they are and move forward from there.
Otherwise, I’d consider removing the `IOException` check, since it might be hiding real issues.
The tests and interface should make the actual behavior and requirements clear.
I'm not fully aware of all the requirements, but ideally, we can follow `TDD` and `refactor aggressively` to reveal the final version of the code and `surface the core business logic`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2862888895
It's not thrown, that's right. So do not add a throw clause.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077332746
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -137,7 +139,7 @@ private Path doLocateExistingPom(Path project) {
}
}
- private Model doRead(XmlReaderRequest request) throws IOException {
Review Comment:
this changes the whole code as this "can not happen"; or if needed then we should change API, if we can, to make code more concise.
ATM this code is based in impl. detail, which should be `communicated; therefor enforced`.
<img width="1731" alt="image" src="https://github.com/user-attachments/assets/414af308-5c71-4c5c-ab45-c4d0bcae14c0" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077436936
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
try {
- return doRead(request);
+ return readOnSelf(request);
} catch (IOException e) {
exceptions.forEach(e::addSuppressed);
throw e;
}
} else {
- return doRead(request);
+ return readOnSelf(request);
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
- } else if (Files.isRegularFile(project)) {
Review Comment:
IMHO, it's either a directory (the higher-tier entity), as there is no file without a directory containing it. Which one came first is another topic, but I think it's that simple—either/or. So, we don't need this check, as the null branch is hard to test.
<img width="607" alt="image" src="https://github.com/user-attachments/assets/fc538c4c-3e2b-43a2-bcc6-f52af9fab24d" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077453834
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -77,17 +79,19 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote
+ * The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
Review Comment:
this is already very nice functional clean code style. good codebase - `functional over imperative OOP`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078310621
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
Review Comment:
gone
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2848545027
> This totally breaks the exception handling.
yes, then we need test, thanks.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2860229309
> > Adding checked exceptions to the API which relies on unchecked exceptions everywhere is problematic
>
> Thanks for the feedback. But just to clarify — this reflects what the current implementation actually does, right? I'm doing this to enforce the current API and approach, so I’d suggest we state things as they are and move forward from there.
Currently, the API is designed to throw XmlReaderException. Check the implemented interface, there's no IOException thrown here.
> Otherwise, I’d consider removing the `IOException` check — it might actually be causing issues. That’s why I want to enforce and challenge this behavior. The tests and interface should make the actual behavior and requirements clear.
>
> I'm not fully aware of all the requirements, but ideally, we can follow `TDD` and `refactor aggressively` to reveal the final version of the code and `surface the core business logic`.
Not sure what you mean exactly, but refactoring is not supposed to change the API in any way.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2081384120
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +72,31 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
Review Comment:
<img width="729" alt="image" src="https://github.com/user-attachments/assets/529d9cd3-e9b7-4926-b576-36419ed6009c" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078312128
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +106,44 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readPomWithParentInheritance(request, request.getPath());
+ }
+
+ private Model readPomWithParentInheritance(XmlReaderRequest request, Path pomFile) throws IOException {
+ List<ModelParserException> exceptions = new ArrayList<>();
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
- List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
- try {
- return doRead(request);
- } catch (IOException e) {
- exceptions.forEach(e::addSuppressed);
- throw e;
- }
- } else {
- return doRead(request);
Review Comment:
dry
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +106,44 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readPomWithParentInheritance(request, request.getPath());
+ }
+
+ private Model readPomWithParentInheritance(XmlReaderRequest request, Path pomFile) throws IOException {
+ List<ModelParserException> exceptions = new ArrayList<>();
Review Comment:
high coupling -1
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +106,44 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readPomWithParentInheritance(request, request.getPath());
+ }
+
+ private Model readPomWithParentInheritance(XmlReaderRequest request, Path pomFile) throws IOException {
+ List<ModelParserException> exceptions = new ArrayList<>();
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
- List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
- try {
- return doRead(request);
Review Comment:
dry
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +72,31 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
Review Comment:
its not CDI but dont needed like in spring as its working without (convention over config)
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +106,44 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readPomWithParentInheritance(request, request.getPath());
+ }
+
+ private Model readPomWithParentInheritance(XmlReaderRequest request, Path pomFile) throws IOException {
+ List<ModelParserException> exceptions = new ArrayList<>();
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
- List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
- try {
- return doRead(request);
- } catch (IOException e) {
- exceptions.forEach(e::addSuppressed);
- throw e;
- }
- } else {
- return doRead(request);
+ }
+ try {
+ return modelXmlFactory.read(request);
+ } catch (IOException e) {
+ exceptions.forEach(e::addSuppressed);
+ throw e;
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
Review Comment:
SOC, SRP, feature envy, one thing only.
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +71,32 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
@Inject
public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<ModelParser> modelParsers) {
Review Comment:
everything can be null, why this required?
```suggestion
public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, List<ModelParser> modelParsers) {
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077453834
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -77,17 +79,19 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote
+ * The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
Review Comment:
we have already very nice functional clean code style. very good codebase. functional over imperative OOP
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077449143
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -109,7 +110,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
* @throws XmlReaderException if an error occurs during the parsing
* @see #toXmlString(Object)
*/
- public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
Review Comment:
supplement this
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -92,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
} else if (outputStream != null) {
new PluginDescriptorStaxWriter().write(outputStream, content);
} else {
- try (OutputStream os = Files.newOutputStream(path)) {
Review Comment:
supplement this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077784138
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -92,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
} else if (outputStream != null) {
new PluginDescriptorStaxWriter().write(outputStream, content);
} else {
- try (OutputStream os = Files.newOutputStream(path)) {
+ try (OutputStream ignored = Files.newOutputStream(path)) {
Review Comment:
https://github.com/apache/maven/pull/2303
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -92,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
} else if (outputStream != null) {
new PluginDescriptorStaxWriter().write(outputStream, content);
} else {
- try (OutputStream os = Files.newOutputStream(path)) {
+ try (OutputStream ignored = Files.newOutputStream(path)) {
Review Comment:
- https://github.com/apache/maven/pull/2303
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2850713504
Yes it’s a test blind spot I want to cover. Sent from my iPhoneOn 5 May 2025, at 12:24, Elliotte Rusty Harold ***@***.***> wrote:
@elharo requested changes on this pull request.
Test cases might be missing here, but it looks like this changes functionality in a negative way by not grouping exceptions
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: ***@***.***>
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077330309
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,139 @@
+package org.apache.maven.impl.model;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.Location;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderException;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class DefaultModelProcessorTest {
Review Comment:
tc:
<img width="603" alt="image" src="https://github.com/user-attachments/assets/ae426643-f070-4f51-8c74-fe06a5907e98" />
<img width="1905" alt="image" src="https://github.com/user-attachments/assets/f265206a-2452-4774-a54e-86f950a91f21" />
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,32 +99,32 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
+ Optional<Model> model = locateAndParseParent(request, pomFile, parser);
if (model.isPresent()) {
return model.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
- try {
- return doRead(request);
- } catch (IOException e) {
- exceptions.forEach(e::addSuppressed);
Review Comment:
Then this is the last branch not covered.
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -137,7 +139,7 @@ private Path doLocateExistingPom(Path project) {
}
}
- private Model doRead(XmlReaderRequest request) throws IOException {
Review Comment:
this changes the whole code as this "can not happen"; or if needed then we should change API, if we can, to make code more concise.
<img width="1731" alt="image" src="https://github.com/user-attachments/assets/414af308-5c71-4c5c-ab45-c4d0bcae14c0" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2861935795
> This totally breaks the exception handling.
I'm not sure—when asking AI and trying to mock it, both seem to say the same thing: "Checked exceptions must be declared!"
That's how I remember Java's behavior ever since.
<img width="814" alt="image" src="https://github.com/user-attachments/assets/32902eb0-b5fd-405c-b65e-5f3cbb5d3c1c" />
<img width="907" alt="image" src="https://github.com/user-attachments/assets/0a5888a7-a762-4e9d-a123-847cc977db7e" />
We shouldn't surprise the consumer by forcing them to handle a checked exception that hasn't been declared. A better experience would be to adapt the API to declare the exception properly, or remove the catch block if the exception can't occur by contract—especially since such cases aren't testable.
> Currently, the API is designed to throw XmlReaderException. Check the implemented interface, there's no IOException thrown here.
>
> >
If no exception is thrown, then there's no need for a catch block—especially if it's not testable.
```
org.mockito.exceptions.base.MockitoException:
Checked exception is invalid for this method!
Invalid: java.io.IOException
```
<img width="1801" alt="image" src="https://github.com/user-attachments/assets/12f1dac0-2ca6-4de9-95c6-6b7e18fe2e2a" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077437814
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -39,27 +39,29 @@
import org.apache.maven.api.spi.ModelParser;
import org.apache.maven.api.spi.ModelParserException;
+import static org.apache.maven.api.spi.ModelParser.STRICT;
+
/**
*
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
- *
+ * <p>
* This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
* made this component available under all its interfaces then it could end up being injected
* into itself leading to a stack overflow.
- *
Review Comment:
https://checkstyle.org/checks/javadoc/javadocparagraph.html
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077436936
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
try {
- return doRead(request);
+ return readOnSelf(request);
} catch (IOException e) {
exceptions.forEach(e::addSuppressed);
throw e;
}
} else {
- return doRead(request);
+ return readOnSelf(request);
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
- } else if (Files.isRegularFile(project)) {
Review Comment:
It's either a directory (the higher-tier entity), as there is no file without a directory containing it. Which one came first is another topic, but I think it's that simple—either/or. So, we don't need this check, as the null branch is hard to test.
<img width="607" alt="image" src="https://github.com/user-attachments/assets/fc538c4c-3e2b-43a2-bcc6-f52af9fab24d" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2080293469
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,75 +71,74 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<ModelParser> modelParsers) {
this.modelXmlFactory = modelXmlFactory;
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
- Path pom = modelParsers.stream()
- .map(m -> m.locate(projectDirectory)
- .map(org.apache.maven.api.services.Source::getPath)
- .orElse(null))
- .filter(Objects::nonNull)
+ return modelParsers.stream()
+ .map(parser -> parser.locate(projectDirectory))
+ .filter(Optional::isPresent)
+ .map(Optional::get)
+ .map(Source::getPath)
+ .peek(path -> throwIfWrongProjectDirLocation(path, projectDirectory))
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
+ .orElseGet(() -> locateExistingPomWithUserDirDefault(projectDirectory));
+ }
+
+ private static void throwIfWrongProjectDirLocation(Path pom, Path projectDirectory) {
if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
}
- return pom;
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
+ return Files.isDirectory(project) ? isRegularFileOrNull(project.resolve("pom.xml")) : project;
+ }
+
+ private static Path isRegularFileOrNull(Path pom) {
+ return Files.isRegularFile(pom) ? pom : null;
}
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
Review Comment:
source --> request
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 closed pull request #2292: Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read`
URL: https://github.com/apache/maven/pull/2292
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077643882
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -92,7 +93,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
} else if (outputStream != null) {
new PluginDescriptorStaxWriter().write(outputStream, content);
} else {
- try (OutputStream os = Files.newOutputStream(path)) {
+ try (OutputStream ignored = Files.newOutputStream(path)) {
Review Comment:
This doesn't look ignored. It looks like it's used?
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/DefaultPluginXmlFactory.java:
##########
@@ -109,7 +110,7 @@ public void write(XmlWriterRequest<PluginDescriptor> request) throws XmlWriterEx
* @throws XmlReaderException if an error occurs during the parsing
* @see #toXmlString(Object)
*/
- public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException {
+ public static PluginDescriptor fromXml(@Nonnull String xml) throws XmlReaderException, IOException {
return new DefaultPluginXmlFactory().fromXmlString(xml);
Review Comment:
If the string contains the XML text there shouldn't be IOExceptions here
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(fallbackModel, result);
+ }
+
+ @Test
+ void readNullPomPathShouldUseFactoryDirectly() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+
+ when(request.getPath()).thenReturn(null);
+ when(factory.read(request)).thenReturn(model);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of());
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void locateExistingPomWithParsersShouldReturnFirstValid() {
+ Path expectedPom = Path.of("project/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(expectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ Path result = processor.locateExistingPom(Path.of("project"));
+
+ assertEquals(expectedPom, result);
+ }
+
+ @Test
+ void locateExistingPomParserReturnsPathOutsideProjectShouldThrow() {
+ Path unexpectedPom = Path.of("other/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(unexpectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ IllegalArgumentException ex =
+ assertThrows(IllegalArgumentException.class, () -> processor.locateExistingPom(Path.of("project")));
+
+ assertTrue(ex.getMessage().contains("does not belong to the given directory"));
+ }
+
+ @Test
+ void readWithAllParsersAndFactoryFailShouldThrowIOExceptionWithSuppressed() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("Parser failed"));
+
+ IOException factoryException = new IOException("Factory failed");
+ when(factory.read(request)).thenThrow(factoryException);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+
+ IOException thrown = assertThrows(IOException.class, () -> processor.read(request));
+ assertEquals("Factory failed", thrown.getMessage());
+ Throwable[] suppressed = thrown.getSuppressed();
+ assertEquals(1, suppressed.length);
+ assertInstanceOf(ModelParserException.class, suppressed[0]);
+ assertEquals("Parser failed", suppressed[0].getMessage());
+ }
+
+ @Test
+ void locateExistingPomFallbackWithValidPomShouldReturnPom() throws Exception {
+ Path projectDir = Files.createTempDirectory("testproject");
+ Path pomFile = projectDir.resolve("pom.xml");
+ Files.createFile(pomFile);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(projectDir);
+
+ assertEquals(pomFile, result);
+
+ Files.deleteIfExists(pomFile);
Review Comment:
cleanup probably belongs in a finally block, or perhaps an @After method
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
Review Comment:
Not sure what this means. Perhaps readFromSelfOrParent?
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
Review Comment:
This mock here seems to make the test meaningless. It looks like all you're testing is what the mock returns when the mocked method is called.
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
try {
- return doRead(request);
+ return readOnSelf(request);
} catch (IOException e) {
exceptions.forEach(e::addSuppressed);
throw e;
}
} else {
- return doRead(request);
+ return readOnSelf(request);
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
- } else if (Files.isRegularFile(project)) {
- return project;
- } else {
- return null;
}
+ return project;
}
- private Model doRead(XmlReaderRequest request) throws IOException {
+ private Model readOnSelf(XmlReaderRequest request) throws IOException {
Review Comment:
Again, I don't understand "read on self"
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(fallbackModel, result);
+ }
+
+ @Test
+ void readNullPomPathShouldUseFactoryDirectly() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+
+ when(request.getPath()).thenReturn(null);
+ when(factory.read(request)).thenReturn(model);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of());
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void locateExistingPomWithParsersShouldReturnFirstValid() {
+ Path expectedPom = Path.of("project/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(expectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ Path result = processor.locateExistingPom(Path.of("project"));
+
+ assertEquals(expectedPom, result);
+ }
+
+ @Test
+ void locateExistingPomParserReturnsPathOutsideProjectShouldThrow() {
+ Path unexpectedPom = Path.of("other/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(unexpectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ IllegalArgumentException ex =
+ assertThrows(IllegalArgumentException.class, () -> processor.locateExistingPom(Path.of("project")));
+
+ assertTrue(ex.getMessage().contains("does not belong to the given directory"));
+ }
+
+ @Test
+ void readWithAllParsersAndFactoryFailShouldThrowIOExceptionWithSuppressed() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("Parser failed"));
+
+ IOException factoryException = new IOException("Factory failed");
+ when(factory.read(request)).thenThrow(factoryException);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+
+ IOException thrown = assertThrows(IOException.class, () -> processor.read(request));
+ assertEquals("Factory failed", thrown.getMessage());
+ Throwable[] suppressed = thrown.getSuppressed();
+ assertEquals(1, suppressed.length);
+ assertInstanceOf(ModelParserException.class, suppressed[0]);
+ assertEquals("Parser failed", suppressed[0].getMessage());
+ }
+
+ @Test
+ void locateExistingPomFallbackWithValidPomShouldReturnPom() throws Exception {
+ Path projectDir = Files.createTempDirectory("testproject");
+ Path pomFile = projectDir.resolve("pom.xml");
+ Files.createFile(pomFile);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(projectDir);
+
+ assertEquals(pomFile, result);
+
+ Files.deleteIfExists(pomFile);
+ Files.deleteIfExists(projectDir);
+ }
+
+ @Test
+ void locateExistingPomFallbackWithFileAsPathShouldReturnThatFile() throws Exception {
+ Path tempPom = Files.createTempFile("pom", ".xml");
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(tempPom);
+
+ assertEquals(tempPom, result);
+
+ Files.deleteIfExists(tempPom);
Review Comment:
cleanup probably belongs in a finally block, or perhaps an @After method
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077453834
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -77,17 +79,19 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote
+ * The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
Review Comment:
we have already very nice functional clean code style. very good codebase. func over imper
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077437814
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -39,27 +39,29 @@
import org.apache.maven.api.spi.ModelParser;
import org.apache.maven.api.spi.ModelParserException;
+import static org.apache.maven.api.spi.ModelParser.STRICT;
+
/**
*
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
- *
+ * <p>
* This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
* made this component available under all its interfaces then it could end up being injected
* into itself leading to a stack overflow.
- *
Review Comment:
there is an checkstyle rule for this
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077437814
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -39,27 +39,29 @@
import org.apache.maven.api.spi.ModelParser;
import org.apache.maven.api.spi.ModelParserException;
+import static org.apache.maven.api.spi.ModelParser.STRICT;
+
/**
*
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
- *
+ * <p>
* This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
* made this component available under all its interfaces then it could end up being injected
* into itself leading to a stack overflow.
- *
Review Comment:
consider migration to https://checkstyle.org/checks/javadoc/javadocparagraph.html.
- https://github.com/openrewrite/rewrite-static-analysis/issues/545
- https://github.com/apache/maven/pull/2302
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2081373513
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -97,47 +101,50 @@ public Path locateExistingPom(Path projectDirectory) {
@Override
public Model read(XmlReaderRequest request) throws IOException {
Objects.requireNonNull(request, "source cannot be null");
- Path pomFile = request.getPath();
+ return readOnSelfOrParent(request, request.getPath());
+ }
+
+ private Model readOnSelfOrParent(XmlReaderRequest request, Path pomFile) throws IOException {
if (pomFile != null) {
- Path projectDirectory = pomFile.getParent();
List<ModelParserException> exceptions = new ArrayList<>();
for (ModelParser parser : modelParsers) {
try {
- Optional<Model> model =
- parser.locateAndParse(projectDirectory, Map.of(ModelParser.STRICT, request.isStrict()));
- if (model.isPresent()) {
- return model.get().withPomFile(pomFile);
+ Optional<Model> parent = readParent(request, pomFile, parser);
+ if (parent.isPresent()) {
+ return parent.get().withPomFile(pomFile);
}
} catch (ModelParserException e) {
exceptions.add(e);
}
}
try {
- return doRead(request);
+ return readOnSelf(request);
} catch (IOException e) {
exceptions.forEach(e::addSuppressed);
throw e;
}
} else {
- return doRead(request);
+ return readOnSelf(request);
}
}
- private Path doLocateExistingPom(Path project) {
- if (project == null) {
- project = Paths.get(System.getProperty("user.dir"));
- }
+ private static Optional<Model> readParent(XmlReaderRequest request, Path pomFile, ModelParser parser) {
+ return parser.locateAndParse(pomFile.getParent(), Map.of(STRICT, request.isStrict()));
+ }
+
+ private Path locateExistingPomWithUserDirDefault(Path project) {
+ return locateExistingPomInDirOrFile(project != null ? project : Paths.get(System.getProperty("user.dir")));
+ }
+
+ private static Path locateExistingPomInDirOrFile(Path project) {
if (Files.isDirectory(project)) {
Path pom = project.resolve("pom.xml");
return Files.isRegularFile(pom) ? pom : null;
- } else if (Files.isRegularFile(project)) {
Review Comment:
tested:
<img width="1821" alt="image" src="https://github.com/user-attachments/assets/3c745922-f957-4f00-9a36-9d0d3df05be3" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2860345256
> Not sure what you mean exactly, but refactoring is not supposed to change the API in any way.
yes of course will adapt then. sorry
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2287: `Modernize codebase with Java improvements - functional DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2848023291
whats the mystery about this?
```
[ERROR] Errors:
[ERROR] EmbeddedMavenExecutorTest.defaultFs3xCaptureOutput(Path) » Timeout defaultFs3xCaptureOutput(java.nio.file.Path) timed out after 15 seconds
[ERROR] ForkedMavenExecutorTest.dump4(Path, Path) » Timeout dump4(java.nio.file.Path, java.nio.file.Path) timed out after 15 seconds
[ERROR] ToolboxToolTest.metadataPath3(Mode)[1] » Timeout metadataPath3(org.apache.maven.cling.executor.ExecutorHelper$Mode) timed out after 15 seconds
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077415845
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,139 @@
+package org.apache.maven.impl.model;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.Location;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderException;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class DefaultModelProcessorTest {
Review Comment:
<img width="590" alt="image" src="https://github.com/user-attachments/assets/cb30cdff-f971-451a-978e-a5afafee6a92" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078317494
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +72,31 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
Review Comment:
its not `javax.CDI` but feels like as its not needed and working without embracing `convention over config`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2862135103
> Currently, the API is designed to throw XmlReaderException. Check the implemented interface, there's no IOException thrown here.
>
> >
please notice dedicated thread: https://github.com/apache/maven/pull/2304/files#r2079127763
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2081376498
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +72,31 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
Review Comment:
> Did you run UTs and ITs ?
always run root `clean install`
> Are you aware that Maven has its own DI framework ? If you remove the required annotations, it will probably break ?
i delete/challenge everything and see if it breaks. All worked out locally thats why i want to lern and clarify, thx.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on PR #2292:
URL: https://github.com/apache/maven/pull/2292#issuecomment-2861942619
thats my headache `Exception 'java.io.IOException' is never thrown in the corresponding try block`
<img width="895" alt="image" src="https://github.com/user-attachments/assets/a8b3c800-e59f-40a9-a5e8-b797d73beaf0" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077437814
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -39,27 +39,29 @@
import org.apache.maven.api.spi.ModelParser;
import org.apache.maven.api.spi.ModelParserException;
+import static org.apache.maven.api.spi.ModelParser.STRICT;
+
/**
*
* Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
- *
+ * <p>
* This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
* made this component available under all its interfaces then it could end up being injected
* into itself leading to a stack overflow.
- *
Review Comment:
consider migration to https://checkstyle.org/checks/javadoc/javadocparagraph.html.
- https://github.com/openrewrite/rewrite-static-analysis/issues/545
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2078307036
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(fallbackModel, result);
+ }
+
+ @Test
+ void readNullPomPathShouldUseFactoryDirectly() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+
+ when(request.getPath()).thenReturn(null);
+ when(factory.read(request)).thenReturn(model);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of());
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void locateExistingPomWithParsersShouldReturnFirstValid() {
+ Path expectedPom = Path.of("project/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(expectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ Path result = processor.locateExistingPom(Path.of("project"));
+
+ assertEquals(expectedPom, result);
+ }
+
+ @Test
+ void locateExistingPomParserReturnsPathOutsideProjectShouldThrow() {
+ Path unexpectedPom = Path.of("other/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(unexpectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ IllegalArgumentException ex =
+ assertThrows(IllegalArgumentException.class, () -> processor.locateExistingPom(Path.of("project")));
+
+ assertTrue(ex.getMessage().contains("does not belong to the given directory"));
+ }
+
+ @Test
+ void readWithAllParsersAndFactoryFailShouldThrowIOExceptionWithSuppressed() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("Parser failed"));
+
+ IOException factoryException = new IOException("Factory failed");
+ when(factory.read(request)).thenThrow(factoryException);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+
+ IOException thrown = assertThrows(IOException.class, () -> processor.read(request));
+ assertEquals("Factory failed", thrown.getMessage());
+ Throwable[] suppressed = thrown.getSuppressed();
+ assertEquals(1, suppressed.length);
+ assertInstanceOf(ModelParserException.class, suppressed[0]);
+ assertEquals("Parser failed", suppressed[0].getMessage());
+ }
+
+ @Test
+ void locateExistingPomFallbackWithValidPomShouldReturnPom() throws Exception {
+ Path projectDir = Files.createTempDirectory("testproject");
+ Path pomFile = projectDir.resolve("pom.xml");
+ Files.createFile(pomFile);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(projectDir);
+
+ assertEquals(pomFile, result);
+
+ Files.deleteIfExists(pomFile);
Review Comment:
yes its DRY
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,184 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.maven.impl.model;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+class DefaultModelProcessorTest {
+
+ @Test
+ void readWithValidParserShouldReturnModel() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(model.withPomFile(path)).thenReturn(model);
+ when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void readAllParsersFailShouldFallbackToFactory() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model fallbackModel = mock(Model.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(false);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("parse fail"));
+
+ when(factory.read(request)).thenReturn(fallbackModel);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(fallbackModel, result);
+ }
+
+ @Test
+ void readNullPomPathShouldUseFactoryDirectly() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Model model = mock(Model.class);
+
+ when(request.getPath()).thenReturn(null);
+ when(factory.read(request)).thenReturn(model);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of());
+ Model result = processor.read(request);
+
+ assertNotNull(result);
+ assertEquals(model, result);
+ }
+
+ @Test
+ void locateExistingPomWithParsersShouldReturnFirstValid() {
+ Path expectedPom = Path.of("project/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(expectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ Path result = processor.locateExistingPom(Path.of("project"));
+
+ assertEquals(expectedPom, result);
+ }
+
+ @Test
+ void locateExistingPomParserReturnsPathOutsideProjectShouldThrow() {
+ Path unexpectedPom = Path.of("other/pom.xml");
+ Source mockSource = mock(Source.class);
+ when(mockSource.getPath()).thenReturn(unexpectedPom);
+
+ ModelParser parser = mock(ModelParser.class);
+ when(parser.locate(any())).thenReturn(Optional.of(mockSource));
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser));
+ IllegalArgumentException ex =
+ assertThrows(IllegalArgumentException.class, () -> processor.locateExistingPom(Path.of("project")));
+
+ assertTrue(ex.getMessage().contains("does not belong to the given directory"));
+ }
+
+ @Test
+ void readWithAllParsersAndFactoryFailShouldThrowIOExceptionWithSuppressed() throws Exception {
+ ModelXmlFactory factory = mock(ModelXmlFactory.class);
+ ModelParser parser = mock(ModelParser.class);
+ XmlReaderRequest request = mock(XmlReaderRequest.class);
+ Path path = Path.of("project/pom.xml");
+
+ when(request.getPath()).thenReturn(path);
+ when(request.isStrict()).thenReturn(true);
+ when(parser.locateAndParse(any(), any())).thenThrow(new ModelParserException("Parser failed"));
+
+ IOException factoryException = new IOException("Factory failed");
+ when(factory.read(request)).thenThrow(factoryException);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser));
+
+ IOException thrown = assertThrows(IOException.class, () -> processor.read(request));
+ assertEquals("Factory failed", thrown.getMessage());
+ Throwable[] suppressed = thrown.getSuppressed();
+ assertEquals(1, suppressed.length);
+ assertInstanceOf(ModelParserException.class, suppressed[0]);
+ assertEquals("Parser failed", suppressed[0].getMessage());
+ }
+
+ @Test
+ void locateExistingPomFallbackWithValidPomShouldReturnPom() throws Exception {
+ Path projectDir = Files.createTempDirectory("testproject");
+ Path pomFile = projectDir.resolve("pom.xml");
+ Files.createFile(pomFile);
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(projectDir);
+
+ assertEquals(pomFile, result);
+
+ Files.deleteIfExists(pomFile);
+ Files.deleteIfExists(projectDir);
+ }
+
+ @Test
+ void locateExistingPomFallbackWithFileAsPathShouldReturnThatFile() throws Exception {
+ Path tempPom = Files.createTempFile("pom", ".xml");
+
+ DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of());
+ Path result = processor.locateExistingPom(tempPom);
+
+ assertEquals(tempPom, result);
+
+ Files.deleteIfExists(tempPom);
Review Comment:
yes its DRY
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077453834
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -77,17 +79,19 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote
+ * The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
Review Comment:
we have already very nice functional clean code style. very good codebase. `functional over imperative OOP`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: `Modernize codebase with Java improvements - functionalize DefaultModelProcessor#read` [maven]
Posted by "Pankraz76 (via GitHub)" <gi...@apache.org>.
Pankraz76 commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2077330309
##########
impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java:
##########
@@ -0,0 +1,139 @@
+package org.apache.maven.impl.model;
+
+import org.apache.maven.api.model.Model;
+import org.apache.maven.api.services.Source;
+import org.apache.maven.api.services.xml.Location;
+import org.apache.maven.api.services.xml.ModelXmlFactory;
+import org.apache.maven.api.services.xml.XmlReaderException;
+import org.apache.maven.api.services.xml.XmlReaderRequest;
+import org.apache.maven.api.spi.ModelParser;
+import org.apache.maven.api.spi.ModelParserException;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.*;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.mockito.Mockito.*;
+
+class DefaultModelProcessorTest {
Review Comment:
tc:
<img width="603" alt="image" src="https://github.com/user-attachments/assets/ae426643-f070-4f51-8c74-fe06a5907e98" />
<img width="1905" alt="image" src="https://github.com/user-attachments/assets/f265206a-2452-4774-a54e-86f950a91f21" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2080518601
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -77,17 +79,19 @@ public DefaultModelProcessor(ModelXmlFactory modelXmlFactory, @Nullable List<Mod
this.modelParsers = modelParsers;
}
+ /**
+ * @implNote
+ * The ModelProcessor#locatePom never returns null while the ModelParser#locatePom needs to return an existing path!
+ */
@Override
public Path locateExistingPom(Path projectDirectory) {
- // Note that the ModelProcessor#locatePom never returns null
- // while the ModelParser#locatePom needs to return an existing path!
Path pom = modelParsers.stream()
.map(m -> m.locate(projectDirectory)
.map(org.apache.maven.api.services.Source::getPath)
.orElse(null))
.filter(Objects::nonNull)
.findFirst()
- .orElseGet(() -> doLocateExistingPom(projectDirectory));
Review Comment:
You changed the semantics, it's wrong.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Pull #2292: Modernize codebase with Java improvements - SOC `DefaultModelProcessor#read` [maven]
Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #2292:
URL: https://github.com/apache/maven/pull/2292#discussion_r2080275677
##########
impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java:
##########
@@ -69,25 +72,31 @@
public class DefaultModelProcessor implements ModelProcessor {
private final ModelXmlFactory modelXmlFactory;
- private final List<ModelParser> modelParsers;
+ private final @Nullable List<ModelParser> modelParsers;
- @Inject
Review Comment:
Are you aware that Maven has its own DI framework ? If you remove the required annotations, it will probably break ?
@Nullable can be used to inform the DI that the requirement is optional.
Did you run UTs and ITs ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@maven.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org