1 package com.soebes.maven.extensions.incremental;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
121
122
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 }