View Javadoc
1   package org.apache.maven.tools.plugin.generator;
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.descriptor.MojoDescriptor;
23  import org.apache.maven.plugin.descriptor.Parameter;
24  import org.apache.maven.plugin.descriptor.PluginDescriptor;
25  import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
26  import org.codehaus.plexus.component.repository.ComponentDependency;
27  import org.codehaus.plexus.util.ReaderFactory;
28  
29  import java.io.BufferedReader;
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.PrintWriter;
33  import java.io.StringReader;
34  import java.io.StringWriter;
35  import java.util.List;
36  
37  /**
38   * @author <a href="mailto:jason@maven.org">Jason van Zyl </a>
39   * @version $Id: PluginDescriptorGeneratorTest.java,v 1.1.1.1 2004/08/09
40   *          18:43:10 jvanzyl Exp $
41   */
42  public class PluginDescriptorGeneratorTest
43      extends AbstractGeneratorTestCase
44  {
45      protected void validate( File destinationDirectory )
46          throws Exception
47      {
48          PluginDescriptorBuilder pdb = new PluginDescriptorBuilder();
49  
50          File pluginDescriptorFile = new File( destinationDirectory, "plugin.xml" );
51  
52          String pd = readFile( pluginDescriptorFile );
53  
54          PluginDescriptor pluginDescriptor = pdb.build( new StringReader( pd ) );
55  
56          assertEquals( 1, pluginDescriptor.getMojos().size() );
57  
58          MojoDescriptor mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 0 );
59  
60          checkMojo( mojoDescriptor );
61  
62          // ----------------------------------------------------------------------
63          // Dependencies
64          // ----------------------------------------------------------------------
65  
66          @SuppressWarnings( "unchecked" )
67          List<ComponentDependency> dependencies = pluginDescriptor.getDependencies();
68  
69          checkDependency( "testGroup", "testArtifact", "0.0.0", dependencies.get( 0 ) );
70  
71          assertEquals( 1, dependencies.size() );
72  
73          ComponentDependency dependency = (ComponentDependency) dependencies.get( 0 );
74          assertEquals( "testGroup", dependency.getGroupId() );
75          assertEquals( "testArtifact", dependency.getArtifactId() );
76          assertEquals( "0.0.0", dependency.getVersion() );
77      }
78  
79      private String readFile( File pluginDescriptorFile )
80          throws IOException
81      {
82          StringWriter sWriter = new StringWriter();
83          PrintWriter pWriter = new PrintWriter( sWriter );
84  
85          BufferedReader reader = new BufferedReader( ReaderFactory.newXmlReader( pluginDescriptorFile ) );
86  
87          String line = null;
88          while ( ( line = reader.readLine() ) != null )
89          {
90              pWriter.println( line );
91          }
92  
93          reader.close();
94  
95          return sWriter.toString();
96      }
97  
98      private void checkMojo( MojoDescriptor mojoDescriptor )
99      {
100         assertEquals( "test:testGoal", mojoDescriptor.getFullGoalName() );
101 
102         assertEquals( "org.apache.maven.tools.plugin.generator.TestMojo", mojoDescriptor.getImplementation() );
103 
104         // The following should be defaults
105         assertEquals( "per-lookup", mojoDescriptor.getInstantiationStrategy() );
106 
107         assertNotNull( mojoDescriptor.isDependencyResolutionRequired() );
108 
109         // check the parameter.
110         checkParameter( (Parameter) mojoDescriptor.getParameters().get( 0 ) );
111     }
112 
113     private void checkParameter( Parameter parameter )
114     {
115         assertEquals( "dir", parameter.getName() );
116         assertEquals( String.class.getName(), parameter.getType() );
117         assertTrue( parameter.isRequired() );
118         assertEquals( "some.alias", parameter.getAlias() );
119     }
120 
121     private void checkDependency( String groupId, String artifactId, String version, ComponentDependency dependency )
122     {
123         assertNotNull( dependency );
124 
125         assertEquals( groupId, dependency.getGroupId() );
126 
127         assertEquals( artifactId, dependency.getArtifactId() );
128 
129         assertEquals( version, dependency.getVersion() );
130     }
131 }