-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathNativeAndJGitProviderTest.java
More file actions
195 lines (174 loc) · 7.23 KB
/
NativeAndJGitProviderTest.java
File metadata and controls
195 lines (174 loc) · 7.23 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* 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 static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Properties;
import org.apache.maven.project.MavenProject;
import org.junit.Assert;
import org.junit.Test;
import pl.project13.core.jgit.DescribeResult;
/**
* Testcases to verify that the {@link DescribeResult} works properly.
*/
public class NativeAndJGitProviderTest extends GitIntegrationTest {
private static final String[] GIT_KEYS =
new String[] {
"git.build.time",
"git.build.host",
"git.branch",
"git.commit.id.full",
"git.commit.id.abbrev",
"git.commit.id.describe",
"git.build.user.name",
"git.build.user.email",
"git.commit.user.name",
"git.commit.user.email",
"git.commit.message.full",
"git.commit.message.short",
"git.commit.time",
"git.commit.author.time",
"git.commit.committer.time",
"git.total.commit.count",
"git.remote.origin.url",
"git.local.branch.ahead",
"git.local.branch.behind",
};
private static final String DEFAULT_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssXXX";
private static final String ISO8601_FORMAT_STRING = "yyyy-MM-dd'T'HH:mm:ssXXX";
@Test
public void testCompareBasic() throws Exception {
// Test on all available basic repos to ensure that the output is identical.
for (AvailableGitTestRepo testRepo : AvailableGitTestRepo.values()) {
if (testRepo != AvailableGitTestRepo.GIT_COMMIT_ID) {
mavenSandbox
.withParentProject("my-basic-project", "jar")
.withNoChildProject()
.withGitRepoInParent(testRepo)
.create();
MavenProject targetProject = mavenSandbox.getParentProject();
verifyNativeAndJGit(testRepo, targetProject, DEFAULT_FORMAT_STRING);
}
}
}
@Test
public void testCompareSubrepoInRoot() throws Exception {
for (AvailableGitTestRepo testRepo : AvailableGitTestRepo.values()) {
if (testRepo != AvailableGitTestRepo.GIT_COMMIT_ID) {
// Don't create a subrepo based on the plugin repo itself.
mavenSandbox
.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInParent(testRepo)
.create();
MavenProject targetProject = mavenSandbox.getParentProject();
verifyNativeAndJGit(testRepo, targetProject, DEFAULT_FORMAT_STRING);
}
}
}
@Test
public void testCompareSubrepoInChild() throws Exception {
for (AvailableGitTestRepo testRepo : AvailableGitTestRepo.values()) {
if (testRepo != AvailableGitTestRepo.GIT_COMMIT_ID) {
// Don't create a subrepo based on the plugin repo itself.
mavenSandbox
.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInParent(testRepo)
.create();
MavenProject targetProject = mavenSandbox.getChildProject();
verifyNativeAndJGit(testRepo, targetProject, DEFAULT_FORMAT_STRING);
}
}
}
@Test
public void testCompareIso8601Time() throws Exception {
// Test on all available basic repos to ensure that the output is identical.
for (AvailableGitTestRepo testRepo : AvailableGitTestRepo.values()) {
if (testRepo != AvailableGitTestRepo.GIT_COMMIT_ID) {
mavenSandbox
.withParentProject("my-basic-project", "jar")
.withNoChildProject()
.withGitRepoInParent(testRepo)
.create();
MavenProject targetProject = mavenSandbox.getParentProject();
verifyNativeAndJGit(testRepo, targetProject, ISO8601_FORMAT_STRING);
}
}
}
private void verifyNativeAndJGit(
AvailableGitTestRepo repo, MavenProject targetProject, String formatString) throws Exception {
setProjectToExecuteMojoIn(targetProject);
mojo.skipPoms = false;
mojo.dateFormat = formatString;
DateFormat format = new SimpleDateFormat(formatString);
mojo.useNativeGit = false;
mojo.execute();
Properties jgitProps = createCopy(targetProject.getProperties());
mojo.useNativeGit = true;
mojo.execute();
Properties nativeProps = createCopy(targetProject.getProperties());
assertGitPropertiesPresentInProject(jgitProps);
assertGitPropertiesPresentInProject(nativeProps);
for (String key : GIT_KEYS) {
if (!key.equals("git.build.time")) {
// git.build.time is excused because the two runs happened at different times.
String jGitKey = jgitProps.getProperty(key);
String nativeKey = nativeProps.getProperty(key);
assertEquals(
"Key difference for key: '" + key + "'; jgit=" + jGitKey
+ "; nativeKey=" + nativeKey + "; for " + repo.getDir(),
jGitKey,
nativeKey);
} else {
// Ensure that the date formats are parseable and within reason. If running all the git
// commands on the
// native provider takes more than 60 seconds, then something is seriously wrong.
long jGitBuildTimeInMs = format.parse(jgitProps.getProperty(key)).getTime();
long nativeBuildTimeInMs = format.parse(nativeProps.getProperty(key)).getTime();
Assert.assertTrue(
"Time ran backwards, jgitTime after nativeTime!",
jGitBuildTimeInMs <= nativeBuildTimeInMs);
Assert.assertTrue(
"Build ran too slow.",
(nativeBuildTimeInMs - jGitBuildTimeInMs)
< 60000L); // If native takes more than 1 minute, something is wrong.
}
}
// Check the commit time to be equal in ms, too.
long jGitCommitTimeInMs = format.parse(jgitProps.getProperty("git.commit.time")).getTime();
long nativeCommitTimeInMs = format.parse(nativeProps.getProperty("git.commit.time")).getTime();
assertEquals(
"commit times parse to different time stamps",
jGitCommitTimeInMs, nativeCommitTimeInMs);
}
private Properties createCopy(Properties orig) {
Properties p = new Properties();
for (String key : orig.stringPropertyNames()) {
p.setProperty(key, orig.getProperty(key));
}
return p;
}
private void assertGitPropertiesPresentInProject(Properties properties) {
for (String key : GIT_KEYS) {
assertThat(properties).satisfies(new ContainsKeyCondition(key));
}
}
}