2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.odlparent.featuretest;
10 import java.io.IOException;
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;
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<>();
33 // Static initialization, as we may be invoked multiple times
34 URL.setURLStreamHandlerFactory(new CustomBundleURLStreamHandlerFactory());
37 public PerRepoTestRunner(final Class<?> testClass) throws InitializationError {
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);
49 protected List<PerFeatureRunner> runnersFromRepoURL(final URL repoURL,final Class<?> testClass,final boolean recursive) throws JAXBException, IOException, InitializationError {
51 return recursiveRunnersFromRepoURL(repoURL,testClass);
53 return runnersFromRepoURL(repoURL,testClass);
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));
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));
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));
85 * @param repoURL url of the repo
87 * @throws JAXBException exception during unmarshalling.
88 * @throws IOException IOException in getting the features.
90 protected Features getFeatures(final URL repoURL) throws JAXBException,
92 return JaxbUtil.unmarshal(repoURL.openStream(), false);
96 protected List<PerFeatureRunner> getChildren() {
101 protected Description describeChild(final PerFeatureRunner child) {
102 return child.getDescription();
106 protected void runChild(final PerFeatureRunner child, final RunNotifier notifier) {
107 LOG.info("About to run test for {}", child.getRepoURL());
112 * @see org.junit.runner.Runner#testCount()
115 public int testCount() {
116 return super.testCount() * children.size();