1 package org.apache.maven.tools.plugin.scanner;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.model.Build;
23 import org.apache.maven.model.Model;
24 import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
25 import org.apache.maven.plugin.descriptor.MojoDescriptor;
26 import org.apache.maven.plugin.descriptor.PluginDescriptor;
27 import org.apache.maven.project.MavenProject;
28 import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
29 import org.apache.maven.tools.plugin.extractor.ExtractionException;
30 import org.apache.maven.tools.plugin.extractor.MojoDescriptorExtractor;
31 import org.junit.Before;
32 import org.junit.Test;
33
34 import java.io.File;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.junit.Assert.fail;
47
48
49
50
51 public class DefaultMojoScannerTest
52 {
53 private Map<String, MojoDescriptorExtractor> extractors;
54
55 private Build build;
56
57 private Model model;
58
59 private MojoScanner scanner;
60
61 private MavenProject project;
62
63 @Before
64 public void setUp()
65 {
66 extractors = new HashMap<>();
67 extractors.put( "one", new ScannerTestExtractor( "one" ) );
68 extractors.put( "two", new ScannerTestExtractor( "two" ) );
69 extractors.put( "three", new ScannerTestExtractor( "three" ) );
70
71 scanner = new DefaultMojoScanner( extractors );
72
73 build = new Build();
74 build.setSourceDirectory( "testdir" );
75
76 model = new Model();
77 model.setBuild( build );
78
79 project = new MavenProject( model );
80 project.setFile( new File( "." ) );
81 }
82
83 @Test
84 public void testUnspecifiedExtractors()
85 throws Exception
86 {
87 PluginDescriptor pluginDescriptor = createPluginDescriptor();
88
89 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
90
91 checkResult( pluginDescriptor, extractors.keySet() );
92 }
93
94 @Test
95 public void testSpecifiedExtractors()
96 throws Exception
97 {
98 Set<String> activeExtractors = new HashSet<>();
99 activeExtractors.add( "one" );
100 activeExtractors.add( "" );
101 activeExtractors.add( null );
102 activeExtractors.add( "three" );
103
104 PluginDescriptor pluginDescriptor = createPluginDescriptor();
105
106 scanner.setActiveExtractors( activeExtractors );
107 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
108
109 checkResult( pluginDescriptor, Arrays.asList( "one", "three" ) );
110 }
111
112 @Test
113 public void testAllExtractorsThroughNull()
114 throws Exception
115 {
116 PluginDescriptor pluginDescriptor = createPluginDescriptor();
117
118 scanner.setActiveExtractors( null );
119 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
120
121 checkResult( pluginDescriptor, extractors.keySet() );
122 }
123
124 @Test
125 public void testNoExtractorsThroughEmptySet()
126 throws Exception
127 {
128 PluginDescriptor pluginDescriptor = createPluginDescriptor();
129
130 scanner.setActiveExtractors( Collections.<String>emptySet() );
131 try
132 {
133 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
134 fail( "Expected exception" );
135 }
136 catch (InvalidPluginDescriptorException e)
137 {
138
139 }
140
141 checkResult( pluginDescriptor, Collections.<String>emptySet() );
142 }
143
144 @Test
145 public void testUnknownExtractor()
146 throws Exception
147 {
148 Set<String> activeExtractors = new HashSet<>();
149 activeExtractors.add( "four" );
150
151 PluginDescriptor pluginDescriptor = createPluginDescriptor();
152
153 scanner.setActiveExtractors( activeExtractors );
154
155 try
156 {
157 scanner.populatePluginDescriptor( new DefaultPluginToolsRequest( project, pluginDescriptor ) );
158 fail( "No error for unknown extractor" );
159 }
160 catch ( ExtractionException e )
161 {
162
163 }
164
165 checkResult( pluginDescriptor, Collections.<String>emptySet() );
166 }
167
168 private PluginDescriptor createPluginDescriptor()
169 {
170 PluginDescriptor pluginDescriptor = new PluginDescriptor();
171 pluginDescriptor.setGroupId( "groupId" );
172 pluginDescriptor.setArtifactId( "artifactId" );
173 pluginDescriptor.setVersion( "version" );
174 pluginDescriptor.setGoalPrefix( "testId" );
175 return pluginDescriptor;
176 }
177
178
179
180
181
182
183
184
185 protected void checkResult( PluginDescriptor pluginDescriptor, Collection<String> expectedGoals )
186 {
187 Set<String> remainingGoals = new HashSet<>( expectedGoals );
188 List<MojoDescriptor> descriptors = pluginDescriptor.getMojos();
189
190 if ( descriptors == null )
191 {
192
193 descriptors = Collections.emptyList();
194 }
195
196 for ( MojoDescriptor desc : descriptors )
197 {
198 assertEquals( pluginDescriptor, desc.getPluginDescriptor() );
199 assertTrue( "Unexpected goal in PluginDescriptor: " + desc.getGoal(),
200 remainingGoals.remove( desc.getGoal() ) );
201 }
202
203 assertEquals( "Expected goals missing from PluginDescriptor: " + remainingGoals, 0, remainingGoals.size() );
204 }
205
206 }