1   package org.apache.maven.plugin.antrun;
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 java.io.File;
23  import java.io.OutputStream;
24  import java.io.PrintStream;
25  import java.util.Arrays;
26  import java.util.Properties;
27  
28  import org.apache.maven.cli.ConsoleDownloadMonitor;
29  import org.apache.maven.embedder.MavenEmbedder;
30  import org.apache.maven.embedder.MavenEmbedderConsoleLogger;
31  import org.apache.maven.embedder.PlexusLoggerAdapter;
32  import org.apache.maven.monitor.event.DefaultEventMonitor;
33  import org.apache.maven.monitor.event.EventMonitor;
34  import org.apache.maven.project.MavenProject;
35  import org.codehaus.plexus.PlexusTestCase;
36  import org.codehaus.plexus.util.StringOutputStream;
37  
38  /**
39   * Class to test AntRun plugin
40   *
41   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
42   * @version $Id$
43   */
44  public class AntRunMojoTest
45      extends PlexusTestCase
46  {
47      /**
48       * @see junit.framework.TestCase#setUp()
49       */
50      protected void setUp()
51          throws Exception
52      {
53          // required for mojo lookups to work
54          super.setUp();
55      }
56  
57      /**
58       * @see junit.framework.TestCase#tearDown()
59       */
60      protected void tearDown()
61          throws Exception
62      {
63          // nop
64      }
65  
66      /**
67       * Method to test Default Antrun generation
68       *
69       * @throws Exception
70       */
71      public void testDefaultProject()
72          throws Exception
73      {
74          String result = invokeMaven( "antrun-default-test", new Properties() );
75          assertTrue( result.indexOf( "[echo] Hello World!" ) != -1 );
76      }
77  
78      /**
79       * Method to test tasks attributes
80       *
81       * @throws Exception
82       */
83      public void testTasksAttributesProject()
84          throws Exception
85      {
86          Properties properties = new Properties();
87  
88          String result = invokeMaven( "tasksattributes-test", properties );
89          assertTrue( result.indexOf( "[echo] To skip me" ) != -1 );
90  
91          properties.put( "maven.test.skip", "true" );
92          result = invokeMaven( "tasksattributes-test", properties );
93          assertTrue( result.indexOf( "[echo] To skip me" ) == -1 );
94      }
95  
96      /**
97       * Invoke Maven for a given test project name
98       * <br/>
99       * The Maven test project should be in a directory called <code>testProject</code> in
100      * "src/test/resources/unit/" directory.
101      * The Maven test project should be called <code>"testProject"-plugin-config.xml</code>.
102      *
103      * @param testProject
104      * @param properties
105      * @return the output of MavenEmbedder
106      * @throws Exception
107      */
108     private String invokeMaven( String testProject, Properties properties )
109         throws Exception
110     {
111         MavenEmbedder maven = new MavenEmbedder();
112         maven.setClassLoader( Thread.currentThread().getContextClassLoader() );
113         maven.setLogger( new MavenEmbedderConsoleLogger() );
114         maven.setLocalRepositoryDirectory( getTestFile( "target/local-repo" ) );
115         maven.setOffline( true );
116         maven.start();
117 
118         EventMonitor eventMonitor = new DefaultEventMonitor( new PlexusLoggerAdapter( new MavenEmbedderConsoleLogger() ) );
119 
120         File testPom = new File( getBasedir(), "src/test/resources/unit/" + testProject + "/" + testProject
121             + "-plugin-config.xml" );
122         MavenProject project = maven.readProjectWithDependencies( testPom );
123 
124         PrintStream oldOut = System.out;
125         OutputStream outOS = new StringOutputStream();
126         PrintStream out = new PrintStream( outOS );
127         System.setOut( out );
128 
129         try
130         {
131             maven.execute( project,
132                            Arrays.asList( new String[] { "org.apache.maven.plugins:maven-antrun-plugin:run" } ),
133                            eventMonitor, new ConsoleDownloadMonitor(), properties, new File( PlexusTestCase
134                                .getBasedir(), "/target/test/unit/" + testProject + "/" ) );
135 
136             return outOS.toString();
137         }
138         finally
139         {
140             System.setOut( oldOut );
141         }
142     }
143 }