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")
040@Deploy("org.nuxeo.ecm.platform.login.default")
041@Deploy("org.nuxeo.ecm.webengine.jaxrs")
042@Deploy("org.nuxeo.ecm.webengine.base")
043@Deploy("org.nuxeo.ecm.webengine.ui")
044@Deploy("org.nuxeo.ecm.platform.test:test-usermanagerimpl/userservice-config.xml")
045@Deploy("org.nuxeo.ecm.webengine.test:login-anonymous-config.xml")
046@Deploy("org.nuxeo.ecm.webengine.test:login-config.xml")
047@Deploy("org.nuxeo.ecm.webengine.test:runtimeserver-contrib.xml")
048@Deploy("org.nuxeo.ecm.core.io")
049@Features({ PlatformFeature.class, WebDriverFeature.class, JettyTransactionalFeature.class,
050        WebEngineFeatureCore.class })
051public class WebEngineFeature extends SimpleFeature implements WorkingDirectoryConfigurator {
052
053    protected URL config;
054
055    @Override
056    public void initialize(FeaturesRunner runner) throws Exception {
057        WebXml anno = FeaturesRunner.getScanner().getFirstAnnotation(runner.getTargetTestClass(), WebXml.class);
058        if (anno == null) {
059            config = getResource("webengine/web/WEB-INF/web.xml");
060        } else {
061            config = runner.getTargetTestClass().getClassLoader().getResource(anno.value());
062        }
063        runner.getFeature(RuntimeFeature.class).getHarness().addWorkingDirectoryConfigurator(this);
064    }
065
066    @Override
067    public void configure(RuntimeHarness harness, File workingDir) throws IOException {
068        SessionFactory.setDefaultRepository("test");
069        File dest = new File(workingDir, "web/root.war/WEB-INF/");
070        dest.mkdirs();
071
072        if (config == null) {
073            throw new java.lang.IllegalStateException(
074                    "No custom web.xml was found. " + "Check your @WebXml annotation on the test class");
075        }
076        dest = new File(workingDir + "/web/root.war/WEB-INF/", "web.xml");
077        try (InputStream in = config.openStream()) {
078            FileUtils.copyInputStreamToFile(in, dest);
079        }
080    }
081
082    private static URL getResource(String resource) {
083        return Thread.currentThread().getContextClassLoader().getResource(resource);
084    }
085
086    // public void deployTestModule() {
087    // URL currentDir =
088    // Thread.currentThread().getContextClassLoader().getResource(
089    // ".");
090    // ModuleManager moduleManager =
091    // Framework.getLocalService(WebEngine.class).getModuleManager();
092    // moduleManager.loadModuleFromDir(new File(currentDir.getFile()));
093    // }
094}