001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.runtime.test.runner;
020
021import java.util.Arrays;
022import java.util.HashMap;
023import java.util.HashSet;
024import java.util.Map;
025import java.util.Set;
026
027import org.junit.Rule;
028import org.junit.rules.MethodRule;
029import org.junit.runners.model.FrameworkMethod;
030import org.junit.runners.model.Statement;
031import org.nuxeo.common.utils.URLStreamHandlerFactoryInstaller;
032import org.nuxeo.runtime.RuntimeServiceEvent;
033import org.nuxeo.runtime.RuntimeServiceListener;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.runtime.model.ComponentManager;
036import org.nuxeo.runtime.test.NXRuntimeTestCase;
037
038import com.google.inject.Binder;
039
040/**
041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
042 */
043@Features({ MDCFeature.class, ConditionalIgnoreRule.Feature.class, RandomBug.Feature.class })
044public class RuntimeFeature extends SimpleFeature {
045
046    protected RuntimeHarness harness;
047
048    protected RuntimeDeployment deployment;
049
050    /**
051     * Providers contributed by other features to override the default service provider used for a nuxeo service.
052     */
053    protected final Map<Class<?>, ServiceProvider<?>> serviceProviders;
054
055    public RuntimeFeature() {
056        serviceProviders = new HashMap<Class<?>, ServiceProvider<?>>();
057    }
058
059    public <T> void addServiceProvider(ServiceProvider<T> provider) {
060        serviceProviders.put(provider.getServiceClass(), provider);
061    }
062
063    public RuntimeHarness getHarness() {
064        return harness;
065    }
066
067    @Override
068    public void initialize(FeaturesRunner runner) throws Exception {
069        harness = new NXRuntimeTestCase(runner.getTargetTestClass());
070        deployment = RuntimeDeployment.onTest(runner);
071    }
072
073    @SuppressWarnings({ "unchecked", "rawtypes" })
074    @Override
075    public void configure(FeaturesRunner runner, Binder binder) {
076        binder.bind(RuntimeHarness.class).toInstance(getHarness());
077        for (String svc : Framework.getRuntime().getComponentManager().getServices()) {
078            try {
079                Class clazz = Thread.currentThread().getContextClassLoader().loadClass(svc);
080                ServiceProvider<?> provider = serviceProviders.get(clazz);
081                if (provider == null) {
082                    provider = new ServiceProvider(clazz);
083                }
084                binder.bind(clazz).toProvider(provider).in(provider.getScope());
085            } catch (Exception e) {
086                throw new RuntimeException("Failed to bind service: " + svc, e);
087            }
088        }
089    }
090
091    @Override
092    public void start(final FeaturesRunner runner) throws Exception {
093        Framework.addListener(new RuntimeServiceListener() {
094
095            @Override
096            public void handleEvent(RuntimeServiceEvent event) {
097                if (event.id != RuntimeServiceEvent.RUNTIME_ABOUT_TO_START) {
098                    return;
099                }
100                Framework.removeListener(this);
101                blacklistComponents(runner);
102            }
103        });
104
105        harness.start();
106        deployment.deploy(runner, harness);
107    }
108
109    @Override
110    public void stop(FeaturesRunner runner) throws Exception {
111        harness.stop();
112    }
113
114    @Rule
115    public MethodRule onCleanupURLStreamHandlers() {
116        return new MethodRule() {
117
118            @Override
119            public Statement apply(final Statement base, FrameworkMethod method, Object target) {
120                return new Statement() {
121                    @Override
122                    public void evaluate() throws Throwable {
123                        try {
124                            base.evaluate();
125                        } finally {
126                            URLStreamHandlerFactoryInstaller.resetURLStreamHandlers();
127                        }
128                    }
129                };
130            }
131
132        };
133    }
134
135    @Rule
136    public MethodRule onMethodDeployment() {
137        return RuntimeDeployment.onMethod();
138    }
139
140    protected void blacklistComponents(FeaturesRunner aRunner) {
141        BlacklistComponent config = aRunner.getConfig(BlacklistComponent.class);
142        if (config.value().length == 0) {
143            return;
144        }
145        final ComponentManager manager = Framework.getRuntime().getComponentManager();
146        Set<String> blacklist = new HashSet<>(manager.getBlacklist());
147        blacklist.addAll(Arrays.asList(config.value()));
148        manager.setBlacklist(blacklist);
149    }
150
151    @Override
152    public void beforeRun(FeaturesRunner runner) throws Exception {
153        harness.fireFrameworkStarted();
154    }
155
156}