|
| 1 | +package org.apache.maven.impl.model; |
| 2 | + |
| 3 | +import org.apache.maven.api.model.Model; |
| 4 | +import org.apache.maven.api.services.Source; |
| 5 | +import org.apache.maven.api.services.xml.Location; |
| 6 | +import org.apache.maven.api.services.xml.ModelXmlFactory; |
| 7 | +import org.apache.maven.api.services.xml.XmlReaderException; |
| 8 | +import org.apache.maven.api.services.xml.XmlReaderRequest; |
| 9 | +import org.apache.maven.api.spi.ModelParser; |
| 10 | +import org.apache.maven.api.spi.ModelParserException; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.util.*; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | +import static org.mockito.Mockito.*; |
| 20 | + |
| 21 | +class DefaultModelProcessorTest { |
| 22 | + |
| 23 | + @Test |
| 24 | + void read_withValidParser_shouldReturnModel() throws Exception { |
| 25 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 26 | + ModelParser parser = mock(ModelParser.class); |
| 27 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 28 | + Model model = mock(Model.class); |
| 29 | + Path path = Path.of("project/pom.xml"); |
| 30 | + |
| 31 | + when(request.getPath()).thenReturn(path); |
| 32 | + when(request.isStrict()).thenReturn(true); |
| 33 | + when(model.withPomFile(path)).thenReturn(model); |
| 34 | + when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model)); |
| 35 | + |
| 36 | + DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser)); |
| 37 | + Model result = processor.read(request); |
| 38 | + |
| 39 | + assertNotNull(result); |
| 40 | + assertEquals(model, result); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void read_allParsersFail_shouldFallbackToFactory() throws Exception { |
| 45 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 46 | + ModelParser parser = mock(ModelParser.class); |
| 47 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 48 | + Model fallbackModel = mock(Model.class); |
| 49 | + Path path = Path.of("project/pom.xml"); |
| 50 | + |
| 51 | + when(request.getPath()).thenReturn(path); |
| 52 | + when(request.isStrict()).thenReturn(false); |
| 53 | + when(parser.locateAndParse(any(), any())) |
| 54 | + .thenThrow(new ModelParserException("parse fail")); |
| 55 | + |
| 56 | + when(factory.read(request)).thenReturn(fallbackModel); |
| 57 | + |
| 58 | + DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of(parser)); |
| 59 | + Model result = processor.read(request); |
| 60 | + |
| 61 | + assertNotNull(result); |
| 62 | + assertEquals(fallbackModel, result); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void read_nullPomPath_shouldUseFactoryDirectly() throws Exception { |
| 67 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 68 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 69 | + Model model = mock(Model.class); |
| 70 | + |
| 71 | + when(request.getPath()).thenReturn(null); |
| 72 | + when(factory.read(request)).thenReturn(model); |
| 73 | + |
| 74 | + DefaultModelProcessor processor = new DefaultModelProcessor(factory, List.of()); |
| 75 | + Model result = processor.read(request); |
| 76 | + |
| 77 | + assertNotNull(result); |
| 78 | + assertEquals(model, result); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void locateExistingPom_withParsers_shouldReturnFirstValid() { |
| 83 | + Path expectedPom = Path.of("project/pom.xml"); |
| 84 | + Source mockSource = mock(Source.class); |
| 85 | + when(mockSource.getPath()).thenReturn(expectedPom); |
| 86 | + |
| 87 | + ModelParser parser = mock(ModelParser.class); |
| 88 | + when(parser.locate(any())).thenReturn(Optional.of(mockSource)); |
| 89 | + |
| 90 | + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser)); |
| 91 | + Path result = processor.locateExistingPom(Path.of("project")); |
| 92 | + |
| 93 | + assertEquals(expectedPom, result); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void locateExistingPom_parserReturnsPathOutsideProject_shouldThrow() { |
| 98 | + Path unexpectedPom = Path.of("other/pom.xml"); |
| 99 | + Source mockSource = mock(Source.class); |
| 100 | + when(mockSource.getPath()).thenReturn(unexpectedPom); |
| 101 | + |
| 102 | + ModelParser parser = mock(ModelParser.class); |
| 103 | + when(parser.locate(any())).thenReturn(Optional.of(mockSource)); |
| 104 | + |
| 105 | + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser)); |
| 106 | + IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> |
| 107 | + processor.locateExistingPom(Path.of("project"))); |
| 108 | + |
| 109 | + assertTrue(ex.getMessage().contains("does not belong to the given directory")); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void locateExistingPom_fallbackWithValidPom_shouldReturnPom() throws Exception { |
| 114 | + Path projectDir = Files.createTempDirectory("testproject"); |
| 115 | + Path pomFile = projectDir.resolve("pom.xml"); |
| 116 | + Files.createFile(pomFile); |
| 117 | + |
| 118 | + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()); |
| 119 | + Path result = processor.locateExistingPom(projectDir); |
| 120 | + |
| 121 | + assertEquals(pomFile, result); |
| 122 | + |
| 123 | + // Clean up |
| 124 | + Files.deleteIfExists(pomFile); |
| 125 | + Files.deleteIfExists(projectDir); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + void locateExistingPom_fallbackWithFileAsPath_shouldReturnThatFile() throws Exception { |
| 130 | + Path tempPom = Files.createTempFile("pom", ".xml"); |
| 131 | + |
| 132 | + DefaultModelProcessor processor = new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()); |
| 133 | + Path result = processor.locateExistingPom(tempPom); |
| 134 | + |
| 135 | + assertEquals(tempPom, result); |
| 136 | + |
| 137 | + Files.deleteIfExists(tempPom); |
| 138 | + } |
| 139 | +} |
0 commit comments