001    package org.apache.maven.tools.plugin.extractor.java;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import junit.framework.TestCase;
023    import org.apache.maven.model.Build;
024    import org.apache.maven.model.Model;
025    import org.apache.maven.plugin.descriptor.MojoDescriptor;
026    import org.apache.maven.plugin.descriptor.Parameter;
027    import org.apache.maven.plugin.descriptor.PluginDescriptor;
028    import org.apache.maven.project.MavenProject;
029    import org.apache.maven.tools.plugin.DefaultPluginToolsRequest;
030    import org.apache.maven.tools.plugin.ExtendedMojoDescriptor;
031    import org.apache.maven.tools.plugin.PluginToolsRequest;
032    import org.apache.maven.tools.plugin.generator.Generator;
033    import org.apache.maven.tools.plugin.generator.PluginDescriptorGenerator;
034    import org.apache.maven.tools.plugin.util.PluginUtils;
035    import org.codehaus.plexus.component.repository.ComponentDependency;
036    import org.codehaus.plexus.logging.Logger;
037    import org.codehaus.plexus.logging.console.ConsoleLogger;
038    import org.codehaus.plexus.util.FileUtils;
039    import org.custommonkey.xmlunit.Diff;
040    import org.custommonkey.xmlunit.XMLUnit;
041    import org.w3c.dom.Document;
042    
043    import java.io.File;
044    import java.net.URL;
045    import java.util.ArrayList;
046    import java.util.List;
047    
048    /**
049     * @author jdcasey
050     */
051    public class JavaMojoDescriptorExtractorTest
052        extends TestCase
053    {
054        private File root;
055    
056        protected void setUp()
057        {
058            File sourceFile = fileOf( "dir-flag.txt" );
059            root = sourceFile.getParentFile();
060        }
061    
062        private File fileOf( String classpathResource )
063        {
064            ClassLoader cl = Thread.currentThread().getContextClassLoader();
065            URL resource = cl.getResource( classpathResource );
066    
067            File result = null;
068            if ( resource != null )
069            {
070                result = FileUtils.toFile( resource );
071            }
072    
073            return result;
074        }
075    
076        private PluginToolsRequest createRequest( String directory )
077        {
078            Model model = new Model();
079            model.setArtifactId( "maven-unitTesting-plugin" );
080    
081            MavenProject project = new MavenProject( model );
082            project.setBuild( new Build(){
083                @Override
084                public String getOutputDirectory()
085                {
086                    return System.getProperty( "filePath" );
087                }
088            });
089    
090            project.setFile( new File( root, "pom.xml" ) );
091            project.addCompileSourceRoot( new File( root, directory ).getPath() );
092    
093            PluginDescriptor pluginDescriptor = new PluginDescriptor();
094            pluginDescriptor.setGoalPrefix( "test" );
095            pluginDescriptor.setDependencies( new ArrayList<ComponentDependency>() );
096    
097            return new DefaultPluginToolsRequest( project, pluginDescriptor ).setEncoding( "UTF-8" );
098        }
099    
100        /**
101         * generate plugin.xml for a test resources directory content.
102         */
103        protected PluginDescriptor generate( String directory )
104            throws Exception
105        {
106            JavaMojoDescriptorExtractor extractor = new JavaMojoDescriptorExtractor();
107            extractor.enableLogging( new ConsoleLogger( Logger.LEVEL_INFO, "test" ) );
108            PluginToolsRequest request = createRequest( directory );
109    
110            List<MojoDescriptor> mojoDescriptors = extractor.execute( request );
111    
112            // to ensure order against plugin-expected.xml
113            PluginUtils.sortMojos( mojoDescriptors );
114    
115            for ( MojoDescriptor mojoDescriptor : mojoDescriptors )
116            {
117                // to ensure order against plugin-expected.xml
118                PluginUtils.sortMojoParameters( mojoDescriptor.getParameters() );
119    
120                request.getPluginDescriptor().addMojo( mojoDescriptor );
121            }
122    
123            Generator descriptorGenerator = new PluginDescriptorGenerator();
124    
125            descriptorGenerator.execute( new File( root, directory ), request );
126    
127            return request.getPluginDescriptor();
128        }
129    
130        /**
131         * compare mojos from generated plugin.xml against plugin-expected.xml
132         */
133        protected void checkExpected( String directory )
134            throws Exception
135        {
136            File testDirectory = new File( root, directory );
137    
138            XMLUnit.setIgnoreWhitespace( true );
139            XMLUnit.setIgnoreComments( true );
140            
141            Document expected =
142                XMLUnit.buildControlDocument( FileUtils.fileRead( new File( testDirectory, "plugin-expected.xml" ), "UTF-8" ) );
143            Document actual =
144                XMLUnit.buildTestDocument( FileUtils.fileRead( new File( testDirectory, "plugin.xml" ), "UTF-8" ) );
145    
146            Diff diff = XMLUnit.compareXML( expected, actual );
147    
148            if ( !diff.identical() )
149            {
150                fail( "generated plugin.xml is not identical as plugin-expected.xml for " + directory + ": " + diff );
151            }
152        }
153    
154        /**
155         * extract plugin descriptor for test resources directory and check against plugin-expected.xml
156         */
157        @SuppressWarnings( "unchecked" )
158        protected List<MojoDescriptor> extract( String directory )
159            throws Exception
160        {
161            PluginDescriptor descriptor = generate( directory );
162    
163            checkExpected( directory );
164    
165            return descriptor.getMojos();
166        }
167    
168        public void testShouldFindTwoMojoDescriptorsInTestSourceDirectory()
169            throws Exception
170        {
171            List<MojoDescriptor> results = extract( "source" );
172    
173            assertEquals( "Extracted mojos", 2, results.size() );
174        }
175    
176        public void testShouldPropagateImplementationParameter()
177            throws Exception
178        {
179            List<MojoDescriptor> results = extract( "source2" );
180    
181            assertEquals( 1, results.size() );
182    
183            MojoDescriptor mojoDescriptor = results.get( 0 );
184    
185            @SuppressWarnings( "unchecked" )
186            List<Parameter> parameters = mojoDescriptor.getParameters();
187    
188            assertEquals( 1, parameters.size() );
189    
190            Parameter parameter = parameters.get( 0 );
191    
192            assertEquals( "Implementation parameter", "source2.sub.MyBla", parameter.getImplementation() );
193        }
194    
195        public void testMaven30Parameters()
196            throws Exception
197        {
198            List<MojoDescriptor> results = extract( "source2" );
199    
200            assertEquals( 1, results.size() );
201    
202            ExtendedMojoDescriptor mojoDescriptor = (ExtendedMojoDescriptor) results.get( 0 );
203            assertTrue( mojoDescriptor.isThreadSafe() );
204            assertEquals( "test", mojoDescriptor.getDependencyCollectionRequired() );
205        }
206    
207        /**
208         * Check that the mojo descriptor extractor will ignore any annotations that are found.
209         * 
210         * @throws Exception
211         */
212        public void testAnnotationInPlugin()
213            throws Exception
214        {
215            List<MojoDescriptor> results = extract( "source3" );
216    
217            assertNull( results );
218        }
219        
220        /**
221         * Check that the mojo descriptor extractor will successfully parse sources with Java 1.5 language features like
222         * generics.
223         */
224        public void testJava15SyntaxParsing()
225            throws Exception
226        {
227            List<MojoDescriptor> results = extract( "java-1.5" );
228    
229            assertEquals( 1, results.size() );
230        }
231    
232    }