|
36 | 36 |
|
37 | 37 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
38 | 38 | import static org.junit.jupiter.api.Assertions.*; |
39 | | -import static org.mockito.Mockito.any; |
40 | | -import static org.mockito.Mockito.mock; |
41 | | -import static org.mockito.Mockito.when; |
| 39 | +import static org.mockito.Mockito.*; |
42 | 40 |
|
43 | 41 | class DefaultModelProcessorTest { |
44 | 42 |
|
@@ -249,7 +247,91 @@ void readWithParserThrowingModelParserExceptionShouldAddAsSuppressed() throws Ex |
249 | 247 | // In this case, since factory succeeds, no exception is thrown |
250 | 248 | } |
251 | 249 |
|
| 250 | + @Test |
| 251 | + void readWithParserReturningEmptyShouldTryNextParser() throws Exception { |
| 252 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 253 | + ModelParser parser1 = mock(ModelParser.class); |
| 254 | + ModelParser parser2 = mock(ModelParser.class); |
| 255 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 256 | + Path pomPath = Path.of("project/pom.xml"); |
| 257 | + when(request.getPath()).thenReturn(pomPath); |
| 258 | + when(request.isStrict()).thenReturn(true); |
| 259 | + |
| 260 | + Model expectedModel = mock(Model.class); |
| 261 | + when(expectedModel.withPomFile(pomPath)).thenReturn(expectedModel); |
| 262 | + |
| 263 | + // First parser returns empty |
| 264 | + when(parser1.locateAndParse(any(), any())).thenReturn(Optional.empty()); |
| 265 | + // Second parser returns model |
| 266 | + when(parser2.locateAndParse(any(), any())).thenReturn(Optional.of(expectedModel)); |
| 267 | + |
| 268 | + Model result = new DefaultModelProcessor(factory, List.of(parser1, parser2)).read(request); |
| 269 | + |
| 270 | + assertSame(expectedModel, result); |
| 271 | + } |
| 272 | + |
| 273 | + @Test |
| 274 | + void readWithAllParsersReturningEmptyShouldFallbackToFactory() throws Exception { |
| 275 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 276 | + ModelParser parser1 = mock(ModelParser.class); |
| 277 | + ModelParser parser2 = mock(ModelParser.class); |
| 278 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 279 | + Path pomPath = Path.of("project/pom.xml"); |
| 280 | + when(request.getPath()).thenReturn(pomPath); |
| 281 | + when(request.isStrict()).thenReturn(true); |
| 282 | + |
| 283 | + Model expectedModel = mock(Model.class); |
| 284 | + |
| 285 | + // Both parsers return empty |
| 286 | + when(parser1.locateAndParse(any(), any())).thenReturn(Optional.empty()); |
| 287 | + when(parser2.locateAndParse(any(), any())).thenReturn(Optional.empty()); |
| 288 | + // Factory returns model |
| 289 | + when(factory.read(request)).thenReturn(expectedModel); |
| 290 | + |
| 291 | + Model result = new DefaultModelProcessor(factory, List.of(parser1, parser2)).read(request); |
| 292 | + |
| 293 | + assertSame(expectedModel, result); |
| 294 | + } |
252 | 295 |
|
| 296 | + @Test |
| 297 | + void readWithParserReturningModelShouldUseThatModel() throws Exception { |
| 298 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 299 | + ModelParser parser = mock(ModelParser.class); |
| 300 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 301 | + Path pomPath = Path.of("project/pom.xml"); |
| 302 | + when(request.getPath()).thenReturn(pomPath); |
| 303 | + when(request.isStrict()).thenReturn(true); |
| 304 | + |
| 305 | + Model expectedModel = mock(Model.class); |
| 306 | + when(expectedModel.withPomFile(pomPath)).thenReturn(expectedModel); |
| 307 | + when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(expectedModel)); |
| 308 | + |
| 309 | + Model result = new DefaultModelProcessor(factory, List.of(parser)).read(request); |
| 310 | + |
| 311 | + assertSame(expectedModel, result); |
| 312 | + // Verify factory was not called |
| 313 | + verify(factory, never()).read((Path) any()); |
| 314 | + } |
| 315 | + |
| 316 | + @Test |
| 317 | + void readWithParserReturningModelShouldSetPomFile() throws Exception { |
| 318 | + ModelXmlFactory factory = mock(ModelXmlFactory.class); |
| 319 | + ModelParser parser = mock(ModelParser.class); |
| 320 | + XmlReaderRequest request = mock(XmlReaderRequest.class); |
| 321 | + Path pomPath = Path.of("project/pom.xml"); |
| 322 | + when(request.getPath()).thenReturn(pomPath); |
| 323 | + when(request.isStrict()).thenReturn(true); |
| 324 | + |
| 325 | + Model originalModel = mock(Model.class); |
| 326 | + Model modelWithPom = mock(Model.class); |
| 327 | + when(originalModel.withPomFile(pomPath)).thenReturn(modelWithPom); |
| 328 | + when(parser.locateAndParse(any(), any())).thenReturn(Optional.of(originalModel)); |
| 329 | + |
| 330 | + Model result = new DefaultModelProcessor(factory, List.of(parser)).read(request); |
| 331 | + |
| 332 | + assertSame(modelWithPom, result); |
| 333 | + verify(originalModel).withPomFile(pomPath); |
| 334 | + } |
253 | 335 |
|
254 | 336 |
|
255 | 337 | } |
0 commit comments