|
31 | 31 | import java.util.Arrays; |
32 | 32 | import java.util.Collections; |
33 | 33 | import java.util.HashMap; |
| 34 | +import java.util.List; |
34 | 35 | import java.util.Map; |
35 | 36 | import java.util.Optional; |
36 | 37 | import java.util.Properties; |
|
39 | 40 | import org.apache.commons.lang3.tuple.Pair; |
40 | 41 | import org.apache.maven.buildcache.DefaultPluginScanConfig; |
41 | 42 | import org.apache.maven.buildcache.hash.HashFactory; |
| 43 | +import org.apache.maven.buildcache.xml.config.AttachedOutputs; |
42 | 44 | import org.apache.maven.buildcache.xml.config.Configuration; |
| 45 | +import org.apache.maven.buildcache.xml.config.DirName; |
43 | 46 | import org.apache.maven.buildcache.xml.config.Remote; |
44 | 47 | import org.apache.maven.execution.MavenExecutionRequest; |
45 | 48 | import org.apache.maven.execution.MavenSession; |
| 49 | +import org.apache.maven.model.Build; |
46 | 50 | import org.apache.maven.model.Plugin; |
47 | 51 | import org.apache.maven.model.PluginExecution; |
48 | 52 | import org.apache.maven.plugin.MojoExecution; |
| 53 | +import org.apache.maven.project.MavenProject; |
49 | 54 | import org.apache.maven.rtinfo.RuntimeInformation; |
50 | 55 | import org.junit.jupiter.api.BeforeEach; |
51 | 56 | import org.junit.jupiter.api.Test; |
@@ -169,7 +174,7 @@ private void assertDefaults(Map<String, Runnable> overrides) { |
169 | 174 | asserts.put("calculateProjectVersionChecksum", () -> assertFalse(testObject.calculateProjectVersionChecksum())); |
170 | 175 | asserts.put("canIgnore", () -> assertFalse(testObject.canIgnore(mock(MojoExecution.class)))); |
171 | 176 | asserts.put("getAlwaysRunPlugins", () -> assertNull(testObject.getAlwaysRunPlugins())); |
172 | | - asserts.put("getAttachedOutputs", () -> assertEquals(Collections.emptyList(), testObject.getAttachedOutputs())); |
| 177 | + // getAttachedOutputs removed - requires MavenProject parameter, tested separately |
173 | 178 | asserts.put("getBaselineCacheUrl", () -> assertNull(testObject.getBaselineCacheUrl())); |
174 | 179 | asserts.put("getDefaultGlob", () -> assertEquals("*", testObject.getDefaultGlob())); |
175 | 180 | asserts.put( |
@@ -482,4 +487,111 @@ void testRemoveSaveFinalIgnoredWhenRemoteSaveDisabled() { |
482 | 487 | Pair.of("getUrl", () -> assertEquals("dummy.url.xyz", testObject.getUrl())), |
483 | 488 | Pair.of("isRemoteCacheEnabled", () -> assertTrue(testObject.isRemoteCacheEnabled()))); |
484 | 489 | } |
| 490 | + |
| 491 | + @Test |
| 492 | + void testDefaultAttachedOutputsWhenNotConfigured() { |
| 493 | + // When attachedOutputs is not configured in XML, should return default list |
| 494 | + Configuration configuration = new Configuration(); |
| 495 | + // Deliberately not setting attachedOutputs |
| 496 | + testCacheConfig.setConfiguration(configuration); |
| 497 | + |
| 498 | + assertEquals(CacheState.INITIALIZED, testObject.initialize()); |
| 499 | + |
| 500 | + // Create mock project with default build configuration |
| 501 | + MavenProject mockProject = mock(MavenProject.class); |
| 502 | + Build mockBuild = mock(Build.class); |
| 503 | + when(mockProject.getBuild()).thenReturn(mockBuild); |
| 504 | + when(mockBuild.getDirectory()).thenReturn("/project/target"); |
| 505 | + when(mockBuild.getOutputDirectory()).thenReturn("/project/target/classes"); |
| 506 | + when(mockBuild.getTestOutputDirectory()).thenReturn("/project/target/test-classes"); |
| 507 | + |
| 508 | + List<DirName> attachedOutputs = testObject.getAttachedOutputs(mockProject); |
| 509 | + assertEquals(2, attachedOutputs.size(), "Should have 2 default attached outputs"); |
| 510 | + |
| 511 | + List<String> dirNames = attachedOutputs.stream() |
| 512 | + .map(DirName::getValue) |
| 513 | + .collect(Collectors.toList()); |
| 514 | + |
| 515 | + assertTrue(dirNames.contains("classes"), "Should include 'classes' directory by default"); |
| 516 | + assertTrue(dirNames.contains("test-classes"), "Should include 'test-classes' directory by default"); |
| 517 | + } |
| 518 | + |
| 519 | + @Test |
| 520 | + void testExplicitAttachedOutputsOverridesDefaults() { |
| 521 | + // When attachedOutputs is explicitly configured, should use those values instead of defaults |
| 522 | + Configuration configuration = new Configuration(); |
| 523 | + AttachedOutputs attachedOutputs = new AttachedOutputs(); |
| 524 | + |
| 525 | + DirName customDir = new DirName(); |
| 526 | + customDir.setValue("custom-output"); |
| 527 | + attachedOutputs.addDirName(customDir); |
| 528 | + |
| 529 | + configuration.setAttachedOutputs(attachedOutputs); |
| 530 | + testCacheConfig.setConfiguration(configuration); |
| 531 | + |
| 532 | + assertEquals(CacheState.INITIALIZED, testObject.initialize()); |
| 533 | + |
| 534 | + // Create mock project (not used when explicit config is set, but required by interface) |
| 535 | + MavenProject mockProject = mock(MavenProject.class); |
| 536 | + Build mockBuild = mock(Build.class); |
| 537 | + when(mockProject.getBuild()).thenReturn(mockBuild); |
| 538 | + when(mockBuild.getDirectory()).thenReturn("/project/target"); |
| 539 | + when(mockBuild.getOutputDirectory()).thenReturn("/project/target/classes"); |
| 540 | + when(mockBuild.getTestOutputDirectory()).thenReturn("/project/target/test-classes"); |
| 541 | + |
| 542 | + List<DirName> result = testObject.getAttachedOutputs(mockProject); |
| 543 | + assertEquals(1, result.size(), "Should have 1 explicitly configured output"); |
| 544 | + assertEquals("custom-output", result.get(0).getValue(), |
| 545 | + "Should use explicitly configured directory, not defaults"); |
| 546 | + } |
| 547 | + |
| 548 | + @Test |
| 549 | + void testDefaultAttachedOutputsDisabledViaProperty() { |
| 550 | + // When attachedOutputs.enabled property is false, should return empty list |
| 551 | + Configuration configuration = new Configuration(); |
| 552 | + testCacheConfig.setConfiguration(configuration); |
| 553 | + |
| 554 | + when(mockProperties.getProperty(CacheConfigImpl.ATTACHED_OUTPUTS_ENABLED_PROPERTY_NAME)) |
| 555 | + .thenReturn("false"); |
| 556 | + |
| 557 | + assertEquals(CacheState.INITIALIZED, testObject.initialize()); |
| 558 | + |
| 559 | + // Create mock project |
| 560 | + MavenProject mockProject = mock(MavenProject.class); |
| 561 | + Build mockBuild = mock(Build.class); |
| 562 | + when(mockProject.getBuild()).thenReturn(mockBuild); |
| 563 | + when(mockBuild.getDirectory()).thenReturn("/project/target"); |
| 564 | + when(mockBuild.getOutputDirectory()).thenReturn("/project/target/classes"); |
| 565 | + when(mockBuild.getTestOutputDirectory()).thenReturn("/project/target/test-classes"); |
| 566 | + |
| 567 | + List<DirName> attachedOutputs = testObject.getAttachedOutputs(mockProject); |
| 568 | + assertEquals(0, attachedOutputs.size(), "Should return empty list when disabled via property"); |
| 569 | + } |
| 570 | + |
| 571 | + @Test |
| 572 | + void testDefaultAttachedOutputsWithCustomDirectories() { |
| 573 | + // When project has custom output directories, should use those |
| 574 | + Configuration configuration = new Configuration(); |
| 575 | + testCacheConfig.setConfiguration(configuration); |
| 576 | + |
| 577 | + assertEquals(CacheState.INITIALIZED, testObject.initialize()); |
| 578 | + |
| 579 | + // Create mock project with custom output directories |
| 580 | + MavenProject mockProject = mock(MavenProject.class); |
| 581 | + Build mockBuild = mock(Build.class); |
| 582 | + when(mockProject.getBuild()).thenReturn(mockBuild); |
| 583 | + when(mockBuild.getDirectory()).thenReturn("/project/build"); |
| 584 | + when(mockBuild.getOutputDirectory()).thenReturn("/project/build/custom-classes"); |
| 585 | + when(mockBuild.getTestOutputDirectory()).thenReturn("/project/build/custom-test-classes"); |
| 586 | + |
| 587 | + List<DirName> attachedOutputs = testObject.getAttachedOutputs(mockProject); |
| 588 | + assertEquals(2, attachedOutputs.size(), "Should have 2 default attached outputs"); |
| 589 | + |
| 590 | + List<String> dirNames = attachedOutputs.stream() |
| 591 | + .map(DirName::getValue) |
| 592 | + .collect(Collectors.toList()); |
| 593 | + |
| 594 | + assertTrue(dirNames.contains("custom-classes"), "Should include custom output directory"); |
| 595 | + assertTrue(dirNames.contains("custom-test-classes"), "Should include custom test output directory"); |
| 596 | + } |
485 | 597 | } |
0 commit comments