|
25 | 25 | import java.nio.file.StandardCopyOption;
|
26 | 26 | import java.util.List;
|
27 | 27 |
|
| 28 | +import org.apache.maven.api.model.InputLocation; |
| 29 | +import org.apache.maven.api.model.InputSource; |
28 | 30 | import org.apache.maven.artifact.Artifact;
|
29 | 31 | import org.apache.maven.artifact.repository.ArtifactRepository;
|
30 | 32 | import org.apache.maven.impl.InternalSession;
|
| 33 | +import org.apache.maven.internal.impl.DefaultProject; |
31 | 34 | import org.apache.maven.internal.impl.InternalMavenSession;
|
| 35 | +import org.apache.maven.model.Profile; |
32 | 36 | import org.junit.jupiter.api.BeforeEach;
|
33 | 37 | import org.junit.jupiter.api.Disabled;
|
34 | 38 | import org.junit.jupiter.api.Test;
|
35 | 39 | import org.junit.jupiter.api.io.TempDir;
|
| 40 | +import org.mockito.Mockito; |
36 | 41 |
|
37 | 42 | import static org.apache.maven.project.ProjectBuildingResultWithProblemMessageMatcher.projectBuildingResultWithProblemMessage;
|
38 | 43 | import static org.codehaus.plexus.testing.PlexusExtension.getTestFile;
|
39 | 44 | import static org.hamcrest.MatcherAssert.assertThat;
|
40 | 45 | import static org.hamcrest.Matchers.contains;
|
41 | 46 | import static org.hamcrest.Matchers.containsString;
|
| 47 | +import static org.hamcrest.Matchers.greaterThan; |
42 | 48 | import static org.hamcrest.Matchers.is;
|
43 | 49 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
44 | 50 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
@@ -345,6 +351,130 @@ void rereadPom_mng7063() throws Exception {
|
345 | 351 | assertThat(project.getName(), is("PROJECT NAME"));
|
346 | 352 | }
|
347 | 353 |
|
| 354 | + @Test |
| 355 | + void testActivatedProfileBySource() throws Exception { |
| 356 | + File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml"); |
| 357 | + |
| 358 | + ProjectBuildingRequest request = newBuildingRequest(); |
| 359 | + request.setLocalRepository(getLocalRepository()); |
| 360 | + request.setActiveProfileIds(List.of("profile1")); |
| 361 | + |
| 362 | + MavenProject project = projectBuilder.build(testPom, request).getProject(); |
| 363 | + |
| 364 | + assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId()))); |
| 365 | + assertTrue(project.getInjectedProfileIds().get("external").isEmpty()); |
| 366 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("profile1"::equals)); |
| 367 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals)); |
| 368 | + assertTrue( |
| 369 | + project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("active-by-default"::equals)); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + void testActivatedDefaultProfileBySource() throws Exception { |
| 374 | + File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml"); |
| 375 | + |
| 376 | + ProjectBuildingRequest request = newBuildingRequest(); |
| 377 | + request.setLocalRepository(getLocalRepository()); |
| 378 | + |
| 379 | + MavenProject project = projectBuilder.build(testPom, request).getProject(); |
| 380 | + |
| 381 | + assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId()))); |
| 382 | + assertTrue(project.getInjectedProfileIds().get("external").isEmpty()); |
| 383 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile1"::equals)); |
| 384 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals)); |
| 385 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("active-by-default"::equals)); |
| 386 | + |
| 387 | + InternalMavenSession session = Mockito.mock(InternalMavenSession.class); |
| 388 | + List<org.apache.maven.api.model.Profile> activeProfiles = |
| 389 | + new DefaultProject(session, project).getDeclaredActiveProfiles(); |
| 390 | + assertEquals(1, activeProfiles.size()); |
| 391 | + org.apache.maven.api.model.Profile profile = activeProfiles.get(0); |
| 392 | + assertEquals("active-by-default", profile.getId()); |
| 393 | + InputLocation location = profile.getLocation(""); |
| 394 | + assertNotNull(location); |
| 395 | + assertThat(location.getLineNumber(), greaterThan(0)); |
| 396 | + assertThat(location.getColumnNumber(), greaterThan(0)); |
| 397 | + assertNotNull(location.getSource()); |
| 398 | + assertThat(location.getSource().getLocation(), containsString("pom-with-profiles/pom.xml")); |
| 399 | + } |
| 400 | + |
| 401 | + @Test |
| 402 | + void testActivatedExternalProfileBySource() throws Exception { |
| 403 | + File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml"); |
| 404 | + |
| 405 | + ProjectBuildingRequest request = newBuildingRequest(); |
| 406 | + request.setLocalRepository(getLocalRepository()); |
| 407 | + |
| 408 | + final Profile externalProfile = new Profile(); |
| 409 | + externalProfile.setLocation( |
| 410 | + "", |
| 411 | + new org.apache.maven.model.InputLocation( |
| 412 | + 1, 1, new org.apache.maven.model.InputSource(new InputSource(null, "settings.xml", null)))); |
| 413 | + externalProfile.setId("external-profile"); |
| 414 | + request.addProfile(externalProfile); |
| 415 | + request.setActiveProfileIds(List.of(externalProfile.getId())); |
| 416 | + |
| 417 | + MavenProject project = projectBuilder.build(testPom, request).getProject(); |
| 418 | + |
| 419 | + assertTrue(project.getInjectedProfileIds().keySet().containsAll(List.of("external", project.getId()))); |
| 420 | + assertTrue(project.getInjectedProfileIds().get("external").stream().anyMatch("external-profile"::equals)); |
| 421 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile1"::equals)); |
| 422 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().noneMatch("profile2"::equals)); |
| 423 | + assertTrue(project.getInjectedProfileIds().get(project.getId()).stream().anyMatch("active-by-default"::equals)); |
| 424 | + |
| 425 | + InternalMavenSession session = Mockito.mock(InternalMavenSession.class); |
| 426 | + List<org.apache.maven.api.model.Profile> activeProfiles = |
| 427 | + new DefaultProject(session, project).getDeclaredActiveProfiles(); |
| 428 | + assertEquals(2, activeProfiles.size()); |
| 429 | + org.apache.maven.api.model.Profile profile = activeProfiles.get(0); |
| 430 | + assertEquals("active-by-default", profile.getId()); |
| 431 | + InputLocation location = profile.getLocation(""); |
| 432 | + assertNotNull(location); |
| 433 | + assertThat(location.getLineNumber(), greaterThan(0)); |
| 434 | + assertThat(location.getColumnNumber(), greaterThan(0)); |
| 435 | + assertNotNull(location.getSource()); |
| 436 | + assertThat(location.getSource().getLocation(), containsString("pom-with-profiles/pom.xml")); |
| 437 | + profile = activeProfiles.get(1); |
| 438 | + assertEquals("external-profile", profile.getId()); |
| 439 | + location = profile.getLocation(""); |
| 440 | + assertNotNull(location); |
| 441 | + assertThat(location.getLineNumber(), greaterThan(0)); |
| 442 | + assertThat(location.getColumnNumber(), greaterThan(0)); |
| 443 | + assertNotNull(location.getSource()); |
| 444 | + assertThat(location.getSource().getLocation(), containsString("settings.xml")); |
| 445 | + } |
| 446 | + |
| 447 | + @Test |
| 448 | + void testActivatedProfileIsResolved() throws Exception { |
| 449 | + File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml"); |
| 450 | + |
| 451 | + ProjectBuildingRequest request = newBuildingRequest(); |
| 452 | + request.setLocalRepository(getLocalRepository()); |
| 453 | + request.setActiveProfileIds(List.of("profile1")); |
| 454 | + |
| 455 | + MavenProject project = projectBuilder.build(testPom, request).getProject(); |
| 456 | + |
| 457 | + assertEquals(1, project.getActiveProfiles().size()); |
| 458 | + assertTrue(project.getActiveProfiles().stream().anyMatch(p -> "profile1".equals(p.getId()))); |
| 459 | + assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile2".equals(p.getId()))); |
| 460 | + assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "active-by-default".equals(p.getId()))); |
| 461 | + } |
| 462 | + |
| 463 | + @Test |
| 464 | + void testActivatedProfileByDefaultIsResolved() throws Exception { |
| 465 | + File testPom = getTestFile("src/test/resources/projects/pom-with-profiles/pom.xml"); |
| 466 | + |
| 467 | + ProjectBuildingRequest request = newBuildingRequest(); |
| 468 | + request.setLocalRepository(getLocalRepository()); |
| 469 | + |
| 470 | + MavenProject project = projectBuilder.build(testPom, request).getProject(); |
| 471 | + |
| 472 | + assertEquals(1, project.getActiveProfiles().size()); |
| 473 | + assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile1".equals(p.getId()))); |
| 474 | + assertTrue(project.getActiveProfiles().stream().noneMatch(p -> "profile2".equals(p.getId()))); |
| 475 | + assertTrue(project.getActiveProfiles().stream().anyMatch(p -> "active-by-default".equals(p.getId()))); |
| 476 | + } |
| 477 | + |
348 | 478 | /**
|
349 | 479 | * Tests whether external version range parent references are build correctly.
|
350 | 480 | *
|
|
0 commit comments