1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package vogar.target.junit;
18
19import java.util.Collections;
20import java.util.LinkedHashSet;
21import java.util.Set;
22import javax.annotation.Nullable;
23import org.junit.runner.Description;
24
25/**
26 * Utilities for bridging between JUnit and Vogar.
27 */
28public class JUnitUtils {
29    private JUnitUtils() {}
30
31    /**
32     * Get the name of the test in {@code <class>(#<method>)?} format.
33     */
34    public static String getTestName(Description description) {
35        String className = description.getClassName();
36        String methodName = description.getMethodName();
37        return getTestName(className, methodName);
38    }
39
40    /**
41     * Get the name of the test in {@code <class>(#<method>)?} format.
42     */
43    public static String getTestName(String className, String methodName) {
44        if (methodName == null) {
45            return className;
46        } else {
47            return className + "#" + methodName;
48        }
49    }
50
51    /**
52     * Merge the qualification and list of method names into a single set.
53     * @param qualification the qualification applied to a class name, i.e. the part after the #
54     * @param args the list of method names
55     * @return The set of method names, possibly empty.
56     */
57    public static Set<String> mergeQualificationAndArgs(@Nullable String qualification, String[] args) {
58        Set<String> methodNames = new LinkedHashSet<>();
59        if (qualification != null) {
60            methodNames.add(qualification);
61        }
62        Collections.addAll(methodNames, args);
63        return methodNames;
64    }
65}
66