2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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.notification.RunNotifier;
25 import org.junit.runners.BlockJUnit4ClassRunner;
26 import org.junit.runners.model.FrameworkMethod;
27 import org.junit.runners.model.InitializationError;
30 * Runs tests listed via the {@link TestNames} annotation.
32 public class NamedRunner extends BlockJUnit4ClassRunner {
35 * Constructs the object.
37 public NamedRunner(Class<?> testClass) throws InitializationError {
42 protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
43 var description = describeChild(method);
45 if (method.getAnnotation(Ignore.class) != null) {
46 notifier.fireTestIgnored(description);
48 } else if (!isNamed(description.getTestClass(), method.getName())) {
49 notifier.fireTestIgnored(description);
52 runLeaf(methodBlock(method), description, notifier);
57 * Determines if the test is in the list of tests to be included.
59 * @param testClass class under test
60 * @param testName name of the test of interest
61 * @return {@code true} if the test is in the list, {@code false} otherwise
63 private boolean isNamed(Class<?> testClass, String testName) {
64 TestNames annot = testClass.getAnnotation(TestNames.class);
66 // no annotation - everything passes
70 for (String name : annot.names()) {
71 if (testName.equals(name)) {
76 for (String prefix : annot.prefixes()) {
77 if (testName.startsWith(prefix)) {