001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Damien Metzler (Leroy Merlin, http://www.leroymerlin.fr/)
016 */
017package org.nuxeo.ecm.webengine.test;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.InputStream;
022import java.net.URL;
023
024import org.nuxeo.common.utils.FileUtils;
025import org.nuxeo.ecm.core.test.JettyTransactionalFeature;
026import org.nuxeo.ecm.platform.test.PlatformFeature;
027import org.nuxeo.ecm.webengine.jaxrs.session.SessionFactory;
028import org.nuxeo.runtime.test.WorkingDirectoryConfigurator;
029import org.nuxeo.runtime.test.runner.Deploy;
030import org.nuxeo.runtime.test.runner.Features;
031import org.nuxeo.runtime.test.runner.FeaturesRunner;
032import org.nuxeo.runtime.test.runner.RuntimeFeature;
033import org.nuxeo.runtime.test.runner.RuntimeHarness;
034import org.nuxeo.runtime.test.runner.SimpleFeature;
035import org.nuxeo.runtime.test.runner.web.WebDriverFeature;
036
037@Deploy({ "org.nuxeo.ecm.platform.login", "org.nuxeo.ecm.platform.login.default", "org.nuxeo.ecm.webengine.admin",
038        "org.nuxeo.ecm.webengine.jaxrs", "org.nuxeo.ecm.webengine.base", "org.nuxeo.ecm.webengine.ui",
039        "org.nuxeo.ecm.webengine.gwt", "org.nuxeo.ecm.platform.test:test-usermanagerimpl/userservice-config.xml",
040        "org.nuxeo.ecm.webengine.test:login-anonymous-config.xml", "org.nuxeo.ecm.webengine.test:login-config.xml",
041        "org.nuxeo.ecm.webengine.test:runtimeserver-contrib.xml", "org.nuxeo.ecm.core.io" })
042@Features({ PlatformFeature.class, WebDriverFeature.class, JettyTransactionalFeature.class, WebEngineFeatureCore.class })
043public class WebEngineFeature extends SimpleFeature implements WorkingDirectoryConfigurator {
044
045    protected URL config;
046
047    @Override
048    public void initialize(FeaturesRunner runner) throws Exception {
049        WebXml anno = FeaturesRunner.getScanner().getFirstAnnotation(runner.getTargetTestClass(), WebXml.class);
050        if (anno == null) {
051            config = getResource("webengine/web/WEB-INF/web.xml");
052        } else {
053            config = runner.getTargetTestClass().getClassLoader().getResource(anno.value());
054        }
055        runner.getFeature(RuntimeFeature.class).getHarness().addWorkingDirectoryConfigurator(this);
056    }
057
058    @Override
059    public void configure(RuntimeHarness harness, File workingDir) throws IOException {
060        SessionFactory.setDefaultRepository("test");
061        File dest = new File(workingDir, "web/root.war/WEB-INF/");
062        dest.mkdirs();
063
064        if (config == null) {
065            throw new java.lang.IllegalStateException("No custom web.xml was found. "
066                    + "Check your @WebXml annotation on the test class");
067        }
068        InputStream in = config.openStream();
069        dest = new File(workingDir + "/web/root.war/WEB-INF/", "web.xml");
070        try {
071            FileUtils.copyToFile(in, dest);
072        } finally {
073            in.close();
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}