Skip to content

Commit

Permalink
Add test and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Dec 31, 2023
1 parent c293e7b commit d19c412
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,9 @@ public abstract class BuildInfoExtractorUtils {

public static Properties mergePropertiesWithSystemAndPropertyFile(Properties existingProps, Log log) {
Properties mergedProps = new Properties();
// Add properties from file.
mergedProps.putAll(searchAdditionalPropertiesFile(mergedProps, log));
// Merge properties from system properties - should be second to override any properties defined in the file.
mergedProps.putAll(addSystemProperties(existingProps));
return mergedProps;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.lang.reflect.Field;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Base64;
import java.util.Map;
import java.util.Properties;
Expand All @@ -48,6 +49,8 @@
public class BuildExtractorUtilsTest {
private static final String POPO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "popo";
private static final String MOMO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "momo";
private static final String KOKO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "koko";
private static final String GOGO_KEY = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "gogo";
private static final String ENV_POPO_KEY = BuildInfoProperties.BUILD_INFO_ENVIRONMENT_PREFIX + "popo";
private static final String ENV_MOMO_KEY = BuildInfoProperties.BUILD_INFO_ENVIRONMENT_PREFIX + "momo";

Expand All @@ -67,8 +70,6 @@ public void getBuildInfoPropertiesFromSystemProps() {
assertEquals(props.size(), 2, "there should only be 2 properties after the filtering");
assertEquals(props.getProperty(POPO_KEY), "buildname", "popo property does not match");
assertEquals(props.getProperty(MOMO_KEY), "1", "momo property does not match");
System.clearProperty(POPO_KEY);
System.clearProperty(MOMO_KEY);
}

public void getBuildInfoPropertiesFromFile() throws IOException {
Expand All @@ -87,10 +88,31 @@ public void getBuildInfoPropertiesFromFile() throws IOException {
assertEquals(fileProps.getProperty(MOMO_KEY), "1", "momo property does not match");
}

public void getBuildInfoPropertiesFromFileAndSystemProps() throws IOException {
try (FileOutputStream fileOutputStream = new FileOutputStream(tempFile.toFile())) {
createProperties().store(fileOutputStream, "");
}

System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());
System.setProperty(KOKO_KEY, "3");
// Override MOMO_KEY from file
System.setProperty(MOMO_KEY, "2");

Properties fileProps = filterDynamicProperties(
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
BUILD_INFO_PROP_PREDICATE);

assertEquals(fileProps.size(), 3, "there should only be 2 properties after the filtering");
assertEquals(fileProps.getProperty(POPO_KEY), "buildname", "popo property does not match");
assertEquals(fileProps.getProperty(MOMO_KEY), "2", "momo property does not match");
assertEquals(fileProps.getProperty(KOKO_KEY), "3", "koko property does not match");
}

@AfterMethod
private void tearDown() throws Exception {
Files.deleteIfExists(tempFile);

Arrays.asList(POPO_KEY, MOMO_KEY, KOKO_KEY, GOGO_KEY).forEach(System::clearProperty);
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE);
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY);
unsetEnv(BuildInfoConfigProperties.PROP_PROPS_FILE_KEY_IV);
Expand All @@ -103,10 +125,8 @@ public void getBuildInfoProperties() throws IOException {
System.setProperty(BuildInfoConfigProperties.PROP_PROPS_FILE, tempFile.toString());

// Put system properties
String kokoKey = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "koko";
String gogoKey = BuildInfoProperties.BUILD_INFO_PROP_PREFIX + "gogo";
System.setProperty(kokoKey, "parent");
System.setProperty(gogoKey, "2");
System.setProperty(KOKO_KEY, "parent");
System.setProperty(GOGO_KEY, "2");

Properties buildInfoProperties = filterDynamicProperties(
mergePropertiesWithSystemAndPropertyFile(new Properties(), getLog()),
Expand All @@ -115,11 +135,8 @@ public void getBuildInfoProperties() throws IOException {
assertEquals(buildInfoProperties.size(), 4, "There should be 4 properties");
assertEquals(buildInfoProperties.getProperty(POPO_KEY), "buildname", "popo property does not match");
assertEquals(buildInfoProperties.getProperty(MOMO_KEY), "1", "momo number property does not match");
assertEquals(buildInfoProperties.getProperty(kokoKey), "parent", "koko parent name property does not match");
assertEquals(buildInfoProperties.getProperty(gogoKey), "2", "gogo parent number property does not match");

System.clearProperty(kokoKey);
System.clearProperty(gogoKey);
assertEquals(buildInfoProperties.getProperty(KOKO_KEY), "parent", "koko parent name property does not match");
assertEquals(buildInfoProperties.getProperty(GOGO_KEY), "2", "gogo parent number property does not match");
}

public void getEnvPropertiesFromFile() throws IOException {
Expand Down

0 comments on commit d19c412

Please sign in to comment.