Skip to content

Commit

Permalink
o JENKINS-10994: better error handling for env var replacement (and (…
Browse files Browse the repository at this point in the history
…non-related) made the HudsonBackup unit-testing-specific constructor protected)

git-svn-id: https://svn.jenkins-ci.org/trunk/hudson/plugins/thinBackup@39904 71c3de6d-444a-0410-be80-ed276b4c234a
  • Loading branch information
msteinkogler committed Sep 15, 2011
1 parent e6de9f0 commit c3ebcef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
Expand Up @@ -70,7 +70,7 @@ public HudsonBackup(final ThinBackupPluginImpl plugin, final BackupType backupTy
/**
* package visible constructor for unit testing purposes only.
*/
HudsonBackup(final ThinBackupPluginImpl plugin, final BackupType backupType, final Date date) {
protected HudsonBackup(final ThinBackupPluginImpl plugin, final BackupType backupType, final Date date) {
this.plugin = plugin;
this.hudsonHome = plugin.getHudsonHome();

Expand Down
18 changes: 16 additions & 2 deletions src/main/java/org/jvnet/hudson/plugins/thinbackup/utils/Utils.java
Expand Up @@ -372,7 +372,11 @@ public static String expandEnvironmentVariables(final String path) {
return internalExpandEnvironmentVariables(path, System.getenv());
}

static String internalExpandEnvironmentVariables(final String path, final Map<String, String> environmentVariables) {
/**
* For unit testing purposes only. Use @link {expandEnvironmentVariables}.
*/
protected static String internalExpandEnvironmentVariables(final String path,
final Map<String, String> environmentVariables) {
final String os = environmentVariables.get("os.name");

String startEnvVarToken = "";
Expand All @@ -396,8 +400,18 @@ static String internalExpandEnvironmentVariables(final String path, final Map<St
final int endIdx = tmpPath.indexOf(endEnvVarToken, startIdx + startEnvVarToken.length());

if (endIdx != -1) {
final String envVar = tmpPath.substring(startIdx + startEnvVarToken.length(), endIdx);
String envVarValue = environmentVariables.get(envVar);
if (envVarValue == null) {
LOGGER
.warning(String
.format(
"Environment variable '%s' was specified in path '%s', but it is not defined in the system's environment variables.",
envVar, path));
envVarValue = "";
}
newPath.append(tmpPath.substring(0, startIdx));
newPath.append(environmentVariables.get(tmpPath.substring(startIdx + startEnvVarToken.length(), endIdx)));
newPath.append(envVarValue);
tmpPath = tmpPath.substring(endIdx + endEnvVarToken.length());
} else {
newPath.append(tmpPath);
Expand Down
Expand Up @@ -152,6 +152,8 @@ public void testExpandEnvironmentVariables() {
Assert.assertEquals("1REPLACEMENT2${3", Utils.internalExpandEnvironmentVariables(unixPath, unixMap));
unixPath = "1${TEST_VAR}2${TEST_VAR}3";
Assert.assertEquals("1REPLACEMENT2REPLACEMENT3", Utils.internalExpandEnvironmentVariables(unixPath, unixMap));
unixPath = "1${NO_VALUE}";
Assert.assertEquals("1", Utils.internalExpandEnvironmentVariables(unixPath, unixMap));

final Map<String, String> windowsMap = new HashMap<String, String>();
windowsMap.put("os.name", "windows");
Expand All @@ -166,6 +168,8 @@ public void testExpandEnvironmentVariables() {
Assert.assertEquals("1REPLACEMENT2%3", Utils.internalExpandEnvironmentVariables(windowsPath, windowsMap));
windowsPath = "1%TEST_VAR%2%TEST_VAR%3";
Assert.assertEquals("1REPLACEMENT2REPLACEMENT3", Utils.internalExpandEnvironmentVariables(windowsPath, windowsMap));
windowsPath = "1%NO_VALUE%";
Assert.assertEquals("1", Utils.internalExpandEnvironmentVariables(windowsPath, windowsMap));
}

}

0 comments on commit c3ebcef

Please sign in to comment.