1   package org.apache.maven.plugin.reactor;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.Arrays;
6   import java.util.List;
7   
8   import junit.framework.TestCase;
9   
10  import org.apache.maven.model.Dependency;
11  import org.apache.maven.plugin.MojoExecutionException;
12  import org.apache.maven.plugin.MojoFailureException;
13  import org.apache.maven.plugin.logging.Log;
14  import org.apache.maven.project.MavenProject;
15  import org.apache.maven.scm.ScmException;
16  import org.apache.maven.scm.ScmFile;
17  import org.apache.maven.scm.ScmFileSet;
18  import org.apache.maven.scm.ScmFileStatus;
19  import org.apache.maven.scm.ScmResult;
20  import org.apache.maven.scm.command.status.StatusScmResult;
21  import org.apache.maven.scm.repository.ScmRepository;
22  import org.apache.maven.shared.invoker.Invoker;
23  import org.codehaus.plexus.util.FileUtils;
24  import org.codehaus.plexus.util.StringUtils;
25  
26  
27  public class MakeMojoTest extends TestCase
28  {
29      MavenProject dataAccess = new MavenProject();
30      MavenProject businessLogic = new MavenProject();
31      MavenProject ui = new MavenProject();
32      List configuredProjects;
33      String ps = File.separator;
34      File baseDir;
35      
36      public void setUp() throws Exception {
37          File tempDir = new File(System.getProperty( "java.io.tmpdir" ));
38          baseDir = File.createTempFile( "makeMojoTest", "", tempDir );
39          baseDir.delete();
40          configureProject( dataAccess, "dataAccess", "reactortest", "1.0" );
41          configureProject( businessLogic, "businessLogic", "reactortest", "1.0" );
42          configureProject( ui, "ui", "reactortest", "1.0" );
43          
44          configuredProjects = Arrays.asList( new MavenProject[] { dataAccess, businessLogic, ui} );
45          
46          // ui depends on businessLogic
47          // businessLogic depends on dataAccess
48          createDependency(businessLogic, ui);
49          createDependency(dataAccess, businessLogic);
50      }
51      
52      public void tearDown() throws Exception
53      {
54          FileUtils.deleteDirectory( baseDir );
55      }
56  
57      void configureProject(MavenProject p, String artifactId, String groupId, String version) throws IOException
58      {
59          p.setArtifactId( artifactId );
60          p.setGroupId( groupId );
61          p.setVersion( version );
62          File file = new File(baseDir, artifactId+"/pom.xml");
63          file.getParentFile().mkdirs();
64          file.createNewFile();
65          p.setFile( file.getAbsoluteFile() );
66      }
67      
68      void createDependency(MavenProject provider, MavenProject consumer)
69      {
70          Dependency d = new Dependency();
71          d.setArtifactId( provider.getArtifactId() );
72          d.setGroupId( provider.getGroupId() );
73          d.setVersion( provider.getVersion() );
74          consumer.getDependencies().add( d );
75      }
76      
77      public void testMake() throws MojoExecutionException, MojoFailureException {
78          MakeMojo m = new MakeMojo();
79          m.collectedProjects = configuredProjects;
80          m.artifactList = "reactortest:businessLogic";
81          m.baseDir = baseDir;
82          m.goals = "install";
83          FakeInvoker fi = new FakeInvoker();
84          m.simpleInvoker = fi;
85          m.execute();
86          assertEquals("dataAccess/pom.xml,businessLogic/pom.xml", fi.getIncludes());
87      }
88      
89      public void testMakeDependents() throws MojoExecutionException, MojoFailureException {
90          MakeDependentsMojo m = new MakeDependentsMojo();
91          m.collectedProjects = configuredProjects;
92          m.artifactList = "reactortest:businessLogic";
93          m.baseDir = baseDir;
94          m.goals = "install";
95          FakeInvoker fi = new FakeInvoker();
96          m.simpleInvoker = fi;
97          m.execute();
98          assertEquals("businessLogic/pom.xml,ui/pom.xml", fi.getIncludes());
99      }
100     
101     public void testMakeScmChanges() throws Exception {
102         MakeScmChanges m = new MakeScmChanges();
103         m.collectedProjects = configuredProjects;
104         m.baseDir = baseDir;
105         m.goals = "install";
106         FakeInvoker fi = new FakeInvoker();
107         m.simpleInvoker = fi;
108         
109         ScmFile sf = new ScmFile(businessLogic.getFile().getAbsolutePath(), ScmFileStatus.MODIFIED);
110         m.scmManager = new FakeScmManager(Arrays.asList(new ScmFile[] {sf} ));
111         m.scmConnection = "";
112         
113         m.execute();
114         assertEquals("businessLogic/pom.xml,ui/pom.xml", fi.getIncludes());
115     }
116     
117     public void testMakeResume() throws Exception {
118         MakeMojo m = new MakeMojo();
119         m.collectedProjects = configuredProjects;
120         m.artifactList = "reactortest:ui";
121         m.continueFromFolder = new File(baseDir, "businessLogic");
122         m.baseDir = baseDir;
123         m.goals = "install";
124         FakeInvoker fi = new FakeInvoker();
125         m.simpleInvoker = fi;
126         m.execute();
127         assertEquals("businessLogic/pom.xml,ui/pom.xml", fi.getIncludes());
128     }
129     
130     class FakeInvoker extends SimpleInvoker {
131         String[] reactorIncludes;
132         List goalList;
133         
134         void runReactor( String[] reactorIncludes, List goalList, Invoker invoker, boolean printOnly, Log log )
135             throws InvokerExecutionException
136         {
137             this.reactorIncludes = reactorIncludes;
138             this.goalList = goalList;
139         }
140         
141         String getIncludes() {
142             String includes = StringUtils.join( reactorIncludes, "," );
143             includes = includes.replaceAll( "\\\\", "/" );
144             return includes;
145         }
146     }
147     
148     class FakeScmManager extends NoopScmManager {
149         List changedFiles;
150         public FakeScmManager(List changedFiles)
151         {
152             this.changedFiles = changedFiles;
153         }
154         
155         public StatusScmResult status( ScmRepository repository, ScmFileSet fileSet )
156             throws ScmException
157         {
158             return new StatusScmResult(changedFiles, new ScmResult(null, null, null, true));
159         }
160     }
161     
162 }