View Javadoc
1   package org.apache.maven.tools.plugin.extractor.javadoc;
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.URL;
24  import java.util.ArrayList;
25  import java.util.List;
26  
27  import org.apache.maven.model.Build;
28  import org.apache.maven.model.Model;
29  import org.apache.maven.plugin.descriptor.MojoDescriptor;
30  import org.apache.maven.plugin.descriptor.Parameter;
31  import org.apache.maven.plugin.descriptor.PluginDescriptor;
32  import org.apache.maven.plugin.logging.SystemStreamLog;
33  import org.apache.maven.project.MavenProject;
34  import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
35  import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
36  import org.apache.maven.tools.plugin.PluginToolsRequest;
37  import org.apache.maven.tools.plugin.extractor.javadoc.JavaJavadocMojoDescriptorExtractor;
38  import org.apache.maven.tools.plugin.generator.Generator;
39  import org.apache.maven.tools.plugin.generator.PluginDescriptorGenerator;
40  import org.apache.maven.tools.plugin.util.PluginUtils;
41  import org.codehaus.plexus.component.repository.ComponentDependency;
42  import org.codehaus.plexus.logging.Logger;
43  import org.codehaus.plexus.logging.console.ConsoleLogger;
44  import org.codehaus.plexus.util.FileUtils;
45  
46  import junit.framework.TestCase;
47  
48  import org.custommonkey.xmlunit.Diff;
49  import org.custommonkey.xmlunit.XMLUnit;
50  import org.w3c.dom.Document;
51  
52  /**
53   * @author jdcasey
54   */
55  public class JavaMojoDescriptorExtractorTest
56      extends TestCase
57  {
58      private File root;
59  
60      protected void setUp()
61      {
62          File sourceFile = fileOf( "dir-flag.txt" );
63          root = sourceFile.getParentFile();
64      }
65  
66      private File fileOf( String classpathResource )
67      {
68          ClassLoader cl = Thread.currentThread().getContextClassLoader();
69          URL resource = cl.getResource( classpathResource );
70  
71          File result = null;
72          if ( resource != null )
73          {
74              result = FileUtils.toFile( resource );
75          }
76  
77          return result;
78      }
79  
80      private PluginToolsRequest createRequest( String directory )
81      {
82          Model model = new Model();
83          model.setArtifactId( "maven-unitTesting-plugin" );
84  
85          MavenProject project = new MavenProject( model );
86          project.setBuild( new Build()
87          {
88              @Override
89              public String getOutputDirectory()
90              {
91                  return new File( "target" ).getAbsolutePath();
92              }
93          } );
94  
95          project.setFile( new File( root, "pom.xml" ) );
96          project.addCompileSourceRoot( new File( root, directory ).getPath() );
97  
98          PluginDescriptor pluginDescriptor = new PluginDescriptor();
99          pluginDescriptor.setGoalPrefix( "test" );
100         pluginDescriptor.setDependencies( new ArrayList<ComponentDependency>() );
101 
102         return new DefaultPluginToolsRequest( project, pluginDescriptor ).setEncoding( "UTF-8" );
103     }
104 
105     /**
106      * generate plugin.xml for a test resources directory content.
107      */
108     protected PluginDescriptor generate( String directory )
109         throws Exception
110     {
111         JavaJavadocMojoDescriptorExtractor extractor = new JavaJavadocMojoDescriptorExtractor();
112         extractor.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "test" ) );
113         PluginToolsRequest request = createRequest( directory );
114 
115         List<MojoDescriptor> mojoDescriptors = extractor.execute( request );
116 
117         // to ensure order against plugin-expected.xml
118         PluginUtils.sortMojos( mojoDescriptors );
119 
120         for ( MojoDescriptor mojoDescriptor : mojoDescriptors )
121         {
122             // to ensure order against plugin-expected.xml
123             PluginUtils.sortMojoParameters( mojoDescriptor.getParameters() );
124 
125             request.getPluginDescriptor().addMojo( mojoDescriptor );
126         }
127 
128         Generator descriptorGenerator = new PluginDescriptorGenerator( new SystemStreamLog() );
129 
130         descriptorGenerator.execute( new File( root, directory ), request );
131 
132         return request.getPluginDescriptor();
133     }
134 
135     /**
136      * compare mojos from generated plugin.xml against plugin-expected.xml
137      */
138     protected void checkExpected( String directory )
139         throws Exception
140     {
141         File testDirectory = new File( root, directory );
142 
143         XMLUnit.setIgnoreWhitespace( true );
144         XMLUnit.setIgnoreComments( true );
145 
146         Document expected = XMLUnit.buildControlDocument(
147             FileUtils.fileRead( new File( testDirectory, "plugin-expected.xml" ), "UTF-8" ) );
148         Document actual =
149             XMLUnit.buildTestDocument( FileUtils.fileRead( new File( testDirectory, "plugin.xml" ), "UTF-8" ) );
150 
151         Diff diff = XMLUnit.compareXML( expected, actual );
152 
153         if ( !diff.identical() )
154         {
155             fail( "generated plugin.xml is not identical as plugin-expected.xml for " + directory + ": " + diff );
156         }
157     }
158 
159     /**
160      * extract plugin descriptor for test resources directory and check against plugin-expected.xml
161      */
162     @SuppressWarnings( "unchecked" )
163     protected List<MojoDescriptor> extract( String directory )
164         throws Exception
165     {
166         PluginDescriptor descriptor = generate( directory );
167 
168         checkExpected( directory );
169 
170         return descriptor.getMojos();
171     }
172 
173     public void testShouldFindTwoMojoDescriptorsInTestSourceDirectory()
174         throws Exception
175     {
176         List<MojoDescriptor> results = extract( "source" );
177 
178         assertEquals( "Extracted mojos", 2, results.size() );
179     }
180 
181     public void testShouldPropagateImplementationParameter()
182         throws Exception
183     {
184         List<MojoDescriptor> results = extract( "source2" );
185 
186         assertEquals( 1, results.size() );
187 
188         MojoDescriptor mojoDescriptor = results.get( 0 );
189 
190         @SuppressWarnings( "unchecked" ) List<Parameter> parameters = mojoDescriptor.getParameters();
191 
192         assertEquals( 1, parameters.size() );
193 
194         Parameter parameter = parameters.get( 0 );
195 
196         assertEquals( "Implementation parameter", "source2.sub.MyBla", parameter.getImplementation() );
197     }
198 
199     public void testMaven30Parameters()
200         throws Exception
201     {
202         List<MojoDescriptor> results = extract( "source2" );
203 
204         assertEquals( 1, results.size() );
205 
206         ExtendedMojoDescriptor mojoDescriptor = (ExtendedMojoDescriptor) results.get( 0 );
207         assertTrue( mojoDescriptor.isThreadSafe() );
208         assertEquals( "test", mojoDescriptor.getDependencyCollectionRequired() );
209     }
210 
211     /**
212      * Check that the mojo descriptor extractor will ignore any annotations that are found.
213      *
214      * @throws Exception
215      */
216     public void testAnnotationInPlugin()
217         throws Exception
218     {
219         List<MojoDescriptor> results = extract( "source3" );
220 
221         assertNull( results );
222     }
223 
224     /**
225      * Check that the mojo descriptor extractor will successfully parse sources with Java 1.5 language features like
226      * generics.
227      */
228     public void testJava15SyntaxParsing()
229         throws Exception
230     {
231         List<MojoDescriptor> results = extract( "java-1.5" );
232 
233         assertEquals( 1, results.size() );
234     }
235 
236 }