Fix javadoc tags to fix the jdk 1.8 javadoc lint checker
[odlparent.git] / features-test / src / main / java / org / opendaylight / odlparent / featuretest / PerRepoTestRunner.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.odlparent.featuretest;
9
10 import java.io.IOException;
11 import java.net.URL;
12 import java.util.ArrayList;
13 import java.util.List;
14 import javax.xml.bind.JAXBException;
15 import org.apache.karaf.features.internal.model.Feature;
16 import org.apache.karaf.features.internal.model.Features;
17 import org.apache.karaf.features.internal.model.JaxbUtil;
18 import org.apache.karaf.tooling.url.CustomBundleURLStreamHandlerFactory;
19 import org.junit.runner.Description;
20 import org.junit.runner.notification.RunNotifier;
21 import org.junit.runners.ParentRunner;
22 import org.junit.runners.model.InitializationError;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class PerRepoTestRunner extends ParentRunner<PerFeatureRunner> {
27     private static final String REPO_RECURSE = "repo.recurse";
28     private static final Logger LOG = LoggerFactory.getLogger(PerRepoTestRunner.class);
29     private static final String FEATURES_FILENAME = "features.xml";
30     private final List<PerFeatureRunner> children = new ArrayList<>();
31
32     static {
33         // Static initialization, as we may be invoked multiple times
34         URL.setURLStreamHandlerFactory(new CustomBundleURLStreamHandlerFactory());
35     }
36
37     public PerRepoTestRunner(final Class<?> testClass) throws InitializationError {
38         super(testClass);
39         try {
40             final URL repoURL = getClass().getClassLoader().getResource(FEATURES_FILENAME);
41             final boolean recursive = Boolean.getBoolean(REPO_RECURSE);
42             LOG.info("Creating test runners for repoURL {} recursive {}",repoURL,recursive);
43             children.addAll(runnersFromRepoURL(repoURL,testClass,recursive));
44         } catch (final Exception e) {
45             throw new InitializationError(e);
46         }
47     }
48
49     protected List<PerFeatureRunner> runnersFromRepoURL(final URL repoURL,final Class<?> testClass,final boolean recursive) throws JAXBException, IOException, InitializationError {
50         if(recursive) {
51             return recursiveRunnersFromRepoURL(repoURL,testClass);
52         } else {
53             return runnersFromRepoURL(repoURL,testClass);
54         }
55     }
56
57     protected List<PerFeatureRunner> runnersFromRepoURL(final URL repoURL,final Class<?> testClass) throws JAXBException, IOException, InitializationError {
58         final List<PerFeatureRunner> runners = new ArrayList<>();
59         final Features features = getFeatures(repoURL);
60         runners.addAll(runnersFromFeatures(repoURL,features,testClass));
61         return runners;
62     }
63
64     protected List<PerFeatureRunner> recursiveRunnersFromRepoURL(final URL repoURL,final Class<?> testClass) throws JAXBException, IOException, InitializationError {
65         final List<PerFeatureRunner> runners = new ArrayList<>();
66         final Features features = getFeatures(repoURL);
67         runners.addAll(runnersFromRepoURL(repoURL,testClass));
68         for(final String repoString: features.getRepository()) {
69             final URL subRepoURL = new URL(repoString);
70             runners.addAll(recursiveRunnersFromRepoURL(subRepoURL,testClass));
71         }
72         return runners;
73     }
74
75     protected List<PerFeatureRunner> runnersFromFeatures(final URL repoURL, final Features features,final Class<?> testClass) throws InitializationError {
76         final List<PerFeatureRunner> runners = new ArrayList<>();
77         final List<Feature> featureList = features.getFeature();
78         for(final Feature f : featureList) {
79             runners.add(new PerFeatureRunner(repoURL, f.getName(), f.getVersion(),testClass));
80         }
81         return runners;
82     }
83
84     /**
85      * @param repoURL url of the repo
86      * @return features
87      * @throws JAXBException exception during unmarshalling.
88      * @throws IOException IOException in getting the features.
89      */
90     protected Features getFeatures(final URL repoURL) throws JAXBException,
91             IOException {
92         return JaxbUtil.unmarshal(repoURL.openStream(), false);
93     }
94
95     @Override
96     protected List<PerFeatureRunner> getChildren() {
97         return children;
98     }
99
100     @Override
101     protected Description describeChild(final PerFeatureRunner child) {
102         return child.getDescription();
103     }
104
105     @Override
106     protected void runChild(final PerFeatureRunner child, final RunNotifier notifier) {
107         LOG.info("About to run test for {}", child.getRepoURL());
108         child.run(notifier);
109     }
110
111     /* (non-Javadoc)
112      * @see org.junit.runner.Runner#testCount()
113      */
114     @Override
115     public int testCount() {
116         return super.testCount() * children.size();
117     }
118 }