1   package org.apache.maven.plugin.invoker;
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.util.ArrayList;
24  import java.util.Arrays;
25  import java.util.Collections;
26  import java.util.List;
27  
28  import org.apache.maven.plugin.testing.AbstractMojoTestCase;
29  import org.apache.maven.plugin.testing.stubs.MavenProjectStub;
30  import org.apache.maven.settings.Settings;
31  import org.apache.maven.shared.invoker.Invoker;
32  import org.codehaus.plexus.util.FileUtils;
33  
34  
35  /**
36   * @author <a href="mailto:olamy@apache.org">olamy</a>
37   * @since 18 nov. 07
38   * @version $Id: InvokerMojoTest.java 681098 2008-07-30 15:58:46Z bentmann $
39   */
40  public class InvokerMojoTest
41      extends AbstractMojoTestCase
42  {
43  
44      /**
45       * test reading goals from a file
46       */
47      public void testReadGoalsFromFile()
48          throws Exception
49      {
50          InvokerMojo invokerMojo = new InvokerMojo();
51          setVariableValueToObject( invokerMojo, "goalsFile", "goals.txt" );
52          String dirPath = getBasedir() + "/src/test/resources/unit/goals-from-file/";
53          List goals = invokerMojo.getGoals( new File( dirPath ) );
54          assertEquals( 3, goals.size() );
55      }
56  
57      public void testSimpleRunValidate()
58          throws Exception
59      {
60          InvokerMojo invokerMojo = new InvokerMojo();
61          setVariableValueToObject( invokerMojo, "goalsFile", "validate-goal.txt" );
62          String dirPath = getBasedir() + "/src/test/resources/unit/goals-from-file/";
63          List goals = invokerMojo.getGoals( new File( dirPath ) );
64          assertEquals( 1, goals.size() );
65          setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
66          List pomIncludes = new ArrayList();
67          pomIncludes.add( "pom.xml" );
68          setVariableValueToObject( invokerMojo, "pomIncludes", pomIncludes );
69          setVariableValueToObject( invokerMojo, "invoker", getContainer().lookup( Invoker.ROLE ) );
70          File cloneProjectsTo = new File( getBasedir(), "target/unit/goals-from-file/" );
71          // clean if exists
72          if ( cloneProjectsTo.exists() )
73          {
74              FileUtils.deleteDirectory( cloneProjectsTo );
75          }
76          //cloneProjectsTo.getParent()
77          MavenProjectStub project = new MavenProjectStub();
78          project.setTestClasspathElements( Collections.EMPTY_LIST );
79          setVariableValueToObject( invokerMojo, "project", project );
80          setVariableValueToObject( invokerMojo, "cloneProjectsTo", cloneProjectsTo );
81          setVariableValueToObject( invokerMojo, "postBuildHookScript", "verify.bsh" );
82          setVariableValueToObject( invokerMojo, "settings", new Settings() );
83          invokerMojo.execute();
84      }
85  
86      public void testSingleInvokerTest()
87          throws Exception
88      {
89          InvokerMojo invokerMojo = new InvokerMojo();
90          setVariableValueToObject( invokerMojo, "goalsFile", "validate-goal.txt" );
91          String dirPath = getBasedir() + "/src/test/resources/unit";
92          List goals = invokerMojo.getGoals( new File( dirPath ) );
93          assertEquals( 1, goals.size() );
94          setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
95          setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*" );
96          String[] poms = invokerMojo.getPoms();
97          System.out.println( Arrays.asList( poms ) );
98          assertEquals( 1, poms.length );
99      }
100 
101     public void testMultiInvokerTest()
102         throws Exception
103     {
104         InvokerMojo invokerMojo = new InvokerMojo();
105         setVariableValueToObject( invokerMojo, "goalsFile", "validate-goal.txt" );
106         String dirPath = getBasedir() + "/src/test/resources/unit";
107         List goals = invokerMojo.getGoals( new File( dirPath ) );
108         assertEquals( 1, goals.size() );
109         setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
110         setVariableValueToObject( invokerMojo, "invokerTest", "*dummy*,*terpolatio*" );
111         String[] poms = invokerMojo.getPoms();
112         System.out.println( Arrays.asList( poms ) );
113         assertEquals( 2, poms.length );
114     }
115 
116     public void testFullPatternInvokerTest()
117         throws Exception
118     {
119         InvokerMojo invokerMojo = new InvokerMojo();
120         setVariableValueToObject( invokerMojo, "goalsFile", "validate-goal.txt" );
121         String dirPath = getBasedir() + "/src/test/resources/unit";
122         List goals = invokerMojo.getGoals( new File( dirPath ) );
123         assertEquals( 1, goals.size() );
124         setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
125         setVariableValueToObject( invokerMojo, "invokerTest", "*" );
126         String[] poms = invokerMojo.getPoms();
127         System.out.println( Arrays.asList( poms ) );
128         assertEquals( 4, poms.length );
129     }
130 
131     public void testAlreadyCloned()
132         throws Exception
133     {
134         assertFalse( InvokerMojo.alreadyCloned( "dir", Collections.EMPTY_LIST ) );
135         assertTrue( InvokerMojo.alreadyCloned( "dir", Collections.singletonList( "dir" ) ) );
136         assertTrue( InvokerMojo.alreadyCloned( "dir" + File.separator + "sub", Collections.singletonList( "dir" ) ) );
137         assertFalse( InvokerMojo.alreadyCloned( "dirs", Collections.singletonList( "dir" ) ) );
138     }    
139 
140     public void testProjectCloning()
141         throws Exception
142     {
143         String dirPath = getBasedir() + "/src/test/resources/unit/nested-projects";
144 
145         File cloneProjectsTo = new File( getBasedir(), "target/unit/nested-projects" );
146         if ( cloneProjectsTo.exists() )
147         {
148             FileUtils.deleteDirectory( cloneProjectsTo );
149         }
150 
151         MavenProjectStub project = new MavenProjectStub();
152         project.setTestClasspathElements( Collections.EMPTY_LIST );
153 
154         InvokerMojo invokerMojo = new InvokerMojo();
155         setVariableValueToObject( invokerMojo, "goals", Collections.singletonList( "validate" ) );
156         setVariableValueToObject( invokerMojo, "projectsDirectory", new File( dirPath ) );
157         setVariableValueToObject( invokerMojo, "pomIncludes", Collections.singletonList( "**/pom.xml" ) );
158         setVariableValueToObject( invokerMojo, "pomExcludes", Collections.singletonList( "pom.xml" ) );
159         setVariableValueToObject( invokerMojo, "cloneProjectsTo", cloneProjectsTo );
160         setVariableValueToObject( invokerMojo, "project", project );
161         setVariableValueToObject( invokerMojo, "settings", new Settings() );
162         setVariableValueToObject( invokerMojo, "invoker", getContainer().lookup( Invoker.ROLE ) );
163 
164         invokerMojo.execute();
165 
166         // NOTE: It is part of the test design that "module" is a prefix of "module-1"
167         assertTrue( new File( cloneProjectsTo, "module" ).isDirectory() );
168         assertTrue( new File( cloneProjectsTo, "module-1" ).isDirectory() );
169         assertTrue( new File( cloneProjectsTo, "module-1/sub-module" ).isDirectory() );
170     }
171 
172 }