001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
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 *     Damien Metzler (Leroy Merlin, http://www.leroymerlin.fr/)
018 */
019package org.nuxeo.ecm.webengine.test;
020
021import java.io.File;
022import java.io.IOException;
023import java.io.InputStream;
024import java.net.URL;
025
026import org.apache.commons.io.FileUtils;
027import org.nuxeo.ecm.core.test.JettyTransactionalFeature;
028import org.nuxeo.ecm.platform.test.PlatformFeature;
029import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
030import org.nuxeo.runtime.test.WorkingDirectoryConfigurator;
031import org.nuxeo.runtime.test.runner.Deploy;
032import org.nuxeo.runtime.test.runner.Features;
033import org.nuxeo.runtime.test.runner.FeaturesRunner;
034import org.nuxeo.runtime.test.runner.RuntimeFeature;
035import org.nuxeo.runtime.test.runner.RuntimeHarness;
036import org.nuxeo.runtime.test.runner.SimpleFeature;
037import org.nuxeo.runtime.test.runner.web.WebDriverFeature;
038
039@Deploy({ "org.nuxeo.ecm.platform.login", "org.nuxeo.ecm.platform.login.default", "org.nuxeo.ecm.webengine.jaxrs",
040        "org.nuxeo.ecm.webengine.base", "org.nuxeo.ecm.webengine.ui", "org.nuxeo.ecm.webengine.gwt",
041        "org.nuxeo.ecm.platform.test:test-usermanagerimpl/userservice-config.xml",
042        "org.nuxeo.ecm.webengine.test:login-anonymous-config.xml", "org.nuxeo.ecm.webengine.test:login-config.xml",
043        "org.nuxeo.ecm.webengine.test:runtimeserver-contrib.xml", "org.nuxeo.ecm.core.io" })
044@Features({ PlatformFeature.class, WebDriverFeature.class, JettyTransactionalFeature.class,
045        WebEngineFeatureCore.class })
046public class WebEngineFeature extends SimpleFeature implements WorkingDirectoryConfigurator {
047
048    protected URL config;
049
050    @Override
051    public void initialize(FeaturesRunner runner) throws Exception {
052        WebXml anno = FeaturesRunner.getScanner().getFirstAnnotation(runner.getTargetTestClass(), WebXml.class);
053        if (anno == null) {
054            config = getResource("webengine/web/WEB-INF/web.xml");
055        } else {
056            config = runner.getTargetTestClass().getClassLoader().getResource(anno.value());
057        }
058        runner.getFeature(RuntimeFeature.class).getHarness().addWorkingDirectoryConfigurator(this);
059    }
060
061    @Override
062    public void configure(RuntimeHarness harness, File workingDir) throws IOException {
063        SessionFactory.setDefaultRepository("test");
064        File dest = new File(workingDir, "web/root.war/WEB-INF/");
065        dest.mkdirs();
066
067        if (config == null) {
068            throw new java.lang.IllegalStateException(
069                    "No custom web.xml was found. " + "Check your @WebXml annotation on the test class");
070        }
071        dest = new File(workingDir + "/web/root.war/WEB-INF/", "web.xml");
072        try (InputStream in = config.openStream()) {
073            FileUtils.copyInputStreamToFile(in, dest);
074        }
075    }
076
077    private static URL getResource(String resource) {
078        return Thread.currentThread().getContextClassLoader().getResource(resource);
079    }
080
081    // public void deployTestModule() {
082    // URL currentDir =
083    // Thread.currentThread().getContextClassLoader().getResource(
084    // ".");
085    // ModuleManager moduleManager =
086    // Framework.getLocalService(WebEngine.class).getModuleManager();
087    // moduleManager.loadModuleFromDir(new File(currentDir.getFile()));
088    // }
089}