Skip to content

Commit

Permalink
[FIXED JENKINS-22530] Properly parse files with multi-line properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
xstex authored and orrc committed May 13, 2014
1 parent 875e3b1 commit 1a7a66e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/main/java/hudson/plugins/android_emulator/util/Utils.java
Expand Up @@ -38,6 +38,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
Expand Down Expand Up @@ -431,25 +432,21 @@ public static void runAndroidTool(Launcher launcher, EnvVars env, OutputStream s
* @return The key-value pairs contained in the file, ignoring any comments or blank lines.
* @throws IOException If the file could not be read.
*/
public static Map<String,String> parseConfigFile(File configFile) throws IOException {
public static Map<String, String> parseConfigFile(File configFile) throws IOException {
FileReader fileReader = new FileReader(configFile);
BufferedReader reader = new BufferedReader(fileReader);
Properties properties = new Properties();
properties.load(reader);
reader.close();

String line;
Map<String,String> values = new HashMap<String,String>();
while ((line = reader.readLine()) != null) {
line = line.trim();
if (line.length() == 0 || line.charAt(0) == '#') {
continue;
}
String[] parts = line.split("=", 2);
values.put(parts[0], parts[1]);
final Map<String, String> values = new HashMap<String, String>();
for (final Map.Entry<Object, Object> entry : properties.entrySet()) {
values.put((String) entry.getKey(), (String) entry.getValue());
}

return values;
}


/**
* Expands the variable in the given string to its value in the environment variables available
* to this build. The Jenkins-specific build variables for this build are then substituted.
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/hudson/plugins/android_emulator/util/UtilsTest.java
Expand Up @@ -2,6 +2,10 @@

import junit.framework.TestCase;

import java.io.File;
import java.io.PrintWriter;
import java.util.Map;

@SuppressWarnings("static-method")
public class UtilsTest extends TestCase {

Expand Down Expand Up @@ -89,4 +93,15 @@ public void testGetApiLevelFromPlatform() {
assertEquals(-1, Utils.getApiLevelFromPlatform("Android 4.2"));
}

public void testReadProperties() throws Exception {
final File temp = File.createTempFile("temp", ".txt");
temp.deleteOnExit();
final PrintWriter writer = new PrintWriter(temp);
writer.println("key=value\\\nsplit\\\nin\\\nlines\n");
writer.close();

final Map<String, String> map = Utils.parseConfigFile(temp);
assertEquals(1, map.size());
}

}

0 comments on commit 1a7a66e

Please sign in to comment.