View Javadoc
1   package org.apache.maven.tools.plugin.extractor.ant;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
23  import org.apache.maven.plugin.descriptor.MojoDescriptor;
24  import org.apache.maven.plugin.descriptor.Parameter;
25  import org.apache.maven.plugin.descriptor.PluginDescriptor;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.project.path.PathTranslator;
28  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
29  import org.apache.maven.tools.plugin.PluginToolsRequest;
30  import org.apache.maven.tools.plugin.extractor.ExtractionException;
31  import org.codehaus.plexus.component.repository.ComponentRequirement;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  import java.io.File;
35  import java.net.URL;
36  import java.util.HashMap;
37  import java.util.HashSet;
38  import java.util.Iterator;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.Set;
42  
43  import junit.framework.TestCase;
44  
45  public class AntMojoDescriptorExtractorTest
46      extends TestCase
47  {
48      
49      public void testBasicMojoExtraction_CheckInjectedParametersAndRequirements()
50          throws InvalidPluginDescriptorException, ExtractionException
51      {
52          Map scriptMap = buildTestMap( "basic" );
53          
54          PluginDescriptor pd = new PluginDescriptor();
55          
56          pd.setArtifactId( "test-plugin" );
57          pd.setGroupId( "org.mytest" );
58          pd.setVersion( "1" );
59          pd.setGoalPrefix( "mytest" );
60          
61          PluginToolsRequest request = new DefaultPluginToolsRequest( new MavenProject(), pd );
62          
63          List metadata = new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata( scriptMap, request );
64          
65          assertEquals( 2, metadata.size() );
66          
67          for ( Iterator it = metadata.iterator(); it.hasNext(); )
68          {
69              MojoDescriptor desc = (MojoDescriptor) it.next();
70              
71              if ( "test".equals( desc.getGoal() ) )
72              {
73                  assertTrue( desc.getImplementation().indexOf( ":" ) < 0 );
74              }
75              else if ( "test2".equals( desc.getGoal() ) )
76              {
77                  assertTrue( desc.getImplementation().endsWith( ":test2" ) );
78              }
79              
80              List params = desc.getParameters();
81              Map paramMap = new HashMap();
82              for ( Iterator paramIterator = params.iterator(); paramIterator.hasNext(); )
83              {
84                  Parameter param = (Parameter) paramIterator.next();
85                  paramMap.put( param.getName(), param );
86              }
87              
88              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.", paramMap.get( "basedir" ) );
89              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.", paramMap.get( "messageLevel" ) );
90              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.", paramMap.get( "project" ) );
91              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.", paramMap.get( "session" ) );
92              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.", paramMap.get( "mojoExecution" ) );
93              
94              List components = desc.getRequirements();
95  
96              assertNotNull( components );
97              assertEquals( 1, components.size() );
98              
99              ComponentRequirement req = (ComponentRequirement) components.get( 0 );
100             assertEquals( "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.", PathTranslator.class.getName(), req.getRole() );
101         }
102     }
103     
104     private Map buildTestMap( String resourceDirName )
105     {
106         Map result = new HashMap();
107         
108         ClassLoader cloader = Thread.currentThread().getContextClassLoader();
109         URL mojosXmlUrl = cloader.getResource( resourceDirName + "/test.mojos.xml" );
110         
111         if ( mojosXmlUrl == null )
112         {
113             fail( "No classpath resource named: '" + resourceDirName + "/test.mojos.xml' could be found." );
114         }
115         
116         File mojosXml = new File( StringUtils.replace( mojosXmlUrl.getPath(), "%20", " " ) );
117         File dir = mojosXml.getParentFile();
118         
119         Set scripts = new HashSet();
120         String[] listing = dir.list();
121         for ( int i = 0; listing != null && i < listing.length; i++ )
122         {
123             if ( listing[i].endsWith( ".mojos.xml" ) )
124             {
125                 File f = new File( dir, listing[i] ).getAbsoluteFile();
126                 
127                 scripts.add( f );
128             }
129         }
130         
131         result.put( dir.getAbsolutePath(), scripts );
132         
133         return result;
134     }
135     
136     // TODO
137 
138 }