001package org.conqat.engine.sourcecode.coverage;
002
003import java.util.Arrays;
004import java.util.Collections;
005import java.util.List;
006
007import org.conqat.engine.resource.util.UniformPathUtils;
008import org.conqat.lib.commons.collections.Pair;
009import org.conqat.lib.commons.string.StringUtils;
010import org.conqat.lib.commons.uniformpath.UniformPath;
011import org.conqat.lib.commons.uniformpath.UniformPathCompatibilityUtil;
012
013/**
014 * Utility methods for converting test IDs to uniform paths and the other way
015 * around.
016 */
017public class TestUniformPathUtils {
018
019        /**
020         * Converts the given test id string to a uniform path considering that the test
021         * name may contain parameters.
022         */
023        public static UniformPath convertToUniformPath(String testUniformPath) {
024                Pair<String, String> testPathAndName = getTestPathAndName(testUniformPath);
025                String testPath = testPathAndName.getFirst();
026                String testName = testPathAndName.getSecond();
027                return UniformPathCompatibilityUtil.convertRelative(testPath).addSuffix(UniformPath.escapeSegment(testName))
028                                .resolveAgainstAbsolutePath(UniformPath.testRoot());
029        }
030
031        /**
032         * Returns a tuple of test path and test name. Splitting happens at the last
033         * slash that is not contained in a pair of brackets.
034         */
035        private static Pair<String, String> getTestPathAndName(String testUniformPath) {
036                List<String> segments = Arrays.asList(testUniformPath.split("/"));
037                for (int i = 0; i < segments.size(); i++) {
038                        if (segments.get(i).contains("[") || i == segments.size() - 1) {
039                                String testPath = StringUtils.concat(segments.subList(0, i), "/");
040                                String testName = StringUtils.concat(segments.subList(i, segments.size()), "/");
041                                return new Pair<>(testPath, testName);
042                        }
043                }
044                return new Pair<>("", testUniformPath);
045        }
046
047        /**
048         * Strips await the -test-/ prefix of a test uniform path so that the result
049         * matches the test id the test runner gave us.
050         */
051        public static String convertToTestId(String uniformPath) {
052                String testIdWithoutPrefix = StringUtils.stripPrefix(uniformPath,
053                                UniformPath.EType.TEST.getPrefix() + UniformPathUtils.SEPARATOR);
054                return StringUtils.unEscapeChars(testIdWithoutPrefix, Collections.singletonList('/'));
055        }
056}