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