KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > tools > plugin > generator > PluginDescriptorGeneratorTest


1 package org.apache.maven.tools.plugin.generator;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.plugin.descriptor.MojoDescriptor;
20 import org.apache.maven.plugin.descriptor.Parameter;
21 import org.apache.maven.plugin.descriptor.PluginDescriptor;
22 import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
23 import org.codehaus.plexus.component.repository.ComponentDependency;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileReader JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.io.StringReader JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.util.List JavaDoc;
33
34 /**
35  * @author <a HREF="mailto:jason@maven.org">Jason van Zyl </a>
36  * @version $Id: PluginDescriptorGeneratorTest.java,v 1.1.1.1 2004/08/09
37  * 18:43:10 jvanzyl Exp $
38  */

39 public class PluginDescriptorGeneratorTest
40     extends AbstractGeneratorTestCase
41 {
42     protected void validate(File JavaDoc destinationDirectory)
43         throws Exception JavaDoc
44     {
45         PluginDescriptorBuilder pdb = new PluginDescriptorBuilder();
46
47         File JavaDoc pluginDescriptorFile = new File JavaDoc( destinationDirectory, "plugin.xml" );
48
49         String JavaDoc pd = readFile( pluginDescriptorFile );
50
51         PluginDescriptor pluginDescriptor = pdb.build( new StringReader JavaDoc( pd ) );
52
53         assertEquals( 1, pluginDescriptor.getMojos().size() );
54         
55         MojoDescriptor mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 0 );
56
57         checkMojo( mojoDescriptor );
58
59         // ----------------------------------------------------------------------
60
// Dependencies
61
// ----------------------------------------------------------------------
62

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