org.conqat.engine.sourcecode.coverage.TestUniformPathUtils Maven / Gradle / Ivy
/*
* Copyright (c) CQSE GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.conqat.engine.sourcecode.coverage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.conqat.engine.resource.util.UniformPathUtils;
import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.Pair;
import org.conqat.lib.commons.string.StringUtils;
import org.conqat.lib.commons.uniformpath.UniformPath;
import org.conqat.lib.commons.uniformpath.UniformPathCompatibilityUtil;
import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
/**
* Utility methods for converting test IDs to uniform paths and the other way
* around.
*/
public class TestUniformPathUtils {
/** Symbol that starts extending the test with parameters */
public static final String TEST_PARAMETERIZATION_START = "[";
/** Symbol that stops extending the test with parameters */
public static final String TEST_PARAMETERIZATION_END = "]";
/**
* Converts the given test id string to a uniform path considering that the test
* name may contain parameters. Parameters are detected as a suffix in brackets
* as e.g. path/to/test[some/parameter]. The parameter is treated as a single
* segment together with the test name, i.e. test[some/parameter] and any
* slashes in there will be escaped.
*/
public static UniformPath convertToTestUniformPath(String testUniformPath) {
Preconditions.checkArgument(!testUniformPath.startsWith(UniformPath.EType.TEST.getPrefix()));
List testPathSegments = getTestPathSegments(testUniformPath);
return UniformPath.of(UniformPath.EType.TEST, testPathSegments);
}
/**
* Converts the given test name segments to a test path prepending the name of
* the token element. This is used to build test paths for tests that have been
* detected in source code. All of the segments will be escaped. If the
* testNameSegments may contain slashes that should be treated as a new
* directory level use {@link #convertToTestUniformPath(String, List)} instead.
*/
public static UniformPath convertToTestUniformPath(String tokenElementPath, String... testNameSegments) {
Preconditions.checkArgument(!tokenElementPath.startsWith(UniformPath.EType.TEST.getPrefix()));
List escapedSegments = CollectionUtils.map(testNameSegments, UniformPath::escapeSegment);
return convertToTestUniformPath(tokenElementPath, escapedSegments);
}
/**
* Converts the given test name segments to a test path prepending the name of
* the token element. This is used to build test paths for tests that have been
* detected in source code. All of the segments must already be escaped and
* therefore may not contain any unescaped slashes.
*/
public static UniformPath convertToTestUniformPath(String tokenElementPath, List escapedTestSegments) {
Preconditions.checkArgument(!tokenElementPath.startsWith(UniformPath.EType.TEST.getPrefix()));
return UniformPathCompatibilityUtil.convertRelative(tokenElementPath)
.addSuffix(Iterables.toArray(escapedTestSegments, String.class))
.resolveAgainstAbsolutePath(UniformPath.testRoot());
}
/**
* Returns a list of escaped test path segments. Splitting happens at the last
* slash that is not contained in a pair of brackets to account for JUnit-like
* parameterized tests.
*/
public static List getTestPathSegments(String testUniformPath) {
List segments = Arrays.asList(testUniformPath.split("/"));
for (int i = 0; i < segments.size(); i++) {
if (segments.get(i).contains(TEST_PARAMETERIZATION_START) || i == segments.size() - 1) {
String testName = StringUtils.concat(segments.subList(i, segments.size()), "/");
List testSegments = new ArrayList<>(segments.subList(0, i));
testSegments.add(UniformPath.escapeSegment(testName));
return testSegments;
}
}
return Collections.singletonList(UniformPath.escapeSegment(testUniformPath));
}
/**
* Strips away the -test-/ prefix of a test uniform path and removes escaping so
* that the result matches the test id the test runner gave us.
*/
public static String convertToTestId(String uniformPath) {
String testIdWithoutPrefix = StringUtils.stripPrefix(uniformPath,
UniformPath.EType.TEST.getPrefix() + UniformPathUtils.SEPARATOR);
return StringUtils.unescapeChars(testIdWithoutPrefix, CollectionUtils.asMap(Pair.createPair("/", "\\/")));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy