Skip to content

Commit 771089f

Browse files
committed
#310 Moved properties replacement into PropertiesReplacer.
1 parent 06c54c8 commit 771089f

File tree

5 files changed

+235
-202
lines changed

5 files changed

+235
-202
lines changed

src/main/java/pl/project13/maven/git/GitCommitIdMojo.java

Lines changed: 4 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Map;
3737
import java.util.Properties;
3838
import java.util.TimeZone;
39-
import java.util.regex.Pattern;
4039

4140
import org.apache.maven.execution.MavenSession;
4241
import org.apache.maven.plugin.AbstractMojo;
@@ -51,10 +50,6 @@
5150
import com.fasterxml.jackson.core.type.TypeReference;
5251
import com.fasterxml.jackson.databind.ObjectMapper;
5352
import com.google.common.annotations.VisibleForTesting;
54-
import com.google.common.base.Function;
55-
import com.google.common.base.Predicate;
56-
import com.google.common.base.Predicates;
57-
import com.google.common.collect.Lists;
5853
import com.google.common.io.Closeables;
5954
import com.google.common.io.Files;
6055
import java.io.OutputStream;
@@ -294,7 +289,7 @@ public class GitCommitIdMojo extends AbstractMojo {
294289
* @since 2.2.3
295290
*/
296291
@Parameter
297-
private List<ReplacementProperty> replacementProperties;
292+
@VisibleForTesting List<ReplacementProperty> replacementProperties;
298293

299294
/**
300295
* The properties we store our data in and then expose them.
@@ -312,6 +307,8 @@ public class GitCommitIdMojo extends AbstractMojo {
312307
@NotNull
313308
private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log);
314309

310+
@NotNull @VisibleForTesting PropertiesReplacer propertiesReplacer = new PropertiesReplacer(log);
311+
315312
@Override
316313
public void execute() throws MojoExecutionException {
317314
try {
@@ -376,7 +373,7 @@ public void execute() throws MojoExecutionException {
376373
loadBuildVersionAndTimeData(properties);
377374
loadBuildHostData(properties);
378375
loadShortDescribe(properties);
379-
performReplacement(properties, replacementProperties);
376+
propertiesReplacer.performReplacement(properties, replacementProperties);
380377
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
381378
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
382379
logProperties(properties);
@@ -397,57 +394,6 @@ public void execute() throws MojoExecutionException {
397394
}
398395
}
399396

400-
@VisibleForTesting void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
401-
if((replacementProperties != null) && (properties != null)) {
402-
for(ReplacementProperty replacementProperty: replacementProperties) {
403-
String propertyKey = replacementProperty.getProperty();
404-
if(propertyKey == null) {
405-
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
406-
String key = (String)entry.getKey();
407-
String content = (String)entry.getValue();
408-
String result = performReplacement(replacementProperty, content);
409-
entry.setValue(result);
410-
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
411-
}
412-
} else {
413-
String content = properties.getProperty(propertyKey);
414-
String result = performReplacement(replacementProperty, content);
415-
properties.setProperty(propertyKey, result);
416-
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
417-
}
418-
}
419-
}
420-
}
421-
422-
private String performReplacement(ReplacementProperty replacementProperty, String content) {
423-
String result = content;
424-
if(replacementProperty != null) {
425-
if(replacementProperty.isRegex()) {
426-
result = replaceRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
427-
} else {
428-
result = replaceNonRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
429-
}
430-
}
431-
return result;
432-
}
433-
434-
private String replaceRegex(String content, String token, String value) {
435-
if((token == null) || (value == null)) {
436-
log.error("found replacementProperty without required token or value.");
437-
return content;
438-
}
439-
final Pattern compiledPattern = Pattern.compile(token);
440-
return compiledPattern.matcher(content).replaceAll(value);
441-
}
442-
443-
private String replaceNonRegex(String content, String token, String value) {
444-
if((token == null) || (value == null)) {
445-
log.error("found replacementProperty without required token or value.");
446-
return content;
447-
}
448-
return content.replace(token, value);
449-
}
450-
451397
/**
452398
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
453399
* If it's true, an GitCommitIdExecutionException will be thrown, otherwise we just log an error message.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* This file is part of git-commit-id-plugin by Konrad 'ktoso' Malawski <konrad.malawski@java.pl>
3+
*
4+
* git-commit-id-plugin is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* git-commit-id-plugin is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public License
15+
* along with git-commit-id-plugin. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package pl.project13.maven.git;
19+
20+
import pl.project13.maven.git.log.LoggerBridge;
21+
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Properties;
25+
import java.util.regex.Pattern;
26+
27+
public class PropertiesReplacer
28+
{
29+
private final LoggerBridge log;
30+
31+
public PropertiesReplacer(LoggerBridge log)
32+
{
33+
this.log = log;
34+
}
35+
36+
public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) {
37+
if((replacementProperties != null) && (properties != null)) {
38+
for(ReplacementProperty replacementProperty: replacementProperties) {
39+
String propertyKey = replacementProperty.getProperty();
40+
if(propertyKey == null) {
41+
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
42+
String key = (String)entry.getKey();
43+
String content = (String)entry.getValue();
44+
String result = performReplacement(replacementProperty, content);
45+
entry.setValue(result);
46+
log.info("apply replace on property " + key + ": original value '" + content + "' with '" + result + "'");
47+
}
48+
} else {
49+
String content = properties.getProperty(propertyKey);
50+
String result = performReplacement(replacementProperty, content);
51+
properties.setProperty(propertyKey, result);
52+
log.info("apply replace on property " + propertyKey + ": original value '" + content + "' with '" + result + "'");
53+
}
54+
}
55+
}
56+
}
57+
58+
private String performReplacement(ReplacementProperty replacementProperty, String content) {
59+
String result = content;
60+
if(replacementProperty != null) {
61+
if(replacementProperty.isRegex()) {
62+
result = replaceRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
63+
} else {
64+
result = replaceNonRegex(content, replacementProperty.getToken(), replacementProperty.getValue());
65+
}
66+
}
67+
return result;
68+
}
69+
70+
private String replaceRegex(String content, String token, String value) {
71+
if((token == null) || (value == null)) {
72+
log.error("found replacementProperty without required token or value.");
73+
return content;
74+
}
75+
final Pattern compiledPattern = Pattern.compile(token);
76+
return compiledPattern.matcher(content).replaceAll(value);
77+
}
78+
79+
private String replaceNonRegex(String content, String token, String value) {
80+
if((token == null) || (value == null)) {
81+
log.error("found replacementProperty without required token or value.");
82+
return content;
83+
}
84+
return content.replace(token, value);
85+
}
86+
}

src/test/java/pl/project13/maven/git/GitCommitIdMojoTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.Maps;
22+
import org.apache.maven.plugin.MojoExecutionException;
2223
import org.apache.maven.project.MavenProject;
2324
import org.eclipse.jgit.lib.Repository;
2425
import org.junit.Before;
@@ -27,6 +28,7 @@
2728

2829
import java.io.File;
2930
import java.io.IOException;
31+
import java.util.List;
3032
import java.util.Map;
3133
import java.util.Properties;
3234

@@ -314,4 +316,15 @@ public void testCraftPropertiesOutputFileWithFullPath() throws IOException {
314316
assertThat(result.getCanonicalPath()).isEqualTo(generateGitPropertiesFilename);
315317
}
316318

319+
@Test
320+
public void shouldPerformReplacements() throws MojoExecutionException
321+
{
322+
mojo.propertiesReplacer = mock(PropertiesReplacer.class);
323+
mojo.replacementProperties = mock(List.class);
324+
325+
mojo.execute();
326+
327+
verify(mojo.propertiesReplacer).performReplacement(any(Properties.class), eq(mojo.replacementProperties));
328+
}
329+
317330
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
package pl.project13.maven.git;
2+
3+
import junitparams.JUnitParamsRunner;
4+
import junitparams.Parameters;
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import pl.project13.maven.git.log.MavenLoggerBridge;
10+
11+
import java.io.IOException;
12+
import java.util.ArrayList;
13+
import java.util.Collection;
14+
import java.util.List;
15+
import java.util.Map;
16+
import java.util.Properties;
17+
18+
import static java.util.Arrays.asList;
19+
import static org.mockito.Mockito.mock;
20+
21+
@RunWith(JUnitParamsRunner.class)
22+
public class PropertiesReplacerTest
23+
{
24+
public static Collection useRegexReplacement() {
25+
return asList(true, false);
26+
}
27+
28+
private PropertiesReplacer propertiesReplacer;
29+
30+
@Before
31+
public void setUp() {
32+
this.propertiesReplacer = new PropertiesReplacer(mock(MavenLoggerBridge.class));
33+
}
34+
35+
@Test
36+
public void testPerformReplacementWithNullValues() throws IOException
37+
{
38+
Properties properties = null;
39+
List<ReplacementProperty> replacementProperties = null;
40+
propertiesReplacer.performReplacement(properties, replacementProperties);
41+
}
42+
43+
@Test
44+
@Parameters(method = "useRegexReplacement")
45+
public void testPerformReplacementWithInvalidReplacement(boolean regex) throws IOException {
46+
Properties actualProperties = build("key1", "value1", "key2", "value2");
47+
48+
List<ReplacementProperty> replacementProperties = new ArrayList<>();
49+
replacementProperties.add(new ReplacementProperty("key1", null, null, regex));
50+
51+
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
52+
}
53+
54+
@Test
55+
@Parameters(method = "useRegexReplacement")
56+
public void testPerformReplacementWithSingleProperty(boolean regex) throws IOException {
57+
Properties actualProperties = build("key1", "value1", "key2", "value2");
58+
59+
List<ReplacementProperty> replacementProperties = new ArrayList<>();
60+
replacementProperties.add(new ReplacementProperty("key1", "value", "another", regex));
61+
62+
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
63+
64+
Properties exptecedProperties = build("key1", "another1", "key2", "value2");
65+
assertEquals(exptecedProperties, actualProperties);
66+
}
67+
68+
@Test
69+
@Parameters(method = "useRegexReplacement")
70+
public void testPerformReplacementWithMultipleProperties(boolean regex) throws IOException {
71+
Properties actualProperties = build("key1", "value1", "key2", "value2");
72+
73+
List<ReplacementProperty> replacementProperties = new ArrayList<>();
74+
replacementProperties.add(new ReplacementProperty(null, "value", "another", regex));
75+
76+
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
77+
78+
Properties exptecedProperties = build("key1", "another1", "key2", "another2");
79+
assertEquals(exptecedProperties, actualProperties);
80+
}
81+
82+
@Test
83+
public void testPerformReplacementWithPatternGroupAndMatching() throws IOException {
84+
Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author/name");
85+
86+
List<ReplacementProperty> replacementProperties = new ArrayList<>();
87+
replacementProperties.add(new ReplacementProperty("git.branch", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true));
88+
89+
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
90+
91+
Properties exptecedProperties = build("git.branch", "feature-feature_name", "git.commit.author", "author/name");
92+
assertEquals(exptecedProperties, actualProperties);
93+
}
94+
95+
@Test
96+
public void testPerformReplacementWithPatternGroupAndNoMatch() throws IOException {
97+
Properties actualProperties = build("git.branch", "feature#feature_name", "git.commit.author", "author#");
98+
99+
List<ReplacementProperty> replacementProperties = new ArrayList<>();
100+
replacementProperties.add(new ReplacementProperty("git.branch", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true));
101+
102+
propertiesReplacer.performReplacement(actualProperties, replacementProperties);
103+
104+
Properties exptecedProperties = build("git.branch", "feature#feature_name", "git.commit.author", "author#");
105+
assertEquals(exptecedProperties, actualProperties);
106+
}
107+
108+
private Properties build(String key1, String value1, String key2, String value2) {
109+
Properties properties = new Properties();
110+
properties.put(key1, value1);
111+
properties.put(key2, value2);
112+
return properties;
113+
}
114+
115+
private void assertEquals(Properties expected, Properties actual) {
116+
if(expected == null) {
117+
Assert.assertNull(actual);
118+
} else if(actual == null) {
119+
Assert.assertNull(expected);
120+
} else {
121+
Assert.assertEquals(expected.size(), actual.size());
122+
for(Map.Entry<Object, Object> expectedElementEntry : expected.entrySet()) {
123+
String expectedKey = (String)expectedElementEntry.getKey();
124+
String expectedValue = (String)expectedElementEntry.getValue();
125+
Assert.assertTrue(actual.containsKey(expectedKey));
126+
String actualValue = actual.getProperty(expectedKey);
127+
Assert.assertEquals(expectedValue, actualValue);
128+
}
129+
}
130+
}
131+
132+
}

0 commit comments

Comments
 (0)