2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.common.rules.test;
23 import org.junit.Ignore;
24 import org.junit.runner.Description;
25 import org.junit.runner.notification.RunNotifier;
26 import org.junit.runners.BlockJUnit4ClassRunner;
27 import org.junit.runners.model.FrameworkMethod;
28 import org.junit.runners.model.InitializationError;
31 * Runs tests listed via the {@link TestNames} annotation.
33 public class NamedRunner extends BlockJUnit4ClassRunner {
36 * Constructs the object.
38 public NamedRunner(Class<?> testClass) throws InitializationError {
43 protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
44 Description description = describeChild(method);
46 if (method.getAnnotation(Ignore.class) != null) {
47 notifier.fireTestIgnored(description);
49 } else if (!isNamed(description.getTestClass(), method.getName())) {
50 notifier.fireTestIgnored(description);
53 runLeaf(methodBlock(method), description, notifier);
58 * Determines if the test is in the list of tests to be included.
60 * @param testClass class under test
61 * @param testName name of the test of interest
62 * @return {@code true} if the test is in the list, {@code false} otherwise
64 private boolean isNamed(Class<?> testClass, String testName) {
65 TestNames annot = testClass.getAnnotation(TestNames.class);
67 // no annotation - everything passes
71 for (String name : annot.names()) {
72 if (testName.equals(name)) {
77 for (String prefix : annot.prefixes()) {
78 if (testName.startsWith(prefix)) {