Skip to content

Commit

Permalink
Changed the way we handles backslashes in the parameter providers.
Browse files Browse the repository at this point in the history
issue #292

backslashes

backslashes
  • Loading branch information
itaiag committed Nov 23, 2017
1 parent 08b4e31 commit 58daa50
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -80,9 +79,11 @@ public Object getFromString(String stringRepresentation) throws Exception {

// then extract the string to be load as properties object
String propertiesString = stringRepresentation.substring(classEndIndex + 1);

Properties properties = new Properties();
try {
// Since we don't want to lose the backslashes, we need to replace each of the single backslashes with
// double backslashes before loading the string to the properties object
propertiesString = propertiesString.replaceAll("(?<!\\\\)\\\\(?!\\\\)", "\\\\\\\\");
properties.load(new StringReader(propertiesString));
} catch (IOException e1) {
log.log(Level.WARNING, "Fail to load properties: " + propertiesString, e1);
Expand Down Expand Up @@ -150,7 +151,6 @@ private static String[] getProeprties(ArrayList<BeanElement> beanElements){
return properties;
}


@Override
public void setProviderConfig(String... args) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ public Object getFromString(String stringRepresentation) throws Exception {

// then extract the string to be load as properties object
String propertiesString = stringRepresentation.substring(classEndIndex + 1);

Properties properties = new Properties();
try {
// Since we don't want to lose the backslashes, we need to replace each of the single backslashes with
// double backslashes before loading the string to the properties object
propertiesString = propertiesString.replaceAll("(?<!\\\\)\\\\(?!\\\\)", "\\\\\\\\");
properties.load(new StringReader(propertiesString));
} catch (IOException e1) {
log.log(Level.WARNING, "Fail to load properties: " + propertiesString, e1);
Expand Down Expand Up @@ -178,7 +180,6 @@ public synchronized Object showUI(Component parent, Scenario currentScenario, Ru
return object;
}


private static LinkedHashMap<String, String> propertiesToMapBeanOrder(Properties properties,
ArrayList<BeanElement> elements) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Properties getRunProperties() throws IOException {
private synchronized Properties loadProperties() throws IOException{
Properties p = new Properties();
if (runPropertiesFile.getAbsoluteFile().exists()) {
p = FileUtils.loadBeanPropertiesFromFile(runPropertiesFile.getAbsolutePath());
p = FileUtils.loadPropertiesFromFile(runPropertiesFile.getAbsolutePath());
log.log(java.util.logging.Level.CONFIG,"load run properties at :"+runPropertiesFile.getAbsolutePath());
}else{
log.log(java.util.logging.Level.CONFIG,"the run properties doesn't exist,return new Properties object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public synchronized Properties getProperties() {
File summaryFile = new File(SUMMARY_FILE_NAME);
if (summaryFile.exists()) {
try {
properties = FileUtils.loadBeanPropertiesFromFile(SUMMARY_FILE_NAME);
properties = FileUtils.loadPropertiesFromFile(SUMMARY_FILE_NAME);
} catch (Exception exception) {
log.log(Level.WARNING, "Fail to load summary properties", exception);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
Expand All @@ -33,11 +31,9 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -91,8 +87,6 @@ public String toString() {

private static Logger log = Logger.getLogger(FileUtils.class.getName());

private final static Set<String> beansCache = new HashSet<>();

public static void copyDirectory(String sourceDirName, String destinationDirName) throws IOException {

copyDirectory(new File(sourceDirName), new File(destinationDirName), null);
Expand Down Expand Up @@ -978,8 +972,6 @@ public static String[] getFilesWithExtension(String directory, String extension)
* @return Properties object of the file
* @throws IOException
*/


public static Properties loadPropertiesFromFile(String fileName) throws IOException {
fileName = replaceSeparator(fileName);
log.finest("Loading properties from file " + fileName);
Expand All @@ -990,29 +982,6 @@ public static Properties loadPropertiesFromFile(String fileName) throws IOExcept
input = new FileInputStream(fileName);
inputStreamReader = new InputStreamReader(input, "UTF-8");
p.load(inputStreamReader);
for (Object paramKey : p.keySet()) {
if (paramKey.toString().endsWith("Bean")
&& !beansCache.contains(p.get(paramKey.toString()).toString())) {
beansCache.add(p.get(paramKey.toString()).toString());
String[] beanParamValueProp = p.get(paramKey.toString()).toString().split("\r\n");
StringBuilder beanParamValueFixedSB = new StringBuilder();
for (String line : beanParamValueProp) {
if (!line.contains("="))
continue;
String[] keyValuePairArr = line.split("=", 2);

Pattern twoOrMoreBackslashesPattern = Pattern.compile(".*([\\\\])\\1{1,}.*");
if (twoOrMoreBackslashesPattern.matcher(keyValuePairArr[1]).find()) {
String fixedParamValBackslash = null;
fixedParamValBackslash = keyValuePairArr[1].replace("\\\\", "\\");
beanParamValueFixedSB.append(keyValuePairArr[0] + "=" + fixedParamValBackslash + "\r\n");

} else
beanParamValueFixedSB.append(line + "\r\n");
}
p.setProperty(paramKey.toString(), beanParamValueFixedSB.toString());
}
}
return p;
} finally { // close input stream
if (inputStreamReader != null) {
Expand All @@ -1023,33 +992,6 @@ public static Properties loadPropertiesFromFile(String fileName) throws IOExcept
}
}
}


public static Properties loadBeanPropertiesFromFile(String fileName) throws IOException {

Properties properties = new Properties();
try (FileInputStream input = new FileInputStream(fileName);
InputStreamReader inputStreamReader = new InputStreamReader(input, "UTF-8");
BufferedReader bfr = new BufferedReader(inputStreamReader);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {

String readLine = null;
while ((readLine = bfr.readLine()) != null) {
//In JsystemApp -> GenericObjectParameterPRovider & array Provider,
//for some reason, inject this string prop representation into another
//properties file, in there the 2nd properies file treats every backslash
//as a special char as well, therefore, were multiplying the backslashes twice on purpose

out.write(readLine.replace("\\", "\\\\\\\\").getBytes());
out.write("\n".getBytes());
} // while

InputStream is = new ByteArrayInputStream(out.toByteArray());
properties.load(is);
}
return properties;
}


/**
* save given properties to a file
Expand Down Expand Up @@ -1366,4 +1308,4 @@ public static void moveDirectory(String sourceDirectory, String destinationDirec
}
}

}
}

0 comments on commit 58daa50

Please sign in to comment.