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.nio.file.Paths;
26  import java.util.HashMap;
27  import java.util.HashSet;
28  import java.util.List;
29  import java.util.Map;
30  import java.util.Set;
31  import org.apache.maven.plugin.descriptor.InvalidPluginDescriptorException;
32  import org.apache.maven.plugin.descriptor.MojoDescriptor;
33  import org.apache.maven.plugin.descriptor.Parameter;
34  import org.apache.maven.plugin.descriptor.PluginDescriptor;
35  import org.apache.maven.project.MavenProject;
36  import org.apache.maven.project.path.PathTranslator;
37  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
38  import org.apache.maven.tools.plugin.PluginToolsRequest;
39  import org.apache.maven.tools.plugin.extractor.ExtractionException;
40  import org.codehaus.plexus.component.repository.ComponentRequirement;
41  import org.junit.Test;
42  
43  import static org.junit.Assert.*;
44  
45  public class AntMojoDescriptorExtractorTest
46  {
47      
48      @Test
49      public void testBasicMojoExtraction_CheckInjectedParametersAndRequirements()
50          throws InvalidPluginDescriptorException, ExtractionException
51      {
52          Map<String, Set<File>> 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<MojoDescriptor> metadata = new AntMojoDescriptorExtractor().extractMojoDescriptorsFromMetadata( scriptMap, request );
64          
65          assertEquals( 2, metadata.size() );
66  
67          for ( MojoDescriptor desc : metadata )
68          {
69              if ( "test".equals( desc.getGoal() ) )
70              {
71                  assertFalse( desc.getImplementation().contains( ":" ) );
72              }
73              else if ( "test2".equals( desc.getGoal() ) )
74              {
75                  assertTrue( desc.getImplementation().endsWith( ":test2" ) );
76              }
77  
78              List<Parameter> params = desc.getParameters();
79              Map<String, Parameter> paramMap = new HashMap<>();
80              for ( Parameter param : params )
81              {
82                  paramMap.put( param.getName(), param );
83              }
84  
85              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'basedir' parameter.", paramMap.get( "basedir" ) );
86              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'messageLevel' parameter.", paramMap.get( "messageLevel" ) );
87              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'project' parameter.", paramMap.get( "project" ) );
88              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'session' parameter.", paramMap.get( "session" ) );
89              assertNotNull( "Mojo descriptor: " + desc.getGoal() + " is missing 'mojoExecution' parameter.", paramMap.get( "mojoExecution" ) );
90  
91              List<ComponentRequirement> components = desc.getRequirements();
92  
93              assertNotNull( components );
94              assertEquals( 1, components.size() );
95  
96              ComponentRequirement req = components.get( 0 );
97              assertEquals( "Mojo descriptor: " + desc.getGoal() + " is missing 'PathTranslator' component requirement.", PathTranslator.class.getName(), req.getRole() );
98          }
99      }
100 
101     private Map<String, Set<File>> buildTestMap( String resourceDirName )
102     {
103         try
104         {
105             Map<String, Set<File>> result = new HashMap<>();
106             ClassLoader cloader = Thread.currentThread().getContextClassLoader();
107             URL mojosXmlUrl = cloader.getResource( resourceDirName + "/test.mojos.xml" );
108 
109             if ( mojosXmlUrl == null )
110             {
111                 fail( "No classpath resource named: '" + resourceDirName + "/test.mojos.xml' could be found." );
112             }
113 
114             File mojosXml = Paths.get( mojosXmlUrl.toURI() ).toFile();
115             File dir = mojosXml.getParentFile();
116 
117             Set<File> scripts = new HashSet<>();
118             String[] listing = dir.list();
119             for ( int i = 0; listing != null && i < listing.length; i++ )
120             {
121                 if ( listing[i].endsWith( ".mojos.xml" ) )
122                 {
123                     File f = new File( dir, listing[i] ).getAbsoluteFile();
124 
125                     scripts.add( f );
126                 }
127             }
128 
129             result.put( dir.getAbsolutePath(), scripts );
130 
131             return result;
132         }
133         catch ( final URISyntaxException e )
134         {
135             throw new AssertionError( e );
136         }
137     }
138 
139     // TODO
140 
141 }