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