1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.tools.plugin.extractor.ant;
20
21 import java.io.File;
22 import java.net.URISyntaxException;
23 import java.net.URL;
24 import java.nio.file.Paths;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30
31 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
32 import org.apache.maven.plugin.descriptor.MojoDescriptor;
33 import org.apache.maven.plugin.descriptor.Parameter;
34 import org.apache.maven.plugin.descriptor.PluginDescriptor;
35 import org.apache.maven.project.MavenProject;
36 import org.apache.maven.project.path.PathTranslator;
37 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
38 import org.apache.maven.tools.plugin.PluginToolsRequest;
39 import org.apache.maven.tools.plugin.extractor.ExtractionException;
40 import org.codehaus.plexus.component.repository.ComponentRequirement;
41 import org.junit.jupiter.api.Test;
42
43 import static org.junit.jupiter.api.Assertions.assertEquals;
44 import static org.junit.jupiter.api.Assertions.assertFalse;
45 import static org.junit.jupiter.api.Assertions.assertNotNull;
46 import static org.junit.jupiter.api.Assertions.assertTrue;
47
48
49 public class AntMojoDescriptorExtractorTest {
50
51 @Test
52 void testBasicMojoExtraction_CheckInjectedParametersAndRequirements()
53 throws InvalidPluginDescriptorException, ExtractionException {
54 Map<String, Set<File>> scriptMap = buildTestMap("basic");
55
56 PluginDescriptor pd = new PluginDescriptor();
57
58 pd.setArtifactId("test-plugin");
59 pd.setGroupId("org.mytest");
60 pd.setVersion("1");
61 pd.setGoalPrefix("mytest");
62
63 PluginToolsRequest request = new DefaultPluginToolsRequest(new MavenProject(), pd);
64
65 List<MojoDescriptor> metadata =
66 new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata(scriptMap, request);
67
68 assertEquals(2, metadata.size());
69
70 for (MojoDescriptor desc : metadata) {
71 if ("test".equals(desc.getGoal())) {
72 assertFalse(desc.getImplementation().contains(":"));
73 } else if ("test2".equals(desc.getGoal())) {
74 assertTrue(desc.getImplementation().endsWith(":test2"));
75 }
76
77 List<Parameter> params = desc.getParameters();
78 Map<String, Parameter> paramMap = new HashMap<>();
79 for (Parameter param : params) {
80 paramMap.put(param.getName(), param);
81 }
82
83 assertNotNull(
84 paramMap.get("basedir"), "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.");
85 assertNotNull(
86 paramMap.get("messageLevel"),
87 "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.");
88 assertNotNull(
89 paramMap.get("project"), "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.");
90 assertNotNull(
91 paramMap.get("session"), "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.");
92 assertNotNull(
93 paramMap.get("mojoExecution"),
94 "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.");
95
96 List<ComponentRequirement> components = desc.getRequirements();
97
98 assertNotNull(components);
99 assertEquals(1, components.size());
100
101 ComponentRequirement req = components.get(0);
102 assertEquals(
103 PathTranslator.class.getName(),
104 req.getRole(),
105 "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.");
106 }
107 }
108
109 private Map<String, Set<File>> buildTestMap(String resourceDirName) {
110 try {
111 Map<String, Set<File>> result = new HashMap<>();
112 ClassLoader cloader = Thread.currentThread().getContextClassLoader();
113 URL mojosXmlUrl = cloader.getResource(resourceDirName + "/test.mojos.xml");
114
115 assertNotNull(
116 mojosXmlUrl,
117 "No classpath resource named: '" + resourceDirName + "/test.mojos.xml' could be found.");
118
119 File mojosXml = Paths.get(mojosXmlUrl.toURI()).toFile();
120 File dir = mojosXml.getParentFile();
121
122 Set<File> scripts = new HashSet<>();
123 String[] listing = dir.list();
124 for (int i = 0; listing != null && i < listing.length; i++) {
125 if (listing[i].endsWith(".mojos.xml")) {
126 File f = new File(dir, listing[i]).getAbsoluteFile();
127
128 scripts.add(f);
129 }
130 }
131
132 result.put(dir.getAbsolutePath(), scripts);
133
134 return result;
135 } catch (final URISyntaxException e) {
136 throw new AssertionError(e);
137 }
138 }
139
140
141
142 }