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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to