001    package org.apache.maven.script.ant;
002    
003    import org.apache.maven.artifact.Artifact;
004    import org.apache.maven.execution.MavenSession;
005    import org.apache.maven.model.Build;
006    import org.apache.maven.model.Model;
007    import org.apache.maven.plugin.MojoExecution;
008    import org.apache.maven.plugin.MojoExecutionException;
009    import org.apache.maven.plugin.descriptor.MojoDescriptor;
010    import org.apache.maven.plugin.descriptor.PluginDescriptor;
011    import org.apache.maven.plugin.descriptor.PluginDescriptorBuilder;
012    import org.apache.maven.project.MavenProject;
013    import org.apache.maven.project.path.PathTranslator;
014    import org.apache.tools.ant.BuildEvent;
015    import org.apache.tools.ant.BuildListener;
016    import org.codehaus.plexus.archiver.ArchiverException;
017    import org.codehaus.plexus.archiver.jar.JarArchiver;
018    import org.codehaus.plexus.component.configurator.ComponentConfigurationException;
019    import org.codehaus.plexus.component.factory.ComponentInstantiationException;
020    import org.codehaus.plexus.component.factory.ant.AntScriptInvoker;
021    import org.codehaus.plexus.component.repository.ComponentRequirement;
022    import org.codehaus.plexus.configuration.PlexusConfigurationException;
023    import org.codehaus.plexus.logging.Logger;
024    import org.codehaus.plexus.logging.console.ConsoleLogger;
025    import org.codehaus.plexus.util.IOUtil;
026    import org.codehaus.plexus.util.StringUtils;
027    import org.easymock.MockControl;
028    
029    import java.io.ByteArrayOutputStream;
030    import java.io.File;
031    import java.io.IOException;
032    import java.io.InputStreamReader;
033    import java.io.PrintStream;
034    import java.io.Reader;
035    import java.net.URL;
036    import java.util.ArrayList;
037    import java.util.Collections;
038    import java.util.HashMap;
039    import java.util.List;
040    import java.util.Map;
041    
042    import junit.framework.TestCase;
043    
044    public class AntMojoWrapperTest
045        extends TestCase
046    {
047    
048        public void test2xStylePlugin()
049            throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
050            ComponentConfigurationException, ArchiverException
051        {
052            String pluginXml = "META-INF/maven/plugin-2.1.xml";
053    
054            List<String> messages = run( pluginXml, true );
055    
056            assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", false );
057            assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", false );
058            assertPresence( messages, "Maven standard project-based classpath references.", false );
059            assertPresence( messages, "Maven standard plugin-based classpath references.", false );
060            assertPresence( messages,
061                            "Maven project, session, mojo-execution, or path-translation parameter information is", false );
062            assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release",
063                            false );
064    
065            assertPresence( messages, "path-is-missing", false );
066        }
067    
068        public void test20StylePlugin()
069            throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
070            ComponentConfigurationException, ArchiverException
071        {
072            String pluginXml = "META-INF/maven/plugin-2.0.xml";
073    
074            List<String> messages = run( pluginXml, false );
075    
076            assertPresence( messages, "Unpacked Ant build scripts (in Maven build directory).", true );
077            assertPresence( messages, "Maven parameter expression evaluator for Ant properties.", true );
078            assertPresence( messages, "Maven standard project-based classpath references.", true );
079            assertPresence( messages, "Maven standard plugin-based classpath references.", true );
080            assertPresence( messages,
081                            "Maven project, session, mojo-execution, or path-translation parameter information is", true );
082            assertPresence( messages, "maven-script-ant < 2.1.0, or used maven-plugin-tools-ant < 2.2 during release", true );
083    
084            assertPresence( messages, "path-is-missing", true );
085        }
086    
087        private void assertPresence( List<String> messages, String test, boolean shouldBePresent )
088        {
089            for ( String message : messages )
090            {
091                if ( message.contains( test ) )
092                {
093                    if ( !shouldBePresent )
094                    {
095                        fail( "Test string: '" + test + "' was found in output, but SHOULD NOT BE THERE." );
096                    }
097                    return;
098                }
099            }
100    
101            if ( shouldBePresent )
102            {
103                fail( "Test string: '" + test + "' was NOT found in output, but SHOULD BE THERE." );
104            }
105        }
106    
107        private List<String> run( String pluginXml, boolean includeImplied )
108            throws PlexusConfigurationException, IOException, ComponentInstantiationException, MojoExecutionException,
109            ComponentConfigurationException, ArchiverException
110        {
111            StackTraceElement stack = new Throwable().getStackTrace()[1];
112            System.out.println( "\n\nRunning: " + stack.getMethodName() + "\n\n" );
113    
114            URL resource = Thread.currentThread().getContextClassLoader().getResource( pluginXml );
115    
116            if ( resource == null )
117            {
118                fail( "plugin descriptor not found: '" + pluginXml + "'." );
119            }
120    
121            Reader reader = null;
122            PluginDescriptor pd;
123            try
124            {
125                reader = new InputStreamReader( resource.openStream() );
126                pd = new PluginDescriptorBuilder().build( reader, pluginXml );
127            }
128            finally
129            {
130                IOUtil.close( reader );
131            }
132    
133            Map<String, Object> config = new HashMap<String, Object>();
134            config.put( "basedir", new File( "." ).getAbsoluteFile() );
135            config.put( "messageLevel", "info" );
136    
137            MojoDescriptor md = pd.getMojo( "test" );
138    
139            AntMojoWrapper wrapper =
140                new AntMojoWrapper( new AntScriptInvoker( md, Thread.currentThread().getContextClassLoader() ) );
141    
142            wrapper.enableLogging( new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
143    
144            MockControl artifactCtl = null;
145            MockControl pathTranslatorCtl = null;
146            if ( includeImplied )
147            {
148                File pluginXmlFile = new File( StringUtils.replace( resource.getPath(), "%20", " " ) );
149    
150                File jarFile = File.createTempFile( "AntMojoWrapperTest.", ".test.jar" );
151                jarFile.deleteOnExit();
152    
153                JarArchiver archiver = new JarArchiver();
154                archiver.enableLogging( new ConsoleLogger( Logger.LEVEL_ERROR, "archiver" ) );
155                archiver.setDestFile( jarFile );
156                archiver.addFile( pluginXmlFile, pluginXml );
157                archiver.createArchive();
158    
159                artifactCtl = MockControl.createControl( Artifact.class );
160                Artifact artifact = (Artifact) artifactCtl.getMock();
161    
162                artifact.getFile();
163                artifactCtl.setReturnValue( jarFile, MockControl.ZERO_OR_MORE );
164    
165                artifact.getGroupId();
166                artifactCtl.setReturnValue( "groupId", MockControl.ZERO_OR_MORE );
167    
168                artifact.getArtifactId();
169                artifactCtl.setReturnValue( "artifactId", MockControl.ZERO_OR_MORE );
170    
171                artifact.getVersion();
172                artifactCtl.setReturnValue( "1", MockControl.ZERO_OR_MORE );
173    
174                artifact.getId();
175                artifactCtl.setReturnValue( "groupId:artifactId:jar:1", MockControl.ZERO_OR_MORE );
176    
177                artifact.getClassifier();
178                artifactCtl.setReturnValue( null, MockControl.ZERO_OR_MORE );
179    
180                pathTranslatorCtl = MockControl.createControl( PathTranslator.class );
181                PathTranslator pt = (PathTranslator) pathTranslatorCtl.getMock();
182    
183                Model model = new Model();
184    
185                Build build = new Build();
186                build.setDirectory( "target" );
187    
188                model.setBuild( build );
189    
190                MavenProject project = new MavenProject( model );
191                project.setFile( new File( "pom.xml" ).getAbsoluteFile() );
192    
193                artifactCtl.replay();
194                pathTranslatorCtl.replay();
195    
196                pd.setPluginArtifact( artifact );
197                pd.setArtifacts( Collections.singletonList( artifact ) );
198    
199                config.put( "project", project );
200                config.put( "session", new MavenSession( null, null, null, null, null, null, null, null, null, null ) );
201                config.put( "mojoExecution", new MojoExecution( md ) );
202    
203                ComponentRequirement cr = new ComponentRequirement();
204                cr.setRole( PathTranslator.class.getName() );
205    
206                wrapper.addComponentRequirement( cr, pt );
207            }
208    
209            wrapper.setComponentConfiguration( config );
210    
211            TestBuildListener tbl = new TestBuildListener();
212            wrapper.getAntProject().addBuildListener( tbl );
213            
214            PrintStream oldOut = System.out;
215            
216            ByteArrayOutputStream baos = new ByteArrayOutputStream();
217            try
218            {
219                System.setOut( new PrintStream( baos ) );
220    
221                wrapper.execute();
222            }
223            finally
224            {
225                System.setOut( oldOut );
226            }
227    
228            System.out.println( "\n\n" + stack.getMethodName() + " executed; verifying...\n\n" );
229    
230            if ( includeImplied )
231            {
232                artifactCtl.verify();
233                pathTranslatorCtl.verify();
234            }
235    
236            List<String> messages = new ArrayList<String>();
237            if ( !tbl.messages.isEmpty() )
238            {
239                messages.addAll( tbl.messages );
240            }
241            
242            messages.add( new String( baos.toByteArray() ) );
243            
244            return messages;
245        }
246    
247        private static final class TestBuildListener
248            implements BuildListener
249        {
250            private List<String> messages = new ArrayList<String>();
251    
252            public void buildFinished( BuildEvent arg0 )
253            {
254            }
255    
256            public void buildStarted( BuildEvent arg0 )
257            {
258            }
259    
260            public void messageLogged( BuildEvent event )
261            {
262                messages.add( event.getMessage() );
263            }
264    
265            public void targetFinished( BuildEvent arg0 )
266            {
267            }
268    
269            public void targetStarted( BuildEvent arg0 )
270            {
271            }
272    
273            public void taskFinished( BuildEvent arg0 )
274            {
275            }
276    
277            public void taskStarted( BuildEvent arg0 )
278            {
279            }
280        };
281    
282    }