Skip to content

Commit

Permalink
[JENKINS-17096] Convert OS specific separators upfront in ConfigSpec
Browse files Browse the repository at this point in the history
Raw config spec may have been defined with different OS separators,
leading to issues when comparing them.
  • Loading branch information
Vlatombe committed Jul 24, 2013
1 parent f369293 commit 732b41e
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
28 changes: 14 additions & 14 deletions src/main/java/hudson/plugins/clearcase/ConfigSpec.java
Expand Up @@ -34,15 +34,15 @@
import org.apache.commons.lang.Validate;

public class ConfigSpec {
private final String raw;
private final String raw;
private final boolean isUnix;

public ConfigSpec(String raw, boolean isUnix) {
Validate.notNull(raw);
this.raw = raw;
this.raw = PathUtil.convertPathForOS(raw, isUnix);
this.isUnix = isUnix;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
Expand All @@ -61,10 +61,10 @@ public boolean equals(Object obj) {
return false;
return true;
}

public Set<String> getLoadRules() {
Set<String> rules = new HashSet<String>();
for (String row : raw.split("[\\r\\n]+")) {
for (String row : StringUtils.split(raw, PathUtil.newLineForOS(isUnix))) {
String trimmedRow = row.trim();
if (trimmedRow.startsWith("load")) {
String rule = row.trim().substring("load".length()).trim();
Expand All @@ -75,11 +75,11 @@ public Set<String> getLoadRules() {
}

public String getLoadRulesString() {
StringBuilder sb = new StringBuilder();
for (String rule : getLoadRules()) {
sb.append("load " + rule + PathUtil.newLineForOS(isUnix));
}
return sb.toString();
StringBuilder sb = new StringBuilder();
for (String rule : getLoadRules()) {
sb.append("load " + rule + PathUtil.newLineForOS(isUnix));
}
return sb.toString();
}

public String getRaw() {
Expand All @@ -94,7 +94,7 @@ public int hashCode() {
result = prime * result + ((raw == null) ? 0 : raw.hashCode());
return result;
}

public ConfigSpec setLoadRules(String[] loadRules) {
StringBuilder sb = stripLoadRulesOnRaw();
if (!ArrayUtils.isEmpty(loadRules)) {
Expand All @@ -113,8 +113,8 @@ public static String cleanLoadRule(String loadRule, boolean isUnix) {
}
String lr = loadRule;
// Remove quotes if needed
if (lr.charAt(0) == '"' && lr.charAt(lr.length()-1) == '"') {
lr = lr.substring(1, lr.length()-1);
if (lr.charAt(0) == '"' && lr.charAt(lr.length() - 1) == '"') {
lr = lr.substring(1, lr.length() - 1);
}
// Prepend OS separator
char firstChar = lr.charAt(0);
Expand Down
57 changes: 57 additions & 0 deletions src/test/java/hudson/plugins/clearcase/ConfigSpecTest.java
@@ -0,0 +1,57 @@
package hudson.plugins.clearcase;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.util.Set;

import hudson.util.IOUtils;

import org.junit.Test;

public class ConfigSpecTest {

@Test
public void testConfigSpecDifferentOS() {
ConfigSpec cs1 = new ConfigSpec("element /MyVOB/... .../MyBranch/LATEST", true);
ConfigSpec cs2 = new ConfigSpec("element \\MyVOB\\... ...\\MyBranch\\LATEST", true);
assertEquals(cs1, cs2);
}

@Test
public void testExtractLoadRulesUnix() throws IOException {
String rawCs = IOUtils.toString(getClass().getResourceAsStream("ct-catcs-2-CRLF.log"));
ConfigSpec cs = new ConfigSpec(rawCs, true);
Set<String> loadRules = cs.getLoadRules();
assertEquals(1, loadRules.size());
assertEquals("/a/b", loadRules.iterator().next());
}

@Test
public void testLineEndings() throws IOException {
String rawCs1 = IOUtils.toString(getClass().getResourceAsStream("ct-catcs-2-LF.log"));
String rawCs2 = IOUtils.toString(getClass().getResourceAsStream("ct-catcs-2-CRLF.log"));
ConfigSpec cs1 = new ConfigSpec(rawCs1, true);
ConfigSpec cs2 = new ConfigSpec(rawCs2, true);
assertEquals(cs1, cs2);
}

@Test
public void testExtractLoadRulesWindows() throws IOException {
String rawCs = IOUtils.toString(getClass().getResourceAsStream("ct-catcs-2-CRLF.log"));
ConfigSpec cs = new ConfigSpec(rawCs, false);
Set<String> loadRules = cs.getLoadRules();
assertEquals(1, loadRules.size());
assertEquals("\\a\\b", loadRules.iterator().next());
}

@Test
public void testStripLoadRulesWindows() throws IOException {
String rawCs = IOUtils.toString(getClass().getResourceAsStream("ct-catcs-2-CRLF.log"));
ConfigSpec cs = new ConfigSpec(rawCs, false);
cs = cs.stripLoadRules();
assertTrue(cs.getLoadRules().isEmpty());
}

}
@@ -0,0 +1,5 @@
element * CHECKEDOUT
element * ...\rel2_bugfix\LATEST
element * \main\LATEST -mkbranch rel2_bugfix

load \a\b
5 changes: 5 additions & 0 deletions src/test/resources/hudson/plugins/clearcase/ct-catcs-2-LF.log
@@ -0,0 +1,5 @@
element * CHECKEDOUT
element * .../rel2_bugfix/LATEST
element * /main/LATEST -mkbranch rel2_bugfix

load /a/b

0 comments on commit 732b41e

Please sign in to comment.