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