View Javadoc
1   package org.apache.maven.tools.plugin.extractor.annotations;
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 org.apache.maven.plugin.AbstractMojo;
23  import org.apache.maven.plugins.annotations.Execute;
24  import org.apache.maven.plugins.annotations.LifecyclePhase;
25  import org.apache.maven.plugins.annotations.Mojo;
26  import org.apache.maven.project.MavenProject;
27  import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ComponentAnnotationContent;
28  import org.apache.maven.tools.plugin.extractor.annotations.datamodel.ParameterAnnotationContent;
29  import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotatedClass;
30  import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScanner;
31  import org.apache.maven.tools.plugin.extractor.annotations.scanner.MojoAnnotationsScannerRequest;
32  import org.codehaus.plexus.PlexusTestCase;
33  import org.fest.assertions.Assertions;
34  
35  import java.io.File;
36  import java.util.Arrays;
37  import java.util.Collection;
38  import java.util.Collections;
39  import java.util.Map;
40  
41  /**
42   * @author Olivier Lamy
43   */
44  public class TestAnnotationsReader
45      extends PlexusTestCase
46  {
47      public void testReadMojoClass()
48          throws Exception
49      {
50          MojoAnnotationsScanner mojoAnnotationsScanner = (MojoAnnotationsScanner) lookup( MojoAnnotationsScanner.ROLE );
51  
52          MojoAnnotationsScannerRequest request = new MojoAnnotationsScannerRequest();
53          request.setClassesDirectories( Collections.singletonList( new File( getBasedir(), "target/test-classes" ) ) );
54          request.setIncludePatterns( Arrays.asList( "**/FooMojo.class" ) );
55          request.setProject( new MavenProject() );
56  
57          Map<String, MojoAnnotatedClass> mojoAnnotatedClasses = mojoAnnotationsScanner.scan( request );
58  
59          System.out.println( "mojoAnnotatedClasses:" + mojoAnnotatedClasses );
60  
61          Assertions.assertThat( mojoAnnotatedClasses ).isNotNull().isNotEmpty().hasSize( 1 );
62  
63          MojoAnnotatedClass mojoAnnotatedClass = mojoAnnotatedClasses.values().iterator().next();
64  
65          assertEquals( FooMojo.class.getName(), mojoAnnotatedClass.getClassName() );
66          assertEquals( AbstractMojo.class.getName(), mojoAnnotatedClass.getParentClassName() );
67  
68          Mojo mojo = mojoAnnotatedClass.getMojo();
69  
70          assertEquals( "foo", mojo.name() );
71          assertEquals( true, mojo.threadSafe() );
72          assertEquals( false, mojo.aggregator() );
73          assertEquals( LifecyclePhase.COMPILE, mojo.defaultPhase() );
74  
75          Execute execute = mojoAnnotatedClass.getExecute();
76  
77          assertEquals( "compiler", execute.goal() );
78          assertEquals( "my-lifecycle", execute.lifecycle() );
79          assertEquals( LifecyclePhase.PACKAGE, execute.phase() );
80  
81          Collection<ComponentAnnotationContent> components = mojoAnnotatedClass.getComponents().values();
82          Assertions.assertThat( components ).isNotNull().isNotEmpty().hasSize( 2 );
83  
84          Collection<ParameterAnnotationContent> parameters = mojoAnnotatedClass.getParameters().values();
85          Assertions.assertThat( parameters ).isNotNull().isNotEmpty().hasSize( 2 ).contains(
86              new ParameterAnnotationContent( "bar", null, "thebar", "coolbar", true, false, String.class.getName() ),
87              new ParameterAnnotationContent( "beer", null, "thebeer", "coolbeer", false, false,
88                                              String.class.getName() ) );
89      }
90  }