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  
28  import java.io.BufferedReader;
29  import java.io.File;
30  import java.io.FileReader;
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          List dependencies = pluginDescriptor.getDependencies();
67  
68          checkDependency( "testGroup", "testArtifact", "0.0.0", (ComponentDependency) dependencies.get( 0 ) );
69  
70          assertEquals( 1, dependencies.size() );
71  
72          ComponentDependency dependency = (ComponentDependency) dependencies.get( 0 );
73          assertEquals( "testGroup", dependency.getGroupId() );
74          assertEquals( "testArtifact", dependency.getArtifactId() );
75          assertEquals( "0.0.0", dependency.getVersion() );
76      }
77  
78      private String readFile( File pluginDescriptorFile )
79          throws IOException
80      {
81          StringWriter sWriter = new StringWriter();
82          PrintWriter pWriter = new PrintWriter( sWriter );
83  
84          BufferedReader reader = new BufferedReader( new FileReader( pluginDescriptorFile ) );
85  
86          String line = null;
87          while ( ( line = reader.readLine() ) != null )
88          {
89              pWriter.println( line );
90          }
91  
92          reader.close();
93  
94          return sWriter.toString();
95      }
96  
97      private void checkMojo( MojoDescriptor mojoDescriptor )
98      {
99          assertEquals( "test:testGoal", mojoDescriptor.getFullGoalName() );
100 
101         assertEquals( "org.apache.maven.tools.plugin.generator.TestMojo", mojoDescriptor.getImplementation() );
102 
103         // The following should be defaults
104         assertEquals( "per-lookup", mojoDescriptor.getInstantiationStrategy() );
105 
106         assertNotNull( mojoDescriptor.isDependencyResolutionRequired() );
107 
108         // check the parameter.
109         checkParameter( (Parameter) mojoDescriptor.getParameters().get( 0 ) );
110     }
111 
112     private void checkParameter( Parameter parameter )
113     {
114         assertEquals( "dir", parameter.getName() );
115         assertEquals( String.class.getName(), parameter.getType() );
116         assertTrue( parameter.isRequired() );
117     }
118 
119     private void checkDependency( String groupId, String artifactId, String version, ComponentDependency dependency )
120     {
121         assertNotNull( dependency );
122 
123         assertEquals( groupId, dependency.getGroupId() );
124 
125         assertEquals( artifactId, dependency.getArtifactId() );
126 
127         assertEquals( version, dependency.getVersion() );
128     }
129 }