|
20 | 20 |
|
21 | 21 | import java.nio.file.Path; |
22 | 22 | import java.nio.file.Paths; |
| 23 | +import java.util.function.Supplier; |
23 | 24 |
|
| 25 | +import org.apache.maven.api.Language; |
24 | 26 | import org.apache.maven.api.ProducedArtifact; |
25 | 27 | import org.apache.maven.api.Project; |
| 28 | +import org.apache.maven.api.ProjectScope; |
26 | 29 | import org.apache.maven.api.services.ArtifactManager; |
27 | 30 | import org.apache.maven.impl.DefaultModelVersionParser; |
| 31 | +import org.apache.maven.impl.DefaultSourceRoot; |
28 | 32 | import org.apache.maven.impl.DefaultVersionParser; |
29 | 33 | import org.apache.maven.project.MavenProject; |
30 | 34 | import org.eclipse.aether.util.version.GenericVersionScheme; |
31 | 35 | import org.junit.jupiter.api.Test; |
32 | 36 | import org.mockito.Mockito; |
33 | 37 |
|
34 | 38 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
35 | 40 | import static org.mockito.Mockito.when; |
36 | 41 |
|
37 | 42 | class DefaultProjectManagerTest { |
38 | 43 |
|
| 44 | + private DefaultProjectManager projectManager; |
| 45 | + |
| 46 | + private Project project; |
| 47 | + |
| 48 | + private ProducedArtifact artifact; |
| 49 | + |
| 50 | + private Path artifactPath; |
| 51 | + |
39 | 52 | @Test |
40 | 53 | void attachArtifact() { |
41 | 54 | InternalMavenSession session = Mockito.mock(InternalMavenSession.class); |
42 | 55 | ArtifactManager artifactManager = Mockito.mock(ArtifactManager.class); |
43 | 56 | MavenProject mavenProject = new MavenProject(); |
44 | | - Project project = new DefaultProject(session, mavenProject); |
45 | | - ProducedArtifact artifact = Mockito.mock(ProducedArtifact.class); |
46 | | - Path path = Paths.get(""); |
| 57 | + project = new DefaultProject(session, mavenProject); |
| 58 | + artifact = Mockito.mock(ProducedArtifact.class); |
| 59 | + artifactPath = Paths.get(""); |
47 | 60 | DefaultVersionParser versionParser = |
48 | 61 | new DefaultVersionParser(new DefaultModelVersionParser(new GenericVersionScheme())); |
49 | | - DefaultProjectManager projectManager = new DefaultProjectManager(session, artifactManager); |
| 62 | + projectManager = new DefaultProjectManager(session, artifactManager); |
50 | 63 |
|
51 | 64 | mavenProject.setGroupId("myGroup"); |
52 | 65 | mavenProject.setArtifactId("myArtifact"); |
53 | 66 | mavenProject.setVersion("1.0-SNAPSHOT"); |
54 | 67 | when(artifact.getGroupId()).thenReturn("myGroup"); |
55 | 68 | when(artifact.getArtifactId()).thenReturn("myArtifact"); |
56 | 69 | when(artifact.getBaseVersion()).thenReturn(versionParser.parseVersion("1.0-SNAPSHOT")); |
57 | | - projectManager.attachArtifact(project, artifact, path); |
| 70 | + projectManager.attachArtifact(project, artifact, artifactPath); |
58 | 71 |
|
| 72 | + // Verify that an exception is thrown when the artifactId differs |
59 | 73 | when(artifact.getArtifactId()).thenReturn("anotherArtifact"); |
60 | | - assertThrows(IllegalArgumentException.class, () -> projectManager.attachArtifact(project, artifact, path)); |
| 74 | + assertExceptionMessageContains("myGroup:myArtifact:1.0-SNAPSHOT", "myGroup:anotherArtifact:1.0-SNAPSHOT"); |
| 75 | + |
| 76 | + // Add a Java module. It should relax the restriction on artifactId. |
| 77 | + projectManager.addSourceRoot( |
| 78 | + project, |
| 79 | + new DefaultSourceRoot( |
| 80 | + ProjectScope.MAIN, |
| 81 | + Language.JAVA_FAMILY, |
| 82 | + "org.foo.bar", |
| 83 | + null, |
| 84 | + Path.of("myProject"), |
| 85 | + null, |
| 86 | + null, |
| 87 | + false, |
| 88 | + null, |
| 89 | + true)); |
| 90 | + |
| 91 | + // Verify that we get the same exception when the artifactId does not match the module name |
| 92 | + assertExceptionMessageContains("", "anotherArtifact"); |
| 93 | + |
| 94 | + // Verify that no exception is thrown when the artifactId is the module name |
| 95 | + when(artifact.getArtifactId()).thenReturn("org.foo.bar"); |
| 96 | + projectManager.attachArtifact(project, artifact, artifactPath); |
| 97 | + |
| 98 | + // Verify that an exception is thrown when the groupId differs |
| 99 | + when(artifact.getGroupId()).thenReturn("anotherGroup"); |
| 100 | + assertExceptionMessageContains("myGroup:myArtifact:1.0-SNAPSHOT", "anotherGroup:org.foo.bar:1.0-SNAPSHOT"); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Verifies that {@code projectManager.attachArtifact(…)} throws an exception, |
| 105 | + * and that the expecption message contains the expected and actual <abbr>GAV</abbr>. |
| 106 | + * |
| 107 | + * @param expectedGAV the actual <abbr>GAV</abbr> that the exception message should contain |
| 108 | + * @param actualGAV the actual <abbr>GAV</abbr> that the exception message should contain |
| 109 | + */ |
| 110 | + private void assertExceptionMessageContains(String expectedGAV, String actualGAV) { |
| 111 | + String cause = assertThrows( |
| 112 | + IllegalArgumentException.class, |
| 113 | + () -> projectManager.attachArtifact(project, artifact, artifactPath)) |
| 114 | + .getMessage(); |
| 115 | + Supplier<String> message = () -> |
| 116 | + String.format("The exception message does not contain the expected GAV. Message was:%n%s%n", cause); |
| 117 | + |
| 118 | + assertTrue(cause.contains(expectedGAV), message); |
| 119 | + assertTrue(cause.contains(actualGAV), message); |
61 | 120 | } |
62 | 121 | } |
0 commit comments