Pankraz76 commented on code in PR #2304: URL: https://github.com/apache/maven/pull/2304#discussion_r2081387417
########## impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java: ########## @@ -77,67 +80,69 @@ 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)); + return throwIfWrongProjectDirLocation( + projectDirectory, + modelParsers.stream() + .map(m -> m.locate(projectDirectory) + .map(org.apache.maven.api.services.Source::getPath) + .orElse(null)) + .filter(Objects::nonNull) + .findFirst() + .orElseGet(() -> locateExistingPomWithFallback(projectDirectory))); + } + + private static Path throwIfWrongProjectDirLocation(Path projectDirectory, Path pom) { Review Comment: SOC ########## impl/maven-impl/src/test/java/org/apache/maven/impl/model/DefaultModelProcessorTest.java: ########## @@ -0,0 +1,400 @@ +/* + * 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.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.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static java.nio.file.Files.createTempDirectory; +import static java.nio.file.Files.deleteIfExists; +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +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.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class DefaultModelProcessorTest { + + @TempDir + Path tempDir; + + @Test + void locateExistingPomShouldHandleRegularFileInput() throws IOException { + // Create a temporary file Review Comment: meldown ########## impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java: ########## @@ -126,18 +126,14 @@ public Model read(XmlReaderRequest request) throws IOException { private Path doLocateExistingPom(Path project) { if (project == null) { project = Paths.get(System.getProperty("user.dir")); - } - if (Files.isDirectory(project)) { + } else 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; Review Comment: covered. ########## impl/maven-impl/src/main/java/org/apache/maven/impl/model/DefaultModelProcessor.java: ########## @@ -77,67 +80,69 @@ 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)); + return throwIfWrongProjectDirLocation( + projectDirectory, + modelParsers.stream() + .map(m -> m.locate(projectDirectory) + .map(org.apache.maven.api.services.Source::getPath) + .orElse(null)) + .filter(Objects::nonNull) + .findFirst() + .orElseGet(() -> locateExistingPomWithFallback(projectDirectory))); + } + + private static Path throwIfWrongProjectDirLocation(Path projectDirectory, Path pom) { 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 locateExistingPomWithFallback(Path project) { Review Comment: SRP -- 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-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org