1cbe5a56af22df81e22fa7d64647274463ed587f
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.common.rules.test;
22
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;
29
30 /**
31  * Runs tests listed via the {@link TestNames} annotation.
32  */
33 public class NamedRunner extends BlockJUnit4ClassRunner {
34
35     /**
36      * Constructs the object.
37      */
38     public NamedRunner(Class<?> testClass) throws InitializationError {
39         super(testClass);
40     }
41
42     @Override
43     protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
44         Description description = describeChild(method);
45
46         if (method.getAnnotation(Ignore.class) != null) {
47             notifier.fireTestIgnored(description);
48
49         } else if (!isNamed(description.getTestClass(), method.getName())) {
50             notifier.fireTestIgnored(description);
51
52         } else {
53             runLeaf(methodBlock(method), description, notifier);
54         }
55     }
56
57     /**
58      * Determines if the test is in the list of tests to be included.
59      *
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
63      */
64     private boolean isNamed(Class<?> testClass, String testName) {
65         TestNames annot = testClass.getAnnotation(TestNames.class);
66         if (annot == null) {
67             // no annotation - everything passes
68             return true;
69         }
70
71         for (String name : annot.names()) {
72             if (testName.equals(name)) {
73                 return true;
74             }
75         }
76
77         for (String prefix : annot.prefixes()) {
78             if (testName.startsWith(prefix)) {
79                 return true;
80             }
81         }
82
83         return false;
84     }
85 }