View Javadoc
1   package com.soebes.maven.extensions.incremental;
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 static org.assertj.core.api.Assertions.assertThat;
23  import static org.mockito.Mockito.mock;
24  import static org.mockito.Mockito.when;
25  
26  import java.io.File;
27  import java.nio.file.Path;
28  import java.util.ArrayList;
29  import java.util.Arrays;
30  import java.util.List;
31  
32  import org.apache.maven.project.MavenProject;
33  import org.apache.maven.scm.ScmFile;
34  import org.apache.maven.scm.ScmFileStatus;
35  import org.junit.Before;
36  import org.junit.Ignore;
37  import org.junit.Test;
38  
39  public class ModuleCalculatorTest {
40  
41      private File baseDir = new File("/usr/local/project/");
42      private List<MavenProject> projectList;
43  
44      private MavenProject parent = createProject("parent", new File(baseDir, "."));
45      private MavenProject assembly = createProject("assembly", new File(baseDir, "assembly"));
46      private MavenProject domain = createProject("domain", new File(baseDir, "domain"));
47      private MavenProject subdomain = createProject("subdomain", new File(baseDir, "domain/subdomain"));
48      
49      private ModuleCalculator moduleCalculator;
50      
51  
52      @Before
53      public void before() {
54  	projectList = new ArrayList<>();
55  	projectList.add(parent);
56  	projectList.add(assembly);
57  	projectList.add(domain);
58  	projectList.add(subdomain);
59      }
60  
61      private MavenProject createProject(String artifactId, File baseDir) {
62  	MavenProject mock = mock(MavenProject.class);
63  	when(mock.getGroupId()).thenReturn("c.s.e");
64  	when(mock.getArtifactId()).thenReturn(artifactId);
65  	when(mock.getVersion()).thenReturn("0.1.0-SNAPSHOT");
66  	when(mock.getBasedir()).thenReturn(baseDir);
67  	return mock;
68      }
69  
70      @Test
71      public void shouldResultInASingleModule() {
72  	Path root = baseDir.toPath();
73  	List<ScmFile> changeList = Arrays
74  		.asList(new ScmFile("domain/src/main/java/com/test.java", ScmFileStatus.MODIFIED));
75  	moduleCalculator = new ModuleCalculator(projectList, changeList);
76  	List<MavenProject> changedModules = moduleCalculator.calculateChangedModules(root);
77  
78  	assertThat(changedModules).hasSize(1).containsExactly(domain);
79      }
80  
81      @Test
82      public void shouldResultInTwoModules() {
83  	Path root = baseDir.toPath();
84  	List<ScmFile> changeList = Arrays.asList(
85  		new ScmFile("domain/src/main/java/com/test.java", ScmFileStatus.MODIFIED),
86  		new ScmFile("assembly/pom.xml", ScmFileStatus.MODIFIED));
87  	moduleCalculator = new ModuleCalculator(projectList, changeList);
88  	List<MavenProject> changedModules = moduleCalculator.calculateChangedModules(root);
89  
90  	assertThat(changedModules).hasSize(2).containsOnly(domain, assembly);
91      }
92  
93      @Test
94      public void shouldResultInTwoModulesTwoChangesInSingleModule() {
95  	Path root = baseDir.toPath();
96  	List<ScmFile> changeList = Arrays.asList(
97  		new ScmFile("domain/src/main/java/com/test.java", ScmFileStatus.MODIFIED),
98  		new ScmFile("domain/src/main/java/Anton.java", ScmFileStatus.MODIFIED),
99  		new ScmFile("assembly/pom.xml", ScmFileStatus.MODIFIED));
100 
101 	moduleCalculator = new ModuleCalculator(projectList, changeList);
102 	List<MavenProject> changedModules = moduleCalculator.calculateChangedModules(root);
103 
104 	assertThat(changedModules).hasSize(2).containsOnly(domain, assembly);
105     }
106 
107     @Test
108     public void shouldResultInTwoModulesDomainAndSubDomain() {
109 	Path root = baseDir.toPath();
110 	List<ScmFile> changeList = Arrays.asList(new ScmFile("domain/subdomain/pom.xml", ScmFileStatus.MODIFIED),
111 		new ScmFile("domain/pom.xml", ScmFileStatus.MODIFIED));
112 	moduleCalculator = new ModuleCalculator(projectList, changeList);
113 	List<MavenProject> changedModules = moduleCalculator.calculateChangedModules(root);
114 
115 	assertThat(changedModules).hasSize(2).containsOnly(domain, subdomain);
116     }
117 
118     @Ignore
119     public void shouldResultInThreeModules() {
120 	// TODO: Think about this test case. What
121 	// should be returned for the root module ?
122 	// If i call mvn -pl root ... it will not work?
123 	Path root = baseDir.toPath();
124 	List<ScmFile> changeList = Arrays.asList(
125 		new ScmFile("domain/subdomain/pom.xml", ScmFileStatus.MODIFIED),
126 		new ScmFile("domain/pom.xml", ScmFileStatus.MODIFIED), 
127 		new ScmFile("pom.xml", ScmFileStatus.MODIFIED));
128 	moduleCalculator = new ModuleCalculator(projectList, changeList);
129 	List<MavenProject> changedModules = moduleCalculator.calculateChangedModules(root);
130 
131 	assertThat(changedModules).hasSize(3).containsOnly(domain, subdomain, parent);
132     }
133 
134 }