|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.impl.model; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.file.Files; |
| 23 | +import java.nio.file.Path; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +import org.apache.maven.api.model.Model; |
| 28 | +import org.apache.maven.api.services.Source; |
| 29 | +import org.apache.maven.api.services.xml.ModelXmlFactory; |
| 30 | +import org.apache.maven.api.services.xml.XmlReaderRequest; |
| 31 | +import org.apache.maven.api.spi.ModelParser; |
| 32 | +import org.apache.maven.api.spi.ModelParserException; |
| 33 | +import org.junit.jupiter.api.AfterEach; |
| 34 | +import org.junit.jupiter.api.Disabled; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.io.TempDir; |
| 37 | + |
| 38 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 41 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 43 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 44 | +import static org.mockito.Mockito.any; |
| 45 | +import static org.mockito.Mockito.mock; |
| 46 | +import static org.mockito.Mockito.when; |
| 47 | + |
| 48 | +class DefaultModelProcessorTest { |
| 49 | + |
| 50 | + @TempDir |
| 51 | + Path tempDir; |
| 52 | + |
| 53 | + Path testProjectDir, testPomFile; |
| 54 | + |
| 55 | + @AfterEach |
| 56 | + void cleanup() throws IOException { |
| 57 | + if (testPomFile != null && Files.exists(testPomFile)) { |
| 58 | + Files.deleteIfExists(testPomFile); |
| 59 | + } |
| 60 | + if (testProjectDir != null && Files.exists(testProjectDir)) { |
| 61 | + Files.deleteIfExists(testProjectDir); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void readWithValidParserShouldReturnModel() throws Exception { |
| 67 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 68 | + ModelParser parser = mock(ModelParser.class); |
| 69 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 70 | + Model model = mock(Model.class); |
| 71 | + Path path = Path.of("project/pom.xml"); |
| 72 | + when(request.getPath()).thenReturn(path); |
| 73 | + when(request.isStrict()).thenReturn(true); |
| 74 | + when(model.withPomFile(path)).thenReturn(model); |
| 75 | + when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(model)); |
| 76 | + Model result = new DefaultModelProcessor(factory, List.of(parser)).read(request); |
| 77 | + assertNotNull(result); |
| 78 | + assertEquals(model, result); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + void readNullPomPathShouldUseFactoryDirectly() throws Exception { |
| 83 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 84 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 85 | + Model model = mock(Model.class); |
| 86 | + when(request.getPath()).thenReturn(null); |
| 87 | + when(factory.read(request)).thenReturn(model); |
| 88 | + Model result = new DefaultModelProcessor(factory, List.of()).read(request); |
| 89 | + assertNotNull(result); |
| 90 | + assertEquals(model, result); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + void locateExistingPomWithParsersShouldReturnFirstValid() { |
| 95 | + Path expectedPom = Path.of("project/pom.xml"); |
| 96 | + Source mockSource = mock(Source.class); |
| 97 | + when(mockSource.getPath()).thenReturn(expectedPom); |
| 98 | + ModelParser parser = mock(ModelParser.class); |
| 99 | + when(parser.locate(any())).thenReturn(Optional.of(mockSource)); |
| 100 | + assertEquals( |
| 101 | + expectedPom, |
| 102 | + new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of(parser)) |
| 103 | + .locateExistingPom(Path.of("project"))); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + @Disabled |
| 108 | + void locateExistingPomParserReturnsPathOutsideProjectShouldThrow() { |
| 109 | + Source mockSource = mock(Source.class); |
| 110 | + when(mockSource.getPath()).thenReturn(Path.of("other/pom.xml")); |
| 111 | + ModelParser parser = mock(ModelParser.class); |
| 112 | + when(parser.locate(any())).thenReturn(Optional.of(mockSource)); |
| 113 | + assertThat(assertThrows(IllegalArgumentException.class, () -> new DefaultModelProcessor( |
| 114 | + mock(ModelXmlFactory.class), List.of(parser)) |
| 115 | + .locateExistingPom(Path.of("project"))) |
| 116 | + .getMessage()) |
| 117 | + .contains("does not belong to the given directory"); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void locateExistingPomFallbackWithValidPomShouldReturnPom() throws Exception { |
| 122 | + testProjectDir = Files.createTempDirectory(tempDir, "testproject"); |
| 123 | + testPomFile = testProjectDir.resolve("pom.xml"); |
| 124 | + Files.createFile(testPomFile); |
| 125 | + assertEquals( |
| 126 | + testPomFile, |
| 127 | + new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testProjectDir)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void locateExistingPomFallbackWithFileAsPathShouldReturnThatFile() throws Exception { |
| 132 | + testPomFile = Files.createTempFile(tempDir, "pom", ".xml"); |
| 133 | + assertEquals( |
| 134 | + testPomFile, |
| 135 | + new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testPomFile)); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + void readWithParserThrowingExceptionShouldCollectException() { |
| 140 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 141 | + ModelParser parser = mock(ModelParser.class); |
| 142 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 143 | + when(request.getPath()).thenReturn(Path.of("project/pom.xml")); |
| 144 | + when(request.isStrict()).thenReturn(true); |
| 145 | + when(parser.locateAndParse(any(), any())).thenThrow(new RuntimeException("Parser error")); |
| 146 | + when(factory.read(request)).thenThrow(new RuntimeException("Factory error")); |
| 147 | + RuntimeException ex = assertThrows( |
| 148 | + RuntimeException.class, () -> new DefaultModelProcessor(factory, List.of(parser)).read(request)); |
| 149 | + assertEquals("Factory error", ex.getMessage()); |
| 150 | + assertEquals(1, ex.getSuppressed().length); |
| 151 | + assertInstanceOf(ModelParserException.class, ex.getSuppressed()[0]); |
| 152 | + assertEquals("Parser error", ex.getSuppressed()[0].getCause().getMessage()); |
| 153 | + } |
| 154 | + |
| 155 | + @Test |
| 156 | + void readWithFactoryThrowingExceptionShouldRethrowWithSuppressed() { |
| 157 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 158 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 159 | + when(request.getPath()).thenReturn(Path.of("project/pom.xml")); |
| 160 | + when(factory.read(request)).thenThrow(new RuntimeException("Factory error")); |
| 161 | + ModelParser parser = mock(ModelParser.class); |
| 162 | + when(parser.locateAndParse(any(), any())).thenThrow(new RuntimeException("Parser error")); |
| 163 | + RuntimeException ex = assertThrows( |
| 164 | + RuntimeException.class, () -> new DefaultModelProcessor(factory, List.of(parser)).read(request)); |
| 165 | + assertEquals("Factory error", ex.getMessage()); |
| 166 | + assertEquals(1, ex.getSuppressed().length); |
| 167 | + assertInstanceOf(ModelParserException.class, ex.getSuppressed()[0]); |
| 168 | + assertEquals("Parser error", ex.getSuppressed()[0].getCause().getMessage()); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + void locateExistingPomWithDirectoryContainingPom() throws IOException { |
| 173 | + testProjectDir = Files.createTempDirectory(tempDir, "project"); |
| 174 | + testPomFile = testProjectDir.resolve("pom.xml"); |
| 175 | + Files.createFile(testPomFile); |
| 176 | + assertEquals( |
| 177 | + testPomFile, |
| 178 | + new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testProjectDir)); |
| 179 | + } |
| 180 | + |
| 181 | + @Test |
| 182 | + void locateExistingPomWithDirectoryWithoutPom() throws IOException { |
| 183 | + testProjectDir = Files.createTempDirectory(tempDir, "project"); |
| 184 | + assertNull(new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testProjectDir)); |
| 185 | + } |
| 186 | + |
| 187 | + @Test |
| 188 | + void locateExistingPomWithPomFile() throws IOException { |
| 189 | + testPomFile = Files.createTempFile(tempDir, "pom", ".xml"); |
| 190 | + assertEquals( |
| 191 | + testPomFile, |
| 192 | + new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(testPomFile)); |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + void locateExistingPomWithNonExistentPath() { |
| 197 | + assertNotNull(new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()) |
| 198 | + .locateExistingPom(tempDir.resolve("nonexistent"))); |
| 199 | + } |
| 200 | + |
| 201 | + @Test |
| 202 | + void locateExistingPomWithNullProjectAndNoPomInUserDirShouldReturnNull() { |
| 203 | + System.setProperty("user.dir", tempDir.toString()); |
| 204 | + assertNull(new DefaultModelProcessor(mock(ModelXmlFactory.class), List.of()).locateExistingPom(null)); |
| 205 | + } |
| 206 | +} |
0 commit comments