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