Skip to content

Commit 06c54c8

Browse files
author
cbuschka
committed
#310: Moved filtering into helper class PropertiesFilterer with test.
1 parent 993f265 commit 06c54c8

File tree

3 files changed

+194
-52
lines changed

3 files changed

+194
-52
lines changed

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

Lines changed: 5 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,9 @@ public class GitCommitIdMojo extends AbstractMojo {
309309
@NotNull
310310
private final LoggerBridge log = new MavenLoggerBridge(this, false);
311311

312+
@NotNull
313+
private PropertiesFilterer propertiesFilterer = new PropertiesFilterer(log);
314+
312315
@Override
313316
public void execute() throws MojoExecutionException {
314317
try {
@@ -374,8 +377,8 @@ public void execute() throws MojoExecutionException {
374377
loadBuildHostData(properties);
375378
loadShortDescribe(properties);
376379
performReplacement(properties, replacementProperties);
377-
filter(properties, includeOnlyProperties);
378-
filterNot(properties, excludeProperties);
380+
propertiesFilterer.filter(properties, includeOnlyProperties, this.prefixDot);
381+
propertiesFilterer.filterNot(properties, excludeProperties, this.prefixDot);
379382
logProperties(properties);
380383

381384
if (generateGitPropertiesFile) {
@@ -445,56 +448,6 @@ private String replaceNonRegex(String content, String token, String value) {
445448
return content.replace(token, value);
446449
}
447450

448-
private void filterNot(Properties properties, @Nullable List<String> exclusions) {
449-
if (exclusions == null || exclusions.isEmpty()) {
450-
return;
451-
}
452-
453-
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
454-
@Override
455-
public Predicate<CharSequence> apply(String exclude) {
456-
return Predicates.containsPattern(exclude);
457-
}
458-
});
459-
460-
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
461-
for (Predicate<CharSequence> predicate : excludePredicates) {
462-
shouldExclude = Predicates.or(shouldExclude, predicate);
463-
}
464-
465-
for (String key : properties.stringPropertyNames()) {
466-
if (isOurProperty(key) && shouldExclude.apply(key)) {
467-
log.debug("shouldExclude.apply({}) = {}", key, shouldExclude.apply(key));
468-
properties.remove(key);
469-
}
470-
}
471-
}
472-
473-
private void filter(Properties properties, @Nullable List<String> inclusions) {
474-
if (inclusions == null || inclusions.isEmpty()) {
475-
return;
476-
}
477-
478-
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
479-
@Override
480-
public Predicate<CharSequence> apply(String exclude) {
481-
return Predicates.containsPattern(exclude);
482-
}
483-
});
484-
485-
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
486-
for (Predicate<CharSequence> predicate : includePredicates) {
487-
shouldInclude = Predicates.or(shouldInclude, predicate);
488-
}
489-
490-
for (String key : properties.stringPropertyNames()) {
491-
if (isOurProperty(key) && !shouldInclude.apply(key)) {
492-
log.debug("!shouldInclude.apply({}) = {}", key, shouldInclude.apply(key));
493-
properties.remove(key);
494-
}
495-
}
496-
}
497-
498451
/**
499452
* Reacts to an exception based on the {@code failOnUnableToExtractRepoInfo} setting.
500453
* If it's true, an GitCommitIdExecutionException will be thrown, otherwise we just log an error message.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 java.util.List;
21+
import java.util.Properties;
22+
23+
import org.jetbrains.annotations.Nullable;
24+
25+
import com.google.common.base.Function;
26+
import com.google.common.base.Predicate;
27+
import com.google.common.base.Predicates;
28+
import com.google.common.collect.Lists;
29+
30+
import pl.project13.maven.git.log.LoggerBridge;
31+
32+
public class PropertiesFilterer {
33+
34+
private LoggerBridge log;
35+
36+
public PropertiesFilterer(LoggerBridge log) {
37+
this.log = log;
38+
}
39+
40+
public void filterNot(Properties properties, @Nullable List<String> exclusions, String prefixDot) {
41+
if (exclusions == null || exclusions.isEmpty()) {
42+
return;
43+
}
44+
45+
List<Predicate<CharSequence>> excludePredicates = Lists.transform(exclusions, new Function<String, Predicate<CharSequence>>() {
46+
@Override
47+
public Predicate<CharSequence> apply(String exclude) {
48+
return Predicates.containsPattern(exclude);
49+
}
50+
});
51+
52+
Predicate<CharSequence> shouldExclude = Predicates.alwaysFalse();
53+
for (Predicate<CharSequence> predicate : excludePredicates) {
54+
shouldExclude = Predicates.or(shouldExclude, predicate);
55+
}
56+
57+
for (String key : properties.stringPropertyNames()) {
58+
if (isOurProperty(key, prefixDot) && shouldExclude.apply(key)) {
59+
log.debug("shouldExclude.apply({}) = {}", key, shouldExclude.apply(key));
60+
properties.remove(key);
61+
}
62+
}
63+
}
64+
65+
public void filter(Properties properties, @Nullable List<String> inclusions, String prefixDot) {
66+
if (inclusions == null || inclusions.isEmpty()) {
67+
return;
68+
}
69+
70+
List<Predicate<CharSequence>> includePredicates = Lists.transform(inclusions, new Function<String, Predicate<CharSequence>>() {
71+
@Override
72+
public Predicate<CharSequence> apply(String exclude) {
73+
return Predicates.containsPattern(exclude);
74+
}
75+
});
76+
77+
Predicate<CharSequence> shouldInclude = Predicates.alwaysFalse();
78+
for (Predicate<CharSequence> predicate : includePredicates) {
79+
shouldInclude = Predicates.or(shouldInclude, predicate);
80+
}
81+
82+
for (String key : properties.stringPropertyNames()) {
83+
if (isOurProperty(key, prefixDot) && !shouldInclude.apply(key)) {
84+
log.debug("!shouldInclude.apply({}) = {}", key, shouldInclude.apply(key));
85+
properties.remove(key);
86+
}
87+
}
88+
}
89+
90+
private boolean isOurProperty(String key, String prefixDot) {
91+
return key.startsWith(prefixDot);
92+
}
93+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package pl.project13.maven.git;
2+
3+
import static org.mockito.Mockito.verify;
4+
import static org.mockito.Mockito.verifyNoMoreInteractions;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
import java.util.HashSet;
10+
import java.util.List;
11+
import java.util.Properties;
12+
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.mockito.InjectMocks;
16+
import org.mockito.Mock;
17+
import org.mockito.Mockito;
18+
import org.mockito.junit.MockitoJUnitRunner;
19+
20+
import pl.project13.maven.git.log.MavenLoggerBridge;
21+
22+
@RunWith(MockitoJUnitRunner.class)
23+
public class PropertiesFiltererTest {
24+
25+
private static final String PREFIX_DOT = "prefix.";
26+
27+
@InjectMocks
28+
private PropertiesFilterer propertiesFilterer;
29+
30+
@Mock
31+
private MavenLoggerBridge log;
32+
33+
@Mock
34+
private Properties properties;
35+
36+
@Test
37+
public void filterNotWithoutExclusions() {
38+
List<String> exclusions = null;
39+
40+
propertiesFilterer.filterNot(properties, exclusions, PREFIX_DOT);
41+
42+
Mockito.verifyZeroInteractions(properties);
43+
}
44+
45+
@Test
46+
public void filterNotWithEmptyExclusions() {
47+
List<String> exclusions = Collections.emptyList();
48+
49+
propertiesFilterer.filterNot(properties, exclusions, PREFIX_DOT);
50+
51+
Mockito.verifyZeroInteractions(properties);
52+
}
53+
54+
@Test
55+
public void filterNotRemovesOwnPropertyInExclusionAndSkipsOtherOnes() {
56+
List<String> inclusions = Arrays.asList("^prefix\\.exclude1.*$", "^prefix\\.exclude2.*$");
57+
when(properties.stringPropertyNames()).thenReturn(new HashSet<>(Arrays.asList("prefix.include", "prefix.exclude1", "prefix.exclude2", "global")));
58+
59+
propertiesFilterer.filterNot(properties, inclusions, PREFIX_DOT);
60+
61+
verify(properties).stringPropertyNames();
62+
verify(properties).remove("prefix.exclude1");
63+
verify(properties).remove("prefix.exclude2");
64+
verifyNoMoreInteractions(properties);
65+
}
66+
67+
@Test
68+
public void filterWithoutInclusions() {
69+
List<String> inclusions = null;
70+
71+
propertiesFilterer.filter(properties, inclusions, PREFIX_DOT);
72+
73+
Mockito.verifyZeroInteractions(properties);
74+
}
75+
76+
@Test
77+
public void filterWithEmptyInclusions() {
78+
List<String> inclusions = Collections.emptyList();
79+
80+
propertiesFilterer.filter(properties, inclusions, PREFIX_DOT);
81+
82+
Mockito.verifyZeroInteractions(properties);
83+
}
84+
85+
@Test
86+
public void filterRemovesOwnPropertyNotInInclusionAndSkipsOtherOnes() {
87+
List<String> inclusions = Arrays.asList("^prefix\\.include1.*$", "^prefix\\.include2.*$");
88+
when(properties.stringPropertyNames()).thenReturn(new HashSet<>(Arrays.asList("prefix.include1", "prefix.include2", "prefix.exclude", "global")));
89+
90+
propertiesFilterer.filter(properties, inclusions, PREFIX_DOT);
91+
92+
verify(properties).stringPropertyNames();
93+
verify(properties).remove("prefix.exclude");
94+
verifyNoMoreInteractions(properties);
95+
}
96+
}

0 commit comments

Comments
 (0)