-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathFileSystemMavenSandbox.java
More file actions
180 lines (154 loc) · 5.73 KB
/
FileSystemMavenSandbox.java
File metadata and controls
180 lines (154 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* This file is part of git-commit-id-maven-plugin
* Originally invented by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
*
* git-commit-id-maven-plugin is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* git-commit-id-maven-plugin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with git-commit-id-maven-plugin. If not, see <http://www.gnu.org/licenses/>.
*/
package pl.project13.maven.git;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
import org.apache.maven.project.MavenProject;
import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;
/**
* Quick and dirty maven projects tree structure to create on disk during integration tests. Can
* have both parent and child projects set up. Copies sample git repository from prototype location
* to newly created project. Has ability to set target project for storing git repository.
*/
public class FileSystemMavenSandbox {
private MavenProject childProject;
private String rootSandboxPath;
private MavenProject parentProject;
private boolean keepSandboxWhenFinishedTest = false;
/**
* Sample git repository location to use as source in integration tests Test should copy content
* of this folder to ".git" in correct destination
*/
private File gitRepoSourceDir;
@Nullable private File gitRepoTargetDir;
public FileSystemMavenSandbox(String rootSandboxPath) {
this.rootSandboxPath = rootSandboxPath;
}
@NonNull
public FileSystemMavenSandbox withParentProject(String parentProjectDirName, String packaging) {
parentProject =
createProject(new File(rootSandboxPath + File.separator + parentProjectDirName), packaging);
return this;
}
@NonNull
public FileSystemMavenSandbox withNoChildProject() {
// no-op: marker for better tests readability
return this;
}
@NonNull
public FileSystemMavenSandbox withChildProject(String childProjectDirName, String packaging) {
childProject =
createProject(new File(parentProject.getBasedir(), childProjectDirName), packaging);
childProject.setParent(parentProject);
return this;
}
@NonNull
public FileSystemMavenSandbox withGitRepoInParent(@NonNull AvailableGitTestRepo repo) {
System.out.println("TEST: Will prepare sandbox repository based on: [" + repo.getDir() + "]");
gitRepoSourceDir = repo.getDir();
gitRepoTargetDir = parentProject.getBasedir();
return this;
}
@NonNull
public FileSystemMavenSandbox withGitRepoInChild(@NonNull AvailableGitTestRepo repo) {
gitRepoSourceDir = repo.getDir();
gitRepoTargetDir = childProject.getBasedir();
return this;
}
@NonNull
public FileSystemMavenSandbox withGitRepoAboveParent(@NonNull AvailableGitTestRepo repo) {
gitRepoSourceDir = repo.getDir();
gitRepoTargetDir = new File(rootSandboxPath);
return this;
}
@NonNull
public FileSystemMavenSandbox withNoGitRepoAvailable() {
gitRepoTargetDir = null;
return this;
}
@NonNull
public FileSystemMavenSandbox create() throws RuntimeException {
try {
createParentDir();
createChildDirIfRequired();
createGitRepoIfRequired();
return this;
} catch (Exception ex) {
throw new RuntimeException(
String.format("Failed creating %s...", getClass().getSimpleName()), ex);
}
}
private void createGitRepoIfRequired() throws IOException {
if (gitRepoTargetDir != null) {
File gitFolder = new File(gitRepoTargetDir, ".git");
FileUtils.copyDirectory(gitRepoSourceDir, gitFolder);
// As the WITH_NO_CHANGES and WITH_CHANGES git trees contain empty
// folders whose existence is crucial for the native git to run (jgit does not mind)
// *and* because empty folders are silently omitted from git checkins, ensure that
// these folders exist
Files.createDirectories(new File(gitFolder, "refs/heads").toPath());
Files.createDirectories(new File(gitFolder, "refs/tags").toPath());
}
}
private void createParentDir() throws IOException {
FileUtils.forceMkdir(parentProject.getBasedir());
}
private void createChildDirIfRequired() throws IOException {
if (childProject != null) {
FileUtils.forceMkdir(childProject.getBasedir());
}
}
public MavenProject getParentProject() {
return parentProject;
}
public MavenProject getChildProject() {
return childProject;
}
@NonNull
private MavenProject createProject(File basedir, String packaging) {
MavenProject project = new MavenProject();
project.setFile(new File(basedir + File.separator + "pom.xml"));
project.setPackaging(packaging);
return project;
}
@Override
public String toString() {
return "FileSystemMavenSandbox{"
+ "gitRepoTargetDir="
+ gitRepoTargetDir
+ ", gitRepoSourceDir="
+ gitRepoSourceDir
+ ", rootSandboxPath='"
+ rootSandboxPath
+ '\''
+ '}';
}
@NonNull
public FileSystemMavenSandbox withKeepSandboxWhenFinishedTest(
boolean keepSandboxWhenFinishedTest) {
// if we want to keep the generated sandbox for overwiew the content of it
this.keepSandboxWhenFinishedTest = keepSandboxWhenFinishedTest;
return this;
}
public boolean isKeepSandboxWhenFinishedTest() {
return keepSandboxWhenFinishedTest;
}
}