001    package org.apache.maven.tools.plugin.annotations;
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 org.apache.maven.plugin.AbstractMojo;
023    import org.apache.maven.plugins.annotations.Execute;
024    import org.apache.maven.plugins.annotations.LifecyclePhase;
025    import org.apache.maven.plugins.annotations.Mojo;
026    import org.apache.maven.project.MavenProject;
027    import org.apache.maven.tools.plugin.annotations.datamodel.ComponentAnnotationContent;
028    import org.apache.maven.tools.plugin.annotations.datamodel.ParameterAnnotationContent;
029    import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotatedClass;
030    import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScanner;
031    import org.apache.maven.tools.plugin.annotations.scanner.MojoAnnotationsScannerRequest;
032    import org.codehaus.plexus.PlexusTestCase;
033    import org.fest.assertions.Assertions;
034    
035    import java.io.File;
036    import java.util.Arrays;
037    import java.util.Collection;
038    import java.util.Collections;
039    import java.util.Map;
040    
041    /**
042     * @author Olivier Lamy
043     */
044    public class TestAnnotationsReader
045        extends PlexusTestCase
046    {
047        public void testReadMojoClass()
048            throws Exception
049        {
050            MojoAnnotationsScanner mojoAnnotationsScanner = (MojoAnnotationsScanner) lookup( MojoAnnotationsScanner.ROLE );
051    
052            MojoAnnotationsScannerRequest request = new MojoAnnotationsScannerRequest();
053            request.setClassesDirectories( Collections.singletonList( new File( "target/test-classes" ) ) );
054            request.setIncludePatterns( Arrays.asList( "**/FooMojo.class" ) );
055            request.setProject( new MavenProject() );
056    
057            Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = mojoAnnotationsScanner.scan( request );
058    
059            System.out.println( "mojoAnnotatedClasses:" + mojoAnnotatedClasses );
060    
061            Assertions.assertThat( mojoAnnotatedClasses ).isNotNull().isNotEmpty().hasSize( 1 );
062    
063            MojoAnnotatedClass mojoAnnotatedClass = mojoAnnotatedClasses.values().iterator().next();
064    
065            assertEquals( FooMojo.class.getName(), mojoAnnotatedClass.getClassName() );
066            assertEquals( AbstractMojo.class.getName(), mojoAnnotatedClass.getParentClassName() );
067    
068            Mojo mojo = mojoAnnotatedClass.getMojo();
069    
070            assertEquals( "foo", mojo.name() );
071            assertEquals( true, mojo.threadSafe() );
072            assertEquals( false, mojo.aggregator() );
073            assertEquals( LifecyclePhase.COMPILE, mojo.defaultPhase() );
074    
075            Execute execute = mojoAnnotatedClass.getExecute();
076    
077            assertEquals( "compiler", execute.goal() );
078            assertEquals( "my-lifecycle", execute.lifecycle() );
079            assertEquals( LifecyclePhase.PACKAGE, execute.phase() );
080    
081            Collection<ComponentAnnotationContent> components = mojoAnnotatedClass.getComponents().values();
082            Assertions.assertThat( components ).isNotNull().isNotEmpty().hasSize( 2 );
083    
084            Collection<ParameterAnnotationContent> parameters = mojoAnnotatedClass.getParameters().values();
085            Assertions.assertThat( parameters ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
086                new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, false, String.class.getName() ),
087                new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, false,
088                                                String.class.getName() ) );
089        }
090    }