001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
018 */
019
020package org.nuxeo.ecm.platform.api.test;
021
022import java.io.File;
023import java.net.URL;
024
025import org.junit.Before;
026import org.junit.After;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.runtime.RuntimeService;
031import org.nuxeo.runtime.api.Framework;
032import org.nuxeo.runtime.util.SimpleRuntime;
033
034/**
035 * Base class for remote unit testing.
036 *
037 * @author <a href="mailto:dm@nuxeo.com">Dragos Mihalache</a>
038 */
039public abstract class NXClientTestCase {
040
041    protected static RuntimeService runtime;
042
043    private static final Log log = LogFactory.getLog(NXClientTestCase.class);
044
045    @Before
046    protected void setUp() throws Exception {
047        initializeRT();
048    }
049
050    @After
051    protected void tearDown() throws Exception {
052        shutdownRT();
053    }
054
055    /**
056     * Subclasses may override this method to create a repository at a specific location.
057     */
058    protected File getHomeDir() {
059        return null;
060    }
061
062    private void initializeRT() throws Exception {
063        final File home = getHomeDir();
064        runtime = new SimpleRuntime(home);
065        Framework.initialize(runtime);
066        deployAll();
067    }
068
069    private static void shutdownRT() throws Exception {
070        Framework.shutdown();
071    }
072
073    protected void deploy(String bundle) {
074        URL url = getResource(bundle);
075        if (null == url) {
076            log.error("cannot deploy bundle: " + bundle + ". not found");
077            Thread.dumpStack();
078            return;
079        }
080        try {
081            Framework.getRuntime().getContext().deploy(url);
082        } catch (Exception e) {
083            log.error("cannot deploy bundle: " + bundle, e);
084        }
085    }
086
087    protected void undeploy(String bundle) {
088        URL url = getResource(bundle);
089        assert url != null;
090        try {
091            Framework.getRuntime().getContext().undeploy(url);
092        } catch (Exception e) {
093            log.error("cannot undeploy bundle: " + bundle, e);
094        }
095    }
096
097    protected URL getResource(String resource) {
098        return runtime.getContext().getResource(resource);
099    }
100
101    protected void deployAll() {
102        // deploy("RemotingService.xml");
103        deploy("EventService.xml");
104    }
105
106}