PropertiesReplacer { public void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties) { if ((replacementProperties != null) && (properties != null)) { for (ReplacementProperty replacementProperty: replacementProperties) { String propertyKey = replacementProperty.getProperty(); if (propertyKey == null) { performReplacementOnAllGeneratedProperties(properties, replacementProperty); } else { performReplacementOnSingleProperty(properties, replacementProperty, propertyKey); } } } } PropertiesReplacer(LoggerBridge log, PluginParameterExpressionEvaluator expressionEvaluator); void performReplacement(Properties properties, List<ReplacementProperty> replacementProperties);  }
@Test public void testPerformReplacementOnEveryPropertyAndExpectNewPropertyGenerated() { Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author#"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty(null, "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("git.branch", "feature/feature_name", "git.branch.something", "feature-feature_name", "git.commit.author", "author#", "git.commit.author.something", "author#"); assertEquals(exptecedProperties, actualProperties); }
@Test @Parameters(method = "testPerformReplacementWithTransformationRule") public void runTransformationTestHelper(String input, String regex, TransformationRule.ApplyEnum applyRule, TransformationRule.ActionEnum actionRule, String expectedOutput) { Properties actualProperties = build("git.branch", input); List<TransformationRule> transformationRules = new ArrayList<>(); transformationRules.add(new TransformationRule(applyRule, actionRule)); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty(null, null, regex, "-", true, false, transformationRules)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("git.branch", expectedOutput); assertEquals(exptecedProperties, actualProperties); }
@Test public void testPerformReplacementWithNullValues() throws IOException { Properties properties = null; List<ReplacementProperty> replacementProperties = null; propertiesReplacer.performReplacement(properties, replacementProperties); }
@Test @Parameters(method = "useRegexReplacement") public void testPerformReplacementWithInvalidReplacement(boolean regex) throws IOException { Properties actualProperties = build("key1", "value1", "key2", "value2"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("key1", null, null, null, regex, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); }
@Test @Parameters(method = "useRegexReplacement") public void testPerformReplacementWithSingleProperty(boolean regex) throws IOException { Properties actualProperties = build("key1", "value1", "key2", "value2"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("key1", null, "value", "another", regex, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("key1", "another1", "key2", "value2"); assertEquals(exptecedProperties, actualProperties); }
@Test @Parameters(method = "useRegexReplacement") public void testPerformReplacementWithSinglePropertyEmptyValue(boolean regex) throws IOException { Properties actualProperties = build("key1", "value1", "key2", "value2"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("key1", null, "value", null, regex, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("key1", "1", "key2", "value2"); assertEquals(exptecedProperties, actualProperties); }
@Test @Parameters(method = "useRegexReplacement") public void testPerformReplacementWithMultipleProperties(boolean regex) throws IOException { Properties actualProperties = build("key1", "value1", "key2", "value2"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty(null, null, "value", "another", regex, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("key1", "another1", "key2", "another2"); assertEquals(exptecedProperties, actualProperties); }
@Test @Parameters(method = "useRegexReplacement") public void testPerformReplacementWithMultiplePropertiesEmptyValue(boolean regex) throws IOException { Properties actualProperties = build("key1", "value1", "key2", "value2"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty(null, null, "value", null, regex, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("key1", "1", "key2", "2"); assertEquals(exptecedProperties, actualProperties); }
@Test public void testPerformReplacementWithPatternGroupAndMatching() throws IOException { Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author/name"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("git.branch", "feature-feature_name", "git.commit.author", "author/name"); assertEquals(exptecedProperties, actualProperties); }
@Test public void testPerformReplacementWithPatternGroupAndNoMatch() throws IOException { Properties actualProperties = build("git.branch", "feature#feature_name", "git.commit.author", "author#"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("git.branch", null, "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("git.branch", "feature#feature_name", "git.commit.author", "author#"); assertEquals(exptecedProperties, actualProperties); }
@Test public void testPerformReplacementOnSinglePropertyAndExpectNewPropertyGenerated() { Properties actualProperties = build("git.branch", "feature/feature_name", "git.commit.author", "author#"); List<ReplacementProperty> replacementProperties = new ArrayList<>(); replacementProperties.add(new ReplacementProperty("git.branch", "something", "^([^\\/]*)\\/([^\\/]*)$", "$1-$2", true, false, null)); propertiesReplacer.performReplacement(actualProperties, replacementProperties); Properties exptecedProperties = build("git.branch", "feature/feature_name", "git.branch.something", "feature-feature_name", "git.commit.author", "author#"); assertEquals(exptecedProperties, actualProperties); }