1 package org.apache.maven.tools.plugin.generator; 2 3 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 ; 26 import java.io.File ; 27 import java.io.FileReader ; 28 import java.io.IOException ; 29 import java.io.PrintWriter ; 30 import java.io.StringReader ; 31 import java.io.StringWriter ; 32 import java.util.List ; 33 34 39 public class PluginDescriptorGeneratorTest 40 extends AbstractGeneratorTestCase 41 { 42 protected void validate(File destinationDirectory) 43 throws Exception 44 { 45 PluginDescriptorBuilder pdb = new PluginDescriptorBuilder(); 46 47 File pluginDescriptorFile = new File ( destinationDirectory, "plugin.xml" ); 48 49 String pd = readFile( pluginDescriptorFile ); 50 51 PluginDescriptor pluginDescriptor = pdb.build( new StringReader ( pd ) ); 52 53 assertEquals( 1, pluginDescriptor.getMojos().size() ); 54 55 MojoDescriptor mojoDescriptor = (MojoDescriptor) pluginDescriptor.getMojos().get( 0 ); 56 57 checkMojo( mojoDescriptor ); 58 59 63 List 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 readFile( File pluginDescriptorFile ) 76 throws IOException 77 { 78 StringWriter sWriter = new StringWriter (); 79 PrintWriter pWriter = new PrintWriter ( sWriter ); 80 81 BufferedReader reader = new BufferedReader ( new FileReader ( pluginDescriptorFile ) ); 82 83 String 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 assertEquals( "per-lookup", mojoDescriptor.getInstantiationStrategy() ); 102 103 assertNotNull( mojoDescriptor.isDependencyResolutionRequired() ); 104 105 checkParameter( (Parameter) mojoDescriptor.getParameters().get( 0 ) ); 107 } 108 109 private void checkParameter( Parameter parameter ) 110 { 111 assertEquals( "dir", parameter.getName() ); 112 assertEquals( String .class.getName(), parameter.getType() ); 113 assertTrue( parameter.isRequired() ); 114 } 115 116 private void checkDependency( String groupId, String artifactId, String 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 |