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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]