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