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 java.io.File;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.util.HashMap;
26  import java.util.HashSet;
27  import java.util.Iterator;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Set;
31  import junit.framework.TestCase;
32  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
33  import org.apache.maven.plugin.descriptor.MojoDescriptor;
34  import org.apache.maven.plugin.descriptor.Parameter;
35  import org.apache.maven.plugin.descriptor.PluginDescriptor;
36  import org.apache.maven.project.MavenProject;
37  import org.apache.maven.project.path.PathTranslator;
38  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
39  import org.apache.maven.tools.plugin.PluginToolsRequest;
40  import org.apache.maven.tools.plugin.extractor.ExtractionException;
41  import org.codehaus.plexus.component.repository.ComponentRequirement;
42  
43  public class AntMojoDescriptorExtractorTest
44      extends TestCase
45  {
46      
47      public void testBasicMojoExtraction_CheckInjectedParametersAndRequirements()
48          throws InvalidPluginDescriptorException, ExtractionException
49      {
50          Map scriptMap = buildTestMap( "basic" );
51          
52          PluginDescriptor pd = new PluginDescriptor();
53          
54          pd.setArtifactId( "test-plugin" );
55          pd.setGroupId( "org.mytest" );
56          pd.setVersion( "1" );
57          pd.setGoalPrefix( "mytest" );
58          
59          PluginToolsRequest request = new DefaultPluginToolsRequest( new MavenProject(), pd );
60          
61          List metadata = new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata( scriptMap, request );
62          
63          assertEquals( 2, metadata.size() );
64          
65          for ( Iterator it = metadata.iterator(); it.hasNext(); )
66          {
67              MojoDescriptor desc = (MojoDescriptor) it.next();
68              
69              if ( "test".equals( desc.getGoal() ) )
70              {
71                  assertTrue( desc.getImplementation().indexOf( ":" ) < 0 );
72              }
73              else if ( "test2".equals( desc.getGoal() ) )
74              {
75                  assertTrue( desc.getImplementation().endsWith( ":test2" ) );
76              }
77              
78              List params = desc.getParameters();
79              Map paramMap = new HashMap();
80              for ( Iterator paramIterator = params.iterator(); paramIterator.hasNext(); )
81              {
82                  Parameter param = (Parameter) paramIterator.next();
83                  paramMap.put( param.getName(), param );
84              }
85              
86              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.", paramMap.get( "basedir" ) );
87              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.", paramMap.get( "messageLevel" ) );
88              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.", paramMap.get( "project" ) );
89              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.", paramMap.get( "session" ) );
90              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.", paramMap.get( "mojoExecution" ) );
91              
92              List components = desc.getRequirements();
93  
94              assertNotNull( components );
95              assertEquals( 1, components.size() );
96              
97              ComponentRequirement req = (ComponentRequirement) components.get( 0 );
98              assertEquals( "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.", PathTranslator.class.getName(), req.getRole() );
99          }
100     }
101 
102     private Map buildTestMap( String resourceDirName )
103     {
104         try
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             // TODO As of JDK 7, replace with Paths.get( resource.toURI() ).toFile()
117             File mojosXml = new File( mojosXmlUrl.toURI() );
118             File dir = mojosXml.getParentFile();
119 
120             Set scripts = new HashSet();
121             String[] listing = dir.list();
122             for ( int i = 0; listing != null && i < listing.length; i++ )
123             {
124                 if ( listing[i].endsWith( ".mojos.xml" ) )
125                 {
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         }
136         catch ( final URISyntaxException e )
137         {
138             throw new AssertionError( e );
139         }
140     }
141 
142     // TODO
143 
144 }