diff --git a/org.moreunit.core/src/org/moreunit/core/matching/TestFolderPathPattern.java b/org.moreunit.core/src/org/moreunit/core/matching/TestFolderPathPattern.java index a49be060..8e6dc85d 100644 --- a/org.moreunit.core/src/org/moreunit/core/matching/TestFolderPathPattern.java +++ b/org.moreunit.core/src/org/moreunit/core/matching/TestFolderPathPattern.java @@ -48,6 +48,8 @@ public class TestFolderPathPattern TEST_PATH_VALIDATOR = compile("^/?[^/\\*\\(\\)]*" + quote(SRC_PROJECT_VARIABLE) + "[^\\*\\(\\)]*$"); } + private static final Pattern GROUP_PATTERN = Pattern.compile("\\([^\\)]+\\)"); + private final String srcPathTemplate; private final String testPathTemplate; private final Pattern testProjectPattern; @@ -204,6 +206,7 @@ private String resolveGroups(String path, String tplWithGroups, String tplWithRe List groupRefs = getGroupRefs(result); reverse(groupRefs); + StringBuilder resultBuilder = new StringBuilder(result); for (int i = 0; i < groupRefs.size(); i++) { GroupRef ref = groupRefs.get(i); @@ -218,8 +221,9 @@ private String resolveGroups(String path, String tplWithGroups, String tplWithRe throw new DoesNotMatchConfigurationException(analizedPath); } - result = result.substring(0, ref.startIdx) + groupContent + result.substring(ref.endIdx); + resultBuilder.replace(ref.startIdx, ref.endIdx, groupContent); } + result = resultBuilder.toString(); } return result; } @@ -251,6 +255,11 @@ public SourceFolderPath getSrcPathFor(Path testPath) throws DoesNotMatchConfigur private String replaceGroupsWithRefs(String template, List groupRefs) { + if (groupRefs.isEmpty()) + { + return template; + } + Map refIndices = new HashMap(); int idx = 1; for (GroupRef ref : groupRefs) @@ -259,12 +268,16 @@ private String replaceGroupsWithRefs(String template, List groupRefs) idx++; } - String result = template; - for (int i = 0; i < groupRefs.size(); i++) + Matcher matcher = GROUP_PATTERN.matcher(template); + StringBuffer sb = new StringBuffer(); + int i = 0; + while (matcher.find() && i < groupRefs.size()) { - result = result.replaceFirst("\\([^\\)]+\\)", "\\\\" + refIndices.get(i)); + matcher.appendReplacement(sb, Matcher.quoteReplacement("\\" + refIndices.get(i))); + i++; } - return result; + matcher.appendTail(sb); + return sb.toString(); } private String getSrcProjectName(String tstProjectName, Path tstPath) throws DoesNotMatchConfigurationException