1   package org.apache.maven.script.ant;
2   
3   import org.apache.maven.artifact.Artifact;
4   import org.apache.maven.execution.MavenSession;
5   import org.apache.maven.model.Build;
6   import org.apache.maven.model.Model;
7   import org.apache.maven.plugin.MojoExecution;
8   import org.apache.maven.plugin.MojoExecutionException;
9   import org.apache.maven.plugin.descriptor.MojoDescriptor;
10  import org.apache.maven.plugin.descriptor.PluginDescriptor;
11  import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
12  import org.apache.maven.project.MavenProject;
13  import org.apache.maven.project.path.PathTranslator;
14  import org.apache.tools.ant.BuildEvent;
15  import org.apache.tools.ant.BuildListener;
16  import org.codehaus.plexus.archiver.ArchiverException;
17  import org.codehaus.plexus.archiver.jar.JarArchiver;
18  import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
19  import org.codehaus.plexus.component.factory.ComponentInstantiationException;
20  import org.codehaus.plexus.component.factory.ant.AntScriptInvoker;
21  import org.codehaus.plexus.component.repository.ComponentRequirement;
22  import org.codehaus.plexus.configuration.PlexusConfigurationException;
23  import org.codehaus.plexus.logging.Logger;
24  import org.codehaus.plexus.logging.console.ConsoleLogger;
25  import org.codehaus.plexus.util.IOUtil;
26  import org.codehaus.plexus.util.StringUtils;
27  import org.easymock.MockControl;
28  
29  import java.io.ByteArrayOutputStream;
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStreamReader;
33  import java.io.PrintStream;
34  import java.io.Reader;
35  import java.net.URL;
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  
43  import junit.framework.TestCase;
44  
45  public class AntMojoWrapperTest
46      extends TestCase
47  {
48  
49      public void test2xStylePlugin()
50          throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
51          ComponentConfigurationException, ArchiverException
52      {
53          String pluginXml = "META-INF/maven/plugin-2.1.xml";
54  
55          List messages = run( pluginXml, true );
56  
57          assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", false );
58          assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", false );
59          assertPresence( messages, "Maven standard project-based classpath references.", false );
60          assertPresence( messages, "Maven standard plugin-based classpath references.", false );
61          assertPresence( messages,
62                          "Maven project, session, mojo-execution, or path-translation parameter information is", false );
63          assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release",
64                          false );
65  
66          assertPresence( messages, "path-is-missing", false );
67      }
68  
69      public void test20StylePlugin()
70          throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
71          ComponentConfigurationException, ArchiverException
72      {
73          String pluginXml = "META-INF/maven/plugin-2.0.xml";
74  
75          List messages = run( pluginXml, false );
76  
77          assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", true );
78          assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", true );
79          assertPresence( messages, "Maven standard project-based classpath references.", true );
80          assertPresence( messages, "Maven standard plugin-based classpath references.", true );
81          assertPresence( messages,
82                          "Maven project, session, mojo-execution, or path-translation parameter information is", true );
83          assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release", true );
84  
85          assertPresence( messages, "path-is-missing", true );
86      }
87  
88      private void assertPresence( List messages, String test, boolean shouldBePresent )
89      {
90          boolean found = false;
91  
92          for ( Iterator it = messages.iterator(); it.hasNext(); )
93          {
94              String message = (String) it.next();
95              if ( message.indexOf( test ) > -1 )
96              {
97                  found = true;
98                  break;
99              }
100         }
101 
102         if ( !shouldBePresent && found )
103         {
104             fail( "Test string: '" + test + "' was found in output, but SHOULD NOT BE THERE." );
105         }
106         else if ( shouldBePresent && !found )
107         {
108             fail( "Test string: '" + test + "' was NOT found in output, but SHOULD BE THERE." );
109         }
110     }
111 
112     private List run( String pluginXml, boolean includeImplied )
113         throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
114         ComponentConfigurationException, ArchiverException
115     {
116         StackTraceElement stack = new Throwable().getStackTrace()[1];
117         System.out.println( "\n\nRunning: " + stack.getMethodName() + "\n\n" );
118 
119         URL resource = Thread.currentThread().getContextClassLoader().getResource( pluginXml );
120 
121         if ( resource == null )
122         {
123             fail( "plugin descriptor not found: '" + pluginXml + "'." );
124         }
125 
126         Reader reader = null;
127         PluginDescriptor pd;
128         try
129         {
130             reader = new InputStreamReader( resource.openStream() );
131             pd = new PluginDescriptorBuilder().build( reader, pluginXml );
132         }
133         finally
134         {
135             IOUtil.close( reader );
136         }
137 
138         Map config = new HashMap();
139         config.put( "basedir", new File( "." ).getAbsoluteFile() );
140         config.put( "messageLevel", "info" );
141 
142         MojoDescriptor md = pd.getMojo( "test" );
143 
144         AntMojoWrapper wrapper =
145             new AntMojoWrapper( new AntScriptInvoker( md, Thread.currentThread().getContextClassLoader() ) );
146 
147         wrapper.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
148 
149         MockControl artifactCtl = null;
150         MockControl pathTranslatorCtl = null;
151         if ( includeImplied )
152         {
153             File pluginXmlFile = new File( StringUtils.replace( resource.getPath(), "%20", " " ) );
154 
155             File jarFile = File.createTempFile( "AntMojoWrapperTest.", ".test.jar" );
156             jarFile.deleteOnExit();
157 
158             JarArchiver archiver = new JarArchiver();
159             archiver.enableLogging( new ConsoleLogger( Logger.LEVEL_ERROR, "archiver" ) );
160             archiver.setDestFile( jarFile );
161             archiver.addFile( pluginXmlFile, pluginXml );
162             archiver.createArchive();
163 
164             artifactCtl = MockControl.createControl( Artifact.class );
165             Artifact artifact = (Artifact) artifactCtl.getMock();
166 
167             artifact.getFile();
168             artifactCtl.setReturnValue( jarFile, MockControl.ZERO_OR_MORE );
169 
170             artifact.getGroupId();
171             artifactCtl.setReturnValue( "groupId", MockControl.ZERO_OR_MORE );
172 
173             artifact.getArtifactId();
174             artifactCtl.setReturnValue( "artifactId", MockControl.ZERO_OR_MORE );
175 
176             artifact.getVersion();
177             artifactCtl.setReturnValue( "1", MockControl.ZERO_OR_MORE );
178 
179             artifact.getId();
180             artifactCtl.setReturnValue( "groupId:artifactId:jar:1", MockControl.ZERO_OR_MORE );
181 
182             artifact.getClassifier();
183             artifactCtl.setReturnValue( null, MockControl.ZERO_OR_MORE );
184 
185             pathTranslatorCtl = MockControl.createControl( PathTranslator.class );
186             PathTranslator pt = (PathTranslator) pathTranslatorCtl.getMock();
187 
188             Model model = new Model();
189 
190             Build build = new Build();
191             build.setDirectory( "target" );
192 
193             model.setBuild( build );
194 
195             MavenProject project = new MavenProject( model );
196             project.setFile( new File( "pom.xml" ).getAbsoluteFile() );
197 
198             artifactCtl.replay();
199             pathTranslatorCtl.replay();
200 
201             pd.setPluginArtifact( artifact );
202             pd.setArtifacts( Collections.singletonList( artifact ) );
203 
204             config.put( "project", project );
205             config.put( "session", new MavenSession( null, null, null, null, null, null, null, null, null, null ) );
206             config.put( "mojoExecution", new MojoExecution( md ) );
207 
208             ComponentRequirement cr = new ComponentRequirement();
209             cr.setRole( PathTranslator.class.getName() );
210 
211             wrapper.addComponentRequirement( cr, pt );
212         }
213 
214         wrapper.setComponentConfiguration( config );
215 
216         TestBuildListener tbl = new TestBuildListener();
217         wrapper.getAntProject().addBuildListener( tbl );
218         
219         PrintStream oldOut = System.out;
220         
221         ByteArrayOutputStream baos = new ByteArrayOutputStream();
222         try
223         {
224             System.setOut( new PrintStream( baos ) );
225 
226             wrapper.execute();
227         }
228         finally
229         {
230             System.setOut( oldOut );
231         }
232 
233         System.out.println( "\n\n" + stack.getMethodName() + " executed; verifying...\n\n" );
234 
235         if ( includeImplied )
236         {
237             artifactCtl.verify();
238             pathTranslatorCtl.verify();
239         }
240 
241         List messages = new ArrayList();
242         if ( !tbl.messages.isEmpty() )
243         {
244             messages.addAll( tbl.messages );
245         }
246         
247         messages.add( new String( baos.toByteArray() ) );
248         
249         return messages;
250     }
251 
252     private static final class TestBuildListener
253         implements BuildListener
254     {
255         private List messages = new ArrayList();
256 
257         public void buildFinished( BuildEvent arg0 )
258         {
259         }
260 
261         public void buildStarted( BuildEvent arg0 )
262         {
263         }
264 
265         public void messageLogged( BuildEvent event )
266         {
267             messages.add( event.getMessage() );
268         }
269 
270         public void targetFinished( BuildEvent arg0 )
271         {
272         }
273 
274         public void targetStarted( BuildEvent arg0 )
275         {
276         }
277 
278         public void taskFinished( BuildEvent arg0 )
279         {
280         }
281 
282         public void taskStarted( BuildEvent arg0 )
283         {
284         }
285     };
286 
287 }